Mathematical Operations between consecutive TTree entries

Hello ROOTers!

I am trying to achieve the following :

In a root file that contains a TTree with various TBranches, I want to subtract the consecutive entries of a certain TBranch and save this in a root histogram.

To make it more clear, my structure is somewhat like that :

TTree
Branch1
Branch2
Branch3

The values that are stored in Branch2 for instance, are 10, 3, 5, 6, 2, 0, …
I want to calculate the difference between consecutive values so I want this : 7, -2, -1, 4, 2, …

I think I have to use GetEntry(), but while I load the root file and type tree->GetEntry(10) or tree->GetEntry(5) or tree->getEntry(124), I always get 116 as a result and I don’t think it’s correct…

How can I achieve that?
How can I loop over consecutive entries of a certain TBranch?

Thank you very much in advance!

Hi!

I think you will need something like this:

    TFile *foo = TFile::Open("myfile.root");
    TTree *t = (TTree*)foo->Get("mytreename");
    
    t->SetBranchStatus("*",0);
    
    Int_t b2Value;
    t->SetBranchStatus("Branch2",1);
    t->SetBranchAddress("Branch2",&b2Value);
    
    t->GetEntry(0);
    Int_t prevB2Value = b2Value;
    
    UInt_t nrOfEntries = t->GetEntries();
    
    for (UInt_t i = 1; i < nrOfEntries; ++i) {
        t->GetEntry(i);
        Int_t Delta = prevB2Value - b2Value;
        // fill Delta in your histogram
        prevB2Value = b2Value;
    }

If your data type in the tree is not Int_t you should adapt the variables accordingly.

Cheers,
Jochen

[code]{
TFile *f = TFile::Open(“hsimple.root”); // from "${ROOTSYS}/tutorials/"
if (!f) return; // just a precaution
TNtuple *t; f->GetObject(“ntuple”, t);
if (!t) {delete f; return;}; // just a precaution
t->Print(); // just for fun

Long64_t entries = t->GetEntries();
if (entries < 2) {delete f; return;}; // just a precaution

TBranch *b = t->GetBranch(“px”);
if (!b) {delete f; return;}; // just a precaution
Float_t px, px0;
b->SetAddress(&px);

TH1F *h = new TH1F(“h”, “px”, 100, -5, 5);
h->SetDirectory(gROOT);
TH1F *hd = new TH1F(“hd”, “#Delta px”, 100, -5, 5);
hd->SetDirectory(gROOT);

b->GetEntry(0);
h->Fill(px);
px0 = px;

for (Long64_t i = 1; i < entries; i++) {
b->GetEntry(i);
h->Fill(px);
hd->Fill(px - px0);
px0 = px;
}

t->ResetBranchAddresses(); // “detach” from local variables
delete f; // automatically deletes “t”, too

TCanvas *c = new TCanvas(“c”, “c”);
c->Divide(1, 2);
c->cd(1);
h->Draw();
c->cd(2);
hd->Draw();
c->cd(0);
c->Modified(); c->Update();
}[/code]

[quote=“jkerdels”]

    TFile *foo = TFile::Open("myfile.root");
    TTree *t = (TTree*)foo->Get("mytreename");
    
    t->SetBranchStatus("*",0);
    
    Int_t b2Value;
    t->SetBranchStatus("Branch2",1);
    t->SetBranchAddress("Branch2",&b2Value);
    
    t->GetEntry(0);
    Int_t prevB2Value = b2Value;
    
    UInt_t nrOfEntries = t->GetEntries();
    
    for (UInt_t i = 1; i < nrOfEntries; ++i) {
        t->GetEntry(i);
        Int_t Delta = prevB2Value - b2Value;
        // fill Delta in your histogram
        prevB2Value = b2Value;
    }

Thank you very much for your answer!
It seems that it works, but maybe something is wrong…
The thing is that in my code I need to process all of the TBranches.
Therefore what I do is the following

[code]//Definition of the histogram to process Delta
TH1F *hdt6 = new TH1F(“hdt6”,“Time ; Time (ns); Counts”,16000000,0,16000000);

t->SetBranchStatus(“time1”,1);
t->SetBranchStatus(“time2”,1);
t->GetEntry(0);
Double_t dtPrev = time1;
nt_fimg->SetBranchStatus("",0);
nt_fimg->SetBranchStatus("
",1);

//Looping over all TBrances including TBranch2
for (Int_t i=0; i<=entries; i++) {
<…>
Double_t Delta = dtPrev - time1;
hdt6->Fill(Delta);
dtPrev = time1;
}[/code]

The result I get from hdt6 is a bit weird, but this might have to do with physics.
The only part that I am not sure of is

t->SetBranchStatus("time1",1); t->SetBranchStatus("time2",1); t->GetEntry(0); Double_t dtPrev = time1;

What happens when I activate only 2 TBranches and call GetEntry(0)?
I wonder if the value that is parsed is the correct one…

Hi,

your code appears incomplete. So if you get “strange” results I would look at a number of things just from the code you provided:

Where do you declare and set the branch addresses of the variable time1?
Why do you need the branch “time2” when you are not using it in the code?
Where is the call to t->GetEntry(i) in the loop?
Why does your loop run from 0 instead of 1?
And, why is there a tree nt_fimg, that appears not to be used?

Cheers,
Jochen

I am so sorry for the inconvenience…

The code is the following

[code]{

TFile foo = TFile::Open(“run_1_in.root”);
TTree t = (TTree)foo->Get(“FIMG”);
t->SetBranchStatus("
",0);

Double_t tof;
Double_t tflash;
Int_t detn;
t->SetBranchStatus(“tof”,1);
t->SetBranchStatus(“tflash”,1);
t->SetBranchStatus(“detn”,1);
t->SetBranchAddress(“tof”,&tof);
t->SetBranchAddress(“tflash”,&tflash);
t->SetBranchAddress(“detn”,&detn);

TH1F *hdt6 = new TH1F(“hdt6”,“Time Differences Det. #6 - ^{235}U ; Time (ns); Counts”,16000000,0,16000000);

t->GetEntry(0);
Double_t prevtof = tof-tflash;

UInt_t nrOfEntries = t->GetEntries();

for (UInt_t i = 1; i < nrOfEntries; ++i) {
t->GetEntry(i);
if (detn==6){
Double_t Delta = prevtof - tof + tflash;
// fill Delta in your histogram
hdt6->Fill(Delta);
prevtof = tof - tflash;
}
}
hdt6->Draw(“histo”);
}[/code]

Hi!

Thanks for posting the whole code. As far as I can see the code looks correct and should work.
Some further remarks:

  • You could clean up some memory stuff at the end, i.e. close the file and delete the pointer to it.
  • The histogram may be added implicitly to the file “foo”. You can “detach” the histogram by calling
    hdt6->SetDirectory(0) after creating it.
  • If you still get “weird” results, check the data types of the branches, i.e., make sure they are really doubles and not, e.g., floats. (but root should tell you if there is a mismatch afaik)

Cheers,
Jochen