Belle II Software development
PIDNtupleModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9// Own header.
10#include <reconstruction/modules/PIDNtuple/PIDNtupleModule.h>
11
12// framework - DataStore
13#include <framework/datastore/StoreArray.h>
14#include <framework/datastore/StoreObjPtr.h>
15
16// framework aux
17#include <framework/gearbox/Const.h>
18#include <framework/logging/Logger.h>
19
20// DataStore classes
21#include <framework/dataobjects/EventMetaData.h>
22#include <mdst/dataobjects/Track.h>
23#include <mdst/dataobjects/MCParticle.h>
24#include <mdst/dataobjects/PIDLikelihood.h>
25
26// ROOT
27#include <TDirectory.h>
28#include <TRandom3.h>
29
30using namespace Belle2;
31
32//-----------------------------------------------------------------
33// Register the Module
34//-----------------------------------------------------------------
35REG_MODULE(PIDNtuple);
36
37//-----------------------------------------------------------------
38// Implementation
39//-----------------------------------------------------------------
40
42 m_norm(0), m_value(0), m_file(0), m_tree(0)
43
44{
45 // set module description (e.g. insert text)
46 setDescription("Writes a flat ntuple of PIDLikelihoods with track info into a root file");
47
48 // Add parameters
49 addParam("outputFileName", m_outputFileName, "Output file name",
50 std::string("PIDNtuple.root"));
51 addParam("makeFlat", m_makeFlat, "make momentum distribution flat up to pMax", false);
52 addParam("p1", m_p1, "parameter of momentum distribution", 0.90311E-01);
53 addParam("p2", m_p2, "parameter of momentum distribution", 0.56846);
54 addParam("pMax", m_pMax, "make distribution flat up to this momentum", 3.0);
55}
56
57
59{
60
61 // required inputs
62 StoreArray<Track> tracks;
63 StoreArray<TrackFitResult> trackfitResults;
64 StoreArray<PIDLikelihood> PIDLikelihoods;
65
66 tracks.isRequired();
67 trackfitResults.isRequired();
68 PIDLikelihoods.isRequired();
69
70 // optional inputs
71 StoreArray<MCParticle> mcparticles;
72 mcparticles.isOptional();
73 tracks.optionalRelationTo(mcparticles);
74
75 // Create and book the ROOT file and TTree
76 TDirectory::TContext context;
77 m_file = new TFile(m_outputFileName.c_str(), "RECREATE");
78 m_tree = new TTree("pid", "PID tree");
79
80 m_tree->Branch("evt", &m_pid.evt, "evt/I");
81 m_tree->Branch("run", &m_pid.run, "run/I");
82 m_tree->Branch("p", &m_pid.p, "p/F");
83 m_tree->Branch("cth", &m_pid.cth, "cth/F");
84 m_tree->Branch("phi", &m_pid.phi, "phi/F");
85 m_tree->Branch("pValue", &m_pid.pValue, "pValue/F");
86 m_tree->Branch("PDG", &m_pid.PDG, "PDG/I");
87 m_tree->Branch("motherPDG", &m_pid.motherPDG, "motherPDG/I");
88 m_tree->Branch("primary", &m_pid.primary, "primary/S");
89 m_tree->Branch("rhoProd", &m_pid.rhoProd, "rhoProd/F");
90 m_tree->Branch("zProd", &m_pid.zProd, "zProd/F");
91 m_tree->Branch("phiProd", &m_pid.phiProd, "phiProd/F");
92 m_tree->Branch("rhoDec", &m_pid.rhoDec, "rhoDec/F");
93 m_tree->Branch("zDec", &m_pid.zDec, "zDec/F");
94 m_tree->Branch("phiDec", &m_pid.phiDec, "phiDec/F");
95 m_tree->Branch("svd", &m_pid.svddedx, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
96 m_tree->Branch("cdc", &m_pid.cdcdedx, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
97 m_tree->Branch("top", &m_pid.top, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
98 m_tree->Branch("arich", &m_pid.arich, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
99 m_tree->Branch("ecl", &m_pid.ecl, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
100 m_tree->Branch("klm", &m_pid.klm, "le/F:lmu:lpi:lk:lp:ld:flag/S:seen/S");
101
102 m_norm = 1;
103 double p0 = m_p1 * log1p(4.0 * m_p2 / m_p1); // distribution peak
104 m_norm = 1.0 / momDistribution(p0);
106}
107
108
110{
111
112 StoreObjPtr<EventMetaData> evtMetaData;
113 StoreArray<Track> tracks;
114
115 // loop over tracks
116 for (const auto& track : tracks) {
117
118 const TrackFitResult* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
119 if (!trackFit) {
120 B2WARNING("No track fit result... Skipping.");
121 continue;
122 }
123
124 const PIDLikelihood* pid = track.getRelated<PIDLikelihood>();
125 if (!pid) {
126 B2WARNING("No track fit result... Skipping.");
127 continue;
128 }
129
130 const MCParticle* mcParticle = track.getRelated<MCParticle>();
131 const MCParticle* mother = 0;
132 if (mcParticle) mother = mcParticle->getMother();
133
134 m_pid.clear();
135 m_pid.evt = evtMetaData->getEvent();
136 m_pid.run = evtMetaData->getRun();
137 ROOT::Math::XYZVector momentum = trackFit->getMomentum();
138 m_pid.p = momentum.R();
139 if (m_makeFlat) {
140 if (gRandom->Rndm() * momDistribution(m_pid.p) > m_value) continue;
141 }
142 m_pid.cth = cos(momentum.Theta());
143 m_pid.phi = momentum.Phi();
144 m_pid.pValue = trackFit->getPValue();
145 if (mcParticle) {
146 m_pid.PDG = mcParticle->getPDG();
147 if (mother) m_pid.motherPDG = mother->getPDG();
149 ROOT::Math::XYZVector prodVertex = mcParticle->getProductionVertex();
150 m_pid.rhoProd = prodVertex.Rho();
151 m_pid.zProd = prodVertex.Z();
152 m_pid.phiProd = prodVertex.Phi();
153 ROOT::Math::XYZVector decVertex = mcParticle->getDecayVertex();
154 m_pid.rhoDec = decVertex.Rho();
155 m_pid.zDec = decVertex.Z();
156 m_pid.phiDec = decVertex.Phi();
157 }
158
159 m_pid.cdcdedx.le = pid->getLogL(Const::electron, Const::CDC);
160 m_pid.cdcdedx.lmu = pid->getLogL(Const::muon, Const::CDC);
161 m_pid.cdcdedx.lpi = pid->getLogL(Const::pion, Const::CDC);
162 m_pid.cdcdedx.lk = pid->getLogL(Const::kaon, Const::CDC);
163 m_pid.cdcdedx.lp = pid->getLogL(Const::proton, Const::CDC);
164 m_pid.cdcdedx.ld = pid->getLogL(Const::deuteron, Const::CDC);
165 m_pid.cdcdedx.flag = pid->isAvailable(Const::CDC);
166 if (mcParticle)
167 m_pid.cdcdedx.seen = mcParticle->hasSeenInDetector(Const::CDC);
168
169 m_pid.svddedx.le = pid->getLogL(Const::electron, Const::SVD);
170 m_pid.svddedx.lmu = pid->getLogL(Const::muon, Const::SVD);
171 m_pid.svddedx.lpi = pid->getLogL(Const::pion, Const::SVD);
172 m_pid.svddedx.lk = pid->getLogL(Const::kaon, Const::SVD);
173 m_pid.svddedx.lp = pid->getLogL(Const::proton, Const::SVD);
174 m_pid.svddedx.ld = pid->getLogL(Const::deuteron, Const::SVD);
175 m_pid.svddedx.flag = pid->isAvailable(Const::SVD);
176 if (mcParticle)
177 m_pid.svddedx.seen = mcParticle->hasSeenInDetector(Const::SVD);
178
179 m_pid.top.le = pid->getLogL(Const::electron, Const::TOP);
180 m_pid.top.lmu = pid->getLogL(Const::muon, Const::TOP);
181 m_pid.top.lpi = pid->getLogL(Const::pion, Const::TOP);
182 m_pid.top.lk = pid->getLogL(Const::kaon, Const::TOP);
183 m_pid.top.lp = pid->getLogL(Const::proton, Const::TOP);
184 m_pid.top.ld = pid->getLogL(Const::deuteron, Const::TOP);
185 m_pid.top.flag = pid->isAvailable(Const::TOP);
186 if (mcParticle)
187 m_pid.top.seen = mcParticle->hasSeenInDetector(Const::TOP);
188
189 m_pid.arich.le = pid->getLogL(Const::electron, Const::ARICH);
190 m_pid.arich.lmu = pid->getLogL(Const::muon, Const::ARICH);
191 m_pid.arich.lpi = pid->getLogL(Const::pion, Const::ARICH);
192 m_pid.arich.lk = pid->getLogL(Const::kaon, Const::ARICH);
193 m_pid.arich.lp = pid->getLogL(Const::proton, Const::ARICH);
194 m_pid.arich.ld = pid->getLogL(Const::deuteron, Const::ARICH);
195 m_pid.arich.flag = pid->isAvailable(Const::ARICH);
196 if (mcParticle)
197 m_pid.arich.seen = mcParticle->hasSeenInDetector(Const::ARICH);
198
199 m_pid.ecl.le = pid->getLogL(Const::electron, Const::ECL);
200 m_pid.ecl.lmu = pid->getLogL(Const::muon, Const::ECL);
201 m_pid.ecl.lpi = pid->getLogL(Const::pion, Const::ECL);
202 m_pid.ecl.lk = pid->getLogL(Const::kaon, Const::ECL);
203 m_pid.ecl.lp = pid->getLogL(Const::proton, Const::ECL);
204 m_pid.ecl.ld = pid->getLogL(Const::deuteron, Const::ECL);
205 m_pid.ecl.flag = pid->isAvailable(Const::ECL);
206 if (mcParticle)
207 m_pid.ecl.seen = mcParticle->hasSeenInDetector(Const::ECL);
208
209 m_pid.klm.le = pid->getLogL(Const::electron, Const::KLM);
210 m_pid.klm.lmu = pid->getLogL(Const::muon, Const::KLM);
211 m_pid.klm.lpi = pid->getLogL(Const::pion, Const::KLM);
212 m_pid.klm.lk = pid->getLogL(Const::kaon, Const::KLM);
213 m_pid.klm.lp = pid->getLogL(Const::proton, Const::KLM);
214 m_pid.klm.ld = pid->getLogL(Const::deuteron, Const::KLM);
215 m_pid.klm.flag = pid->isAvailable(Const::KLM);
216 if (mcParticle)
217 m_pid.klm.seen = mcParticle->hasSeenInDetector(Const::KLM);
218
219 m_tree->Fill();
220 }
221
222}
223
224
226{
227 TDirectory::TContext context;
228 m_file->cd();
229 m_tree->Write();
230 m_file->Close();
231}
232
static const ChargedStable muon
muon particle
Definition: Const.h:660
static const ChargedStable pion
charged pion particle
Definition: Const.h:661
static const ChargedStable proton
proton particle
Definition: Const.h:663
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:662
static const ChargedStable electron
electron particle
Definition: Const.h:659
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:664
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:47
ROOT::Math::XYZVector getDecayVertex() const
Return decay vertex.
Definition: MCParticle.h:219
bool hasSeenInDetector(Const::DetectorSet set) const
Return if the seen-in flag for a specific subdetector is set or not.
Definition: MCParticle.h:310
ROOT::Math::XYZVector getProductionVertex() const
Return production vertex position.
Definition: MCParticle.h:189
unsigned int getStatus(unsigned short int bitmask=USHRT_MAX) const
Return status code of particle.
Definition: MCParticle.h:122
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:112
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class to collect log likelihoods from TOP, ARICH, dEdx, ECL and KLM aimed for output to mdst includes...
Definition: PIDLikelihood.h:29
TTree * m_tree
TTree with PIDTree structure.
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
bool m_makeFlat
if true, make momentum distribution flat up to m_pMax
virtual void terminate() override
Termination action.
double m_p2
parameter of momentum distribution
PID::PIDTree m_pid
PID tree structure.
PIDNtupleModule()
Constructor.
double momDistribution(double p) const
parameterized momentum distribution
double m_pMax
flatten distribution up to this momentum
double m_p1
parameter of momentum distribution
double m_value
distribution value at m_pMax
double m_norm
distribution normalization
std::string m_outputFileName
output file name
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Values of the result of a track fit with a given particle hypothesis.
double getPValue() const
Getter for Chi2 Probability of the track fit.
ROOT::Math::XYZVector getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:600
Abstract base class for different kinds of events.
Float_t lk
log likelihood for kaon
Definition: PIDTree.h:27
Short_t flag
flag: information is available (1) or not (0)
Definition: PIDTree.h:30
Float_t ld
log likelihood for deuteron
Definition: PIDTree.h:29
Short_t seen
is seen in this component (from related MCParticle)
Definition: PIDTree.h:31
Float_t le
log likelihood for electron
Definition: PIDTree.h:24
Float_t lp
log likelihood for proton
Definition: PIDTree.h:28
Float_t lpi
log likelihood for pion
Definition: PIDTree.h:26
Float_t lmu
log likelihood for muon
Definition: PIDTree.h:25
Float_t zDec
decay vertex (cylindrical coordinate z) of MCParticle
Definition: PIDTree.h:68
Float_t p
momentum magnitude of Track
Definition: PIDTree.h:56
LogLikelihoods top
log likelihoods from TOP
Definition: PIDTree.h:73
LogLikelihoods cdcdedx
log likelihoods from CDC dE/dx
Definition: PIDTree.h:71
Float_t rhoDec
decay vertex (cylindrical coordinate r) of MCParticle
Definition: PIDTree.h:67
LogLikelihoods ecl
log likelihoods from ECL
Definition: PIDTree.h:75
Int_t run
run number
Definition: PIDTree.h:54
Short_t primary
is a primary particle (from related MCParticle)
Definition: PIDTree.h:63
Int_t evt
event number
Definition: PIDTree.h:53
Float_t phi
azimuthal angle of Track
Definition: PIDTree.h:58
LogLikelihoods svddedx
log likelihoods from SVD dE/dx
Definition: PIDTree.h:72
Int_t motherPDG
PDG code of related mother MCParticle.
Definition: PIDTree.h:62
Float_t phiProd
production vertex (cylindrical coordinate phi) of MCParticle
Definition: PIDTree.h:66
Float_t cth
cosine of polar angle of Track
Definition: PIDTree.h:57
Float_t rhoProd
production vertex (cylindrical coordinate r) of MCParticle
Definition: PIDTree.h:64
void clear()
Clear the structure: set elements to zero.
Definition: PIDTree.h:88
Float_t phiDec
decay vertex (cylindrical coordinate phi) of MCParticle
Definition: PIDTree.h:69
LogLikelihoods klm
log likelihoods from KLM
Definition: PIDTree.h:76
Int_t PDG
PDG code of related MCParticle.
Definition: PIDTree.h:61
LogLikelihoods arich
log likelihoods from ARICH
Definition: PIDTree.h:74
Float_t pValue
p-value of Track fit
Definition: PIDTree.h:59
Float_t zProd
production vertex (cylindrical coordinate z) of MCParticle
Definition: PIDTree.h:65