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