Add selection to data plots

Hi, I’ve a root file (see attachment). It’s a tree having ntuple. I have to plot the Calo_Time and Calo_EnDep values.
I’ve to do this
I choose 2 detectors (for example 8 and 9) and I’ve to plot the Calo_Time differenes between them
Calo_Time[8]-Calo_Time[9]; then I’ve to fit the plot by a gaussian function and to get mean value and sigma. Then I’ve to plot the Calo_EnDep[8] and Calo_EnDep[9] values just for the events that are in the range [mu-3*sigma; mu + 3sigma] where mu and sigma are the fit parameters getted by first plot.

I wrote the macro (see attachment) but I get error due to the "if " lines.

calo.cpp (6.2 KB)

si-500330.root (1.5 MB)

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


For next time, please state the ROOT version you use, and the actual error message you get (copy and paste, ideally with some lines before and after).

I suspect that the error reported is here:

	 	if ("Calo_Time[%d]-Calo_Time[%d]",a,b))> (p1-3*p2) && (Form("Calo_Time[%d]-Calo_Time[%d]",a,b)) < (p1+3*p2)

This is not valid C++.

I don’t understand the calls to Form, or why this should be a string. I suspect that you meant to write Calo_Time[a]-Calo_Time[b]> p1-3*p2 && Calo_Time[a]-Calo_Time[b] < p1+3*p2?

Similarly, t->Draw(Form("Calo_Time[%d]-Calo_Time[%d]",a,b)); makes very little sense: you fill a histogram with a string, where I suspect you want to fill it with a value (the difference between the two Calo_Time values). This would then be t->Draw(Calo_Time[a]-Calo_Time[b]);

In general, you might benefit from some help from your advisor; this would allow you to make progress quicker than with this forum.

Cheers, Axel.

Hi Axel, first, thanks for your reply.

  1. Root 5.34/38 Windows

  2. About the error see the attachment

  3. Yes, I want to write Calo_Time[a]-Calo_Time[b]> p1-3*p2 && Calo_Time[a]-Calo_Time[b] < p1+3*p2?, I used the call to Form because of I defined the variables a,b at the beginning of the macro so that I can modify them quickly (this because I also will study Calo_Time[10]-Calo_Time[15], etc. etc]. The Form function works fine in the row t->Draw(Form("Calo_Time[%d]-Calo_Time[%d]",a,b)); (cuet wrote me this code here Differences between arrays)

  4. I just saw that the error was given because of I forgot the Form here if ("Calo_Time[%d]-Calo_Time[%d]",a,b))> (p1-3*p2) && (Form("Calo_Time[%d]-Calo_Time[%d]",a,b)) < (p1+3*p2)

now, by replacing this line with

 if (Form("Calo_Time[%d]-Calo_Time[%d]",a,b))> (p1-3*p2) && (Form("Calo_Time[%d]-Calo_Time[%d]",a,b)) < (p1+3*p2)

I don’t get the error (In the attachment the macro after the bug fixed)
calo.cpp (6.2 KB)

  1. Even if I don’t get the error, I don’t get the right results, because of the total events are 6119 and I get 6119 also by using the cut selection (I should get a lower number of events because of I reject the events out of the range )
if ( ((Calo_Time[a]-Calo_Time[b]) > (p1-3.*p2)) && ((Calo_Time[a]-Calo_Time[b]) < (p1+3.*p2)) )

Hi Wile, thanks for your reply. By following your message, I replaced
this line //if (Form("Calo_Time[%d]-Calo_Time[%d]",a,b))> (p1-3*p2) && (Form("Calo_Time[%d]-Calo_Time[%d]",a,b)) < (p1+3*p2)

if ( ((Calo_Time[8]-Calo_Time[9]) > (p1-3.*p2)) && ((Calo_Time[8]-Calo_Time[9]) < (p1+3.*p2)) )

but I get the error in the attachment


calo.cpp (7.2 KB)

Instead of:

if (Form("Calo_Time[%d]-Calo_Time[%d]",a,b))> (p1-3*p2) && (Form("Calo_Time[%d]-Calo_Time[%d]",a,b)) < (p1+3*p2)
//if ( ((Calo_Time[8]-Calo_Time[9]) > (p1-3.*p2)) && ((Calo_Time[8]-Calo_Time[9]) < (p1+3.*p2)) )
  t->Draw (Form("Calo_Time[%d]",a));


t->Draw( TString::Format("Calo_Time[%d]", a), TString::Format("TMath::Abs(Calo_Time[%d] - Calo_Time[%d] - (%f)) < 3.*(%f)", a, b, p1, p2) );

Hi wile, unfortunately I still get error

calo.cpp (7.6 KB)

I made a mistake in the new “t->Draw(...);” line. In two places, replace"([%f])" with “(%f)” (I corrected it in my previous post, too).

If you still get an “illegal pointer ... 0x0”, right after the new “t->Draw(...);” line, try to add:

gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
1 Like

Hi Wile ! Thank you! it worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.