Write data on a txt file

Goodmorning, I do a two gaussian fit Time a - Time b, where Time a and Time b are the arrival times of particles in 2 detectors. So I get the mean p1 and the sigma p2 of the 2 gaussian fit fuction (obviusly I get two means and two sigma, but I choose the best one looking the peak of the value).
By the fit values I define

TCut cut = TString::Format("TMath::Abs(Calo_Time[%d] - Calo_Time[%d] - (%f)) < (%d)*(%f) ", a, b, p1, csig, p2).Data();

(I setted csigma=3) So I plot events inside the range -3sigma<mu<3sigma in this way

TString heneastring = TString::Format("Calo_EnDep[%d] >> htemp(10000., 0., 10000.)", a);
t->Draw(heneastring, cut);

I would like to know if there is a way to write on a txt file the event number in the range -3sigma<mu<3sigma

Thank you


Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.20.04
Platform: Windows
Compiler: Not Provided


or:

root [0] .> selected_event.txt
t->Scan(...)
.>

Hi @couet thank you but I don’t have to read data from a TTree and save them, but I’ve to save the fit parameters values …

You said:

you did not mentioned th fit parameter there …

Sorry @couet. I wrote bad in my last post. I mean that I must write on the txt file the events selected by the fit…but I first have to do the fit…I can’t know the selected events without the fit…

Scan() also has the election parameter. So, to print the events selected by the fit, just do:

TString heneastring = TString::Format("Calo_EnDep[%d] >> htemp(10000., 0., 10000.)", a);
t->Scan(heneastring, cut);

Hi @couet thank you. I wrote

TString myfileout = TString::Format("%sselected_events.txt",outfolder8sub.Data());
[—]

((TTreePlayer*)(t->GetPlayer()))->SetScanRedirect(true);
			((TTreePlayer*)(t->GetPlayer()))->SetScanFileName(myfileout);

[…]

t->Scan(henea5substring,cut && cut3sub  && cut4sub && cut5sub);

but I get this file
selected_events.txt (47.2 KB)

it looks like that ROOT wrote all the lines where there are the selected events.
Instead I want write just how many events there are…and I’d like to write something to understand for example

“n events pass the selection made by cut && cut3sub && cut4sub && cut5sub”
" m events pass the selection by …"
“y events pass the selection by…”

where obviusly n, m,y are the numbers of selecter events. How can I write it?

This is the macro
caloeff.cpp (50.1 KB)

Let’s go step by step.
Do you get the right output on terminal when you do:

t->Scan(henea5substring,cut && cut3sub  && cut4sub && cut5sub);

?

Hi @couet it doesn’t write on terminal…

log.txt (15.7 KB)

Do simply the Scan without the tick to output on file… We would like first to understand is Scan gives you the right output.

Ok, @couet I commented the line

((TTreePlayer*)(t->GetPlayer()))->SetScanFileName(myfileout);

and now on the terminal I read

Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000.,0.,15000.)"
==> 1855 selected entries
File <lemma-scan.dat> created

1855 events is the correct number because it’s the same number written in the stat box made by
`t->Draw(henea5substring, cut && cut3sub && cut4sub && cut5sub);

but why does it say "Bad numerical expression?`

Check the selection or the variables you are scanning.
You must have some mistakes there.

I’m scanning this

TString henea5substring = TString::Format("Calo_EnDep[%d] >> htemp(15000., 0., 15000.)", e);

I sobstituted

TString henea5substring = TString::Format("Calo_EnDep[%d] >> htemp(15000., 0., 15000.)", e);

by

TString henea5substring = TString::Format(“Calo_EnDep[%d] >> htemp(15000., 0., 15000.)”, e);

but now I get this error

Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000.,0.,15000.)"
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {Calo_EnDep[15] > htemp(15000., 0., 15000.),(((TMath::Abs(Calo_Time[9] - Calo_Time[12] - (3.327851)) < (3)*(1.471876) )&&(TMath::Abs(Calo_Time[9] - Calo_Time[13] - (4.435837)) < (3)*(0.685633) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[14] - (3.423232)) < (3)*(0.646554) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[15] - (1.719628)) < (3)*(1.780356) )}
Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000.,0.,15000.)"
File <lemma-scan.dat> created
TString henea5substring = TString::Format(“Calo_EnDep[%d] > htemp(15000., 0., 15000.)”, e);

The first parameter for in htemp(…), is the number of bin. It should be an int. (you put a float)
Can you check henea5substring ?

I sobstituted
TString henea5substring = TString::Format(“Calo_EnDep[%d] > htemp(15000., 0., 15000.)”, e);

by
TString henea5substring = TString::Format(“Calo_EnDep[%d] > htemp(15000, 0., 15000.)”, e);

(without the dot)

but I get the error

Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000,0.,15000.)"
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {Calo_EnDep[15] > htemp(15000, 0., 15000.),(((TMath::Abs(Calo_Time[9] - Calo_Time[12] - (3.327851)) < (3)*(1.471876) )&&(TMath::Abs(Calo_Time[9] - Calo_Time[13] - (4.435837)) < (3)*(0.685633) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[14] - (3.423232)) < (3)*(0.646554) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[15] - (1.719628)) < (3)*(1.780356) )}
Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000,0.,15000.)"
==> 1855 selected entries
File <lemma-scan.dat> created

But I get the plot
si-500596_ene9-12-13-14-15.pdf (94.1 KB)

by the line

t->Draw(henea5substring, cut && cut3sub && cut4sub && cut5sub);

This is the ROOT file…maybe, it can help you to understand the problem

si-500596.root (1.4 MB)

I do not get any error if I do:

lemma->Draw("Calo_EnDep[1] >> htemp(15000, 0., 15000.)")

or

lemma->Scan("Calo_EnDep[1] >> htemp(15000, 0., 15000.)")

Hi @couet
if I write lemma->Scan("Calo_EnDep[1] >> htemp(15000, 0., 15000.)") in the macro I get this error


and it’s strange because lemma is the tree of the Root file…

if I write

t->Scan("Calo_EnDep[1] >> htemp(15000, 0., 15000.)");

I get this one

I know sometimes ROOT for Windows is a bit different than ROOT for Linux or Mac.
Maybe does @bellenot know if I should use a different code in Root for Windows?
Bellenot I summarize what I need…

  1. I have this Root file si-500596.root (1.4 MB) and this macro caloeff.cpp (50.1 KB)
  2. By the macro I fit, by a two gaussian fit Time x- Time y where Time x and Time y are the arrival time of particles in 2 detectors x and y. So I get the mean and the sigma of the 2 gaussian fit fuction (obviusly I get two means and two sigma, but I choose the best one looking the peak of the value).
  3. By the fit values I define, for example, TCut cut5sub = TString::Format("TMath::Abs(Calo_Time[%d] - Calo_Time[%d] - (%f)) < (%d)*(%f) ", a, e, p15sub, csig, p25sub).Data();

(I setted csigma=3) So I plot events inside the range -3sigma<mu<3sigma in this way

TString henea5substring = TString::Format(“Calo_EnDep[%d] > htemp(15000, 0., 15000.)”, e);
t->Draw(henea5substring, cut && cut3sub && cut4sub && cut5sub);

  1. Obviusly, in the ROOT stat box of henea5substring I can read the numbers of events selected by cut && cut3sub && cut4sub && cut5sub, but I should also create a txt file to write how many events are selected in all my heneaNsubstring (N=5,6, etc) … So I want to create a txt file in which ROOT writes
  • There are … events passing the selection made by using absorbers…
    -There are… events passing the selection made by using absorbers…
    -There are … events passing the selection made by using absorbers…

@couet is trying to help me, but we see some differences when he runs the macro and when I run it…so maybe there is some difference using ROOT for Windows (in the past I had the same problem, because ROOT for Windows sometimes requires some different code).

I will try your latest macro and data file

Here is what I get on Windows:

C:\Users\bellenot\rootdev\Forum\faca87\20200526>root -l
root [0] .x caloeff.cpp
TFile**         C:/si-calo-sep18/si-500596.root
 TFile*         C:/si-calo-sep18/si-500596.root
  KEY: TTree    lemma;1 silicon ntuple
 FCN=1.08942 FROM MIGRAD    STATUS=CONVERGED     116 CALLS         117 TOTAL
                     EDM=1.22903e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     1.32674e+00   9.52806e-01   2.34567e-04   1.34475e-04
   2  Mean        -1.36606e+02   9.14127e+01   9.15657e-03  -1.18095e-07
   3  Sigma        2.79920e+01   3.68341e+01   3.87047e-04  -2.03334e-04
 FCN=669.343 FROM MIGRAD    STATUS=CONVERGED     169 CALLS         170 TOTAL
                     EDM=4.41979e-07    STRATEGY= 1  ERROR MATRIX UNCERTAINTY   2.4 per cent
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     1.19943e+03   2.26541e+01  -3.24456e-01   2.14752e-05
   2  Mean         3.32429e+00   2.82664e-02  -1.12737e-03  -5.23698e-02
   3  Sigma        1.48201e+00   1.71296e-02   3.84324e-05  -6.80181e-01
Warning in <Fit>: Abnormal termination of minimization.
 FCN=541.33 FROM MIGRAD    STATUS=CALL LIMIT   1781 CALLS        1782 TOTAL
                     EDM=2.71913e-05    STRATEGY= 1  ERROR MATRIX UNCERTAINTY  12.1 per cent
  EXT PARAMETER                APPROXIMATE        STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant 1   6.99425e+00   4.04645e+01   1.39317e-01  -1.43387e-02
   2  Mean 1       1.17922e+04   4.97715e+04   1.22767e+02   2.95097e-05
   3  Sigma 1      6.31725e+03   1.73503e+04   2.89945e+01  -5.55203e-05
   4  Constant 2   1.20342e+03   2.20135e+01  -6.67278e-03   1.31304e-04
   5  Mean 2       3.32785e+00   2.73163e-02  -8.92691e-06   1.15136e-01
   6  Sigma 2      1.47188e+00   1.69370e-02  -2.22376e-06   2.67800e-01
Info in <TCanvas::Print>: pdf file C:/testbeam/si-500596/sub_9-12/si-500596_deltatime9-12.pdf has been created
 FCN=6.75702 FROM MIGRAD    STATUS=CONVERGED     175 CALLS         176 TOTAL
                     EDM=9.04894e-08    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     1.31916e+02   1.56788e+02   1.59411e-02  -2.01724e-05
   2  Mean         1.13389e+00   2.63608e+00   1.39124e-04   2.62923e-03
   3  Sigma        1.61045e+00   8.11371e-01   1.73199e-05  -2.31553e-02
 FCN=656.273 FROM MIGRAD    STATUS=CONVERGED      78 CALLS          79 TOTAL
                     EDM=1.86168e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     1.19347e+03   2.66675e+01   2.37476e-01   3.14396e-05
   2  Mean         3.54445e+00   3.76606e-02   2.67675e-04   5.85453e-03
   3  Sigma        1.32711e+00   2.17247e-02   3.24291e-05   1.56101e-01
 FCN=78.8201 FROM MIGRAD    STATUS=CONVERGED     362 CALLS         363 TOTAL
                     EDM=2.01921e-08    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant 1   6.47918e+02   2.52468e+01   6.41719e-02   4.91901e-06
   2  Mean 1       2.61454e+00   6.49990e-02   1.64148e-04   2.95180e-03
   3  Sigma 1      1.58661e+00   3.15962e-02   9.63513e-05   4.35764e-03
   4  Constant 2   1.23472e+03   4.55449e+01   1.39759e-01   4.22548e-06
   5  Mean 2       4.43584e+00   2.41418e-02   8.85685e-05   3.37929e-03
   6  Sigma 2      6.85633e-01   2.14424e-02   5.73462e-05   1.38613e-02
Info in <TCanvas::Print>: pdf file C:/testbeam/si-500596/sub_9-12/sub_13/si-500596_deltatime9-12-13.pdf has been created
 FCN=451.388 FROM MIGRAD    STATUS=CONVERGED     128 CALLS         129 TOTAL
                     EDM=4.05412e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     7.89221e+02   2.75844e+01   1.75715e-01   2.98272e-05
   2  Mean         3.04711e+00   3.49652e-02   2.44922e-04  -3.14170e-02
   3  Sigma        1.10026e+00   3.03627e-02   3.33575e-05  -1.21639e-02
 FCN=452.388 FROM MIGRAD    STATUS=CONVERGED     129 CALLS         130 TOTAL
                     EDM=9.98131e-10    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     7.89217e+02   2.75839e+01   1.75868e-01  -9.47497e-07
   2  Mean         3.04712e+00   3.49647e-02   2.45196e-04  -1.52874e-03
   3  Sigma        1.10026e+00   3.03622e-02   3.62242e-05  -1.18474e-02
 FCN=89.4645 FROM MIGRAD    STATUS=CONVERGED     358 CALLS         359 TOTAL
                     EDM=7.77805e-08    STRATEGY= 1  ERROR MATRIX UNCERTAINTY   2.0 per cent
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant 1   2.77316e+02   1.81579e+01  -5.64890e-02  -3.81220e-05
   2  Mean 1       1.91272e+00   8.03630e-02  -3.54700e-04   5.55460e-03
   3  Sigma 1      1.69870e+00   4.15577e-02  -1.00020e-06  -1.08902e-01
   4  Constant 2   8.49090e+02   3.49903e+01   9.54077e-03  -2.16922e-06
   5  Mean 2       3.42323e+00   2.56177e-02   2.59480e-05   1.25764e-02
   6  Sigma 2      6.46554e-01   2.14979e-02   8.01515e-05  -7.62383e-03
Info in <TCanvas::Print>: pdf file C:/testbeam/si-500596/sub_9-12/sub_13/sub_14/si-500596_deltatime9-12-13-14.pdf has been created
 FCN=199.937 FROM MIGRAD    STATUS=CONVERGED      80 CALLS          81 TOTAL
                     EDM=1.38075e-08    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     3.76332e+02   1.14254e+01   6.36025e-02  -1.12606e-05
   2  Mean         1.71973e+00   5.42463e-02   3.03093e-04  -2.43044e-03
   3  Sigma        1.78030e+00   3.24422e-02   2.69902e-05  -1.42672e-02
 FCN=198.933 FROM MIGRAD    STATUS=CONVERGED      72 CALLS          73 TOTAL
                     EDM=6.84639e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant     3.76323e+02   1.14256e+01   6.34801e-02  -8.46076e-05
   2  Mean         1.71972e+00   5.42459e-02   3.02566e-04   1.13936e-02
   3  Sigma        1.78033e+00   3.24453e-02   2.76259e-05   8.86588e-02
 FCN=201.933 FROM MIGRAD    STATUS=CONVERGED     137 CALLS         138 TOTAL
                     EDM=1.81463e-07    STRATEGY= 1  ERROR MATRIX UNCERTAINTY   1.7 per cent
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant 1   1.88163e+02   8.66478e+00  -7.93506e-03   2.41394e-05
   2  Mean 1       1.71980e+00   6.18302e-02  -1.07358e-04  -8.34883e-03
   3  Sigma 1      1.78029e+00   3.51551e-02   1.31223e-04   2.25700e-02
   4  Constant 2   1.88166e+02   8.66399e+00  -7.92713e-03   2.41413e-05
   5  Mean 2       1.71963e+00   6.34708e-02  -1.23287e-04   9.59919e-03
   6  Sigma 2      1.78036e+00   3.65889e-02   1.37920e-04  -8.45385e-03
Info in <TCanvas::Print>: pdf file C:/testbeam/si-500596/sub_9-12/sub_13/sub_14/sub_15/si-500596_deltatime9-12-13-14-15.pdf has been created
Error in <TTreeFormula::Compile>:  Bad numerical expression : "htemp(15000,0.,15000.)"
Info in <TSelectorDraw::AbortProcess>: Variable compilation failed: {Calo_EnDep[15] > htemp(15000, 0., 15000.),(((TMath::Abs(Calo_Time[9] - Calo_Time[12] - (3.327851)) < (3)*(1.471876) )&&(TMath::Abs(Calo_Time[9] - Calo_Time[13] - (4.435837)) < (3)*(0.685633) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[14] - (3.423232)) < (3)*(0.646554) ))&&(TMath::Abs(Calo_Time[9] - Calo_Time[15] - (1.719628)) < (3)*(1.780356) )}

So your code is not correct, the error is: Bad numerical expression : "htemp(15000,0.,15000.)"