#include #include #include #include #include #include #include #include "km3net-dataformat/offline/Head.hh" #include "km3net-dataformat/offline/MultiHead.hh" #include "km3net-dataformat/offline/Evt.hh" #include "JLang/JException.hh" #include "JSupport/JMeta.hh" #include "JSupport/JSupport.hh" #include "JSupport/JFileRecorder.hh" #include "JSupport/JSingleFileScanner.hh" #include "JSupport/JTreeScanner.hh" #include "JSupport/JMonteCarloFileSupportkit.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { /** * Auxiliary data structure for sorting of Monte Carlo events. */ struct JCompare_t { /** * Compare two events. * * \param first first event * \param second second event * \return true if first event earlier than second; else false */ inline bool operator()(const Evt& first, const Evt& second) const { return first.mc_event_time.AsDouble() < second.mc_event_time.AsDouble(); } }; } /** * \file * Auxiliary program for time sorting of Monte-Carlo events. * * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef typename JTYPELIST::typelist typelist; string inputFile; string outputFile; size_t numberOfFiles; Long64_t autoflush; bool option; int debug; try { JParser<> zap("Auxiliary program for time sorting of Monte-Carlo events."); zap['f'] = make_field(inputFile); zap['o'] = make_field(outputFile, "If number of files > " << 1 << ", file name should contain wild card \'" << FILENAME_WILDCARD << "\'"); zap['N'] = make_field(numberOfFiles) = 1; zap['R'] = make_field(autoflush) = 1000; zap['O'] = make_field(option, "Write [multi] header to each output file"); zap['d'] = make_field(debug) = 2; zap(argc, argv); } catch(const exception& error) { FATAL(error.what() << endl); } if (numberOfFiles == 0) { numberOfFiles = 1; } if (numberOfFiles > 1 && !hasWildCard(outputFile)) { FATAL("Output file name should contain wildcard \'" << FILENAME_WILDCARD << "\'" << endl); } JTreeScanner in(inputFile); counter_type number_of_events = in.getEntries(); counter_type ns = (number_of_events + numberOfFiles - 1) / numberOfFiles; counter_type ni = 0; vector buffer; for (size_t i = 0; i != numberOfFiles; ++i, ni += ns) { const JLimit limit(ni, min(ni + ns, number_of_events)); const string filename(hasWildCard(outputFile) ? setWildCard(outputFile, to_string(i)) : outputFile); STATUS("Processing " << filename << ' ' << limit << "." << flush); buffer.clear(); for (in.setLimit(limit); in.hasNext(); ) { buffer.push_back(*in.next()); } STATUS("." << flush); sort(buffer.begin(), buffer.end(), JCompare_t()); STATUS("." << flush); JFileRecorder out(filename.c_str()); out.open(); dynamic_cast&>(*out).getTreeWriter().SetAutoFlush(autoflush); out.put(JMeta(argc, argv)); for (const auto& evt : buffer) { out.put(evt); } if (i == 0 || option) { JSingleFileScanner::typelist> io(inputFile); io >> out; } out.close(); STATUS("OK" << endl); } return 0; }