Write_ntuple_to_file_cpp.ipynb Open in SWAN Download

Filling an n-tuple (simulating the conductivity of a material in different conditions of pressure and temperature) and writing it to a file


Ported to Notebook by: Theis Hansen

To use the ROOT toolkit, we need to import ROOT onto our Notebook, which we also set to C++

The Tuple

We create a file which will contain our ntuple and the tuple itself.

In [1]:
TFile ofile("conductivity_experiment.root","RECREATE");
TNtuple cond_data("cond_data","Example N-Tuple","Potential:Current:Temperature:Pressure");

Then we fill it randomly (to simulate acquired data) using the TRandom3 random generator. We are also applying some "smearing" (measurement errors): 1% error on voltage (pot), pressure and current and 1.3 absolute error on temperature. At the end of the loop body we fill the ntuple.

In [2]:
TRandom3 rndm;
float pot,cur,temp,pres;
for (int i=0;i<10000;++i){
    pot=rndm.Uniform(0.,10.);
    temp=rndm.Uniform(250.,350.);
    pres=rndm.Uniform(0.5,1.5);
    cur=pot/(10.+0.05*(temp-300.)-0.2*(pres-1));
    
    pot*=rndm.Gaus(1.,0.01);
    temp+=rndm.Gaus(0.,0.3);
    pres*=rndm.Gaus(1.,0.02);
    cur*=rndm.Gaus(1.,0.01);    
    
    cond_data.Fill(pot,cur,temp,pres);
}

Save the TNtuple and close the file.

In [3]:
cond_data.Write();
ofile.Close();