Re: helper macro for analysis

Richard Dubois (richard@SLAC.stanford.edu)
Thu, 18 Mar 1999 15:11:48 -0800


The problem with the Init function was found quickly: some sloppiness
in the order of setting things to zero masked casting problems and a
typo in the Init function itself.

This seems to work for Init now:

_________________________ MyEvent.h _________________________________

//////////////////////////////////////////////////////////
// This class is intended to provide useful manipulation
// of the Root Event loop. Users put their analysis code
// into the Go function (in MyEvent.C). They should not
// need to look at MyEvent.h (except to see the interface)
//
// It is targetted at the LCD Event structure, which has
// Event as the main branch with sub-branches. This version
// handles the case of reading back from a file. At the moment
// there must be separate versions for running the FastMC and
// FullRecon interactively.
//
// 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:
//
// .x startmacro.C // load shared libs
// .L MyEvent.C // 'complile' class
// MyEvent* m = new MyEvent("MyRootFile.root"); // create MyEvent
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->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 MyEvent.C.
//
// Version 0.1 17-Mar-1999 Richard Creation
//////////////////////////////////////////////////////////

#ifndef MyEvent_h
#define MyEvent_h

class MyEvent {
public :
TFile* f; // input file
TTree *fTree; //pointer to the analyzed TTree
Event* event;

MyEvent() {}; // default ctr
MyEvent(char* rootFileName); // ctr with root file name
~MyEvent() {;} // default dtr
void StartWithEvent(Int_t event); // start next Go with this event
void Init(char* rootFileName); // re-init with this root file
void AllHistClear(); // Reset() all user histograms
void AllHistDelete(); // delete all 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 nEvent=100000); // loop over events

private:
Int_t m_StartEvent; // starting event
Int_t HistsDefined; // flag for whether histograms have been defined

TObjArray* HistList; // list of user histograms
};

#endif

MyEvent::MyEvent(char* rootFileName)
{
// set up root file and init things

printf(" opening file %s \n",rootFileName);

f = 0; fTree = 0; event = 0;
Init(rootFileName);
m_StartEvent = 0;
}

void MyEvent::StartWithEvent(Int_t event) {
m_StartEvent = event;
}

void MyEvent::Init(char* rootFileName)
{
// re-initialize file, tree, event

AllHistDelete();
HistsDefined = 1;
HistList = 0;

// Set branch addresses

if (f) {
delete event;
delete fTree;
delete f;
}

f = new TFile(rootFileName);
fTree = (TTree*)gDirectory->Get("T");
event = new Event();

fTree->SetBranchAddress("Event",&event);
}

void MyEvent::Rewind()
{
// Start input file at beginning

m_StartEvent = 0;

return;
}

void MyEvent::MakeHistList() {
// make a TObjArray of histograms

if (HistList) delete HistList;

HistList = new TObjArray();

TList* list = gDirectory->GetList();
TIter iter(list);

TObject* obj = 0;

while (obj=iter.Next()) {

if (obj->InheritsFrom("TH1")) {
HistList->Add(obj);
}
}

}

void MyEvent::AllHistClear() {

for (Int_t i=0; i < HistList->GetEntries(); i++) {
TObject* obj = HistList->At(i);

((TH1*)HistList->At(i))->Reset();
}

}

void MyEvent::AllHistDelete() {

if (HistList) {

for (Int_t i=0; i < HistList->GetEntries(); i++) {
TObject* obj = HistList->At(i);
((TH1*)obj)->SetDirectory(0);
delete obj;
}

delete HistList;
}
}

--
Richard Dubois
SLD, Stanford Linear Accelerator Center
Richard@slac.stanford.edu
http://www.slac.stanford.edu/~richard/
650-926-3824
650-926-2923 (FAX)