Logo ROOT   6.07/09
Reference Guide
clonesA_Event.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
4 /// Example to write & read a Tree built with a complex class inheritance tree.
5 /// It demonstrates usage of inheritance and TClonesArrays
6 /// This is simplified / stripped extract of an event structure which was used
7 /// within the Marabou project.
8 ///
9 /// To run this example, do:
10 /// ~~~
11 /// root > .x clonesA_Event.C
12 /// ~~~
13 /// \macro_code
14 ///
15 /// \author The ROOT Team
16 
17 #ifndef CLONESA_EVENT_SECOND_RUN
18 
19 void clonesA_Event() {
20  TString dir = gSystem->DirName(__FILE__);
21  gROOT->ProcessLine(TString(".L ")+dir+"/clonesA_Event.cxx+");
22 #define CLONESA_EVENT_SECOND_RUN yes
23  gROOT->ProcessLine("#include \"" __FILE__ "\"");
24  gROOT->ProcessLine("clonesA_Event(true)");
25 }
26 
27 #else
28 
29 void clonesA_Event_w()
30 {
31 // protect against old ROOT versions
32  if ( gROOT->GetVersionInt() < 30503 ) {
33  cout << "Works only with ROOT version >= 3.05/03" << endl;
34  return;
35  }
36  if ( gROOT->GetVersionDate() < 20030406 ) {
37  cout << "Works only with ROOT CVS version after 5. 4. 2003" << endl;
38  return;
39  }
40 
41  //write a Tree
42  TFile *hfile = new TFile("clonesA_Event.root","RECREATE","Test TClonesArray");
43  TTree *tree = new TTree("clonesA_Event","An example of a ROOT tree");
44  TUsrSevtData1 *event1 = new TUsrSevtData1();
45  TUsrSevtData2 *event2 = new TUsrSevtData2();
46  tree->Branch("top1","TUsrSevtData1",&event1,8000,99);
47  tree->Branch("top2","TUsrSevtData2",&event2,8000,99);
48  for (Int_t ev = 0; ev < 10; ev++) {
49  cout << "event " << ev << endl;
50  event1->SetEvent(ev);
51  event2->SetEvent(ev);
52  tree->Fill();
53  if (ev <3) tree->Show(ev);
54  }
55  tree->Write();
56  tree->Print();
57  delete hfile;
58 }
59 
60 void clonesA_Event_r()
61 {
62  //read the Tree
63  TFile * hfile = new TFile("clonesA_Event.root");
64  TTree *tree = (TTree*)hfile->Get("clonesA_Event");
65 
66  TUsrSevtData1 * event1 = 0;
67  TUsrSevtData2 * event2 = 0;
68  tree->SetBranchAddress("top1",&event1);
69  tree->SetBranchAddress("top2",&event2);
70  for (Int_t ev = 0; ev < 8; ev++) {
71  tree->Show(ev);
72  cout << "Pileup event1: " << event1->GetPileup() << endl;
73  cout << "Pileup event2: " << event2->GetPileup() << endl;
74  event1->Clear();
75  event2->Clear();
76  // gObjectTable->Print(); // detect possible memory leaks
77  }
78  delete hfile;
79 }
80 
81 void clonesA_Event(bool /*secondrun*/) {
82  // Embedding this load inside the first run of the script is not yet
83  // supported in v6
84  // gROOT->ProcessLine(".L clonesA_Event.cxx+"); // compile shared lib
85  clonesA_Event_w(); // write the tree
86  clonesA_Event_r(); // read back the tree
87 }
88 
89 #endif
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:364
Int_t GetPileup()
Definition: clonesA_Event.h:86
Basic string class.
Definition: TString.h:137
void SetEvent(Int_t ev)
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:997
void Clear(Option_t *="")
Definition: clonesA_Event.h:51
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6689
Int_t GetPileup()
Definition: clonesA_Event.h:69
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7719
virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
Print values of all active leaves for entry.
Definition: TTree.cxx:8620
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9001
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
void SetEvent(Int_t ev)
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1651
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98