Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
write_ntuple_to_file_advanced.C
Go to the documentation of this file.
1// Fill an n-tuple and write it to a file simulating measurement of
2// conductivity of a material in different conditions of pressure
3// and temperature using branches.
4
6 const std::string& outputFileName="conductivity_experiment.root"
7 ,unsigned int numDataPoints=1000000){
8
9 TFile ofile(outputFileName.c_str(),"RECREATE");
10
11 // Initialise the TNtuple
12 TTree cond_data("cond_data", "Example N-Tuple");
13
14 // define the variables and book them for the ntuple
15 float pot,cur,temp,pres;
16 cond_data.Branch("Potential", &pot, "Potential/F");
17 cond_data.Branch("Current", &cur, "Current/F");
18 cond_data.Branch("Temperature", &temp, "Temperature/F");
19 cond_data.Branch("Pressure", &pres, "Pressure/F");
20
21 for (int i=0;i<numDataPoints;++i){
22 // Fill it randomly to fake the acquired data
23 pot=gRandom->Uniform(0.,10.)*gRandom->Gaus(1.,0.01);
24 temp=gRandom->Uniform(250.,350.)+gRandom->Gaus(0.,0.3);
25 pres=gRandom->Uniform(0.5,1.5)*gRandom->Gaus(1.,0.02);
26 cur=pot/(10.+0.05*(temp-300.)-0.2*(pres-1.))*
27 gRandom->Gaus(1.,0.01);
28 // write to ntuple
29 cond_data.Fill();}
30
31 // Save the ntuple and close the file
32 cond_data.Write();
33 ofile.Close();
34}
externTRandom * gRandom
Definition TRandom.h:62
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:982
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4654
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition TTree.h:397
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write this object to the current directory.
Definition TTree.cxx:10022
void write_ntuple_to_file_advanced(const std::string &outputFileName="conductivity_experiment.root", unsigned int numDataPoints=1000000)