LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcio_merge_files.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCWriter.h"
4 #include "IO/LCReader.h"
5 #include "IO/LCEventListener.h"
6 #include "IO/LCRunListener.h"
7 
8 #include "EVENT/LCIO.h"
9 
10 #include <cstdlib>
11 #include <iostream>
12 
14 
15 using namespace std ;
16 using namespace lcio ;
17 
18 
22 // class for processing run and event records
23 class RunEventProcessor : public LCRunListener, public LCEventListener{
24 
25  protected:
26  LCWriter* lcWrt{NULL} ;
27  int nEvent{0} ;
28  unsigned int _nFiles{0} ;
29 
30  public:
31 
32  RunEventProcessor(const RunEventProcessor&) = delete ;
33  RunEventProcessor operator=(const RunEventProcessor&) = delete ;
34 
35  RunEventProcessor(const char* outFileName, unsigned int nFiles) : nEvent(0), _nFiles(nFiles) {
36 
37  // open outputfile
38  lcWrt = LCFactory::getInstance()->createLCWriter() ;
39 
40  try{ lcWrt->open( outFileName , LCIO::WRITE_NEW ) ; }
41 
42  catch(IOException& e){
43  cout << "[RunEventProcessor()] Can't open file for writing - "
44  << e.what() << endl ;
45  exit(1) ;
46  }
47 
48  }
49 
51  // close outputfile
52  lcWrt->close() ;
53  cout << "merged " << nEvent << " events from " << _nFiles << " input files." << endl ;
54  }
55 
56  void modifyEvent( LCEvent * /* evt */) { /*no changes to event ! */ ; }
57 
58  void processEvent( LCEvent * evt ) {
59 
60  // just copy events to outputfiles
61  lcWrt->writeEvent( evt ) ;
62  nEvent ++ ;
63  // cout << " event: " << evt->getEventNumber()
64  // << " [run: " << evt->getRunNumber() << "] copied" << endl ;
65  }
66 
67  void modifyRunHeader(LCRunHeader* /*run*/ ){ /*no changes to event ! */ ;}
68 
69  // don't manipulate run headers - use analyze
70  void processRunHeader( LCRunHeader* run){
71 
72  cout << "." ;
73  cout.flush() ;
74  // just copy run headers to the outputfile
75  lcWrt->writeRunHeader( run ) ;
76  // cout << "run : " << run->getRunNumber() << " ["
77  // << run->getDescription() << "] - header copied " << endl ;
78  }
79 
80 } ;
81 
82 //=============================================================================
83 
84 int main(int argc, char** argv ){
85 
86  char* outFileName ;
87 
88  try{ // a large try block for debugging ....
89 
90  if( argc < 3 ){
91  cout << "usage: " << argv[0] << " <output-file> <input-file1> [[input-file2],...]" << endl ;
92  exit(1) ;
93  }
94 
95  // read file names from command line
96  outFileName = argv[1] ;
97 
98  unsigned int nFiles = argc - 2 ;
99 
100  for(unsigned int i=0 ; i < nFiles ; i++){
101  FILEN.push_back( argv[2+i] ) ; // 2+ because of program-name and output-file
102  }
103 
104  // create reader and writer for input and output streams
105  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
106 
107  try{
108  lcReader->open( FILEN ) ;
109  }
110  catch( IOException& e){
111  cout << "Can't open files : " << e.what() << endl ;
112  exit(1) ;
113  }
114 
115 
116  // create a new RunEventProcessor, register it with the reader
117  // and read and proccess the whole stream
118  {
119  RunEventProcessor evtProc( outFileName, nFiles ) ;
120 
121  lcReader->registerLCRunListener( &evtProc ) ;
122  lcReader->registerLCEventListener( &evtProc ) ;
123 
124  lcReader->readStream() ;
125  }
126 
127  lcReader->close() ;
128  delete lcReader ;
129 
130  }
131 
132  catch(exception& ex){
133  cout << "something went wrong: " << ex.what() << endl ;
134  }
135 
136  return 0 ;
137 
138 }
139 
140 //=============================================================================
141 
void modifyEvent(LCEvent *)
T endl(T...args)
Little tool that copies LCIO files on an event by event and run by run basis, thus fixing files that ...
Definition: copyfix.cc:23
void processEvent(LCEvent *evt)
T push_back(T...args)
T exit(T...args)
T what(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...
void processRunHeader(LCRunHeader *run)
STL class.
RunEventProcessor(const char *outFileName, unsigned int nFiles)
static std::vector< std::string > FILEN
LCReader * lcReader
Definition: lsh.cc:78
void modifyRunHeader(LCRunHeader *)