Belle II Software  release-08-01-10
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 
30 using namespace Belle2;
31 
32 //-----------------------------------------------------------------
33 // Register the Module
34 //-----------------------------------------------------------------
35 REG_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");
48 
49  // Add parameters
50  addParam("outputFileName", m_outputFileName, "Output file name",
51  std::string("PIDNtuple.root"));
52  addParam("makeFlat", m_makeFlat, "make momentum distribution flat up to pMax", false);
53  addParam("p1", m_p1, "parameter of momentum distribution", 0.90311E-01);
54  addParam("p2", m_p2, "parameter of momentum distribution", 0.56846);
55  addParam("pMax", m_pMax, "make distribution flat up to this momentum", 3.0);
56 }
57 
59 
61 {
62 
63  // required inputs
64  StoreArray<Track> tracks;
65  StoreArray<TrackFitResult> trackfitResults;
66  StoreArray<PIDLikelihood> PIDLikelihoods;
67 
68  tracks.isRequired();
69  trackfitResults.isRequired();
70  PIDLikelihoods.isRequired();
71 
72  // optional inputs
73  StoreArray<MCParticle> mcparticles;
74  mcparticles.isOptional();
75  tracks.optionalRelationTo(mcparticles);
76 
77  // Create and book the ROOT file and TTree
78  TDirectory::TContext context;
79  m_file = new TFile(m_outputFileName.c_str(), "RECREATE");
80  m_tree = new TTree("pid", "PID tree");
81 
82  m_tree->Branch("evt", &m_pid.evt, "evt/I");
83  m_tree->Branch("run", &m_pid.run, "run/I");
84  m_tree->Branch("p", &m_pid.p, "p/F");
85  m_tree->Branch("cth", &m_pid.cth, "cth/F");
86  m_tree->Branch("phi", &m_pid.phi, "phi/F");
87  m_tree->Branch("pValue", &m_pid.pValue, "pValue/F");
88  m_tree->Branch("PDG", &m_pid.PDG, "PDG/I");
89  m_tree->Branch("motherPDG", &m_pid.motherPDG, "motherPDG/I");
90  m_tree->Branch("primary", &m_pid.primary, "primary/S");
91  m_tree->Branch("rhoProd", &m_pid.rhoProd, "rhoProd/F");
92  m_tree->Branch("zProd", &m_pid.zProd, "zProd/F");
93  m_tree->Branch("phiProd", &m_pid.phiProd, "phiProd/F");
94  m_tree->Branch("rhoDec", &m_pid.rhoDec, "rhoDec/F");
95  m_tree->Branch("zDec", &m_pid.zDec, "zDec/F");
96  m_tree->Branch("phiDec", &m_pid.phiDec, "phiDec/F");
97  m_tree->Branch("cdcdedx", &m_pid.cdcdedx, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
98  m_tree->Branch("svddedx", &m_pid.svddedx, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
99  m_tree->Branch("top", &m_pid.top, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
100  m_tree->Branch("arich", &m_pid.arich, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
101  m_tree->Branch("ecl", &m_pid.ecl, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
102  m_tree->Branch("klm", &m_pid.klm, "le/F:lmu:lpi:lk:lp:flag/S:seen/S");
103 
104  m_norm = 1;
105  double p0 = m_p1 * log1p(4.0 * m_p2 / m_p1); // distribution peak
106  m_norm = 1.0 / momDistribution(p0);
108 }
109 
111 {
112 }
113 
115 {
116 
117  StoreObjPtr<EventMetaData> evtMetaData;
118  StoreArray<Track> tracks;
119 
120  // loop over tracks
121  for (const auto& track : tracks) {
122 
123  const TrackFitResult* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
124  if (!trackFit) {
125  B2WARNING("No track fit result... Skipping.");
126  continue;
127  }
128 
129  const PIDLikelihood* pid = track.getRelated<PIDLikelihood>();
130  if (!pid) {
131  B2WARNING("No track fit result... Skipping.");
132  continue;
133  }
134 
135  const MCParticle* mcParticle = track.getRelated<MCParticle>();
136  const MCParticle* mother = 0;
137  if (mcParticle) mother = mcParticle->getMother();
138 
139  m_pid.clear();
140  m_pid.evt = evtMetaData->getEvent();
141  m_pid.run = evtMetaData->getRun();
142  ROOT::Math::XYZVector momentum = trackFit->getMomentum();
143  m_pid.p = momentum.R();
144  if (m_makeFlat) {
145  if (gRandom->Rndm() * momDistribution(m_pid.p) > m_value) continue;
146  }
147  m_pid.cth = cos(momentum.Theta());
148  m_pid.phi = momentum.Phi();
149  m_pid.pValue = trackFit->getPValue();
150  if (mcParticle) {
151  m_pid.PDG = mcParticle->getPDG();
152  if (mother) m_pid.motherPDG = mother->getPDG();
154  ROOT::Math::XYZVector prodVertex = mcParticle->getProductionVertex();
155  m_pid.rhoProd = prodVertex.Rho();
156  m_pid.zProd = prodVertex.Z();
157  m_pid.phiProd = prodVertex.Phi();
158  ROOT::Math::XYZVector decVertex = mcParticle->getDecayVertex();
159  m_pid.rhoDec = decVertex.Rho();
160  m_pid.zDec = decVertex.Z();
161  m_pid.phiDec = decVertex.Phi();
162  }
163 
164  m_pid.cdcdedx.le = pid->getLogL(Const::electron, Const::CDC);
165  m_pid.cdcdedx.lmu = pid->getLogL(Const::muon, Const::CDC);
166  m_pid.cdcdedx.lpi = pid->getLogL(Const::pion, Const::CDC);
167  m_pid.cdcdedx.lk = pid->getLogL(Const::kaon, Const::CDC);
168  m_pid.cdcdedx.lp = pid->getLogL(Const::proton, Const::CDC);
169  m_pid.cdcdedx.flag = pid->isAvailable(Const::CDC);
170  if (mcParticle)
171  m_pid.cdcdedx.seen = mcParticle->hasSeenInDetector(Const::CDC);
172 
173  m_pid.svddedx.le = pid->getLogL(Const::electron, Const::SVD);
174  m_pid.svddedx.lmu = pid->getLogL(Const::muon, Const::SVD);
175  m_pid.svddedx.lpi = pid->getLogL(Const::pion, Const::SVD);
176  m_pid.svddedx.lk = pid->getLogL(Const::kaon, Const::SVD);
177  m_pid.svddedx.lp = pid->getLogL(Const::proton, Const::SVD);
178  m_pid.svddedx.flag = pid->isAvailable(Const::SVD);
179  if (mcParticle)
180  m_pid.svddedx.seen = mcParticle->hasSeenInDetector(Const::SVD);
181 
182  m_pid.top.le = pid->getLogL(Const::electron, Const::TOP);
183  m_pid.top.lmu = pid->getLogL(Const::muon, Const::TOP);
184  m_pid.top.lpi = pid->getLogL(Const::pion, Const::TOP);
185  m_pid.top.lk = pid->getLogL(Const::kaon, Const::TOP);
186  m_pid.top.lp = pid->getLogL(Const::proton, Const::TOP);
187  m_pid.top.flag = pid->isAvailable(Const::TOP);
188  if (mcParticle)
189  m_pid.top.seen = mcParticle->hasSeenInDetector(Const::TOP);
190 
191  m_pid.arich.le = pid->getLogL(Const::electron, Const::ARICH);
192  m_pid.arich.lmu = pid->getLogL(Const::muon, Const::ARICH);
193  m_pid.arich.lpi = pid->getLogL(Const::pion, Const::ARICH);
194  m_pid.arich.lk = pid->getLogL(Const::kaon, Const::ARICH);
195  m_pid.arich.lp = pid->getLogL(Const::proton, Const::ARICH);
196  m_pid.arich.flag = pid->isAvailable(Const::ARICH);
197  if (mcParticle)
198  m_pid.arich.seen = mcParticle->hasSeenInDetector(Const::ARICH);
199 
200  m_pid.ecl.le = pid->getLogL(Const::electron, Const::ECL);
201  m_pid.ecl.lmu = pid->getLogL(Const::muon, Const::ECL);
202  m_pid.ecl.lpi = pid->getLogL(Const::pion, Const::ECL);
203  m_pid.ecl.lk = pid->getLogL(Const::kaon, Const::ECL);
204  m_pid.ecl.lp = pid->getLogL(Const::proton, 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.flag = pid->isAvailable(Const::KLM);
215  if (mcParticle)
216  m_pid.klm.seen = mcParticle->hasSeenInDetector(Const::KLM);
217 
218  m_tree->Fill();
219  }
220 
221 }
222 
223 
225 
227 {
228  TDirectory::TContext context;
229  m_file->cd();
230  m_tree->Write();
231  m_file->Close();
232 }
233 
static const ChargedStable muon
muon particle
Definition: Const.h:651
static const ChargedStable pion
charged pion particle
Definition: Const.h:652
static const ChargedStable proton
proton particle
Definition: Const.h:654
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:653
static const ChargedStable electron
electron particle
Definition: Const.h:650
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
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
Class to collect log likelihoods from TOP, ARICH, dEdx, ECL and KLM aimed for output to mdst includes...
Definition: PIDLikelihood.h:26
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 endRun() override
End-of-run action.
virtual void terminate() override
Termination action.
double m_p2
parameter of momentum distribution
PID::PIDTree m_pid
PID tree structure.
virtual void beginRun() override
Called when entering a new run.
PIDNtupleModule()
Constructor.
virtual ~PIDNtupleModule()
Destructor.
void printModuleParams() const
Prints module parameters.
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.
REG_MODULE(arichBtest)
Register the Module.
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
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:29
Short_t seen
is seen in this component (from related MCParticle)
Definition: PIDTree.h:30
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:72
Float_t p
momentum magnitude of Track
Definition: PIDTree.h:60
LogLikelihoods top
log likelihoods from TOP
Definition: PIDTree.h:77
LogLikelihoods cdcdedx
log likelihoods from CDC dE/dx
Definition: PIDTree.h:75
Float_t rhoDec
decay vertex (cylindrical coordinate r) of MCParticle
Definition: PIDTree.h:71
LogLikelihoods ecl
log likelihoods from ECL
Definition: PIDTree.h:79
Int_t run
run number
Definition: PIDTree.h:58
Short_t primary
is a primary particle (from related MCParticle)
Definition: PIDTree.h:67
Int_t evt
event number
Definition: PIDTree.h:57
Float_t phi
azimuthal angle of Track
Definition: PIDTree.h:62
LogLikelihoods svddedx
log likelihoods from SVD dE/dx
Definition: PIDTree.h:76
Int_t motherPDG
PDG code of related mother MCParticle.
Definition: PIDTree.h:66
Float_t phiProd
production vertex (cylindrical coordinate phi) of MCParticle
Definition: PIDTree.h:70
Float_t cth
cosine of polar angle of Track
Definition: PIDTree.h:61
Float_t rhoProd
production vertex (cylindrical coordinate r) of MCParticle
Definition: PIDTree.h:68
void clear()
Clear the structure: set elements to zero.
Definition: PIDTree.h:92
Float_t phiDec
decay vertex (cylindrical coordinate phi) of MCParticle
Definition: PIDTree.h:73
LogLikelihoods klm
log likelihoods from KLM
Definition: PIDTree.h:80
Int_t PDG
PDG code of related MCParticle.
Definition: PIDTree.h:65
LogLikelihoods arich
log likelihoods from ARICH
Definition: PIDTree.h:78
Float_t pValue
p-value of Track fit
Definition: PIDTree.h:63
Float_t zProd
production vertex (cylindrical coordinate z) of MCParticle
Definition: PIDTree.h:69