Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree131_clones_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 tree131_clones_event.C
12/// ~~~
13/// \macro_code
14///
15/// \author The ROOT Team
16
17#ifndef CLONESA_EVENT_SECOND_RUN
18
20{
21 std::string s1(__FILE__);
22 TString dir = gSystem->UnixPathName(s1.substr(0, s1.find_last_of("\\/")).c_str());
23 gROOT->ProcessLine(TString(".L ") + dir + "/clones_event.cxx+");
24#define CLONESA_EVENT_SECOND_RUN yes
25 gROOT->ProcessLine("#include \"" __FILE__ "\"");
26 gROOT->ProcessLine("tree131_clones_event(true)");
27}
28
29#else
30
32{
33 // protect against old ROOT versions
34 if ( gROOT->GetVersionInt() < 30503 ) {
35 std::cout << "Works only with ROOT version >= 3.05/03" << std::endl;
36 return;
37 }
38 if ( gROOT->GetVersionDate() < 20030406 ) {
39 std::cout << "Works only with ROOT CVS version after 5. 4. 2003" << std::endl;
40 return;
41 }
42
43 // write a Tree
44 auto hfile = TFile::Open("clones_event.root", "RECREATE", "Test TClonesArray");
45 auto TTree("clones_event", "An example of a ROOT tree");
46 auto event1 = new TUsrSevtData1();
47 auto event2 = new TUsrSevtData2();
48 event1, 8000, 99);
49 event2, 8000, 99);
50 for (Int_t ev = 0; ev < 10; ev++) {
51 std::cout << "event " << ev << std::endl;
52 event1->SetEvent(ev);
53 event2->SetEvent(ev);
54 tree->Fill();
55 if (ev <3)
56 ev);
57 }
58 tree->Write();
59 tree->Print();
60 delete hfile;
61}
62
64{
65 //read the Tree
66 auto hfile = TFile::Open("clones_event.root");
67 auto TTree>("clones_event");
68
71 event1);
72 event2);
73 for (Int_t ev = 0; ev < 8; ev++) {
74 ev);
75 std::cout << "Pileup event1: " << event1->GetPileup() << std::endl;
76 std::cout << "Pileup event2: " << event2->GetPileup() << std::endl;
77 event1->Clear();
78 event2->Clear();
79 // gObjectTable->Print(); // detect possible memory leaks
80 }
81 delete hfile;
82}
83
84void tree131_clones_event(bool /*secondrun*/)
85{
86 // Embedding this load inside the first run of the script is not yet
87 // supported in v6
88 // gROOT->ProcessLine(".L clones_event.cxx+"); // compile shared lib
89 write_clones_event(); // write the tree
90 read_clones_event(); // read back the tree
91}
92
93#endif
#define s1(x)
Definition RSha256.hxx:91
int Int_t
Definition RtypesCore.h:45
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:414
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4131
Basic string class.
Definition TString.h:139
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1075
A TTree represents a columnar dataset.
Definition TTree.h:84