Project
Loading...
Searching...
No Matches
converter.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
23#include <TApplication.h>
24#include <TEveManager.h>
25#include <TEnv.h>
26#include <filesystem>
27#include <fairlogger/Logger.h>
28#include <csignal>
29#include <thread>
30#include <chrono>
31
32using namespace std::chrono_literals;
33
34// source file name, destination (not existing) file name, if limit > 0 then limit EACH type of data
35int singleFileConversion(const std::string& src, const std::string& dst, const int limit = -1)
36{
37 LOGF(info, "Translate: %s -> %s", src, dst);
39
41 std::filesystem::path(src).extension());
43 std::filesystem::path(dst).extension());
44
45 std::chrono::time_point currentTime = std::chrono::high_resolution_clock::now();
46 std::chrono::time_point endTime = std::chrono::high_resolution_clock::now();
47
48 srcSerializer->fromFile(vEvent, src);
49 endTime = std::chrono::high_resolution_clock::now();
50 // LOGF(info, "read took %f", std::chrono::duration_cast<std::chrono::microseconds>(endTime - currentTime).count() * 1e-6);
51 if (limit > 0) {
52 vEvent = vEvent.limit(limit);
53 }
54
55 currentTime = std::chrono::high_resolution_clock::now();
56 dstSerializer->toFile(vEvent, dst);
57 endTime = std::chrono::high_resolution_clock::now();
58 // LOGF(info, "write took %f", std::chrono::duration_cast<std::chrono::microseconds>(endTime - currentTime).count() * 1e-6);
59 return 0;
60}
61
62// reads source folder files, find missing files in destination folder and convert them
63// source folder (/path-to-folder/.ext1) , destination folder (/path-to-folder/.ext2)
64int folderConversion(const std::string& srcFolder, const std::string& dstFolder)
65{
66 std::vector<std::string> supported = {".json", ".root", ".eve"};
67 auto ext1 = srcFolder.substr(srcFolder.rfind('.'));
68 auto ext2 = dstFolder.substr(dstFolder.rfind('.'));
69
70 if (supported.end() == std::find(supported.begin(), supported.end(), ext1)) {
71 LOGF(error, "source folder should end with source extension: /path-to-folder/.ext1 ");
72 exit(-1);
73 }
74 if (supported.end() == std::find(supported.begin(), supported.end(), ext2)) {
75 LOGF(error, "destination folder should end with destination extension: /path-to-folder/.ext2 ");
76 return -1;
77 }
78 auto src = srcFolder.substr(0, srcFolder.size() - std::string(ext1).size());
79 auto dst = dstFolder.substr(0, dstFolder.size() - std::string(ext2).size());
80
81 if (src == dst) {
82 LOGF(error, "source folder same as destination folder ");
83 return -1;
84 }
85 if (!std::filesystem::is_directory(src)) {
86 LOGF(error, "source folder do not exist ");
87 return -1;
88 }
89 if (!std::filesystem::is_directory(dst)) {
90 LOGF(error, "destination folder do not exist ");
91 return -1;
92 }
93 std::vector<std::string> vExt1 = {ext1};
94 auto sourceList = o2::event_visualisation::DirectoryLoader::load(src, "_", vExt1);
95 std::vector<std::string> vExt2 = {ext2};
96 auto destinationList = o2::event_visualisation::DirectoryLoader::load(dst, "_", vExt2);
97
98 // first delete destination files which has not corresponding source files
99 for (auto& e : destinationList) {
100 auto match = e.substr(0, e.size() - ext2.size()) + ext1;
101 if (sourceList.end() == std::find(sourceList.begin(), sourceList.end(), match)) {
102 auto path = std::filesystem::path(dst + "" + e);
103 std::filesystem::remove(path);
104 }
105 }
106
107 // second translate source files which has not corresponding destination files
108 for (auto& e : sourceList) {
109 auto match = e.substr(0, e.size() - ext1.size()) + ext2;
110 if (destinationList.end() == std::find(destinationList.begin(), destinationList.end(), match)) {
111 // LOGF(info, "translate %s ->%s", src+e, dst+match);
113 }
114 }
115
116 return 0;
117}
118
119void my_handler(int s)
120{
121 printf("Caught signal %d\n", s);
122 exit(1);
123}
124
125int main(int argc, char** argv)
126{
127 struct sigaction sigIntHandler {
128 };
129 sigIntHandler.sa_handler = my_handler;
130 sigemptyset(&sigIntHandler.sa_mask);
131 sigIntHandler.sa_flags = 0;
132
133 sigaction(SIGINT, &sigIntHandler, nullptr);
134 LOGF(info, "Welcome in O2 event conversion tool");
135
136 if (argc == 3) {
137 singleFileConversion(argv[1], argv[2]); // std::quick_exit(...
138 return 0;
139 }
140 if (argc == 4 and std::string(argv[1]) == std::string("-l")) {
141 singleFileConversion(argv[2], argv[3], 3); // std::quick_exit(...
142 return 0;
143 }
144 if (argc == 4 and std::string(argv[1]) == std::string("-f")) {
145 folderConversion(argv[2], argv[3]); // std::quick_exit(...
146 return 0;
147 }
148 if (argc == 4 and std::string(argv[1]) == std::string("-c")) {
149 while (true) {
150 std::this_thread::sleep_for(2000ms);
151 folderConversion(argv[2], argv[3]);
152 }
153 return 0;
154 }
155 LOGF(error, "two filename required, second should point to not existent file");
156 return -1; // std::quick_exit(-1);
157}
Loading content of the Folder and returning sorted.
static std::deque< std::string > load(const std::string &path, const std::string &marker, const std::vector< std::string > &ext)
static VisualisationEventSerializer * getInstance(std::string ext)
VisualisationEvent limit(std::size_t maximum_number_of_items)
void my_handler(int s)
int folderConversion(const std::string &srcFolder, const std::string &dstFolder)
Definition converter.cxx:64
int singleFileConversion(const std::string &src, const std::string &dst, const int limit=-1)
Definition converter.cxx:35
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
GLenum src
Definition glcorearb.h:1767
GLenum GLenum dst
Definition glcorearb.h:1767
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
#define main