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
24#include <TEveManager.h>
25#include <filesystem>
26#include <fairlogger/Logger.h>
27#include <csignal>
28#include <thread>
29#include <chrono>
30
31#include <boost/program_options.hpp>
32#include <boost/program_options/options_description.hpp>
33#include <boost/program_options/variables_map.hpp>
34
35using namespace std::chrono_literals;
36
37// source file name, destination (not existing) file name, if limit > 0 then limit EACH type of data
38int singleFileConversion(const std::string& src, o2::event_visualisation::Location& dst, const int limit = -1)
39{
40 LOGF(info, "Translate: %s -> %s", src, dst.fileName());
42
44 std::filesystem::path(src).extension());
45 auto dstExtension = std::filesystem::path(
46 src)
47 .extension(); // if there is no destination, there will be no extension change
48 if (!dst.fileName().empty()) {
49 dstExtension = std::filesystem::path(dst.fileName()).extension();
50 }
52 dstExtension);
53
54 std::chrono::time_point currentTime = std::chrono::high_resolution_clock::now();
55 std::chrono::time_point endTime = std::chrono::high_resolution_clock::now();
56
57 srcSerializer->fromFile(vEvent, src);
58 endTime = std::chrono::high_resolution_clock::now();
59 // LOGF(info, "read took %f", std::chrono::duration_cast<std::chrono::microseconds>(endTime - currentTime).count() * 1e-6);
60 if (limit > 0) {
61 vEvent = vEvent.limit(limit);
62 }
63
64 currentTime = std::chrono::high_resolution_clock::now();
65 dstSerializer->toFile(vEvent, dst);
66 endTime = std::chrono::high_resolution_clock::now();
67 // LOGF(info, "write took %f", std::chrono::duration_cast<std::chrono::microseconds>(endTime - currentTime).count() * 1e-6);
68 return 0;
69}
70
71// reads source folder files, find missing files in destination folder and convert them
72// source folder (/path-to-folder/.ext1) , destination folder (/path-to-folder/.ext2)
73int folderConversion(const std::string& srcFolder, const o2::event_visualisation::Location& dstFolderLocation)
74{
75 const std::string dstFolder = dstFolderLocation.fileName();
76 std::vector<std::string> supported = {".json", ".root", ".eve"};
77 auto ext1 = srcFolder.substr(srcFolder.rfind('.'));
78 auto ext2 = dstFolder.substr(dstFolder.rfind('.'));
79
80 if (supported.end() == std::find(supported.begin(), supported.end(), ext1)) {
81 LOGF(error, "source folder should end with source extension: /path-to-folder/.ext1 ");
82 exit(-1);
83 }
84 if (supported.end() == std::find(supported.begin(), supported.end(), ext2)) {
85 LOGF(error, "destination folder should end with destination extension: /path-to-folder/.ext2 ");
86 return -1;
87 }
88 auto src = srcFolder.substr(0, srcFolder.size() - std::string(ext1).size());
89 auto dst = dstFolder.substr(0, dstFolder.size() - std::string(ext2).size());
90
91 if (src == dst) {
92 LOGF(error, "source folder same as destination folder ");
93 return -1;
94 }
95 if (!std::filesystem::is_directory(src)) {
96 LOGF(error, "source folder do not exist ");
97 return -1;
98 }
99 if (!std::filesystem::is_directory(dst)) {
100 LOGF(error, "destination folder do not exist ");
101 return -1;
102 }
103 std::vector<std::string> vExt1 = {ext1};
104 auto sourceList = o2::event_visualisation::DirectoryLoader::load(src, "_", vExt1);
105 std::vector<std::string> vExt2 = {ext2};
106 auto destinationList = o2::event_visualisation::DirectoryLoader::load(dst, "_", vExt2);
107
108 // first delete destination files which has not corresponding source files
109 for (auto& e : destinationList) {
110 auto match = e.substr(0, e.size() - ext2.size()) + ext1;
111 if (sourceList.end() == std::find(sourceList.begin(), sourceList.end(), match)) {
112 auto path = std::filesystem::path(dst + "" + e);
113 std::filesystem::remove(path);
114 }
115 }
116
117 // second translate source files which has not corresponding destination files
118 for (auto& e : sourceList) {
119 auto match = e.substr(0, e.size() - ext1.size()) + ext2;
120 if (destinationList.end() == std::find(destinationList.begin(), destinationList.end(), match)) {
121 // LOGF(info, "translate %s ->%s", src+e, dst+match);
123 .port = dstFolderLocation.port(),
124 .host = dstFolderLocation.hostName()});
126 ;
128 ;
129 }
130 }
131
132 return 0;
133}
134
135void my_handler(int s)
136{
137 printf("Caught signal %d\n", s);
138 exit(1);
139}
140
141namespace po = boost::program_options;
142
143using namespace std;
144
145int main(int argc, char** argv)
146{
147 struct sigaction sigIntHandler {
148 };
149 sigIntHandler.sa_handler = my_handler;
150 sigemptyset(&sigIntHandler.sa_mask);
151 sigIntHandler.sa_flags = 0;
152
153 sigaction(SIGINT, &sigIntHandler, nullptr);
154 LOGF(info, "Welcome in O2 event conversion tool");
155
156 try {
157 int port;
158 string host;
159 int limit;
160 bool folderMode;
161 bool continuousMode;
162 vector<string> sources;
163 po::options_description desc("Allowed options");
164 desc.add_options()("help,h", "produce help message")("port", po::value(&port)->default_value(-1), "port number")("host", po::value(&host)->default_value("localhost"), "host name")("sources", po::value(&sources), "sources")("limit,l", po::value(&limit)->default_value(-1), "limit number of elements")("folder,f", po::bool_switch(&folderMode)->default_value(false), "convert folders")("continuous,c", po::bool_switch(&continuousMode)->default_value(false), "continuous folder mode");
165
166 po::positional_options_description p;
167 p.add("sources", 2);
168
169 po::variables_map vm;
170 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
171 po::notify(vm);
172
173 if (vm.count("help")) {
174 cout << desc << "\n";
175 return 0;
176 }
177
178 if (vm.count("sources")) {
179 if (vm["sources"].as<vector<string>>().size() != 2) {
180 cout << "two positional parameters expected" << "\n";
181 return 0;
182 }
183 }
185 locationParams.fileName = sources[1];
186 locationParams.port = port;
187 locationParams.host = host;
188
190
191 if (folderMode) {
193 } else if (continuousMode) {
194 while (true) {
195 std::this_thread::sleep_for(2000ms);
197 }
198 } else {
200 return 0;
201 }
202 }
203
204 catch (exception& e) {
205 cerr << "error: " << e.what() << "\n";
206 return 1;
207 } catch (...) {
208 cerr << "Exception of unknown type!\n";
209 }
210 return -1; // std::quick_exit(-1);
211}
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)
std::string hostName() const
Definition Location.h:65
std::string fileName() const
Definition Location.h:64
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 o2::event_visualisation::Location &dstFolderLocation)
Definition converter.cxx:73
int singleFileConversion(const std::string &src, o2::event_visualisation::Location &dst, const int limit=-1)
Definition converter.cxx:38
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
GLenum src
Definition glcorearb.h:1767
GLsizeiptr size
Definition glcorearb.h:659
GLenum GLenum dst
Definition glcorearb.h:1767
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
GLint location
Definition glcorearb.h:800
GLsizei GLenum * sources
Definition glcorearb.h:2516
Defining DataPointCompositeObject explicitly as copiable.
#define main