Dear Root experts,
I was wondering if I can use a TCut in an “if” condition.
For example, let’s say I am reading a branch called “InF_Lepton1_isMuon” which contains vectors of int (only 0s or 1s) and fill a histogram. I would like to apply a cut which is saved as a TCut. Something like the following:
TCut mycut = “InF_Lepton1_isMuon->at(i) == 1”;
for (int i=0; iGetEntries(); i++) {
InTree->GetEntry(i);
if (mycut)
Hist->Fill(InF_Lepton1_isMuon->at(i));
}
When I do this I get no errors from the compiler or in the runtime, but when I look at the results I see that the condition is not applied.
Is it possible to apply a condition saved as a TCut either in an if condition or ant other way?
Thank you in advance for the help
Please read tips for efficient and successful posting and posting code
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided
“InF_Lepton1_isMuon->at(i)
” makes no sense.
You probably want (for each tree entry “i
”):
for (unsigned j = 0; j < InF_Lepton1_isMuon.size(); j++) if (InF_Lepton1_isMuon->at(j) == 1) Hist->Fill(InF_Lepton1_isMuon->at(j));
Hi @Wile_E_Coyote ,
Thank you very much for the quick response. You are right, I need to do this for each entry “i”. Your suggestion works well and I was actually using it before but the reason I would like to save and apply it as a TCut is that I could loop over some different conditions and save them in the TCut object and apply. For example:
std::vector<<std::string>> TMuonCutNames{"IsPFMuon","IsGlobalMuon","IsTrackerMuon","IsLooseMuon","IsMediumMuon"};
for (int iTM=0; iTM<(int)TMuonCutNames.size(); iTM++) {
std::string ConditionString = (std::string)TempMCTMuonHistograms[iTM]->GetTitle() + "->at(Index) != 0";
TCut Condition = ConditionString.c_str();
std::cout << Condition << endl;
if (Condition) {
TempMCTMuonHistograms[iTM]->Fill(InF_JPsiMass->at(Index));
}
}
This is not the complete code but I wanted you have a feeling of why I insist on saving the condition in a TCut object.
I am happy to show the entire code too if it helps.
Thanks again
Try:
TString ConditionString = TString::Format("%s->at(%d) != 0", TempMCTMuonHistograms[iTM]->GetTitle(), (int)Index);
TCut Condition(ConditionString);
Hi,
I tried your suggestion and it compiles and runs but looking at the results, I see that the cuts are not applied as if there was no condition applied.
Thanks again for looking into this
And why do you think “if (Condition)
” is supposed to work?
TCut t("1 == 1"), f("1 == 0");
if (t) std::cout << "passed\n";
if (f) std::cout << "passed\n";
The TCut is primarily designed for the TTree::Draw only.
Well, if you can, try to play with the ROOT::RDataFrame, which offers some ways to “mix” strings with C++ code.
@Wile_E_Coyote
Thank you very much for your help, I’ll try to go through “RDataFrame” and see what I find.
Thanks again