LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stdhepjob.cc
Go to the documentation of this file.
1 
2 #include "lcio.h"
3 
4 #include "IO/LCWriter.h"
5 #include "EVENT/LCIO.h"
6 #include "IMPL/LCEventImpl.h"
7 #include "IMPL/LCRunHeaderImpl.h"
8 
9 #include "UTIL/LCStdHepRdr.h"
10 
11 #include <cstdlib>
12 #include <iostream>
13 #include <sstream>
14 #include <memory>
15 
16 
17 // using namespace std ;
18 using namespace lcio ;
19 
20 
25 int main(int argc, char** argv ){
26 
27  if(argc < 4) {
28 
29  std::cout << " usage: stdhepjob infile.stdhep outfile.slcio maxEvt " << std::endl
30  << " infile.stdhep - input file name " << std::endl
31  << " outfile.slcio - ouput file name " << std::endl
32  << " maxEvt - max number of events to read [-1: all]" << std::endl ;
33 
34  return 1;
35  }
36 
37  std::string inFile = argv[1] ;
38  std::string outFile = argv[2] ;
39  int maxEvt = std::atoi( argv[3] ) ;
40 
41 
42  std::cout << "==================================================== " << std::endl
43  << " stdhepjob : " << std::endl ;
44 
45  // Open an instance of the StdHep Reader with the given filename
46  LCStdHepRdr rdr( inFile.c_str() ) ;
47 
48  std::cout << " opened file : " << inFile << std::endl ;
49 
50  rdr.printHeader() ;
51 
52  // create sio writer
53  std::unique_ptr<LCWriter> lcWrt( LCFactory::getInstance()->createLCWriter() ) ;
54 
55  lcWrt->open( outFile ) ;
56 
57  std::unique_ptr<LCRunHeaderImpl> runHdr( new LCRunHeaderImpl ) ;
58 
59  runHdr->setRunNumber( 0 ) ;
60 
61  std::string detName("Unknown") ;
62  runHdr->setDetectorName( detName ) ;
63 
64  std::stringstream description ;
65 
66  description << " file generated with LCIO stdhepjob from " << inFile ;
67 
68  runHdr->setDescription( description.str() ) ;
69 
70  lcWrt->writeRunHeader( runHdr.get() ) ;
71 
72  int count = 0;
73 
74  try {
75 
76  while( maxEvt < 0 || count < maxEvt ){
77 
78  std::unique_ptr<LCEventImpl> evt( new LCEventImpl() ) ;
79 
80  evt->setRunNumber( 0 ) ;
81  evt->setEventNumber( count ) ;
82  evt->setDetectorName( detName ) ;
83 
84  // read the next stdhep event and add an MCParticle collection to the event
85  rdr.updateNextEvent( evt.get() , "MCParticle" ) ;
86 
87  lcWrt->writeEvent( evt.get() ) ;
88 
89  ++count ;
90 
91  } // evt loop
92  }
93 
94  catch( IO::EndOfDataException& e ) {
95  }
96 
97  std::cout << " converted " << count << " events - written to LCIO " << outFile << std::endl ;
98 
99  std::cout << "==================================================== "
100  << std::endl << std::endl ;
101 
102  lcWrt->close() ;
103 
104  return 0 ;
105 
106 }
107 
T atoi(T...args)
T endl(T...args)
STL class.
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
T str(T...args)
T get(T...args)
STL class.
LCRunHeader * runHdr
Definition: lsh.cc:79
T c_str(T...args)
EndOfDataException for signaling the end of a data stream.
Definition: Exceptions.h:108