/////////////////////////////////////////////////////////// /*! \class RootTreeAnalysis \brief This class is intended to provide useful manipulation of the Root Event loop. Users put their analysis code into the Go function (in RootTreeAnalysis.cxx). They should not need to look at RootTreeAnalysis.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("RootTreeAnalysis.C"); // 'compile' class RootTreeAnalysis* m = new RootTreeAnalysis("MyRootFile.root"); // create RootTreeAnalysis 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 RootTreeAnalysis.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 RootTreeAnalysis_h #define RootTreeAnalysis_h // Forward declarations class Event; class Recon; class RootTreeAnalysis { public : TFile *histFile; // histogram file TFile *f; // input raw file TFile *r; // input recon file TTree *rawTree; //pointer to the analyzed TTree TTree *reconTree; //pointer to the analyzed TTree Event *evt; Recon *rec; RootTreeAnalysis(); /// default constructor RootTreeAnalysis(char* rawFileName="", char* reconFileName=""); /// ctr with root file name ~RootTreeAnalysis(); /// default destructor void StartWithEvent(Int_t event); /// start next Go with this event void Init(char* rawFileName="",char* reconFileName=""); /// 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 RootTreeAnalysis::~RootTreeAnalysis() { // destructor: cleanup histFile->Write(); HClr(); if (histFile) delete histFile; if (HistList) delete HistList; if (f) delete f; if (evt) delete evt; if (r) delete r; if (rec) delete rec; } RootTreeAnalysis::RootTreeAnalysis() { f = 0; histFile=0; rawTree = 0; evt = 0; r = 0; reconTree = 0; rec = 0; } RootTreeAnalysis::RootTreeAnalysis(char* rawFileName,char* reconFileName) { // constructor: set up root file and init things printf(" opening files %s %s \n",rawFileName, reconFileName); f = 0; histFile=0; rawTree = 0; evt = 0; r = 0; reconTree = 0; rec = 0; HistList = 0; HistDefine(); MakeHistList(); Init(rawFileName,reconFileName); } void RootTreeAnalysis::StartWithEvent(Int_t event) { // set a starting event for Go. m_StartEvent = event; } void RootTreeAnalysis::Init(char* rawFileName, char* reconFileName) { // 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); } if (r) { delete rec; rec = 0; delete reconTree; reconTree = 0; delete r; r=0; } if (reconFileName != "") { r = new TFile(reconFileName); if (r->IsOpen() == kTRUE) { reconTree = (TTree*)gDirectory->Get("T"); rec = new Recon(); } else { r = 0; printf("recon data file could not be opened!!\n"); } reconTree->SetBranchAddress("Recon",&rec); } m_StartEvent = 0; } void RootTreeAnalysis::Rewind() { // Start input file at beginning m_StartEvent = 0; return; } void RootTreeAnalysis::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 RootTreeAnalysis::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 RootTreeAnalysis::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; } }