Histos, TMultiGraph objects as a members of TTree

Dear Rooters,

I would like to save for each event in the tree corresponding TH1F histos, and TMulitGraph objects (for checking and other reasons). I meet two problems with this:

  1. I save TH1F histo as follows:

in event.h:

class event: public TObject {
public:
SetHisto(TH1F* p ) {p->Copy(fHisto)};
private:
TH1F fHisto; //|| histo to save as a leaf
};

in fill.cc

TH1F *p = new TH1F(…);
p->Fill(something);
event->SetHisto§;

Afterwards it is saved into the root file without any complains. The problem is : how to access it for each event? Is there an elegant way to navigate event by event and plot the histos?

  1. second problem is with TMultiGraph object: I do not understand how to save it to event? Copy() does not work (it does not comlain but also does nothing), there is no copy constructor, …

Please help if you know the solution.
Best regards, Dmitry

Why do you want to copy the TH1 and TMultiGraph. Simply declare two pointers in your Event class
TH1 * fHisto;
TMultiGraph *fGraph;

having 2 functions
SetHisto(TH1* h) {fHisto=h;}
SetGraph("TMultiGraph *g) {fGraph=g;}

A copy of the two objects will be automatically saved by TTree::Fill

Rene

Thank you Rene!

It works (saves in the tree). I am still missed how to read it back. Let me illustrate my problem by the following small test macro. It can be run as
root[] .L testTMultiGraph.C
root[] testTMultiGraph()
root[] Plot(1);

it crashes when I want to access the TMultiGraph object because most probably I am doing it wrongly.
Thanks a lot.
Dmitry

marco testTMultiGraph.C follows:


class test: public TObject {
public:
test() {};
~test() {};
SetGraph(TMultiGraph p) {fGraph = p;}
SetX(Double_t x) {fX = x;}
TMultiGraph
GetGraph() {return fGraph;}
private:
Double_t fX; //
TMultiGraph* fGraph; //||
};

void testTMultiGraph (Int_t MaxEv=10) {
test event = new test();
TTree
fRecoTree = 0;
TFile fFile = new TFile(“testTMultiGraph.root”,“RECREATE”);
fRecoTree = new TTree(“tree”,“TestTree”);
fRecoTree->Branch(“events”,“test”,&event);
Double_t x[10],y[10];
Int_t ev(0);
while (ev++<MaxEv) {
for (Int_t i=0;i<10;i++) {
x[i] = i;
y[i] = i
i;
}
TGraph *g1 = new TGraph(10,x,y);
g1->SetMarkerSize(.5);
g1->SetMarkerColor(kRed);
g1->SetMarkerStyle(23);
TMultiGraph *gAll = new TMultiGraph(“TestGraph”,"");
gAll->Add(g1,“lp”);
event->SetGraph(gAll);
event->SetX(ev);
fRecoTree->Fill();
cout << "Event # " << ev << endl;
}
fFile->Write();
fFile->Close();

}

void Plot(Int_t ev=0) {
TFile f(“testTMultiGraph.root”);
TTree p = (TTree)f.Get(“tree”);
test* event = new test();
p->SetBranchAddress(“events”,&event);
p->LoadTree(ev); // crashes here…
// p->GetEntry(ev); // and here
TMultiGraph* fGraph = event->GetGraph();
fGraph->Draw();

}

There were several problems in your example. See corrected and working example in the attachement

Rene
atest.C (1.28 KB)

Great! Thank you very much for your help!
Dmitry