Dear experts,
I hope to ask the recommended way to read TRefArray using TTreeReader. It is related to this thread. I am trying to read from the output file generated by Delphes. And the FatJet.Constituents
branch stores the TRefArray
which point to the objects I need. With TTreeReaderArray
I can directly read out it but if I loop other branch before, the result of TTreeReader
got distortion.
The test code and root file are listed here if helpful. The snippet is here:
#include "classes/DelphesClasses.h"
R__LOAD_LIBRARY(libDelphes)
bool test_consti(const TRefArray & constituents) {
if(constituents.GetEntries()<1) return false;
std::cout<<"[0] UID "<<constituents.GetUID(0)<< " class "<<constituents.At(0)->ClassName()<<std::endl;
if(TString(constituents.At(0)->ClassName())=="GenParticle") {
auto const p = dynamic_cast<const GenParticle*>(constituents.At(0));
std::cout << p->PT<<std::endl;
}
if(TString(constituents.At(0)->ClassName())=="ParticleFlowCandidate") {
auto const p = dynamic_cast<const ParticleFlowCandidate*>(constituents.At(0));
std::cout << p->PT<<std::endl;
}
if(TString(constituents.At(0)->ClassName())=="Muon") {
auto const p = dynamic_cast<const Muon*>(constituents.At(0));
std::cout << p->PT<<std::endl;
}
return true;
}
void tester(){
TChain* chain = new TChain("Delphes");
chain->Add(TString::Format("top.reco.root"));
TTreeReader r(chain);
TTreeReaderArray<int> rp(r, "Particle.PID");
TTreeReaderArray<TRefArray> rv(r, "FatJet.Constituents");
int count=0;
while (r.Next()) {
count++;
if(count<158)
continue;
std::cout<< count-1 << std::endl;
// for (const int & each : rp) {
// ;
// }
for (const TRefArray & each : rv) {
test_consti(each);
}
if(count>158)
break;
}
}
Un-comment the 3 lines of codes would change the result of TTreeReaderArray<TRefArray>
.
I am not sure whether I mis-use the TTreeReader and thank you for any comment or suggestion.
Best,
Qi
_ROOT Version:6.30/02
_Platform:x86_64-centos7-gcc12-opt (3.10.0-1127.el7.x86_64)
_Compiler:linuxx8664gcc