ARA ROOT Test BEd Software

utilities/makeAraEventTree.cxx

00001 #include <cstdio>
00002 #include <fstream>
00003 #include <iostream>
00004 #include <zlib.h>
00005 #include <libgen.h>     
00006  
00007 using namespace std;
00008 
00009 #include "TTree.h"
00010 #include "TFile.h"
00011 #include "TSystem.h"
00012 
00013 #define HACK_FOR_ROOT
00014 
00015 #include "araStructures.h"
00016 #include "RawAraEvent.h"  
00017 
00018 void processEvent();
00019 void makeEventTree(char *inputName, char *outDir);
00020 
00021 AraEventBody_t theEventBody;
00022 TFile *theFile;
00023 TTree *eventTree;
00024 RawAraEvent *theEvent=0;
00025 char outName[FILENAME_MAX];
00026 char errorLog[FILENAME_MAX];
00027 UInt_t realTime;
00028 Int_t runNumber;
00029 Int_t lastRunNumber;
00030 Int_t badEventCount, dodgyEventCount=0;
00031 
00032 int main(int argc, char **argv) {
00033   if(argc<3) {
00034     std::cout << "Usage: " << basename(argv[0]) << " <file list> <out file>" << std::endl;
00035     return -1;
00036   }
00037   makeEventTree(argv[1],argv[2]);
00038   return 0;
00039 }
00040   
00041 
00042 void makeEventTree(char *inputName, char *outFile) {
00043 
00044   bool debug = false; // set to true to increase the amount of commentary output
00045 
00046   cout << "user specification - input file list = " << inputName << "\t" << "outFile = " << outFile << endl;
00047   strncpy(outName,outFile,FILENAME_MAX);
00048   if ( debug ) {
00049      cout << "                   - full outFile = " << outFile << endl;
00050   }
00051 
00052   
00053   theEvent = new RawAraEvent();
00054   //    cout << sizeof(AraEventBody_t) << endl;
00055   ifstream SillyFile(inputName);
00056 
00057   //create a errorLog file with the number of bad events
00058   size_t len=strlen(outName);
00059   strncpy(errorLog,outName,len-5);
00060   sprintf(errorLog, "%sErrorLog.txt", errorLog);
00061   ofstream errorFile;
00062   errorFile.open(errorLog);
00063   errorFile << "Output from makeAraEventTree\n------------------------------------------\n\n";
00064   errorFile << "Opening file " << inputName << endl;
00065 
00066   int numBytes=0;
00067   char fileName[FILENAME_MAX];
00068   int error=0;
00069   //    int eventNumber=1;
00070 
00071   int total_evt_count = 0;
00072   int file_count=0;
00073   while( SillyFile >> fileName ) {
00074     cout << "processing file: " << fileName << endl;
00075     file_count++;
00076     static int lastEventNumber=-1;    
00077     const char *subDir = gSystem->DirName(fileName);
00078     //    const char *subSubDir = gSystem->DirName(subDir);
00079     //    const char *eventDir = gSystem->DirName(subSubDir);
00080     const char *runDir = gSystem->DirName(subDir);
00081     const char *justRun = gSystem->BaseName(runDir);
00082     sscanf(justRun,"run_%d",&runNumber);
00083     //    cout << justRun << "\t" << runNumber <<endl;
00084     
00085     gzFile infile = gzopen(fileName, "rb");    
00086     numBytes=gzread(infile,&theEventBody,sizeof(AraEventBody_t));
00087     int evt_count = 1;
00088     total_evt_count++;
00089     while ( numBytes == sizeof(AraEventBody_t) ) {
00090       if ( debug                      ||
00091            ( (evt_count % 100) == 1 ) ) {
00092          cout << "Event count: " << "for_file = " << evt_count << " - all_toll = " << total_evt_count << endl;
00093          errorFile << "Event count: " << "for_file = " << evt_count << " - all_toll = " << total_evt_count << endl;
00094 
00095       }
00096       if(numBytes!=sizeof(AraEventBody_t)) {
00097         if(numBytes){
00098           cerr << "Read problem: " <<numBytes << " of " << sizeof(AraEventBody_t) << endl;
00099           errorFile << "Read Problem: " <<numBytes << " of " << sizeof(AraEventBody_t) << endl;
00100         }
00101         error=1;
00102         break;
00103       }
00104       //      cout << "Event: " << theEventBody.hd.eventNumber << endl;
00105       if(TMath::Abs(Double_t(theEventBody.hd.eventNumber)-lastEventNumber)>1000 && lastEventNumber>=0) {
00106         std::cerr << "Dodgy event\t" << theEventBody.hd.eventNumber << "\n";
00107         errorFile << "Dodgy event\t" << theEventBody.hd.eventNumber << "\n";
00108         dodgyEventCount++;
00109       } 
00110       else {     
00111         processEvent();
00112         lastEventNumber=theEventBody.hd.eventNumber;
00113       }
00114       numBytes=gzread(infile,&theEventBody,sizeof(AraEventBody_t));
00115       evt_count++;
00116       total_evt_count++;
00117     } // end of while loop over events in the input file
00118     gzclose(infile);
00119     //  if(error) break;
00120 
00121   } // end of while loop over the ev_ files in the file list
00122   eventTree->AutoSave();
00123   //    theFile->Close();
00124 
00125   errorFile << "\n" << "Total number of events was " <<  total_evt_count << "\nNumber of bad events was " << badEventCount
00126             << " Number of dodgy events was " << dodgyEventCount << endl;
00127 
00128   errorFile.close();
00129 
00130 }
00131 
00132 void processEvent() {
00133   //  cout << "processEvent:\t" << theEventBody.eventNumber << endl;
00134   static int doneInit=0;
00135   
00136   if(!doneInit) {
00137     //    char dirName[FILENAME_MAX];
00138     //    char fileName[FILENAME_MAX];
00139     //    sprintf(dirName,"%s/run%d",outDirName,runNumber);
00140     //    gSystem->mkdir(dirName,kTRUE);
00141     //    sprintf(fileName,"%s/eventFile%d.root",dirName,runNumber);
00142     cout << "Creating File: " << outName << endl;
00143     theFile = new TFile(outName,"RECREATE");
00144     eventTree = new TTree("eventTree","Tree of ARA Events");
00145     eventTree->Branch("run",&runNumber,"run/I");
00146     eventTree->Branch("event","RawAraEvent",&theEvent);
00147     
00148     doneInit=1;
00149   }  
00150   //  cout << "Here: "  << theEvent.eventNumber << endl;
00151   if(theEvent) delete theEvent;
00152   theEvent = new RawAraEvent(&theEventBody);
00153   
00154   if(theEvent->getFirstHitBus(25)!=theEvent->getFirstHitBus(26)) {
00155      std::cerr << "Bad event?\n"; 
00156      ++badEventCount;
00157 
00158   }
00159   else {
00160      eventTree->Fill();  
00161   }
00162   lastRunNumber=runNumber;
00163   //  delete theEvent;
00164 }

Generated on Wed Aug 8 16:18:56 2012 for ARA ROOT Test Bed Software by doxygen 1.4.7