Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDAQSplit.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 
7 #include "JDAQ/JDAQEventIO.hh"
9 #include "JDAQ/JDAQEvaluator.hh"
10 
12 
14 #include "JSupport/JTreeScanner.hh"
17 #include "JSupport/JMeta.hh"
18 #include "JSupport/JSupport.hh"
21 
22 #include "JLang/JSinglePointer.hh"
23 
24 #include "Jeep/JParser.hh"
25 #include "Jeep/JMessage.hh"
26 
27 
28 namespace {
29 
30  static const char WILDCARD = '%'; //!< wild card for output files
31 
32  using namespace JPP;
33 
34 
35  /**
36  * Iterface for copying part of data.
37  */
38  struct JCopyInterface {
39  /**
40  * Virtual destructor.
41  */
42  virtual ~JCopyInterface()
43  {}
44 
45  /**
46  * Copy part of data.
47  *
48  * \param range range to be copied
49  */
50  virtual void copy(const JDAQUTCTimeRange& range) = 0;
51  };
52 
53 
54  /**
55  * Implementation for copying part of data.
56  */
57  template<class T>
58  struct JCopy :
59  public JCopyInterface
60  {
61  /**
62  * Copy data from input file to output.
63  *
64  * \param file_name file name
65  * \param out output
66  */
67  JCopy(const std::string& file_name, JObjectOutput<T>& out) :
68  in (file_name),
69  out(out)
70  {
71  i = in.begin();
72  }
73 
74 
75  /**
76  * Copy part of data.
77  *
78  * \param range range to be copied
79  */
80  virtual void copy(const JDAQUTCTimeRange& range)
81  {
82  using namespace std;
83 
85 
86  STATUS(T::Class_Name() << endl);
87 
88  NOTICE("Time range " << range << endl);
89 
90  for ( ; i != in.end() && i->getTimesliceStart().getTimeNanoSecond() < range.getLowerLimit().getTimeNanoSecond(); ++i) {
91  STATUS("skip " << FIXED(20,1) << i->getTimesliceStart().getTimeNanoSecond() << '\r'); DEBUG(endl);
92  }
93  STATUS(endl);
94 
95  for ( ; i != in.end() && i->getTimesliceStart().getTimeNanoSecond() <= range.getUpperLimit().getTimeNanoSecond(); ++i) {
96 
97  STATUS("copy " << FIXED(20,1) << i->getTimesliceStart().getTimeNanoSecond() << '\r'); DEBUG(endl);
98 
99  out.put(*i);
100  }
101  STATUS(endl);
102  }
103 
105 
106  private:
108  JObjectOutput<T>& out;
109  };
110 
111 
112  /**
113  * Copy master.
114  */
115  template<class JTypelist_t>
116  struct JCopyMaster :
117  public std::vector< JSinglePointer<JCopyInterface> >,
118  public JFileRecorder<JTriggerTypes_t>
119  {
120  /**
121  * Constructor.
122  *
123  * \param input_file input file
124  * \param selection selection
125  */
126  JCopyMaster(const std::string& input_file, const JROOTClassSelection& selection) :
127  selection(selection)
128  {
129  add(JType<JTypelist_t>(), input_file);
130  }
131 
132  /**
133  * Copy part of output file.
134  *
135  * \param range range to be copied
136  * \param output_file file name
137  */
138  void copy(const JDAQUTCTimeRange& range)
139  {
140  for (iterator i = this->begin(); i != this->end(); ++i) {
141  (*i)->copy(range);
142  }
143  }
144 
145  static int debug;
146  const JROOTClassSelection selection;
147 
148  private:
149  /**
150  * Recursive method for adding data copier.
151  *
152  * \param type data type
153  * \param input_file file name
154  */
155  template<class JHead_t, class JTail_t>
156  void add(JType< JTypeList<JHead_t, JTail_t> > type, const std::string& input_file)
157  {
158  add(JType<JHead_t>(), input_file);
159  add(JType<JTail_t>(), input_file);
160  }
161 
162  /**
163  * Add data copier.
164  *
165  * \param type data type
166  * \param input_file file name
167  */
168  template<class T>
169  void add(JType<T> type, const std::string& input_file)
170  {
171  if (selection(type)) {
172 
174 
175  push_back(new JCopy<T>(input_file, *this));
176  }
177  }
178 
179  /**
180  * Termination method for adding data copier.
181  *
182  * \param type data type
183  * \param input_file file name
184  */
185  void add(JType<JNullType> type, const std::string& input_file)
186  {}
187  };
188 
189  /**
190  */
191  template<class JTypelist_t>
193 }
194 
195 
196 
197 /**
198  * \file
199  * Auxiliary program to split data.
200  *
201  * \author mdejong
202  */
203 int main(int argc, char **argv)
204 {
205  using namespace std;
206  using namespace JPP;
207 
208  typedef vector<JDAQUTCTimeRange> ranges_type;
209 
210  std::string inputFile;
211  std::string outputFile;
212  size_t numberOfFiles;
213  ranges_type ranges;
214  JROOTClassSelection selection = getROOTClassSelection<JDAQTypes_t>();
215  int debug;
216 
217  try {
218 
219  JParser<> zap("Auxiliary program to split data.");
220 
221  zap['f'] = make_field(inputFile);
222  zap['o'] = make_field(outputFile);
223  zap['N'] = make_field(numberOfFiles) = 0;
224  zap['r'] = make_field(ranges) = JPARSER::initialised();
225  zap['C'] = make_field(selection,
226  "Precede name of data structure by a '+' or '-' "
227  "to add or remove data types in the output, respectively."
228  "\nROOT wildcards are accepted.") = JPARSER::initialised();
229  zap['d'] = make_field(debug) = 1;
230 
231  zap(argc, argv);
232  }
233  catch(const exception& error) {
234  FATAL(error.what() << endl);
235  }
236 
237 
238  if (numberOfFiles == 0 && ranges.empty()) {
239  FATAL("Invalid splitting " << numberOfFiles << '/' << ranges.size() << endl);
240  }
241 
242  const size_t pos = outputFile.find(WILDCARD);
243 
244  if (pos == string::npos) {
245  FATAL("Output file name " << outputFile << " does not contain wild card '" << WILDCARD << "'" << endl);
246  }
247 
248  const JDAQUTCTimeRange total = getUTCTimeRange<JDAQSummaryslice>(inputFile);
249 
250  if (!total.is_valid()) {
251  FATAL("No (valid) summary data in input file " << inputFile << ' ' << total << endl);
252  }
253 
254  NOTICE("Total time range " << total << endl);
255 
256  if (ranges.empty()) {
257 
258  const double length = (total.getUpperLimit().getTimeNanoSecond() -
259  total.getLowerLimit().getTimeNanoSecond()) / numberOfFiles;
260 
261  for (double t1 = total.getLowerLimit().getTimeNanoSecond(); t1 < total.getUpperLimit().getTimeNanoSecond(); t1 += length) {
262  ranges.push_back(JDAQUTCTimeRange(JDAQUTCExtended(t1), JDAQUTCExtended(t1 + length)));
263  }
264  }
265 
266 
268 
269  JCopyMaster<JDAQTypes_t> master(inputFile, selection);
270 
271  const int width = (int) (log10(ranges.size() + 1) + 1);
272 
273  for (size_t i = 0; i != ranges.size(); ++i) {
274 
275  const string file_name = MAKE_STRING(outputFile.substr(0,pos) << FILL(width,'0') << i << FILL() << outputFile.substr(pos+1));
276 
277  STATUS("Writing " << file_name << endl);
278 
279  master.open(file_name.c_str());
280 
281  master.put(getTriggerParameters(inputFile));
282 
283  master.copy(ranges[i]);
284 
285  master.close();
286  }
287 }
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1500
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
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.
Auxiliary class for ROOT class selection.
then echo Test string reversed by master(hit< return > to continue)." JProcess -c "JEcho" -rC fi if (( 1 ))
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Auxiliary class for a type holder.
Definition: JType.hh:19
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
Data structure for UTC time.
Template definition for direct access of elements in ROOT TChain.
Definition: JTreeScanner.hh:91
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
Type list.
Definition: JTypeList.hh:22
Scanning of objects from a single file according a format that follows from the extension of each fil...
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
ROOT I/O of application specific meta data.
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
#define NOTICE(A)
Definition: JMessage.hh:64
Support methods.
int debug
debug level
Definition: JSirene.cc:63
Range of values.
Definition: JRange.hh:38
General purpose messaging.
JTOOLS::JRange< JDAQUTCExtended > JDAQUTCTimeRange
Type definition for DAQ UTC time range.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
Template interface of object output for single data type.
virtual bool put(const T &object)=0
Object output.
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
JTriggerParameters getTriggerParameters(const JMultipleFileScanner_t &file_list)
Get trigger parameters.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62