#include #include "TFile.h" #include "TTree.h" #include "TBrowser.h" #include "TH2.h" #include "TRandom.h" #include "TROOT.h" #include "TString.h" // This example is a variant of hsimple.C but using a TTree instead // of a TNtuple. It shows : // -how to fill a Tree with a few simple variables. // -how to read this Tree // -how to browse and analyze the Tree via the TBrowser and TTreeViewer // This example can be run in many different ways: // way1: .x macroTree1.C using the CINT interpreter // way2: .x macroTree1.C++ using the automatic compiler interface // way3: .L macroTree1.C or .L macroTree1.C++ // macroTree1() // One can also run the write and read parts in two separate sessions. // For example following one of the sessions above, one can start the session: // .L macroTree1.C // tree1r(); // // Author: Rene Brun // Modified: Christian Stratowa class ClassA { //class ClassA: public TObject { private: Float_t fPx; Float_t fPy; Float_t fPz; public: ClassA() {} virtual ~ClassA() {} void SetPx(Float_t px) {fPx = px;} void SetPy(Float_t py) {fPy = py;} void SetPz(Float_t pz) {fPz = pz;} Float_t GetPx() const {return fPx;} Float_t GetPy() const {return fPy;} Float_t GetPz() const {return fPz;} #if !defined (__CINT__) || defined (__MAKECINT__) ClassDef(ClassA,1) #endif }; class ClassB: public ClassA { private: Double_t fRandom; TString fString; public : ClassB() {} virtual ~ClassB() {} void SetRandom(Double_t rd) {fRandom = rd;} void SetString(const char *str) {fString = TString(str,32);} Double_t GetRandom() const {return fRandom;} const char* GetString() const {return fString.Data();} #if !defined (__CINT__) || defined (__MAKECINT__) ClassDef(ClassB,1) //ClassB #endif }; class ClassC: public ClassB { private: Int_t fEvent; public : ClassC() {} virtual ~ClassC() {} void SetEvent(Int_t ev) {fEvent = ev;} Int_t GetEvent() const {return fEvent;} #if !defined (__CINT__) || defined (__MAKECINT__) ClassDef(ClassC,1) //MainFrame #endif }; #if !defined (__CINT__) || defined (__MAKECINT__) ClassImp(ClassA); ClassImp(ClassB); ClassImp(ClassC); #endif void tree1w(Int_t size) { //create a Tree file tree1.root cout << "------tree1w------" << endl; //create the file TFile f("tree1.root","recreate"); // create new tree TTree t1("t1","a simple Tree with simple classes"); ClassC *myC = new ClassC(); t1.Branch("BranchC", "ClassC", &myC, 64000, 99); //fill the tree Float_t px, py, pz; Double_t random; TString str; Int_t ev; for (Int_t i=0;iRannor(px,py); pz = px*px + py*py; random = gRandom->Rndm(); str = TString(""); str += random; ev = i; if(i<4)cout << "px = " << px << " random = " << random << " str = " << str << endl; myC->SetPx(px); myC->SetPy(py); myC->SetPz(pz); myC->SetRandom(random); myC->SetString(str); myC->SetEvent(ev); t1.Fill(); } //save the Tree header. The file will be automatically closed //when going out of the function scope t1.Write(); } void tree1r() { //read the Tree generated by tree1w and fill two histograms cout << "------tree1w------" << endl; //note that we use "new" to create the TFile and TTree objects ! //because we want to keep these objects alive when we leave this function. TFile *f = new TFile("tree1.root"); ClassC *myC = 0; TTree *t1 = (TTree*)f->Get("t1"); t1->SetBranchAddress("BranchC",&myC); //read all entries and fill the histograms Float_t px, py, pz; Double_t random; TString str = ""; Int_t ev; Int_t nentries = (Int_t)t1->GetEntries(); for (Int_t i=0;iGetEntry(i); px = myC->GetPx(); py = myC->GetPy(); random = myC->GetRandom(); str = myC->GetString(); if(i<4)cout << "px = " << px << " random = " << random << " str = " << str << endl; } } void macroTree1(Int_t size = 10000) { tree1w(size); tree1r(); }