Belle II Software  release-06-02-00
DedxVariables.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 // needed to build variables here
10 #include <reconstruction/variables/DedxVariables.h>
11 #include <analysis/VariableManager/Manager.h>
12 
13 // framework - DataStore
14 #include <framework/gearbox/Const.h>
15 #include <framework/logging/Logger.h>
16 #include <framework/utilities/Conversion.h>
17 
18 // dataobjects
19 #include <analysis/dataobjects/Particle.h>
20 #include <mdst/dataobjects/Track.h>
21 #include <reconstruction/dataobjects/CDCDedxTrack.h>
22 #include <reconstruction/dataobjects/VXDDedxTrack.h>
23 
24 #include <TString.h>
25 
26 #include <cmath>
27 
28 namespace Belle2 {
37  CDCDedxTrack const* getDedxFromParticle(Particle const* particle)
38  {
39  const Track* track = particle->getTrack();
40  if (!track) {
41  return nullptr;
42  }
43 
44  const CDCDedxTrack* dedxTrack = track->getRelatedTo<CDCDedxTrack>();
45  if (!dedxTrack) {
46  return nullptr;
47  }
48 
49  return dedxTrack;
50  }
51 
56  {
57  const Track* track = particle->getTrack();
58  if (!track) {
59  return nullptr;
60  }
61 
62  const VXDDedxTrack* dedxTrack = track->getRelatedTo<VXDDedxTrack>();
63  if (!dedxTrack) {
64  return nullptr;
65  }
66  return dedxTrack;
67  }
68 
69 
70  namespace Variable {
71 
72  double CDCdedx(const Particle* part)
73  {
74  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
75  if (!dedxTrack) {
76  return -999.0;
77  } else {
78  return dedxTrack->getDedx();
79  }
80  }
81 
82  double CDCdedxnosat(const Particle* part)
83  {
84  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
85  if (!dedxTrack) {
86  return -999.0;
87  } else {
88  return dedxTrack->getDedxNoSat();
89  }
90  }
91 
92  double pCDC(const Particle* part)
93  {
94  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
95  if (!dedxTrack) {
96  return -999.0;
97  } else {
98  return dedxTrack->getMomentum();
99  }
100  }
101 
102  double costhCDC(const Particle* part)
103  {
104  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
105  if (!dedxTrack) {
106  return -999.0;
107  } else {
108  return dedxTrack->getCosTheta();
109  }
110  }
111 
112 
113  double CDCdEdx_nhits(const Particle* part)
114  {
115  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
116  if (!dedxTrack) {
117  return -999.0;
118  } else {
119  return dedxTrack->size();
120  }
121  }
122 
123  double CDCdEdx_lnhits(const Particle* part)
124  {
125  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
126  if (!dedxTrack) {
127  return -999.0;
128  } else {
129  return dedxTrack->getNLayerHits();
130  }
131  }
132 
133  double CDCdEdx_lnhitsused(const Particle* part)
134  {
135  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
136  if (!dedxTrack) {
137  return -999.0;
138  } else {
139  return dedxTrack->getNLayerHitsUsed();
140  }
141  }
142 
143  Manager::FunctionPtr CDCdEdx_PIDvars(const std::vector<std::string>& arguments)
144  {
145  if (arguments.size() < 2) {
146  B2ERROR("Min two arguments required to get variables related chi, predicted mean and reso");
147  return nullptr;
148  }
149 
150  TString var = "";
151  try {
152  var = arguments[0];
153  } catch (std::invalid_argument& e) {
154  B2ERROR("First argument of variable must be a variable name (chi or pmean or preso)");
155  return nullptr;
156  }
157 
158  int pdgCode;
159  try {
160  pdgCode = Belle2::convertString<int>(arguments[1]);
161  } catch (std::invalid_argument& e) {
162  B2ERROR("Second argument of variable must be a PDG code");
163  return nullptr;
164  }
165 
166  int index = -999;
167  if (abs(pdgCode) == Const::electron.getPDGCode())index = 0;
168  else if (abs(pdgCode) == Const::muon.getPDGCode())index = 1;
169  else if (abs(pdgCode) == Const::pion.getPDGCode())index = 2;
170  else if (abs(pdgCode) == Const::kaon.getPDGCode())index = 3;
171  else if (abs(pdgCode) == Const::proton.getPDGCode())index = 4;
172  else if (abs(pdgCode) == Const::deuteron.getPDGCode())index = 5;
173 
174  auto func = [index, var](const Particle * part) -> double {
175  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
176  if (!dedxTrack)
177  {
178  return std::numeric_limits<float>::quiet_NaN();
179  } else {
180  if (var == "chi") return dedxTrack->getChi(index);
181  else if (var == "pmean") return dedxTrack->getPmean(index);
182  else if (var == "preso") return dedxTrack->getPreso(index);
183  else return std::numeric_limits<float>::quiet_NaN();
184  }
185  };
186  return func;
187  }
188 
189  double CDCdEdx_chiE(const Particle* part)
190  {
191  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 11)")->function(part);
192  }
193 
194  double CDCdEdx_chiMu(const Particle* part)
195  {
196  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 13)")->function(part);
197  }
198 
199  double CDCdEdx_chiPi(const Particle* part)
200  {
201  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 211)")->function(part);
202  }
203 
204  double CDCdEdx_chiK(const Particle* part)
205  {
206  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 321)")->function(part);
207  }
208 
209  double CDCdEdx_chiP(const Particle* part)
210  {
211  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 2212)")->function(part);
212  }
213 
214  double CDCdEdx_chiD(const Particle* part)
215  {
216  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 1000010020)")->function(part);
217  }
218 
219 //Variables for SVD dedx
220  double SVD_p(const Particle* part)
221  {
222  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
223  if (!dedxTrack) {
224  return -999.0;
225  } else {
226  return dedxTrack->getMomentum();
227  }
228  }
229 
230  double SVD_pTrue(const Particle* part)
231  {
232  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
233  if (!dedxTrack) {
234  return -999.0;
235  } else {
236  return dedxTrack->getTrueMomentum();
237  }
238  }
239 
240  double SVDdedx(const Particle* part)
241  {
242  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
243  if (!dedxTrack) {
244  return -999.0;
245  } else {
246  return dedxTrack->getDedx(Const::EDetector::SVD);
247  }
248  }
249 
250  double SVD_CosTheta(const Particle* part)
251  {
252  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
253  if (!dedxTrack) {
254  return -999.0;
255  } else {
256  return dedxTrack->getCosTheta();
257  }
258  }
259  double SVD_nHits(const Particle* part)
260  {
261  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
262  if (!dedxTrack) {
263  return -999.0;
264  } else {
265  return dedxTrack->size();
266  }
267  }
268 
269  VARIABLE_GROUP("Dedx");
270  //CDC variables
271  REGISTER_VARIABLE("CDCdEdx", CDCdedx, "CDC dE/dx truncated mean");
272  REGISTER_VARIABLE("CDCdEdxnosat", CDCdedxnosat,
273  "CDC dE/dx truncated mean without saturation correction (NA for current track level MC)");
274  REGISTER_VARIABLE("pCDC", pCDC, "Momentum valid in the CDC");
275  REGISTER_VARIABLE("costhCDC", costhCDC, "costheta valid in the CDC");
276  REGISTER_VARIABLE("CDCdEdx_nhits", CDCdEdx_nhits, "total hits of dedx track");
277  REGISTER_VARIABLE("CDCdEdx_lnhits", CDCdEdx_lnhits, "layer hits for dedx track");
278  REGISTER_VARIABLE("CDCdEdx_lnhitsused", CDCdEdx_lnhitsused, "truncated hits of dedx track");
279 
280  REGISTER_VARIABLE("CDCdEdx_PIDvars(var,PDG) var (= chi or pmean or preso) and PDG is of charged particles", CDCdEdx_PIDvars,
281  "advance CDC dEdx PID related variables for charged particle");
282 
283  REGISTER_VARIABLE("CDCdEdx_chiE", CDCdEdx_chiE, "Chi value of electrons from CDC dEdx");
284  REGISTER_VARIABLE("CDCdEdx_chiMu", CDCdEdx_chiMu, "Chi value of muons from CDC dEdx");
285  REGISTER_VARIABLE("CDCdEdx_chiPi", CDCdEdx_chiPi, "Chi value of pions from CDC dEdx");
286  REGISTER_VARIABLE("CDCdEdx_chiK", CDCdEdx_chiK, "Chi value of kaons from CDC dEdx");
287  REGISTER_VARIABLE("CDCdEdx_chiP", CDCdEdx_chiP, "Chi value of protons from CDC dEdx");
288  REGISTER_VARIABLE("CDCdEdx_chiD", CDCdEdx_chiD, "Chi value of duetrons from CDC dEdx");
289 
290  //SVD variables
291  REGISTER_VARIABLE("SVDdEdx", SVDdedx, "SVD dE/dx truncated mean");
292  REGISTER_VARIABLE("pSVD", SVD_p, "momentum valid in the SVD");
293  REGISTER_VARIABLE("SVD_pTrue", SVD_pTrue, "true MC momentum valid in the SVD");
294  REGISTER_VARIABLE("SVD_CosTheta", SVD_CosTheta, "cos(theta) of the track valid in the SVD");
295  REGISTER_VARIABLE("SVD_nHits", SVD_nHits, "number of hits of the track valid in the SVD");
296 
297  }
299 }
Debug output for CDCDedxPID module.
Definition: CDCDedxTrack.h:25
static const ChargedStable muon
muon particle
Definition: Const.h:541
static const ChargedStable pion
charged pion particle
Definition: Const.h:542
static const ChargedStable proton
proton particle
Definition: Const.h:544
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:543
static const ChargedStable electron
electron particle
Definition: Const.h:540
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:545
Class to store reconstructed particles.
Definition: Particle.h:74
Class that bundles various TrackFitResults.
Definition: Track.h:25
Debug output for VXDDedxPID module.
Definition: VXDDedxTrack.h:27
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:31
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
std::function< double(const Particle *)> FunctionPtr
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:108
VXDDedxTrack const * getSVDDedxFromParticle(Particle const *particle)
SVD dEdx value from particle.
CDCDedxTrack const * getDedxFromParticle(Particle const *particle)
CDC dEdx value from particle.
Abstract base class for different kinds of events.
FunctionPtr function
Pointer to function.
Definition: Manager.h:134