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