Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMultipleFileScanner.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JMULTIPLEFILESCANNER__
2 #define __JSUPPORT__JMULTIPLEFILESCANNER__
3 
4 #include <string>
5 #include <vector>
6 #include <limits>
7 
8 #include "TChain.h"
9 #include "TChainElement.h"
10 
11 #include "JLang/JObjectIterator.hh"
12 #include "JLang/JTypeList.hh"
13 #include "JLang/JNullType.hh"
14 #include "JLang/JMultiEquals.hh"
15 #include "JLang/JException.hh"
16 
17 #include "JSupport/JLimit.hh"
18 #include "JSupport/JFileScanner.hh"
19 
20 
21 /**
22  * \file
23  * Scanning of objects from multiple files according a format that follows from the extension of each file name.
24  * \author mdejong
25  */
26 namespace JSUPPORT {}
27 namespace JPP { using namespace JSUPPORT; }
28 
29 namespace JSUPPORT {
30 
32  using JLANG::JPointer;
33  using JLANG::JTypeList;
34  using JLANG::JTYPELIST;
35  using JLANG::JNullType;
36  using JLANG::JMultiEquals;
37  using JLANG::skip_type;
38  using JLANG::JException;
39 
40 
41  /**
42  * Auxiliary base class for list of file names.
43  */
45  public std::vector<std::string>
46  {
47  /**
48  * Default constructor.
49  */
51  {}
52 
53 
54  /**
55  * Constructor.
56  *
57  * \param file_name file name
58  */
59  JMultipleFileScanner_t(const std::string& file_name) :
60  std::vector<std::string>(1, file_name)
61  {}
62 
63 
64  /**
65  * Constructor.
66  *
67  * \param chain ROOT chain
68  */
69  JMultipleFileScanner_t(const TChain& chain)
70  {
71  setFilelist(chain);
72  }
73 
74 
75  /**
76  * Get file list.
77  *
78  * \return list of file names
79  */
81  {
82  return static_cast<const JMultipleFileScanner_t&>(*this);
83  }
84 
85 
86  /**
87  * Get file list.
88  *
89  * \return list of file names
90  */
92  {
93  return static_cast<JMultipleFileScanner_t&>(*this);
94  }
95 
96 
97  /**
98  * Set file list.
99  *
100  * \param file_list list of file names
101  */
102  void setFilelist(const JMultipleFileScanner_t& file_list)
103  {
104  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
105  }
106 
107 
108  /**
109  * Set file list.
110  *
111  * \param chain ROOT chain
112  */
113  void setFilelist(const TChain& chain)
114  {
115  this->clear();
116 
117  const TObjArray* array = chain.GetListOfFiles();
118 
119  for (Int_t i = 0; i != array->GetEntries(); ++i) {
120 
121  const TChainElement* p = (const TChainElement*) array->At(i);
122 
123  this->push_back(p->GetTitle());
124  }
125  }
126 
127 
128  /**
129  * Read file list from input.
130  *
131  * \param in input stream
132  * \param object file list
133  * \return input stream
134  */
135  friend inline std::istream& operator>>(std::istream& in, JMultipleFileScanner_t& object)
136  {
137  for (std::string buffer; in >> buffer; ) {
138  object.push_back(buffer.c_str());
139  }
140 
141  return in;
142  }
143 
144 
145  /**
146  * Write file list to output.
147  *
148  * \param out output stream
149  * \param object file list
150  * \return output stream
151  */
152  friend inline std::ostream& operator<<(std::ostream& out, const JMultipleFileScanner_t& object)
153  {
154  for (const_iterator i = object.begin(); i != object.end(); ++i) {
155  out << *i << std::endl;
156  }
157 
158  return out;
159  }
160  };
161 
162 
163  /**
164  * General purpose class for object reading from a list of file names.
165  */
166  template<class T = JNullType>
168 
169 
170  /**
171  * Template specialisation of JMultipleFileScanner for undefined type.\n
172  * This specialisation is used as a virtual base class for all implementations of JMultipleFileScanner for defined type(s).\n
173  * The method JMultipleFileScanner::configure should be used to set the internal parameters.
174  *
175  * This class is a simple container for a list of file names and has an additional counter limit by extending from JSUPPORT::JLimit.\n
176  * This counter limit will be used to limit the number of calls to the method <tt>next</tt> of the derived class.\n
177  * Note that the counter limit can only be set via the member or friend methods of class JSUPPORT::JLimit.
178  */
179  template<>
181  public JMultipleFileScanner_t,
182  public JLimit,
183  public JMultiEquals<JMultipleFileScanner<JNullType>,
184  JTYPELIST<JLimit, JMultipleFileScanner_t>::typelist>
185  {
186  public:
187 
189 
190  /**
191  * Default constructor.
192  */
194  {}
195 
196 
197  /**
198  * Virtual destructor.
199  */
201  {}
202 
203 
204  /**
205  * Configure.
206  *
207  * \param input list of file names
208  * \param limit limit
209  */
210  void configure(const input_type& input, const JLimit& limit)
211  {
212  this->setFilelist(input);
213  this->setLimit (limit);
214  }
215 
216 
217  /**
218  * Read multiple file scanner from input.
219  *
220  * Note that only file names are read.
221  *
222  * \param in input stream
223  * \param object multiple file scanner
224  * \return input stream
225  */
226  friend inline std::istream& operator>>(std::istream& in, JMultipleFileScanner& object)
227  {
228  return in >> object.getFilelist();
229  }
230 
231 
232  /**
233  * Write multiple file scanner to output.
234  *
235  * Note that only file names are written.
236  *
237  * \param out output stream
238  * \param object multiple file scanner
239  * \return output stream
240  */
241  friend inline std::ostream& operator<<(std::ostream& out, const JMultipleFileScanner& object)
242  {
243  return out << object.getFilelist();
244  }
245  };
246 
247 
248  /**
249  * Implementation of object reading for single data type from a list of file names.
250  *
251  * This class extends the JMultipleFileScanner<JNullType> class and
252  * implements the JLANG::JRewindableObjectIterator interface.\n
253  * When the method hasNext() is called,
254  * the next file in the list is opened when the previous file is exhausted.
255  */
256  template<class T>
257  class JMultipleFileScanner :
258  public virtual JMultipleFileScanner<>,
259  public JRewindableObjectIterator<T>
260  {
261  public:
262 
265 
266  /**
267  * Default constructor.
268  */
270  index (0),
271  counter(0)
272  {}
273 
274 
275  /**
276  * Copy constructor.
277  *
278  * Note that the counter limit is not copied and the index and counter are set to zero.
279  *
280  * \param input input
281  */
282  template<class JTypelist_t>
284  index (0),
285  counter(0)
286  {
287  this->configure(input.getFilelist(), JLimit());
288  }
289 
290 
291  /**
292  * Constructor.
293  *
294  * \param file_list list of file names
295  */
296  JMultipleFileScanner(const input_type& file_list) :
297  index (0),
298  counter(0)
299  {
300  configure(file_list, JLimit());
301  }
302 
303 
304  /**
305  * Constructor.
306  *
307  * \param file_list list of file names
308  * \param limit limit
309  */
310  JMultipleFileScanner(const input_type& file_list, const JLimit& limit) :
311  index (0),
312  counter(0)
313  {
314  configure(file_list, limit);
315  }
316 
317 
318  /**
319  * Get current file name.
320  *
321  * Note that this method should only be called when method hasNext() returns true.
322  *
323  * \return file name
324  */
325  const std::string& getFilename() const
326  {
327  return this->at(index);
328  }
329 
330 
331  /**
332  * Get counter.
333  *
334  * \return counter
335  */
337  {
338  return counter;
339  }
340 
341 
342  /**
343  * Rewind.
344  */
345  virtual void rewind() override
346  {
347  if (scanner.is_open()) {
348  scanner.close();
349  }
350 
351  index = 0;
352  counter = 0;
353 
354  scanner.reset();
355  }
356 
357 
358  /**
359  * Check availability of next element.
360  *
361  * \return true if the iteration has more elements; else false
362  */
363  virtual bool hasNext() override
364  {
365  if (is_valid()) {
366 
367  if (counter < getUpperLimit() && index != this->size()) {
368 
369  // first time around
370 
371  if (!scanner.is_open()) {
372  scanner.open(getFilename().c_str());
373  }
374 
375  if (counter < getLowerLimit()) {
376  counter += scanner.skip(getLowerLimit() - counter);
377  }
378 
379  if (!scanner.hasNext()) {
380 
381  scanner.close();
382 
383  ++index;
384 
385  return hasNext();
386  }
387 
388  return true;
389 
390  } else {
391 
392  // last time around
393 
394  if (scanner.is_open()) {
395  scanner.close();
396  }
397 
398  scanner.reset();
399  }
400  }
401 
402  return false;
403  }
404 
405 
406  /**
407  * Get next element.
408  *
409  * \return pointer to element
410  */
411  virtual const pointer_type& next() override
412  {
413  ++counter;
414 
415  return scanner.next();
416  }
417 
418 
419  /**
420  * Skip items.
421  *
422  * \param ns number of items to skip
423  * \return number of items skipped
424  */
425  virtual skip_type skip(const skip_type ns) override
426  {
427  skip_type i = 0;
428 
429  while (this->hasNext() && i != ns) {
430  i += scanner.skip(ns - i);
431  }
432 
433  counter += i;
434 
435  return i;
436  }
437 
438 
439  protected:
441  unsigned int index;
443  };
444 
445 
446  /**
447  * Implementation of object reading for multiple data types from a list of file names.
448  *
449  * This class recursively implements the JLANG::JRewindableObjectIterator interface
450  * for all data types by deriving from:
451  * - JMultipleFileScanner<JHead_t>; and
452  * - JMultipleFileScanner<JTail_t>.
453  *
454  * The method rewind(), rewinds each object iterator.
455  */
456  template<class JHead_t, class JTail_t>
457  class JMultipleFileScanner< JTypeList<JHead_t, JTail_t> > :
458  public virtual JMultipleFileScanner<>,
459  public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >,
460  public JMultipleFileScanner<JHead_t>,
461  public JMultipleFileScanner<JTail_t>
462  {
463  public:
464 
466 
469 
470 
471  /**
472  * Default constructor.
473  */
476  {}
477 
478 
479  /**
480  * Copy constructor.
481  *
482  * Note that the counter limit is not copied and the index and counter are set to zero.
483  *
484  * \param input input
485  */
486  template<class JTypelist_t>
488  {
489  this->configure(input.getFilelist(), JLimit());
490  }
491 
492 
493  /**
494  * Constructor.
495  *
496  * \param file_list list of file names
497  */
499  {
500  this->configure(file_list, JLimit());
501  }
502 
503 
504  /**
505  * Constructor.
506  *
507  * \param file_list list of file names
508  * \param limit limit
509  */
510  JMultipleFileScanner(const input_type& file_list, const JLimit& limit)
511  {
512  this->configure(file_list, limit);
513  }
514 
515 
516  /**
517  * Rewind.\n
518  * This method rewinds the JMultipleFileScanner for each data type.
519  */
520  virtual void rewind() override
521  {
524  }
525 
526 
527  /**
528  * Skip items.
529  *
530  * \param ns number of items to skip
531  * \return number of items skipped
532  */
533  virtual skip_type skip(const skip_type ns) override
534  {
535  if (JMultipleFileScanner<JHead_t>::skip(ns) == ns &&
537  return ns;
538  else
539  throw JException("JMultipleFileScanner::skip(): inconsistent number of items skipped.");
540  }
541  };
542 
543 
544  /**
545  * Terminator class of recursive JMultipleFileScanner class.
546  */
547  template<class JHead_t>
549  public JMultipleFileScanner<JHead_t>
550  {
551  public:
552 
554 
555  /**
556  * Default constructor.
557  */
559  JMultipleFileScanner<JHead_t>()
560  {}
561 
562 
563  /**
564  * Copy constructor.
565  *
566  * Note that the counter limit is not copied and the index and counter are set to zero.
567  *
568  * \param input input
569  */
570  template<class JTypelist_t>
572  {
573  this->configure(input.getFilelist(), JLimit());
574  }
575 
576 
577  /**
578  * Constructor.
579  *
580  * \param file_list list of file names
581  */
583  {
584  this->configure(file_list, JLimit());
585  }
586 
587 
588  /**
589  * Constructor.
590  *
591  * \param file_list list of file names
592  * \param limit limit
593  */
594  JMultipleFileScanner(const input_type& file_list, const JLimit& limit)
595  {
596  this->configure(file_list, limit);
597  }
598  };
599 }
600 
601 #endif
General exception.
Definition: JException.hh:23
virtual skip_type skip(const skip_type ns) override
Skip items.
Exceptions.
JMultipleFileScanner_t & getFilelist()
Get file list.
JMultipleFileScanner(const JMultipleFileScanner< JTypelist_t > &input)
Copy constructor.
Interface for object iteration with rewinding.
JMultipleFileScanner()
Default constructor.
virtual ~JMultipleFileScanner()
Virtual destructor.
friend std::istream & operator>>(std::istream &in, JMultipleFileScanner_t &object)
Read file list from input.
JRewindableObjectIterator< T >::pointer_type pointer_type
unsigned int skip_type
Type definition for number of objects to skip.
JMultipleFileScanner_t(const TChain &chain)
Constructor.
void setFilelist(const JMultipleFileScanner_t &file_list)
Set file list.
Long64_t counter_type
Type definition for counter.
JMultipleFileScanner_t(const std::string &file_name)
Constructor.
JMultipleFileScanner(const input_type &file_list)
Constructor.
friend std::istream & operator>>(std::istream &in, JMultipleFileScanner &object)
Read multiple file scanner from input.
Template definition of auxiliary base class for data structures composed of multiple base classes wit...
Definition: JMultiEquals.hh:32
void setFilelist(const TChain &chain)
Set file list.
Type list.
Definition: JTypeList.hh:22
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JMultipleFileScanner_t()
Default constructor.
virtual bool hasNext() override
Check availability of next element.
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
bool is_valid(const json &js)
Check validity of JSon data.
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
const JMultipleFileScanner_t & getFilelist() const
Get file list.
const std::string & getFilename() const
Get current file name.
friend std::ostream & operator<<(std::ostream &out, const JMultipleFileScanner_t &object)
Write file list to output.
JMultipleFileScanner(const JMultipleFileScanner< JTypelist_t > &input)
Copy constructor.
JMultipleFileScanner(const input_type &file_list, const JLimit &limit)
Constructor.
Auxiliary base class for list of file names.
JMultipleFileScanner::input_type input_type
JMultipleFileScanner(const JMultipleFileScanner< JTypelist_t > &input)
Copy constructor.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
General purpose class for object reading from a list of file names.
virtual const pointer_type & next() override
Get next element.
friend std::ostream & operator<<(std::ostream &out, const JMultipleFileScanner &object)
Write multiple file scanner to output.
Object reading from file.
Definition: JFileScanner.hh:36
void configure(const input_type &input, const JLimit &limit)
Configure.
counter_type getCounter() const
Get counter.
JMultipleFileScanner(const input_type &file_list, const JLimit &limit)
Constructor.
Auxiliaries for defining the range of iterations of objects.
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 typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
virtual void rewind() override
Rewind.
virtual skip_type skip(const skip_type ns) override
Skip items.
JMultipleFileScanner(const input_type &file_list, const JLimit &limit)
Constructor.