LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcio_event_counter.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCReader.h"
4 #include "UTIL/LCStdHepRdr.h"
5 #include "IMPL/LCEventImpl.h"
6 
7 #include <cstdlib>
8 
10 
11 using namespace std ;
12 using namespace lcio ;
13 
17 int main(int argc, char** argv ){
18 
19  // read file names from command line (only argument)
20  if( argc < 2) {
21  cout << " count the number of events in the given input files" << endl << endl;
22  cout << " usage: lcio_event_counter <input-file1> [[input-file2],...]" << endl ;
23  exit(1) ;
24  }
25 
26  int nFiles = argc-1 ;
27  long total_events = 0 ;
28 
29  for(int i=1 ; i <= nFiles ; i++){
30  FILEN.push_back( argv[i] ) ;
31  }
32 
33 
34  // loop through the list of input files
35  for(int i=0 ; i < nFiles ; i++){
36 
37  // check if file extension is stdhep
38  if( FILEN[i].substr(FILEN[i].rfind(".") + 1) == "stdhep") {
39  LCStdHepRdr stdhepReader( FILEN[i].c_str() );
40 
41  // the getNumberOfEvents() method in LCStdHepRdr reads the number of events
42  // entered in the file header, there is no guarantee that this number is correct !!
43  // total_events += stdhepReader.getNumberOfEvents() ;
44 
45  // in most stdhep files the real number of events is one less than the number declared in the header file !!
46  total_events += stdhepReader.getNumberOfEvents() - 1 ;
47 
48  // using the code below the correct number is returned by looping through the
49  // whole events in the file, this is however a performance killer!
50  //try{
51  // while( true ){
52  // std::auto_ptr<LCEventImpl> evt( new LCEventImpl() ) ;
53  // stdhepReader.updateNextEvent( evt.get() , "MCParticle" ) ;
54  // total_events++ ;
55  // }
56  //}
57  //catch( IO::EndOfDataException& e ) { /* no-op */ }
58  }
59  // otherwise use lcReader
60  else{
61  // create reader for lcio files
62  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
63 
64  lcReader->open( FILEN[i] ) ;
65  total_events += lcReader->getNumberOfEvents() ;
66  lcReader->close() ;
67 
68  delete lcReader ;
69  }
70  }
71 
72  cout << total_events << endl ;
73 
74  return 0 ;
75 }
76 
77 
static std::vector< std::string > FILEN
T endl(T...args)
T push_back(T...args)
T exit(T...args)
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
LCReader * lcReader
Definition: lsh.cc:78