Write data on a txt file

Just replace this:

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

by this:

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

(the error message was explicit enough…)

Yes i found the same. The macro is running for me too. Now we are back to you initial question. I will try to replace Draw by Scan and save the output in a file.

thank you the both @couet and @bellenot.
Yes sorry! Yesterday I was trying to add the writing on the txt file…so I did some mistakes during it!

This one caloeff.cpp (49.8 KB) that I was using BEFORE trying to add the writing on a file function! So it works (I just tried it and there are no errors).

Now I would like to add the writing on a file fuction, to write the number of selected events by the time coincidence, that is what I wrote here

thank you

In your macro, after:

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

add the lines:

      TString scansubstring = TString::Format("Calo_EnDep[%d]", e);
      t->Scan(scansubstring, cut && cut3sub  && cut4sub && cut5sub);

And you will get the result of the Scan in the file: lemma-scan.dat

1 Like

thank you @couet
Following your message, I wrote

TString myfileout = TString::Format("%sselected_events.txt",outfolder8sub.Data()); // TO USE FOR FILES WITH TARGET
[…]

((TTreePlayer*)(t->GetPlayer()))->SetScanRedirect(true);
((TTreePlayer*)(t->GetPlayer()))->SetScanFileName(myfileout);
[…]
TString scan5substring = TString::Format("Calo_EnDep[%d]", e);
t->Scan(scan5substring, cut && cut3sub && cut4sub && cut5sub);

I get this file

selected_events.txt (47.2 KB)

in which ROOT writes all selected events (the line of the ntupla and the released energy), but it doesn’t write how many events I selected …it just write it on the terminal


Instead, I don’t neet to write all the selected lines, but I need how many events are selected. I know that I can read it on the terminal and to write it by hand, but if there is a way to automatize this process, it would be great

Hi,
just an idea, with TTree::Draw you could save the selected entries to a TEntryList (search for " Saving the result of Draw to a TEventList, a TEntryList or a TEntryListArray") and then check entryList->GetN() to get the number of selected events.

Alternatively, you can check out RDataFrame. It does not support TTree::Scan’s selection syntax, but you can write arbitrary selections in C++ and then easily count the number of entries that pass them:

ROOT::RDataFrame df("treename", "filename.root");
auto count = df.Filter("x > 0 && !y.empty() && y[0] > 10").Count().GetValue();

Cheers,
Enrico

1 Like

thank you @eguiraud, how can I save the TTree::Draw results in a TEntryList?

There is an example at the link I provided above, you have to search for “Saving the result of Draw to a TEventList, a TEntryList or a TEntryListArray” a bit down the page.

Cheers,
Enrico

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