Belle II Software  release-05-01-25
DedxVariables.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jake Bennett
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // needed to build variables here
12 #include <reconstruction/variables/DedxVariables.h>
13 #include <analysis/VariableManager/Manager.h>
14 
15 // framework - DataStore
16 #include <framework/gearbox/Const.h>
17 #include <framework/logging/Logger.h>
18 #include <framework/utilities/Conversion.h>
19 
20 // dataobjects
21 #include <analysis/dataobjects/Particle.h>
22 #include <mdst/dataobjects/Track.h>
23 #include <reconstruction/dataobjects/CDCDedxTrack.h>
24 #include <reconstruction/dataobjects/VXDDedxTrack.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 
55  VXDDedxTrack const* getSVDDedxFromParticle(Particle const* particle)
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  double CDCdEdx_llog(const Particle* part, const int index)
144  {
145  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
146  if (!dedxTrack) {
147  return -999.0;
148  } else {
149  return dedxTrack->getLogl(index);
150  }
151  }
152 
153  double CDCdEdx_llogE(const Particle* part)
154  {
155  return CDCdEdx_llog(part, 0);
156  }
157 
158  double CDCdEdx_llogMu(const Particle* part)
159  {
160  return CDCdEdx_llog(part, 1);
161  }
162 
163  double CDCdEdx_llogPi(const Particle* part)
164  {
165  return CDCdEdx_llog(part, 2);
166  }
167 
168  double CDCdEdx_llogK(const Particle* part)
169  {
170  return CDCdEdx_llog(part, 3);
171  }
172 
173  double CDCdEdx_llogP(const Particle* part)
174  {
175  return CDCdEdx_llog(part, 4);
176  }
177 
178  double CDCdEdx_llogD(const Particle* part)
179  {
180  return CDCdEdx_llog(part, 5);
181  }
182 
183 
184 
185  Manager::FunctionPtr CDCdEdx_PIDvars(const std::vector<std::string>& arguments)
186  {
187  if (arguments.size() < 2) {
188  B2ERROR("Min two arguments required to get variables related chi, predicted mean and reso");
189  return nullptr;
190  }
191 
192  TString var = "";
193  try {
194  var = arguments[0];
195  } catch (std::invalid_argument& e) {
196  B2ERROR("First argument of variable must be a variable name (chi or pmean or preso)");
197  return nullptr;
198  }
199 
200  int pdgCode;
201  try {
202  pdgCode = Belle2::convertString<int>(arguments[1]);
203  } catch (std::invalid_argument& e) {
204  B2ERROR("Second argument of variable must be a PDG code");
205  return nullptr;
206  }
207 
208  int index = -999;
209  if (abs(pdgCode) == 11)index = 0;
210  else if (abs(pdgCode) == 13)index = 1;
211  else if (abs(pdgCode) == 211)index = 2;
212  else if (abs(pdgCode) == 321)index = 3;
213  else if (abs(pdgCode) == 2212)index = 4;
214  else if (abs(pdgCode) == 1000010020)index = 5;
215 
216  auto func = [index, var](const Particle * part) -> double {
217  const CDCDedxTrack* dedxTrack = getDedxFromParticle(part);
218  if (!dedxTrack)
219  {
220  return std::numeric_limits<float>::quiet_NaN();
221  } else {
222  if (var == "chi") return dedxTrack->getChi(index);
223  else if (var == "pmean") return dedxTrack->getPmean(index);
224  else if (var == "preso") return dedxTrack->getPreso(index);
225  else return std::numeric_limits<float>::quiet_NaN();
226  }
227  };
228  return func;
229  }
230 
231  double CDCdEdx_chiE(const Particle* part)
232  {
233  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 11)")->function(part);
234  }
235 
236  double CDCdEdx_chiMu(const Particle* part)
237  {
238  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 13)")->function(part);
239  }
240 
241  double CDCdEdx_chiPi(const Particle* part)
242  {
243  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 211)")->function(part);
244  }
245 
246  double CDCdEdx_chiK(const Particle* part)
247  {
248  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 321)")->function(part);
249  }
250 
251  double CDCdEdx_chiP(const Particle* part)
252  {
253  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 2212)")->function(part);
254  }
255 
256  double CDCdEdx_chiD(const Particle* part)
257  {
258  return Manager::Instance().getVariable("CDCdEdx_PIDvars(chi, 1000010020)")->function(part);
259  }
260 
261 //Variables for SVD dedx
262  double SVD_p(const Particle* part)
263  {
264  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
265  if (!dedxTrack) {
266  return -999.0;
267  } else {
268  return dedxTrack->getMomentum();
269  }
270  }
271 
272  double SVD_pTrue(const Particle* part)
273  {
274  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
275  if (!dedxTrack) {
276  return -999.0;
277  } else {
278  return dedxTrack->getTrueMomentum();
279  }
280  }
281 
282  double SVDdedx(const Particle* part)
283  {
284  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
285  if (!dedxTrack) {
286  return -999.0;
287  } else {
288  return dedxTrack->getDedx(Const::EDetector::SVD);
289  }
290  }
291 
292  double SVD_CosTheta(const Particle* part)
293  {
294  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
295  if (!dedxTrack) {
296  return -999.0;
297  } else {
298  return dedxTrack->getCosTheta();
299  }
300  }
301  double SVD_nHits(const Particle* part)
302  {
303  const VXDDedxTrack* dedxTrack = getSVDDedxFromParticle(part);
304  if (!dedxTrack) {
305  return -999.0;
306  } else {
307  return dedxTrack->size();
308  }
309  }
310 
311  VARIABLE_GROUP("Dedx");
312 //CDC variables
313  REGISTER_VARIABLE("CDCdEdx", CDCdedx, "CDC dE/dx truncated mean");
314  REGISTER_VARIABLE("CDCdEdxnosat", CDCdedxnosat, "CDC dE/dx truncated mean without saturation correction");
315  REGISTER_VARIABLE("pCDC", pCDC, "Momentum valid in the CDC");
316  REGISTER_VARIABLE("costhCDC", costhCDC, "costheta valid in the CDC");
317  REGISTER_VARIABLE("CDCdEdx_nhits", CDCdEdx_nhits, "total hits of dedx track");
318  REGISTER_VARIABLE("CDCdEdx_lnhits", CDCdEdx_lnhits, "layer hits for dedx track");
319  REGISTER_VARIABLE("CDCdEdx_lnhitsused", CDCdEdx_lnhitsused, "truncated hits of dedx track");
320 
321  REGISTER_VARIABLE("CDCdEdx_PIDvars(var,PDG) var (= chi or pmean or preso) and PDG is of charged particles", CDCdEdx_PIDvars,
322  "advance CDC dEdx PID related variables for charged particle");
323 
324  REGISTER_VARIABLE("CDCdEdx_chiE", CDCdEdx_chiE, "Chi value of electrons from CDC dEdx");
325  REGISTER_VARIABLE("CDCdEdx_chiMu", CDCdEdx_chiMu, "Chi value of muons from CDC dEdx");
326  REGISTER_VARIABLE("CDCdEdx_chiPi", CDCdEdx_chiPi, "Chi value of pions from CDC dEdx");
327  REGISTER_VARIABLE("CDCdEdx_chiK", CDCdEdx_chiK, "Chi value of kaons from CDC dEdx");
328  REGISTER_VARIABLE("CDCdEdx_chiP", CDCdEdx_chiP, "Chi value of protons from CDC dEdx");
329  REGISTER_VARIABLE("CDCdEdx_chiD", CDCdEdx_chiD, "Chi value of duetrons from CDC dEdx");
330 
331  REGISTER_VARIABLE("CDCdEdx_llogE", CDCdEdx_llogE, "Log likelihood value of electrons from CDC dEdx");
332  REGISTER_VARIABLE("CDCdEdx_llogMu", CDCdEdx_llogMu, "Log likelihood value of muons from CDC dEdx");
333  REGISTER_VARIABLE("CDCdEdx_llogPi", CDCdEdx_llogPi, "Log likelihood value of pions from CDC dEdx");
334  REGISTER_VARIABLE("CDCdEdx_llogK", CDCdEdx_llogK, "Log likelihood value of kaons from CDC dEdx");
335  REGISTER_VARIABLE("CDCdEdx_llogP", CDCdEdx_llogP, "Log likelihood value of protons from CDC dEdx");
336  REGISTER_VARIABLE("CDCdEdx_llogD", CDCdEdx_llogD, "Log likelihood value of duetrons from CDC dEdx");
337 
338 //SVD variables
339  REGISTER_VARIABLE("SVDdEdx", SVDdedx, "SVD dE/dx truncated mean");
340  REGISTER_VARIABLE("pSVD", SVD_p, "momentum valid in the SVD");
341  REGISTER_VARIABLE("SVD_pTrue", SVD_pTrue, "true MC momentum valid in the SVD");
342  REGISTER_VARIABLE("SVD_CosTheta", SVD_CosTheta, "cos(theta) of the track valid in the SVD");
343  REGISTER_VARIABLE("SVD_nHits", SVD_nHits, "number of hits of the track valid in the SVD");
344 
345  }
347 }
Belle2::Variable::Manager::getVariable
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:33
Belle2::getSVDDedxFromParticle
VXDDedxTrack const * getSVDDedxFromParticle(Particle const *particle)
SVD dEdx value from particle.
Definition: DedxVariables.cc:63
Belle2::Variable::Manager::Var::function
FunctionPtr function
Pointer to function.
Definition: Manager.h:138
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Variable::Manager::FunctionPtr
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:118
Belle2::getDedxFromParticle
CDCDedxTrack const * getDedxFromParticle(Particle const *particle)
CDC dEdx value from particle.
Definition: DedxVariables.cc:45
Belle2::Variable::Manager::Instance
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:27