/////////////////////////////////////////////////////////// /*! \class RawTreeAnalysis \brief This class is intended to provide useful manipulation of the Root Event loop. Users put their analysis code into the Go function (in RawTreeAnalysis.cxx). They should not need to look at RawTreeAnalysis.h (except to see the interface) allows for: init and re-init use of a Root file clear all histograms 'go n events' allowing to continue on in the file or 'rewind' Example of use: gROOT->Macro("startmacro.C") // load shared libs gROOT->LoadMacro("RawTreeAnalysis.C"); // 'compile' class RawTreeAnalysis* m = new RawTreeAnalysis("MyRootFile.root"); // create RawTreeAnalysis object m->Go(500); // loop over 500 events. Go contains your analysis code ... look at histograms ... m->Go() // look at remainder of file ... look at histograms ... m->HClr(); // clear histograms m->Init("AnotherRootFile.root"); m->Go(50); ... and so on ... After editing your Go function, you need to issue a gROOT->Reset() and repeat the above sequence starting from the .L RawTreeAnalysis.C. Version 0.1 17-Mar-1999 Richard Creation Version 1.0 Spring, 2000 Revised for use with GLAST 1999 TestBeam Version 2.0 25-Mar-2001 Revised for use with GLAST 2001 Balloon */ #ifndef RawTreeAnalysis_h #define RawTreeAnalysis_h // Forward declarations class Event; class RawTreeAnalysis { public : TFile *histFile; // histogram file TFile *f; // input raw file TFile *r; // input recon file TTree *rawTree; //pointer to the analyzed TTree Event *evt; RawTreeAnalysis(); /// default constructor RawTreeAnalysis(char* rawFileName=""); /// ctr with root file name ~RawTreeAnalysis(); /// default destructor void StartWithEvent(Int_t event); /// start next Go with this event void Init(char* rawFileName=""); /// re-init with this root file void HClr(); /// Reset() all user histograms void AllHistDelete(); /// delete all user histograms void HistDefine(); /// define user histograms void MakeHistList(); /// make list of user histograms void Rewind(); /// reset for next Go to start at beginning of file void Go(Int_t numEvents=100000); /// loop over events private: /// starting event Int_t m_StartEvent; /// list of user histograms TObjArray* HistList; }; #endif RawTreeAnalysis::~RawTreeAnalysis() { // destructor: cleanup histFile->Write(); HClr(); if (histFile) delete histFile; if (HistList) delete HistList; if (f) delete f; if (evt) delete evt; } RawTreeAnalysis::RawTreeAnalysis() { f = 0; histFile=0; rawTree = 0; evt = 0; } RawTreeAnalysis::RawTreeAnalysis(char* rawFileName) { // constructor: set up root file and init things printf(" opening files %s \n",rawFileName); f = 0; histFile=0; rawTree = 0; evt = 0; HistList = 0; HistDefine(); MakeHistList(); Init(rawFileName); } void RawTreeAnalysis::StartWithEvent(Int_t event) { // set a starting event for Go. m_StartEvent = event; } void RawTreeAnalysis::Init(char* rawFileName) { // re-initialize file, tree, event. Histograms are *not* cleared. // Set branch addresses if (f) { delete evt; evt = 0; delete rawTree; rawTree = 0; delete f; f=0; } if (rawFileName != "") { f = new TFile(rawFileName); if (f->IsOpen() == kTRUE) { rawTree = (TTree*)gDirectory->Get("T"); evt = new Event(); } else { f = 0; printf("raw data file could not be opened!!\n"); } rawTree->SetBranchAddress("Event",&evt); } m_StartEvent = 0; } void RawTreeAnalysis::Rewind() { // Start input file at beginning m_StartEvent = 0; return; } void RawTreeAnalysis::MakeHistList() { // make a TObjArray of histograms if (HistList) delete HistList; TFile *histFile = (TFile*)gROOT->GetFile("Histograms.root"); HistList = new TObjArray(); TList* list = histFile->GetList(); TIter* iter = new TIter(list); TObject* obj = 0; while (obj=iter->Next()) { if (obj->InheritsFrom("TH1")) { HistList->Add(obj); } } delete iter; } void RawTreeAnalysis::HClr() { // clear histograms if (!HistList) return; for (Int_t i=0; i < HistList->GetEntries(); i++) { TObject* obj = HistList->At(i); ((TH1*)HistList->At(i))->Reset(); } } void RawTreeAnalysis::AllHistDelete() { // delete histograms if (HistList) { for (Int_t i=0; i < HistList->GetEntries(); i++) { TObject* obj = HistList->At(i); ((TH1*)obj)->SetDirectory(0); delete obj; } delete HistList; } }