Jpp
 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"
20 
21 #include "JLang/JSinglePointer.hh"
22 
23 #include "Jeep/JParser.hh"
24 #include "Jeep/JMessage.hh"
25 
26 
27 namespace {
28 
29  static const char WILDCARD = '%'; //!< wild card for output files
30 
31  using namespace JPP;
32 
33 
34  /**
35  * Iterface for copying part of data.
36  */
37  struct JCopyInterface {
38  /**
39  * Virtual destructor.
40  */
41  virtual ~JCopyInterface()
42  {}
43 
44  /**
45  * Copy part of data.
46  *
47  * \param range range to be copied
48  */
49  virtual void copy(const JFrameIndexRange& range) = 0;
50  };
51 
52 
53  /**
54  * Implementation for copying part of data.
55  */
56  template<class T>
57  struct JCopy :
58  public JCopyInterface
59  {
60  /**
61  * Copy data from input file to output.
62  *
63  * \param file_name file name
64  * \param out output
65  */
66  JCopy(const std::string& file_name, JObjectOutput<T>& out) :
67  in (file_name),
68  out(out)
69  {
70  i = in.begin();
71  }
72 
73  /**
74  * Copy part of data.
75  *
76  * \param range range to be copied
77  */
78  virtual void copy(const JFrameIndexRange& range)
79  {
80  using namespace std;
81 
83 
84  STATUS(T::Class_Name() << endl);
85 
86  for ( ; i != in.end() && i->getFrameIndex() < range.getLowerLimit(); ++i) {
87  STATUS("skip " << setw(9) << i->getFrameIndex() << '\r'); DEBUG(endl);
88  }
89  STATUS(endl);
90 
91  for ( ; i != in.end() && i->getFrameIndex() <= range.getUpperLimit(); ++i) {
92 
93  STATUS("copy " << setw(9) << i->getFrameIndex() << '\r'); DEBUG(endl);
94 
95  out.put(*i);
96  }
97  STATUS(endl);
98  }
99 
101 
102  private:
104  JObjectOutput<T>& out;
105  };
106 
107 
108  /**
109  * Copy master.
110  */
111  template<class JTypelist_t>
112  struct JCopyMaster :
113  public std::vector< JSinglePointer<JCopyInterface> >,
114  public JFileRecorder<JTriggerTypes_t>
115  {
116  /**
117  */
118  JCopyMaster(const std::string& input_file)
119  {
120  add(JType<JTypelist_t>(), input_file);
121  }
122 
123  /**
124  * Copy part of output file.
125  *
126  * \param range range to be copied
127  * \param output_file file name
128  */
129  void copy(const JFrameIndexRange& range)
130  {
131  for (iterator i = this->begin(); i != this->end(); ++i) {
132  (*i)->copy(range);
133  }
134  }
135 
136  static int debug;
137 
138  private:
139  /**
140  * Recursive method for adding data copier.
141  *
142  * \param type data type
143  * \param input_file file name
144  */
145  template<class JHead_t, class JTail_t>
146  void add(JType< JTypeList<JHead_t, JTail_t> > type, const std::string& input_file)
147  {
148  add(JType<JHead_t>(), input_file);
149  add(JType<JTail_t>(), input_file);
150  }
151 
152  /**
153  * Add data copier.
154  *
155  * \param type data type
156  * \param input_file file name
157  */
158  template<class T>
159  void add(JType<T> type, const std::string& input_file)
160  {
162 
163  push_back(new JCopy<T>(input_file, *this));
164  }
165 
166  /**
167  * Termination method for adding data copier.
168  *
169  * \param type data type
170  * \param input_file file name
171  */
172  void add(JType<JNullType> type, const std::string& input_file)
173  {}
174  };
175 
176  /**
177  */
178  template<class JTypelist_t>
180 }
181 
182 
183 
184 /**
185  * \file
186  * Auxiliary program to split data.
187  *
188  * \author mdejong
189  */
190 int main(int argc, char **argv)
191 {
192  using namespace std;
193  using namespace JPP;
194 
195  typedef vector<JFrameIndexRange> ranges_type;
196 
197  std::string inputFile;
198  std::string outputFile;
199  size_t numberOfFiles;
200  ranges_type ranges;
201  int debug;
202 
203  try {
204 
205  JParser<> zap("Auxiliary program to split data.");
206 
207  zap['f'] = make_field(inputFile);
208  zap['o'] = make_field(outputFile);
209  zap['N'] = make_field(numberOfFiles) = 0;
210  zap['r'] = make_field(ranges) = JPARSER::initialised();
211  zap['d'] = make_field(debug) = 1;
212 
213  zap(argc, argv);
214  }
215  catch(const exception& error) {
216  FATAL(error.what() << endl);
217  }
218 
219 
220  if (numberOfFiles == 0 && ranges.empty()) {
221  FATAL("Invalid splitting " << numberOfFiles << '/' << ranges.size() << endl);
222  }
223 
224  const size_t pos = outputFile.find(WILDCARD);
225 
226  if (pos == string::npos) {
227  FATAL("Output file name " << outputFile << " does not contain wild card '" << WILDCARD << "'" << endl);
228  }
229 
230  const JFrameIndexRange total = getFrameIndexRange<JDAQSummaryslice>(inputFile);
231 
232  DEBUG("Total frame index range " << total << endl);
233 
234  if (total.getLength() == 0) {
235  FATAL("No summary data in input file " << inputFile << endl);
236  }
237 
238 
239  if (ranges.empty()) {
240 
241  int length = total.getLength() / numberOfFiles;
242 
243  if (length == 0) {
244  length = 1;
245  }
246 
247  for (JFrameIndexRange range(total.getLowerLimit(),
248  total.getLowerLimit() + length);
249  range.getLowerLimit() <= total.getUpperLimit();
250  range.add(length + 1)) {
251  ranges.push_back(range);
252  }
253  }
254 
255 
257 
258  JCopyMaster<JDAQTypes_t> master(inputFile);
259 
260  for (ranges_type::const_iterator range = ranges.begin(); range != ranges.end(); ++range) {
261 
262  const string file_name = MAKE_STRING(outputFile.substr(0,pos) << "_"
263  << range->getLowerLimit() << "-"
264  << range->getUpperLimit() << "_"
265  << outputFile.substr(pos+1));
266 
267  STATUS("Writing " << file_name << endl);
268 
269  master.open(file_name.c_str());
270 
271  master.put(getTriggerParameters(inputFile));
272 
273  master.copy(*range);
274 
275  master.close();
276  }
277 }
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1493
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
ROOT TTree parameter settings.
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
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:63
Auxiliary class for a type holder.
Definition: JType.hh:19
string outputFile
Template definition for direct access of elements in ROOT TChain.
Definition: JTreeScanner.hh:91
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
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:1954
range_type & add(argument_type x)
Add offset.
Definition: JRange.hh:448
ROOT I/O of application specific meta data.
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
Support methods.
int debug
debug level
Definition: JSirene.cc:61
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:153
Forward declarations for definitions of I/O redirect and pipe operators.
virtual bool put(const T &object)=0
Object output.
JTriggerParameters getTriggerParameters(const JMultipleFileScanner_t &file_list)
Get trigger parameters.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15