12 #include <top/modules/TOPNtuple/TOPNtupleModule.h>
14 #include <top/geometry/TOPGeometryPar.h>
17 #include <framework/datastore/StoreArray.h>
18 #include <framework/datastore/StoreObjPtr.h>
21 #include <framework/gearbox/Const.h>
22 #include <framework/logging/Logger.h>
25 #include <framework/dataobjects/EventMetaData.h>
26 #include <mdst/dataobjects/Track.h>
27 #include <mdst/dataobjects/MCParticle.h>
28 #include <top/dataobjects/TOPLikelihood.h>
29 #include <top/dataobjects/TOPBarHit.h>
30 #include <tracking/dataobjects/ExtHit.h>
56 setDescription(
"Writes ntuple of TOPLikelihoods with tracking info into a root file");
59 addParam(
"outputFileName", m_outputFileName,
"Output file name",
60 string(
"TOPNtuple.root"));
64 TOPNtupleModule::~TOPNtupleModule()
68 void TOPNtupleModule::initialize()
71 m_file = TFile::Open(m_outputFileName.c_str(),
"RECREATE");
72 if (m_file->IsZombie()) {
73 B2FATAL(
"Couldn't open file '" << m_outputFileName <<
"' for writing!");
77 m_tree =
new TTree(
"top",
"TOP validation ntuple");
79 m_tree->Branch(
"evt", &m_top.evt,
"evt/I");
80 m_tree->Branch(
"run", &m_top.run,
"run/I");
82 m_tree->Branch(
"p", &m_top.p,
"p/F");
83 m_tree->Branch(
"cth", &m_top.cth,
"cth/F");
84 m_tree->Branch(
"phi", &m_top.phi,
"phi/F");
85 m_tree->Branch(
"pValue", &m_top.pValue,
"pValue/F");
87 m_tree->Branch(
"PDG", &m_top.PDG,
"PDG/I");
88 m_tree->Branch(
"motherPDG", &m_top.motherPDG,
"motherPDG/I");
89 m_tree->Branch(
"primary", &m_top.primary,
"primary/S");
90 m_tree->Branch(
"seen", &m_top.seen,
"seen/S");
91 m_tree->Branch(
"rhoProd", &m_top.rhoProd,
"rhoProd/F");
92 m_tree->Branch(
"zProd", &m_top.zProd,
"zProd/F");
93 m_tree->Branch(
"phiProd", &m_top.phiProd,
"phiProd/F");
94 m_tree->Branch(
"rhoDec", &m_top.rhoDec,
"rhoDec/F");
95 m_tree->Branch(
"zDec", &m_top.zDec,
"zDec/F");
96 m_tree->Branch(
"phiDec", &m_top.phiDec,
"phiDec/F");
98 m_tree->Branch(
"numPhot", &m_top.numPhot,
"numPhot/I");
99 m_tree->Branch(
"numBkg", &m_top.numBkg,
"numBkg/F");
100 m_tree->Branch(
"phot", &m_top.phot,
"e/F:mu:pi:K:p:d");
101 m_tree->Branch(
"logL", &m_top.logL,
"e/F:mu:pi:K:p:d");
103 m_tree->Branch(
"extHit", &m_top.extHit,
"moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
104 m_tree->Branch(
"barHit", &m_top.barHit,
"moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
109 extHits.isRequired();
111 likelihoods.isRequired();
113 mcParticles.isOptional();
115 barHits.isOptional();
119 void TOPNtupleModule::beginRun()
123 void TOPNtupleModule::event()
129 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
131 for (
const auto& track : tracks) {
132 const auto* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
133 if (!trackFit)
continue;
136 if (top->getFlag() != 1)
continue;
142 if (mcParticle) mother = mcParticle->getMother();
146 m_top.evt = evtMetaData->getEvent();
147 m_top.run = evtMetaData->getRun();
149 TVector3 mom = trackFit->getMomentum();
151 m_top.cth = mom.CosTheta();
152 m_top.phi = mom.Phi();
153 m_top.pValue = trackFit->getPValue();
156 m_top.PDG = mcParticle->getPDG();
157 if (mother) m_top.motherPDG = mother->getPDG();
158 m_top.primary = mcParticle->getStatus(MCParticle::c_PrimaryParticle);
159 m_top.seen = mcParticle->hasSeenInDetector(Const::TOP);
160 TVector3 prodVertex = mcParticle->getProductionVertex();
161 m_top.rhoProd = prodVertex.Perp();
162 m_top.zProd = prodVertex.Z();
163 m_top.phiProd = prodVertex.Phi();
164 TVector3 decVertex = mcParticle->getDecayVertex();
165 m_top.rhoDec = decVertex.Perp();
166 m_top.zDec = decVertex.Z();
167 m_top.phiDec = decVertex.Phi();
170 m_top.numPhot = top->getNphot();
171 m_top.numBkg = top->getEstBkg();
172 m_top.phot.e = top->getEstPhot(Const::electron);
173 m_top.phot.mu = top->getEstPhot(Const::muon);
174 m_top.phot.pi = top->getEstPhot(Const::pion);
175 m_top.phot.K = top->getEstPhot(Const::kaon);
176 m_top.phot.p = top->getEstPhot(Const::proton);
177 m_top.phot.d = top->getEstPhot(Const::deuteron);
179 m_top.logL.e = top->getLogL(Const::electron);
180 m_top.logL.mu = top->getLogL(Const::muon);
181 m_top.logL.pi = top->getLogL(Const::pion);
182 m_top.logL.K = top->getLogL(Const::kaon);
183 m_top.logL.p = top->getLogL(Const::proton);
184 m_top.logL.d = top->getLogL(Const::deuteron);
190 if (geo->isModuleIDValid(moduleID)) {
191 const auto& module = geo->getModule(moduleID);
192 position = module.pointToLocal(position);
193 momentum = module.momentumToLocal(momentum);
195 m_top.extHit.moduleID = moduleID;
197 m_top.extHit.x = position.X();
198 m_top.extHit.y = position.Y();
199 m_top.extHit.z = position.Z();
200 m_top.extHit.p = momentum.Mag();
201 m_top.extHit.theta = momentum.Theta();
202 m_top.extHit.phi = momentum.Phi();
203 m_top.extHit.time = extHit->
getTOF();
207 int moduleID = barHit->getModuleID();
208 TVector3 position = barHit->getPosition();
209 TVector3 momentum = barHit->getMomentum();
210 if (geo->isModuleIDValid(moduleID)) {
211 const auto& module = geo->getModule(moduleID);
212 position = module.pointToLocal(position);
213 momentum = module.momentumToLocal(momentum);
215 m_top.barHit.moduleID = moduleID;
216 m_top.barHit.PDG = barHit->getPDG();
217 m_top.barHit.x = position.X();
218 m_top.barHit.y = position.Y();
219 m_top.barHit.z = position.Z();
220 m_top.barHit.p = momentum.Mag();
221 m_top.barHit.theta = momentum.Theta();
222 m_top.barHit.phi = momentum.Phi();
223 m_top.barHit.time = barHit->getTime();
232 void TOPNtupleModule::endRun()
236 void TOPNtupleModule::terminate()