LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcio_split_file.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "UTIL/LCSplitWriter.h"
4 #include "IO/LCReader.h"
5 #include "IO/LCEventListener.h"
6 #include "IO/LCRunListener.h"
7 
8 #include "EVENT/LCIO.h"
9 
10 
11 #include <cstdlib>
12 #include <iostream>
13 #include <sstream>
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 
29 public:
30 
31  RunEventProcessor(const RunEventProcessor&) = delete ;
32  RunEventProcessor operator=(const RunEventProcessor&) = delete ;
33 
34  RunEventProcessor(const char* outFileName, int splitSize) : nEvent(0) {
35 
36  // open outputfile
37  lcWrt = new LCSplitWriter( LCFactory::getInstance()->createLCWriter(), splitSize ) ;
38 
39  try{ lcWrt->open( outFileName ) ; }
40 
41  catch(IOException& e){
42  cout << "[RunEventProcessor()] Can't open file for writing - "
43  << e.what() << endl ;
44  exit(1) ;
45  }
46 
47  }
48 
50  // close outputfile
51  lcWrt->close() ;
52  cout << endl << " " << nEvent << " events copied ! " << endl ;
53  }
54 
55  void modifyEvent( LCEvent * /*evt*/ ) { /*no changes to event ! */ ; }
56 
57  void processEvent( LCEvent * evt ) {
58 
59  // just copy events to outputfiles
60  lcWrt->writeEvent( evt ) ;
61  nEvent ++ ;
62  // cout << " event: " << evt->getEventNumber()
63  // << " [run: " << evt->getRunNumber() << "] copied" << endl ;
64  }
65 
66  void modifyRunHeader(LCRunHeader* /*run*/){ /*no changes to event ! */ ;}
67 
68  // don't manipulate run headers - use analyze
69  void processRunHeader( LCRunHeader* run){
70 
71  // just copy run headers to the outputfile
72  lcWrt->writeRunHeader( run ) ;
73 // cout << "run : " << run->getRunNumber() << " ["
74 // << run->getDescription() << "] - header copied " << endl ;
75  }
76 
77 } ;
78 
79 //=============================================================================
80 
81 int main(int argc, char** argv ){
82 
83  char* inFileName ;
84  char* outFileName ;
85 
86  try{ // a large try block for debugging ....
87 
88  // create reader and writer for input and output streams
89  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
90 
91  if( argc != 4 ){
92  cout << " usage: lcio_splitfile infilename outfilename sizeInBytes \n"
93  << " e.g.: lcio_split_file simjob.slcio splitjob.slcio 200000 \n"
94  << endl ;
95  exit(1) ;
96  }
97  // read file names from command line
98  inFileName = argv[1] ;
99  outFileName = argv[2] ;
100  std::stringstream ss( argv[3] ) ;
101  int splitSize ;
102  ss >> splitSize ;
103 
104  try{ lcReader->open( inFileName ) ; }
105 
106  catch( IOException& e){
107  cout << "Can't open file : " << e.what() << endl ;
108  exit(1) ;
109  }
110 
111  // create a new RunEventProcessor, register it with the reader
112  // and read and proccess the whole stream
113  {
114  RunEventProcessor evtProc( outFileName, splitSize ) ;
115 
116  lcReader->registerLCRunListener( &evtProc ) ;
117  lcReader->registerLCEventListener( &evtProc ) ;
118 
119  lcReader->readStream() ;
120  }
121 
122  lcReader->close() ;
123 
124  }
125 
126  catch(exception& ex){
127  cout << "something went wrong: " << ex.what() << endl ;
128  }
129 
130  return 0 ;
131 
132 }
133 
134 //=============================================================================
135 
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)
RunEventProcessor(const char *outFileName, int splitSize)
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.
LCReader * lcReader
Definition: lsh.cc:78
void modifyRunHeader(LCRunHeader *)