LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anajob.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCReader.h"
4 #include "IMPL/LCTOOLS.h"
5 #include "EVENT/LCRunHeader.h"
6 
7 #include <cstdlib>
8 #include <sstream>
9 
11 
12 using namespace std ;
13 using namespace lcio ;
14 
23 int main(int argc, char** argv ){
24 
25  // read file names from command line (only argument)
26  if( argc < 2) {
27  cout << " usage: anajob <input-file1> [[input-file2],...]" << endl << endl ;
28  cout << " set the environment variable LCIO_READ_COL_NAMES to a space separated list" << endl ;
29  cout << " of collection names that you would like to read (all are read if not set)" << endl ;
30  exit(1) ;
31  }
32  for(int i=1 ; i < argc ; i++){
33  FILEN.push_back( argv[i] ) ;
34  }
35  int nFiles = argc-1 ;
36 
37  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
38 
39  // ------ check if LCIO_READ_COL_NAMES is set -------------
40 
41  char* rColChar = getenv ("LCIO_READ_COL_NAMES");
42 
43  if ( rColChar != 0 ) {
44 
45  std::vector< std::string > colSubset ;
46  std::stringstream sts( rColChar ) ;
47  std::string colName;
48 
49  while( sts >> colName) {
50 
51  colSubset.push_back( colName ) ;
52  }
53 
54  lcReader->setReadCollectionNames( colSubset ) ;
55  }
56  //-----------------------------------------------------------
57 
58 
59 
60  // first we read the run information
61 
62  cout << "anajob: will open and read from files: " << endl ;
63 
64  for(int i=0 ; i < nFiles ; i++){
65 
66  lcReader->open( FILEN[i] ) ;
67 
68  cout << endl << " " << FILEN[i]
69  << " [ number of runs: " << lcReader->getNumberOfRuns()
70  << ", number of events: " << lcReader->getNumberOfEvents() << " ] "
71  << endl
72  << endl ;
73 
74  lcReader->close() ;
75  }
76 
77 
78  // open list of files
79  lcReader->open( FILEN ) ;
80 
81  LCRunHeader *runHdr ;
82 
83  // use a try catch block here: if sth. went wrong with reading the run data we
84  // still can try and read the event data - see below
85  try{
86 
87  // loop over all run headers
88  while( ( runHdr = lcReader->readNextRunHeader() ) != 0 ){
89 
90  LCTOOLS::dumpRunHeader( runHdr ) ;
91 // cout << " Run : " << runHdr->getRunNumber()
92 // << " - " << runHdr->getDetectorName()
93 // << ": " << runHdr->getDescription() << endl ;
94  }
95 
96  }catch(IOException& e){
97  cout << " io error when reading run data : " << e.what() << endl ;
98  }
99  cout << endl ;
100 
101  lcReader->close() ;
102 
103 
104  // now loop over the file again and dump event data
105 
106  lcReader->open( FILEN ) ;
107 
108  // cout << " reopened " << FILEN << " for reading " << endl ;
109  cout << " will reopen and read from files: " << endl ;
110  for(int i=0 ; i < nFiles ; i++){
111  cout << " " << FILEN[i] << endl ;
112  }
113 
114 
115  LCEvent* evt ;
116  int nEvents = 0 ;
117 
118  //----------- the event loop -----------
119  while( (evt = lcReader->readNextEvent()) != 0 ) {
120 
121  LCTOOLS::dumpEvent( evt ) ;
122  nEvents ++ ;
123  }
124  // -------- end of event loop -----------
125 
126  cout << endl << " " << nEvents << " events read from files: " << endl ;
127  for(int i=0 ; i < nFiles ; i++){
128  cout << " " << FILEN[i] << endl ;
129  }
130 
131 
132  lcReader->close() ;
133  delete lcReader ;
134  return 0 ;
135 }
136 
137 
static std::vector< std::string > FILEN
Definition: anajob.cc:10
T endl(T...args)
STL class.
T getenv(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...
LCRunHeader * runHdr
Definition: lsh.cc:79
LCReader * lcReader
Definition: lsh.cc:78
void dumpEvent(EVENT::LCEvent *event)