16 #include <RecEventFile.h>
20 #include <AugerEvent.h>
22 #include <boost/algorithm/string/trim.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/program_options.hpp>
25 #include <boost/filesystem/operations.hpp>
49 cerr <<
"cannot open file '" << file <<
"'!" << endl;
56 while (getline(ifs, line)) {
59 cerr <<
"line " << lineno <<
": \"" << line <<
"\"\n";
61 boost::algorithm::trim(line);
62 const auto id = boost::lexical_cast<
ulong>(line);
65 cerr <<
"got " <<
id <<
'\n';
66 }
catch (
const boost::bad_lexical_cast& e) {
67 cerr <<
"line " << lineno <<
" of the file '" << file <<
"' could not be converted to an ID" << endl;
78 for (
const auto&
id : ids)
86 for (
const auto&
file : files) {
87 const unique_ptr<TFile> f{TFile::Open(
file.c_str(),
"READ")};
89 cerr <<
"cannot read file '" <<
file <<
"'" << endl;
92 const auto*
const keys = f->GetListOfKeys();
93 const auto nKeys = keys->GetEntries();
95 cerr <<
"warning: file '" <<
file <<
"' has no keys!" << endl;
98 for (
int i = 0; i < nKeys; ++i) {
99 const auto key = (TKey*
const)keys->At(i);
102 cerr <<
"key " << i <<
" is a nullptr!";
105 const char*
const name = key->GetName();
106 if (!name || !(
'0' <= *name && *name <=
'9')) {
108 cerr <<
"could not convert key \"" << name <<
'"' << endl;
111 cout << stoul(name) << endl;
120 namespace bpo = boost::program_options;
121 bpo::options_description optionSwitches(
"Options");
122 optionSwitches.add_options()
126 "Output some debug information.")
128 bpo::value<string>(),
129 "File containing event IDs to select.")
131 "Dump all IDs in input IoAuger ROOT files.")
133 "Search for events through native IoAuger interface (instead using faster ROOT keys).")
135 bpo::value<string>(),
136 "Output IoAuger ROOT file containing all selected events.")
138 bpo::options_description optionHidden;
139 optionHidden.add_options()
141 bpo::value<vector<string>>()->required(),
142 "Input ROOT file(s).");
143 bpo::positional_options_description positional;
144 positional.add(
"input", -1);
145 bpo::options_description options;
146 options.add(optionSwitches).add(optionHidden);
147 bpo::variables_map vm;
149 bpo::store(bpo::command_line_parser(argc, argv).options(options).positional(positional).
run(), vm);
150 if (vm.count(
"help")) {
151 cerr << optionSwitches << endl;
154 }
catch (bpo::error& err) {
155 cerr <<
"Command line error : " << err.what() <<
'\n'
156 << optionSwitches << endl;
162 const bool native = vm.count(
"native");
164 if (!vm.count(
"input")) {
165 cerr <<
"no input files specified!" << endl;
169 const auto inputFiles = vm[
"input"].as<vector<string>>();
171 if (vm.count(
"dump")) {
176 if (!vm.count(
"output")) {
177 cerr <<
"no output file specified!" << endl;
181 const auto outputFile = vm[
"output"].as<
string>();
184 cerr <<
"not overwriting exisiting output file '" << outputFile <<
'\'' << endl;
188 if (!vm.count(
"ids")) {
189 cerr <<
"no ID file specified!" << endl;
193 auto ids =
ParseIds(vm[
"ids"].as<string>());
195 const size_t nSearched = ids.size();
198 cerr <<
"no events to extract; IDs file is empty!" << endl;
203 cerr <<
"IDs to find:\n";
207 cout <<
"Events to extract: " << nSearched <<
"\n"
208 "Using " << (native ?
"native " :
"key ") <<
"interface" << endl;
212 unique_ptr<TFile> outputRootFile;
213 unique_ptr<AugerFile> outputAugerFile;
220 for (
const auto& inFile : inputFiles) {
221 cout <<
"Opening " << inFile <<
" ... [" << nFound <<
'/' << nSearched <<
']' << endl;
222 AugerFile auFile(inFile.c_str());
223 while (auFile.ReadNext(event) !=
AugerFile::eEOF && nFound != nSearched) {
224 auto it = ids.find(event.Id());
225 if (it == ids.end() &&
event.HasSd())
226 it = ids.find(event.Sd().Id);
227 if (it != ids.end()) {
229 if (!outputAugerFile)
231 outputAugerFile->Write(event);
233 cout <<
"found ID " << *it << endl;
245 for (
const auto& inFile : inputFiles) {
247 cout <<
"Opening " << inFile <<
" ... [" << nFound <<
'/' << nSearched <<
']' << endl;
249 const unique_ptr<TFile> f{TFile::Open(inFile.c_str(),
"READ")};
251 cerr <<
"Error: cannot read file '" << inFile <<
'\'' << endl;
255 const auto*
const keys = f->GetListOfKeys();
256 const auto nKeys = keys->GetEntries();
258 cerr <<
"warning: file '" << inFile <<
"' has no keys!" << endl;
262 cerr <<
"file '" << inFile <<
"' has " << nKeys <<
" keys" << endl;
263 for (
int i = 0; i < nKeys; ++i) {
264 const auto key = (TKey*
const)keys->At(i);
266 cerr <<
"key " << i <<
" is a nullptr!";
269 const char*
const name = key->GetName();
270 if (!name || !(
'0' <= *name && *name <=
'9')) {
271 cerr <<
"key " << i <<
" is malformed: \"" << (name ? name :
"nullptr") <<
'\"' << endl;
274 const ulong id = stoul(name);
276 cout <<
"id from key " << i <<
" \"" << name <<
"\" is " <<
id << endl;
277 const auto it = ids.find(
id);
278 if (it != ids.end()) {
281 cout <<
"found ID " <<
id << endl;
283 outputRootFile.reset(TFile::Open(outputFile.c_str(),
"RECREATE"));
284 auto*
const obj = key->ReadObj();
285 outputRootFile->cd();
298 cerr <<
"IDs not found:\n";
302 cout <<
"Events extracted: [" << nFound <<
"/" << nSearched <<
"]" << endl;
int main(int argc, char *argv[])
fwk::VModule::ResultFlag(fwk::VModule::* run)(evt::Event &)