Logo ROOT  
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
19void clonesA_Event() {
20 std::string s1(__FILE__);
21 TString dir = gSystem->UnixPathName(s1.substr(0, s1.find_last_of("\\/")).c_str());
22 gROOT->ProcessLine(TString(".L ")+dir+"/clonesA_Event.cxx+");
23#define CLONESA_EVENT_SECOND_RUN yes
24 gROOT->ProcessLine("#include \"" __FILE__ "\"");
25 gROOT->ProcessLine("clonesA_Event(true)");
26}
27
28#else
29
30void clonesA_Event_w()
31{
32// protect against old ROOT versions
33 if ( gROOT->GetVersionInt() < 30503 ) {
34 cout << "Works only with ROOT version >= 3.05/03" << endl;
35 return;
36 }
37 if ( gROOT->GetVersionDate() < 20030406 ) {
38 cout << "Works only with ROOT CVS version after 5. 4. 2003" << endl;
39 return;
40 }
41
42 //write a Tree
43 TFile *hfile = new TFile("clonesA_Event.root","RECREATE","Test TClonesArray");
44 TTree *tree = new TTree("clonesA_Event","An example of a ROOT tree");
45 TUsrSevtData1 *event1 = new TUsrSevtData1();
46 TUsrSevtData2 *event2 = new TUsrSevtData2();
47 tree->Branch("top1","TUsrSevtData1",&event1,8000,99);
48 tree->Branch("top2","TUsrSevtData2",&event2,8000,99);
49 for (Int_t ev = 0; ev < 10; ev++) {
50 cout << "event " << ev << endl;
51 event1->SetEvent(ev);
52 event2->SetEvent(ev);
53 tree->Fill();
54 if (ev <3) tree->Show(ev);
55 }
56 tree->Write();
57 tree->Print();
58 delete hfile;
59}
60
61void clonesA_Event_r()
62{
63 //read the Tree
64 TFile * hfile = new TFile("clonesA_Event.root");
65 TTree *tree = (TTree*)hfile->Get("clonesA_Event");
66
67 TUsrSevtData1 * event1 = 0;
68 TUsrSevtData2 * event2 = 0;
69 tree->SetBranchAddress("top1",&event1);
70 tree->SetBranchAddress("top2",&event2);
71 for (Int_t ev = 0; ev < 8; ev++) {
72 tree->Show(ev);
73 cout << "Pileup event1: " << event1->GetPileup() << endl;
74 cout << "Pileup event2: " << event2->GetPileup() << endl;
75 event1->Clear();
76 event2->Clear();
77 // gObjectTable->Print(); // detect possible memory leaks
78 }
79 delete hfile;
80}
81
82void clonesA_Event(bool /*secondrun*/) {
83 // Embedding this load inside the first run of the script is not yet
84 // supported in v6
85 // gROOT->ProcessLine(".L clonesA_Event.cxx+"); // compile shared lib
86 clonesA_Event_w(); // write the tree
87 clonesA_Event_r(); // read back the tree
88}
89
90#endif
#define s1(x)
Definition: RSha256.hxx:91
int Int_t
Definition: RtypesCore.h:41
#define gROOT
Definition: TROOT.h:415
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
void Clear(Option_t *="")
Definition: clonesA_Event.h:51
Basic string class.
Definition: TString.h:131
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1054
A TTree represents a columnar dataset.
Definition: TTree.h:72
void SetEvent(Int_t ev)
Int_t GetPileup()
Definition: clonesA_Event.h:69
Int_t GetPileup()
Definition: clonesA_Event.h:86
void SetEvent(Int_t ev)
Definition: tree.py:1