Tcut when I import a TH1F from a file

Hello, some days ago, I used this macro
TBAlign.C (26.3 KB)
TBAlign.h (4.3 KB)
fit_line.C (1.2 KB)
to traslate the local x coordinates of events read by 3 trackers (called 33, 35 and 37)
stored in this Root file

si-500596.root (1.4 MB)

to the laboratory ones and I stored the them in the arrays x33[0], x35[0] and x37[0]. Moreover, I fitted the events of the x33[0], x35[0] and x37[0] using the fit_line function and I stored the slope values in the bpar[0] array.

Today I had to plot the bpar[0] distribution for the events simultaneously read by the three trackers i.e. when x33[0] && x35[0] && x37[0] are NOT empty. so I added the code

TH1F *hb333537=new TH1F("hb333537","b parameter 33-3537", 100,0.,0.);

as you can see here

TBAlign.C (23.6 KB)
and I stored all the TH1F in this file
si-500596-trasl.root (1.8 MB)

Now I’ve to read the TH1F and to do some plots using some conditions, so I wrote this macro

graphb.cpp (7.2 KB)

to do it. I wrote my Tcut conditions, but I’m not able to use them …as you can see I tried

TString hxb01string = TString::Format("hxb01 >> htemp(100, 0., 0.)"); 
        fin->Draw(hxb01string, xh99);

but I get error

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/24
Compiler: VS2019

At least two mistakes:

  1. fin is a TFile … you cannot Draw a TFile . You should get the TTree inside that TFile
  2. to access the string inside a TString you need .Data() .i.e hxb01string.Data()

Hi @couet thank you.

So I first wrote
and I get 4185 events, then I tried to use some Tcut

fChain->Draw(hxb01string.Data(),cutx37 && cut90 && xh99);
but I still get 4185 events…then Root doesn’t apply the cuts.

graphb.cpp (4.5 KB)

Or your cut does not cut anything ? can you try with an “obvious” selection to see if it works ?

Thank you @couet
given that this is the plot

then, you see there are events b<-0.06, I tried this cut

TCut cutbpar = TString::Format("bpar[0] > -0.0.06 ").Data();

but i still get 4185 events (i.e. I’ve not the cut…)

graphb.cpp (4.5 KB)

are you sure ??? -0.0.06 ?? that’s not a valid number …

Thank you @couet …sorry, it was a mistake… now I wrote the correct number
TCut cutbpar = TString::Format("bpar[0] > -0.006 ").Data();

but same problem, I don’t get the cut…

graphb.cpp (4.5 KB)

what is hxb01string.Data() ?

Hi @couet I replaced

TString hxb01string = TString::Format(“hxb01 >> htemp(100, 0., 0.)”);


TString hxb01string = TString::Format(“hxb01 >> htemp(100, 0., 0.)”).Data();





but I get the same problem…

graphb.cpp (4.5 KB)

This fails before you even try to do the fits:



TCut cutbpar = TString::Format("bpar[0] > -0.006 ").Data();
TString hxb01string = TString::Format("hxb01 >> htemp(100, 0., 0.)").Data();
TH1F *hxb01 = (TH1F *)fin->Get("hb333537");

with this error:

Error in <TTreeFormula::Compile>:  Bad numerical expression : "hxb01"
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {hxb01 ,bpar[0] > -0.006 }

In your file, hb333537 (hxb01) is a histogram separate from the tree lemma (fChain), and it’s not recognised as a variable in lemma.
Additionally, you mention you want bpar[0] > -0.006, but looking at the plot, that would cut out all the data in that histogram (if b[par] is indeed “slope”).

Thank you @dastudillo then…do you know how can I fix to apply the cuts please?

Since you created the hb333537 histogram in the first place, why not bypass this hb333537 histogram, and instead create another TTree with the events from your initial selection (i.e. same events that were used to fill hb333537), whatever it was, but containing their “slopes” and “bpar” (and any other variables you may want to use for additional cuts)? Then you can just read this new tree and cut using variables inside it. There may be other ways starting from what you already did, but this should work.

Thank you @dastudillo, actually I store the bpar[0] in the Root file you can see that in this macro

TBAlign.h (4.3 KB) TBAlign.C (23.6 KB)

I wrote

	if(nx33==1 && nx35==1 && nx37==1){
	  			if(x[0]>-999 && x[1]>-999 && x[2]>-999){
					fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z
					xLG[0]=a+b*zLG[0];					 // x LG coordinates of hits 
					std::cout << "x0 = " << x[0] << "\t x1 = " << x[1]  << " \t x2 = " << x[2] << "\t xLG = " << xLG[0] << "\t z0 = " << z[0] << "\t z1 = " << z[1]  << "\t z2 = " << z[2] << "\t zLG = " << zLG[0] << std::endl;
					results << jentry << "\t " <<  a << "\t " << ae << "\t " << b << "\t " << be <<  "\t " << chi2 << "\t "<< q << "\t "<< xLG[0] << endl;

i.e. I store bpar[0]=b in the Root file. But when I plot bpar[0] I get more tha 4185 events…then I think that ROOT creates empty culomns when there are not the three hits…that’s because I filled
hb333537. Indeed in a first time, I didn’t fill hb333537 but I used this macro

graphb.cpp (4.5 KB)

You see by this macro I called the bpar[0] in the Root file and I tried to select events by this line

fChain->Draw(hxb01string, subdet33 && subdet35 && subdet37 && xh99);

but i didn’t get events. So my supervisor’s colleague said me that i can’t do it then I filled the hb333537

I also tried to define the cuts in the TBAlign macro

	TCut cut912 = TString::Format("TMath::Abs(Calo_Time[9] - Calo_Time[12] - (3.32785)) < (3)*(1.47188) ").Data();
  				TCut cut913 = TString::Format("TMath::Abs(Calo_Time[9] - Calo_Time[13] - (4.43584)) < (3)*(0.685633) ").Data();
				TCut cut914 = TString::Format("TMath::Abs(Calo_Time[9] - Calo_Time[14] - (3.42323)) < (3)*(0.646554) ").Data();
				TCut cut915 = TString::Format("TMath::Abs(Calo_Time[9] - Calo_Time[15] - (1.71963)) < (3)*(1.78036) ").Data();
					TCut cut90 = TString::Format("TMath::Abs(Calo_Time[9] - Calo_Time[0] - (-125.007)) < (3)*(2.67618) ").Data();
					TCut xh99 = TString::Format("xh > -99 ").Data();
					TCut cutx37 = TString::Format("TMath::Abs(x37[0] - (-20.7411)) < (1)*(1.16973) ").Data();
					TCut subdet37 = TString::Format("subdet == 37 ").Data();

and to fill in this way

if (subdet37 && xh99) hbT->Fill(bpar[0]);
					if (subdet37 && xh99 && cutx37 ) hbTx37->Fill(bpar[0]);
					if (subdet37 && xh99 && cut90 ) hbTx37LG0->Fill(bpar[0]);
					if (subdet37 && xh99 && cut912 && cut913 && cut914 && cut915 ) hbTx37H->Fill(bpar[0]);

but when I plot… I get 4185 event in all the plots, but I know that it is wrong… because for example there were 3 lead glasses and not all the events went passed through the lead glass 0 (i.e. cut90).

TBAlign.C (25.0 KB)

In your macro graphb.cpp it seems you are accessing the TTree lemma in the .root file you posted earlier. Then it that TTree (lemma) you try to draw the variable bpar … but there is no variable bpar in the TTree lemma… did I missed something ?

Hi @couet, maybe did you open the file si-500596.root? The bpar[0] is in the file si-500596-trasl.root because I create this branch by the TBAlign I don’t know if you remember, we talked about this macro in this topic Fit of an array with @Wile_E_Coyote .

By the macro TBAlign I traslated the local coordinates to laboratory ones, moreover, I fit the events in the three trackers and I also fill the bpar[0] (the slope of the fits).

By this macro

graphb.cpp (4.5 KB)

I plotted the bpar[0] for events read by the tracker 37, i.e I used the condition

fChain->Draw(hxb01string, subdet37 && xh99);

and I got 5289 events.

Now I must plot the bpar distribution for events that have hits in the three trackers 33, 35 and 37

so I tried to write in the graphb.cpp macro

`fChain->Draw(hxb01string, subdet33 && subdet35 && subdet37 && xh99);

but I got 0 events the I said it to my supervisor and his colleague and he said me that this code doesn’t work and I’ve to set the condition that x33[0] && x35[0] && x37[0] .

You see, yesterday I was able to fill a histogram for events having hits in the three trackers but I also have to apply other cuts and it looked like that the Tcut didn’t work for a filled histogram…

Now I also tried to add in the TBAlign a new array in this way

if (x33[0]!=NULL && x35[0]!=NULL && x37[0]!=NULL) bparT[0]=b;

but in this way I get the same events that I get just for bpar[0]=b

TBAlign.C (25.3 KB)

I also tried in this way

TCut subdet33 = TString::Format("x33[0]!=NULL").Data();
					TCut subdet35 = TString::Format("x35[0]!=NULL").Data();
					TCut subdet37 = TString::Format("x37[0]!=NULL").Data();
 TString hxb01string = TString::Format("bparT[0] >> htemp(100, 0., 0.)"); 
         fChain->Draw(hxb01string,subdet33 && subdet35 && subdet37 && subdet37b && xh99 && cutx37 && cut912 && cut913 && cut914 && cut915 );

but I get the error

graphb.cpp (4.9 KB)

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