Jpp  18.0.0-rc.3
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  * Configure.
251  *
252  * \param input list of file names
253  */
254  void configure(const input_type& input)
255  {
256  this->configure(input, JLimit());
257  }
258 
259 
260  /**
261  * Read multiple file scanner from input.
262  *
263  * Note that only file names are read.
264  *
265  * \param in input stream
266  * \param object multiple file scanner
267  * \return input stream
268  */
269  friend inline std::istream& operator>>(std::istream& in, JMultipleFileScanner& object)
270  {
271  return in >> object.getFilelist();
272  }
273 
274 
275  /**
276  * Write multiple file scanner to output.
277  *
278  * Note that only file names are written.
279  *
280  * \param out output stream
281  * \param object multiple file scanner
282  * \return output stream
283  */
284  friend inline std::ostream& operator<<(std::ostream& out, const JMultipleFileScanner& object)
285  {
286  return out << object.getFilelist();
287  }
288  };
289 
290 
291  /**
292  * Implementation of object reading for single data type from a list of file names.
293  *
294  * This class extends the JMultipleFileScanner<JNullType> class and
295  * implements the JLANG::JRewindableObjectIterator interface.\n
296  * When the method hasNext() is called,
297  * the next file in the list is opened when the previous file is exhausted.
298  */
299  template<class T>
300  class JMultipleFileScanner :
301  public virtual JMultipleFileScanner<>,
302  public JRewindableObjectIterator<T>
303  {
304  public:
305 
308 
309  /**
310  * Default constructor.
311  */
313  index (0),
314  counter(0)
315  {}
316 
317 
318  /**
319  * Copy constructor.
320  *
321  * Note that the counter limit is not copied and the index and counter are set to zero.
322  *
323  * \param input input
324  */
325  template<class JTypelist_t>
327  index (0),
328  counter(0)
329  {
330  this->configure(input.getFilelist(), JLimit());
331  }
332 
333 
334  /**
335  * Constructor.
336  *
337  * \param file_list list of file names
338  */
339  JMultipleFileScanner(const input_type& file_list) :
340  index (0),
341  counter(0)
342  {
343  configure(file_list, JLimit());
344  }
345 
346 
347  /**
348  * Constructor.
349  *
350  * \param file_list list of file names
351  * \param limit limit
352  */
353  JMultipleFileScanner(const input_type& file_list, const JLimit& limit) :
354  index (0),
355  counter(0)
356  {
357  configure(file_list, limit);
358  }
359 
360 
361  /**
362  * Get current file name.
363  *
364  * Note that this method should only be called when method hasNext() returns true.
365  *
366  * \return file name
367  */
368  const std::string& getFilename() const
369  {
370  return this->at(index);
371  }
372 
373 
374  /**
375  * Get counter.
376  *
377  * \return counter
378  */
380  {
381  return counter;
382  }
383 
384 
385  /**
386  * Rewind.
387  */
388  virtual void rewind() override
389  {
390  if (scanner.is_open()) {
391  scanner.close();
392  }
393 
394  index = 0;
395  counter = 0;
396 
397  scanner.reset();
398  }
399 
400 
401  /**
402  * Check availability of next element.
403  *
404  * \return true if the iteration has more elements; else false
405  */
406  virtual bool hasNext() override
407  {
408  if (is_valid()) {
409 
410  if (counter < getUpperLimit() && index != this->size()) {
411 
412  // first time around
413 
414  if (!scanner.is_open()) {
415  scanner.open(getFilename().c_str());
416  }
417 
418  if (counter < getLowerLimit()) {
419  counter += scanner.skip(getLowerLimit() - counter);
420  }
421 
422  if (!scanner.hasNext()) {
423 
424  scanner.close();
425 
426  ++index;
427 
428  return hasNext();
429  }
430 
431  return true;
432 
433  } else {
434 
435  // last time around
436 
437  if (scanner.is_open()) {
438  scanner.close();
439  }
440 
441  scanner.reset();
442  }
443  }
444 
445  return false;
446  }
447 
448 
449  /**
450  * Get next element.
451  *
452  * \return pointer to element
453  */
454  virtual const pointer_type& next() override
455  {
456  ++counter;
457 
458  return scanner.next();
459  }
460 
461 
462  /**
463  * Skip items.
464  *
465  * \param ns number of items to skip
466  * \return number of items skipped
467  */
468  virtual skip_type skip(const skip_type ns) override
469  {
470  skip_type i = 0;
471 
472  while (this->hasNext() && i != ns) {
473  i += scanner.skip(ns - i);
474  }
475 
476  counter += i;
477 
478  return i;
479  }
480 
481 
482  protected:
484  unsigned int index;
486  };
487 
488 
489  /**
490  * Implementation of object reading for multiple data types from a list of file names.
491  *
492  * This class recursively implements the JLANG::JRewindableObjectIterator interface
493  * for all data types by deriving from:
494  * - JMultipleFileScanner<JHead_t>; and
495  * - JMultipleFileScanner<JTail_t>.
496  *
497  * The method rewind(), rewinds each object iterator.
498  */
499  template<class JHead_t, class JTail_t>
500  class JMultipleFileScanner< JTypeList<JHead_t, JTail_t> > :
501  public virtual JMultipleFileScanner<>,
502  public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >,
503  public JMultipleFileScanner<JHead_t>,
504  public JMultipleFileScanner<JTail_t>
505  {
506  public:
507 
509 
512 
513 
514  /**
515  * Default constructor.
516  */
519  {}
520 
521 
522  /**
523  * Copy constructor.
524  *
525  * Note that the counter limit is not copied and the index and counter are set to zero.
526  *
527  * \param input input
528  */
529  template<class JTypelist_t>
531  {
532  this->configure(input.getFilelist(), JLimit());
533  }
534 
535 
536  /**
537  * Constructor.
538  *
539  * \param file_list list of file names
540  */
542  {
543  this->configure(file_list, JLimit());
544  }
545 
546 
547  /**
548  * Constructor.
549  *
550  * \param file_list list of file names
551  * \param limit limit
552  */
553  JMultipleFileScanner(const input_type& file_list, const JLimit& limit)
554  {
555  this->configure(file_list, limit);
556  }
557 
558 
559  /**
560  * Rewind.\n
561  * This method rewinds the JMultipleFileScanner for each data type.
562  */
563  virtual void rewind() override
564  {
567  }
568 
569 
570  /**
571  * Skip items.
572  *
573  * \param ns number of items to skip
574  * \return number of items skipped
575  */
576  virtual skip_type skip(const skip_type ns) override
577  {
578  if (JMultipleFileScanner<JHead_t>::skip(ns) == ns &&
580  return ns;
581  else
582  THROW(JException, "JMultipleFileScanner::skip(): inconsistent number of items skipped.");
583  }
584  };
585 
586 
587  /**
588  * Terminator class of recursive JMultipleFileScanner class.
589  */
590  template<class JHead_t>
592  public JMultipleFileScanner<JHead_t>
593  {
594  public:
595 
597 
598  /**
599  * Default constructor.
600  */
602  JMultipleFileScanner<JHead_t>()
603  {}
604 
605 
606  /**
607  * Copy constructor.
608  *
609  * Note that the counter limit is not copied and the index and counter are set to zero.
610  *
611  * \param input input
612  */
613  template<class JTypelist_t>
615  {
616  this->configure(input.getFilelist(), JLimit());
617  }
618 
619 
620  /**
621  * Constructor.
622  *
623  * \param file_list list of file names
624  */
626  {
627  this->configure(file_list, JLimit());
628  }
629 
630 
631  /**
632  * Constructor.
633  *
634  * \param file_list list of file names
635  * \param limit limit
636  */
637  JMultipleFileScanner(const input_type& file_list, const JLimit& limit)
638  {
639  this->configure(file_list, limit);
640  }
641  };
642 }
643 
644 #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
bool is_valid(const json &js)
Check validity of JSon data.
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.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::vector< size_t > ns
void configure(const input_type &input)
Configure.
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
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
then awk string
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:109
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:37
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:128
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
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.