ARA ROOT Test BEd Software

utilities/makeAraCalibratedEventTree.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 #include "UsefulAraEvent.h"
00018 
00019 void processEvent();
00020 void makeEventTree(char *inputName, char *outDir);
00021 
00022 AraEventBody_t theEventBody;
00023 TFile *theFile;
00024 TTree *eventTree;
00025 RawAraEvent *theEvent=0;
00026 UsefulAraEvent *theUsefulEvent=0;
00027 char outName[FILENAME_MAX];
00028 UInt_t realTime;
00029 Int_t runNumber;
00030 Int_t lastRunNumber;
00031 
00032 
00033 int main(int argc, char **argv) {
00034   if(argc<3) {
00035     std::cout << "Usage: " << basename(argv[0]) << " <file list> <out file>" << std::endl;
00036     return -1;
00037   }
00038   makeEventTree(argv[1],argv[2]);
00039   return 0;
00040 }
00041   
00042 
00043 void makeEventTree(char *inputName, char *outFile) {
00044 
00045   bool debug = false; // set to true to increase the amount of commentary output
00046 
00047   cout << "user specification - input file list = " << inputName << "\t" << "outFile = " << outFile << endl;
00048   strncpy(outName,outFile,FILENAME_MAX);
00049   if ( debug ) {
00050      cout << "                   - full outFile = " << outFile << endl;
00051   }
00052   theEvent = new RawAraEvent();
00053   theUsefulEvent = new UsefulAraEvent();
00054 
00055   //    cout << sizeof(AraEventBody_t) << endl;
00056   ifstream SillyFile(inputName);
00057 
00058   int numBytes=0;
00059   char fileName[FILENAME_MAX];
00060   int error=0;
00061   //    int eventNumber=1;
00062 
00063   int total_evt_count = 0;
00064   int file_count=0;
00065   while( SillyFile >> fileName ) {
00066     cout << "processing file: " << fileName << endl;
00067     file_count++;
00068     static int lastEventNumber=-1;    
00069     const char *subDir = gSystem->DirName(fileName);
00070     //    const char *subSubDir = gSystem->DirName(subDir);
00071     //    const char *eventDir = gSystem->DirName(subSubDir);
00072     const char *runDir = gSystem->DirName(subDir);
00073     const char *justRun = gSystem->BaseName(runDir);
00074     sscanf(justRun,"run_%d",&runNumber);
00075     //    cout << justRun << "\t" << runNumber <<endl;
00076     
00077     gzFile infile = gzopen(fileName, "rb");    
00078     numBytes=gzread(infile,&theEventBody,sizeof(AraEventBody_t));
00079     int evt_count = 1;
00080     total_evt_count++;
00081     while ( numBytes == sizeof(AraEventBody_t) ) {
00082       if ( debug                      ||
00083            ( (evt_count % 100) == 1 ) ) {
00084          cout << "Event count: " << "for_file = " << evt_count << " - all_toll = " << total_evt_count << endl;
00085       }
00086       if(numBytes!=sizeof(AraEventBody_t)) {
00087         if(numBytes)
00088           cerr << "Read problem: " <<numBytes << " of " << sizeof(AraEventBody_t) << endl;
00089         error=1;
00090         break;
00091       }
00092       //      cout << "Event: " << theEventBody.hd.eventNumber << endl;
00093       if(TMath::Abs(Double_t(theEventBody.hd.eventNumber)-lastEventNumber)>1000 && lastEventNumber>=0) {
00094         std::cerr << "Dodgy event\t" << theEventBody.hd.eventNumber << "\n";
00095       } 
00096       else {     
00097         processEvent();
00098         lastEventNumber=theEventBody.hd.eventNumber;
00099       }
00100       numBytes=gzread(infile,&theEventBody,sizeof(AraEventBody_t));
00101       evt_count++;
00102       total_evt_count++;
00103     } // end of while loop over events in the input file
00104     gzclose(infile);
00105     //  if(error) break;
00106 
00107   } // end of while loop over the ev_ files in the file list
00108   eventTree->AutoSave();
00109   //    theFile->Close();
00110 }
00111 
00112 void processEvent() {
00113   //  cout << "processEvent:\t" << theEventBody.eventNumber << endl;
00114   static int doneInit=0;
00115   
00116   if(!doneInit) {
00117     //    char dirName[FILENAME_MAX];
00118     //    char fileName[FILENAME_MAX];
00119     //    sprintf(dirName,"%s/run%d",outDirName,runNumber);
00120     //    gSystem->mkdir(dirName,kTRUE);
00121     //    sprintf(fileName,"%s/eventFile%d.root",dirName,runNumber);
00122     cout << "Creating File: " << outName << endl;
00123     theFile = new TFile(outName,"RECREATE");
00124     eventTree = new TTree("eventTree","Tree of ARA Events");
00125     eventTree->Branch("run",&runNumber,"run/I");
00126     eventTree->Branch("event","RawAraEvent",&theEvent);
00127     eventTree->Branch("calevent","UsefulAraEvent",&theUsefulEvent);
00128     
00129     doneInit=1;
00130   }
00131   //  cout << "Here: "  << theEvent.eventNumber << endl;
00132   if(theEvent) delete theEvent;
00133   if(theUsefulEvent) delete theUsefulEvent;
00134   theEvent = new RawAraEvent(&theEventBody);
00135   theUsefulEvent = new UsefulAraEvent( theEvent , AraCalType::kFirstCalib );
00136   eventTree->Fill();  
00137   lastRunNumber=runNumber;
00138   //  delete theEvent;
00139 }

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