Creating a new leaf by making calculations in another leaf

Hi there,

since my first problem is solved I have another one :slight_smile:

This is my analyze code (part of it at least) and what I want to do additionally is to make a histogram of two following unix_seconds leaves.

[code]#define Analyze_cxx
#include “Analyze.h”
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

void Analyze::Loop()
{

if (fChain == 0) return;

//The Setup Code goes here


TH1* timeHist = new TH1D("unix_seconds","Zeit",10000,1288998000,1289002220);
timeHist->GetXaxis()->SetTitle("Time");
timeHist->GetXaxis()->SetTimeDisplay(1); // X axis is a time axis
timeHist->GetXaxis()->SetTimeFormat("%H\:%M\:%S");


timeHist->GetYaxis()->SetTitle("number of events");

Long64_t nentries = fChain->GetEntriesFast();

Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;

   //The Loop Code goes here
   
   timeHist->Fill(unix_seconds);

}

//The Wrap-Up Code goes here

timeHist->Draw("");

}
[/code]

If I wanted to substract two different leaves and plot I believe a line like this should go into the loop section

Double_t newLeaf = TMath::(Leaf1-Leaf2); right?
The thing is, I want to substract the previous value from unix_seconds from the “now” value of unix_seconds.
How could I do that?

I believe I could do a workaround and calculate each one before I convert it to a root Tree but that feels like cheating :slight_smile:

simply do this:

[code] Long64_t nbytes = 0, nb = 0;
double old_unix_seconds = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;

  //The Loop Code goes here
  
  timeHist->Fill(unix_seconds-old_unix_seconds);
 old_unix_seconds = unix_seconds;

}
[/code]
Rene

Thank you, that worked perfectly.

Now one last question (for today at least):
Is there a way to let root determine the limits of the histogram? Now I simply type in the first and the last value of the leaf unix_seconds in my Analyze file. To do that I have to open a 700MB ASCI File, which takes a bit time on my machine.
Can I tell root to check the leaf to determine the limits?

I am referring to this line

Thank you

you can use TTree::GetMaximum, Minimum,eg

double xmin = fChain->GetMinimum("unix_seconds"); double xmax = fChain->GetMaximum("unix_seconds"); TH1* timeHist = new TH1D("unix_seconds","Zeit",10000,xmin,xmax);

Rene

Thank you very much. Worked perfectly.

One more thing though :wink:

I am plotting 4 channels each in its own histogram.
Additionally I want to plot all 4 measured channels in one histogram.

So I created a histogram in the Set-Up section:

TH1* ch_all_12bitHist = new TH1D("ch_all_12_bit","Histogram of all Channels 12bit",4096,0,4095); ch_all_12bitHist->GetXaxis()->SetTitle("Integral [ ]"); ch_all_12bitHist->GetYaxis()->SetTitle("number of events");

but I don’t fill it in the Loop Section.
One of my colleages said I should write these commands in the Wrap-Up Section.

ch00_12bitHist->Draw(""); ch01_12bitHist->Draw("same"); ch02_12bitHist->Draw("same"); ch03_12bitHist->Draw("same");
It works, but it makes the statistics from the last channel written in there (ch03_12bitHist atm) and it names everything after the last Histogram drawn. Also I have to set the color for each Hist in the set-up section for each individual histogram in order to have them plotted in this Histogram with a different color for each channel. But then each single Histogram has a non black color when it is not needed
I feel like I am tricking root here and surely there must be a smoother way?

When a histogram is drawn with the option “SAME”, the statistics box
is not drawn. To force the statistics box drawing with the option
"SAME", the option “SAMES” must be used.
If the new statistics box hides the previous statistics box, one can change
its position with these lines (“h” being the pointer to the histogram):

Root > TPaveStats *st = (TPaveStats*)h->FindObject("stats") Root > st->SetX1NDC(newx1); //new x start position Root > st->SetX2NDC(newx2); //new x end position
See the TH1::Draw documentation for more detail.

Philippe.