Cuts on Histograms

Hello All,

I have a root file with a large number of events. Is there a way to place an energy cut on the histogram and report the number of events above that energy directly? By directly I mean not using a while loop. I can do this now with the following:

for (ind=0;ind < n_events; ind ++){ Hist1->GetEvent(ind); if (energy > LOWERENERGY) GoodEvents++; }

This while loop take a while to process. My question is if there is a quicker way to do this. Many thanks.

Karthik.

Hi,

You could try:GoodEvents = Hist1->GetEntries(TString::Format("energy > %f",LOWERENERGY));

Cheers,
Philippe.

1 Like

Thanks Philippe. That worked for getting entries.

Now, I am faced with the problem of making a histogram from the events after a cut.
Again, I am using a loop now, along the lines of :

... TTree *LowEngEvents= (TTree*)gDirectory->Get("LowEngEvents"); TH1F *MH= new TH1F("MH","Module Histogram",40,0,39); nentries = LowEngEvents->GetEntries(); for (event=0;event<nentries;event++){ LowEngEvents->GetEntry(event); if (energy >=2.1 && energy <=3.8) MH->Fill(PositionX); ... }

So the questions is can I draw this histogram directly with the energy cut without looping though the events.
Thanks

Karthik

[quote=“pcanal”]Hi,

You could try:GoodEvents = Hist1->GetEntries(TString::Format("energy > %f",LOWERENERGY));

Cheers,
Philippe.[/quote]

Hi,

I suppose you mean/need: LowEngEvents->Draw("PositionX","energy >=2.1 && energy <=3.8");(which will implicitly loop over all the events for you).

Cheers,
Philippe.

I should have clarified this before: I have no problem drawing it.
My question was how to access the histogram after drawing it.
Say for instance, you wanted to save the histogram after the cuts are imposed.

[quote=“pcanal”]Hi,

I suppose you mean/need: LowEngEvents->Draw("PositionX","energy >=2.1 && energy <=3.8");(which will implicitly loop over all the events for you).

Cheers,
Philippe.[/quote]

[quote]My question was how to access the histogram after drawing it.[/quote]I am a bit confused (your question and the code your copy/paste do not seem to ‘match’). It really depends on how you create the histogram, if you created using your own loop then you already have a handle on the histogram and you can save it directly. If you are using TTree::Draw then you can retrieve the histogram using its default name (htemp) or give the histo a specific name:owEngEvents->Draw("PositionX>>posx","energy >=2.1 && energy <=3.8"); TH1 *posx_histo = (TH1*)gPad->GetListOfPrimitives()->FindObject("pox");

Cheers,
Philippe.

[quote=“pcanal”]owEngEvents->Draw("PositionX>>posx","energy >=2.1 && energy <=3.8"); TH1 *posx_histo = (TH1*)gPad->GetListOfPrimitives()->FindObject("pox");

Cheers,
Philippe.[/quote]

That was exactly what I wanted - thank you very much. It seems to be faster to draw a histogram this way than looping though events.

However, I have a small problem/observation:
I tried both these methods and I notice the histograms created look different.

Method 1: Looping though events in a TREE:

[code]TH1I 	*RH = new TH1I("RH","RSector ID",40,0,39);
Int_t	nentries = RootTree->GetEntries();
Int_t	eventno=0;
Int_t 	Xposition;      
Float_t	energy;       

RootTree->SetBranchAddress("energy",    &energy);
RootTree->SetBranchAddress("Xposition", &Xposition);

for ( eventno = 0; eventno < nentries; eventno += 1 ) {
	RootTree->GetEntry(eventno);
	if ((energy >= ELow) && (energy <= EHigh)) {
		RH->Fill(Xposition);
	}
}

RH->SaveAs("histloop.C");[/code]

Method 2: Using Cuts:

[code] char Conditions[500];
sprintf(Conditions, “energy >= %f”,ELow);
sprintf(Conditions, “%s && energy <= %f”,Conditions,EHigh);
printf ( “Cut = %s\n”,Conditions);

RootTree->Draw("Xposition>>RID",Conditions);
TH1 *rid = (TH1*)gPad->GetListOfPrimitives()->FindObject("RID");
rid->SaveAs("histcuts.C");[/code]

Then I plotted the two output files (histloop.C and histcuts.C) using root:

root [0] .x histcuts.C root [0] .x histloop.C

and was suprised to see that they actually differ in the x-axis. The one using cuts is offset by half a bin.
The screen shot is shown below:

I added the red line to show the offset between the two methods. The one on the top is the histogram I expect from the data.

Any idea why this happens? How do I fix this so that using cuts has the same histogram as the one using loops.

Many thanks,
Karthik

[quote]Any idea why this happens? [/quote]Simply because the automatic tool chose a different range than you did. In particular, it is start on a half mark in order to make the histogram semantic clearer and more accurate (since usually you take the mid-point of the bin as an approximation of its content, with a range 0, 39 the value will look like '0.5, 1.5, etc. in some of the use cases).

Cheers,
Philippe.

1 Like