Jpp  18.0.1-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEvtSplit.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <vector>
5 #include <deque>
6 #include <queue>
7 #include <algorithm>
8 
12 
13 #include "JLang/JException.hh"
14 
15 #include "JSupport/JMeta.hh"
16 #include "JSupport/JSupport.hh"
19 #include "JSupport/JTreeScanner.hh"
21 
22 #include "Jeep/JeepToolkit.hh"
23 #include "Jeep/JParser.hh"
24 #include "Jeep/JMessage.hh"
25 
26 
27 namespace {
28 
29  /**
30  * Auxiliary data structure for sorting of Monte Carlo events.
31  */
32  struct JCompare_t {
33  /**
34  * Compare two events.
35  *
36  * \param first first event
37  * \param second second event
38  * \return true if first event earlier than second; else false
39  */
40  inline bool operator()(const Evt& first, const Evt& second) const
41  {
42  return first.mc_event_time.AsDouble() < second.mc_event_time.AsDouble();
43  }
44  };
45 }
46 
47 
48 /**
49  * \file
50  * Auxiliary program for time sorting of Monte-Carlo events.
51  *
52  * \author mdejong
53  */
54 int main(int argc, char **argv)
55 {
56  using namespace std;
57  using namespace JPP;
58 
59  typedef typename JTYPELIST<Head, MultiHead, Evt, JMeta>::typelist typelist;
60 
61  string inputFile;
62  string outputFile;
63  size_t numberOfFiles;
64  Long64_t autoflush;
65  int debug;
66 
67  try {
68 
69  JParser<> zap("Auxiliary program for time sorting of Monte-Carlo events.");
70 
71  zap['f'] = make_field(inputFile);
72  zap['o'] = make_field(outputFile, "If number of files > " << 1
73  << ", file name should contain wild card \'" << FILENAME_WILD_CARD << "\'");
74  zap['N'] = make_field(numberOfFiles) = 1;
75  zap['R'] = make_field(autoflush) = 1000;
76  zap['d'] = make_field(debug) = 2;
77 
78  zap(argc, argv);
79  }
80  catch(const exception& error) {
81  FATAL(error.what() << endl);
82  }
83 
84 
85  if (numberOfFiles == 0) {
86  numberOfFiles = 1;
87  }
88 
89  if (numberOfFiles > 1 && !hasWildCard(outputFile)) {
90  FATAL("Output file name should contain wildcard \'" << FILENAME_WILD_CARD << "\'" << endl);
91  }
92 
93  JTreeScanner<Evt> in(inputFile);
94 
95  counter_type number_of_events = in.getEntries();
96 
97  counter_type ns = (number_of_events + numberOfFiles - 1) / numberOfFiles;
98  counter_type ni = 0;
99 
100  vector<Evt> buffer;
101 
102  for (size_t i = 0; i != numberOfFiles; ++i, ni += ns) {
103 
104  const JLimit limit(ni, min(ni + ns, number_of_events));
105 
106  const string filename(hasWildCard(outputFile) ? setWildCard(outputFile, to_string(i)) : outputFile);
107 
108  STATUS("Processing " << filename << ' ' << limit << "." << flush);
109 
110  buffer.clear();
111 
112  for (in.setLimit(limit); in.hasNext(); ) {
113  buffer.push_back(*in.next());
114  }
115 
116  STATUS("." << flush);
117 
118  sort(buffer.begin(), buffer.end(), JCompare_t());
119 
120  STATUS("." << flush);
121 
122  JFileRecorder<typelist> out(filename.c_str());
123 
124  out.open();
125 
126  dynamic_cast<JTreeWriterObjectOutput<Evt>&>(*out).getTreeWriter().SetAutoFlush(autoflush);
127 
128  out.put(JMeta(argc, argv));
129 
130  for (const auto& evt : buffer) {
131  out.put(evt);
132  }
133 
134  if (i == 0) {
135 
137 
138  io >> out;
139  }
140 
141  out.close();
142 
143  STATUS("OK" << endl);
144  }
145 
146  return 0;
147 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1514
Exceptions.
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
std::vector< size_t > ns
Long64_t counter_type
Type definition for counter.
string outputFile
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Type list.
Definition: JTypeList.hh:22
Scanning of objects from a single file according a format that follows from the extension of each fil...
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Auxiliary methods for handling file names, type names and environment.
TTimeStamp mc_event_time
MC: true generation time (UTC) of the event, (default: 01 Jan 1970 00:00:00)
Definition: Evt.hh:46
ROOT I/O of application specific meta data.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
std::string setWildCard(const std::string &file_name, const std::string &value)
Get file name by setting wild card to given value.
Definition: JeepToolkit.hh:66
JTreeWriter< T > & getTreeWriter()
Get TreeWriter.
Utility class to parse command line options.
std::string to_string(const T &value)
Convert value to string.
static const char FILENAME_WILD_CARD
wild card character for file name substitution
Definition: JeepToolkit.hh:44
virtual void open(const char *file_name) override
Open file.
Object reading from a list of files.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
int debug
debug level
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:20
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
Definition: JeepToolkit.hh:53