/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/EXAMPLE/copyfix.cc

Go to the documentation of this file.
00001 #include "lcio.h"
00002 
00003 #include "IO/LCWriter.h"
00004 #include "IO/LCReader.h"
00005 #include "IO/LCEventListener.h"
00006 #include "IO/LCRunListener.h"
00007 
00008 #include "EVENT/LCIO.h"
00009 
00010 
00011 #include <cstdlib>
00012 #include <iostream>
00013 
00014 using namespace std ;
00015 using namespace lcio ;
00016 
00017 
00018 /**Little tool that copies LCIO files on an event by event and run by run basis,
00019  * thus fixing files that have incomplete records at the end due to a program crash.
00020  */
00021 
00022 // class for processing run and event records 
00023 class RunEventProcessor : public LCRunListener, public LCEventListener{
00024   
00025 protected:
00026   LCWriter* lcWrt ;
00027   int nEvent ;
00028   
00029 public:
00030   
00031   RunEventProcessor(const char* outFileName) : nEvent(0) {
00032     
00033     // open outputfile
00034     lcWrt = LCFactory::getInstance()->createLCWriter() ;
00035 
00036     try{ lcWrt->open( outFileName , LCIO::WRITE_NEW ) ; } 
00037     
00038     catch(IOException& e){
00039       cout << "[RunEventProcessor()] Can't open file for writing -  " 
00040            << e.what()  << endl ;
00041       exit(1) ;
00042     }
00043     
00044   }
00045   
00046   ~RunEventProcessor(){
00047     // close outputfile
00048     lcWrt->close()  ;
00049     cout << endl << "      "  << nEvent << " events copied ! " << endl ; 
00050   }
00051   
00052   void modifyEvent( LCEvent * evt ) {  /*no changes to event ! */ ; }
00053 
00054   void processEvent( LCEvent * evt ) {  
00055     
00056     // just copy events to outputfiles  
00057     lcWrt->writeEvent( evt ) ;
00058     nEvent ++ ;
00059 //     cout << "         event: " << evt->getEventNumber() 
00060 //       << " [run: " << evt->getRunNumber() << "] copied" << endl ;
00061   }
00062 
00063   void modifyRunHeader(LCRunHeader* run){  /*no changes to event ! */ ;}
00064 
00065   // don't manipulate run headers - use analyze 
00066   void processRunHeader( LCRunHeader* run){
00067 
00068     // just copy run headers to the outputfile
00069     lcWrt->writeRunHeader( run ) ;
00070 //     cout << "run : " << run->getRunNumber() << " [" 
00071 //       << run->getDescription() << "] - header copied " << endl ;
00072   }
00073 
00074 } ;
00075 
00076 //=============================================================================
00077 
00078 int main(int argc, char** argv ){
00079   
00080   char* inFileName  ;
00081   char* outFileName ;
00082 
00083   try{ // a large try block for debugging ....
00084     
00085     // create reader and writer for input and output streams 
00086     LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
00087     
00088     if( argc < 3 ){
00089       cout << "usage: copyfix infilename outfilename " << endl ;
00090       exit(1)  ;
00091     }
00092     // read file names from command line 
00093     inFileName = argv[1] ;
00094     outFileName  = argv[2] ;
00095 
00096 
00097     try{  lcReader->open( inFileName ) ; } 
00098     
00099     catch( IOException& e){
00100       cout << "Can't open file : " << e.what()  << endl ;
00101       exit(1) ;
00102     }
00103     
00104     // create a new RunEventProcessor, register it with the reader
00105     // and read and proccess the whole stream 
00106     {
00107       RunEventProcessor evtProc( outFileName)  ;
00108       
00109       lcReader->registerLCRunListener( &evtProc ) ; 
00110       lcReader->registerLCEventListener( &evtProc ) ; 
00111       
00112       lcReader->readStream() ;
00113     } 
00114     
00115     lcReader->close() ;
00116 
00117   }
00118 
00119   catch(exception& ex){
00120     cout << "something went wrong:  " << ex.what() << endl ; 
00121   }
00122 
00123   return 0 ;
00124   
00125 }
00126 
00127 //=============================================================================
00128 

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7