Belle II Software  release-05-01-25
TOPalign.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <top/reconstruction/TOPalign.h>
12 #include <framework/logging/Logger.h>
13 #include <iostream>
14 
15 extern "C" {
16  void data_clear_();
17  void data_put_(int*, int*, float*, float*, int*);
18  void set_top_par_(float*, float*);
19  void rtra_clear_();
20  void rtra_set_hypo_(int*, float*);
21  void rtra_set_hypid_(int*, int*);
22  void rtra_put_(float*, float*, float*, float*, float*, float*, float*,
23  int*, int*, int*, int*);
24  void set_pdf_opt_(int*, int*, int*);
25  void top_alignment_(int*, float*, float*, double*, float*, float*, int*);
26  int rtra_get_nfot_(int*);
27 }
28 
29 using namespace std;
30 
31 namespace Belle2 {
36  namespace TOP {
37 
38  TOPalign::TOPalign()
39  {
40  m_parNames.push_back("x");
41  m_parNames.push_back("y");
42  m_parNames.push_back("z");
43  m_parNames.push_back("alpha");
44  m_parNames.push_back("beta");
45  m_parNames.push_back("gamma");
46  m_parNames.push_back("t0");
47  m_parNames.push_back("dn/n");
48  unsigned numPar = m_parNames.size();
49  m_parInit.resize(numPar, 0);
50  m_par = m_parInit;
51  m_steps.resize(numPar, 0);
52  m_fixed.resize(numPar, false);
53  m_COV.resize(numPar * numPar, 0);
54  m_U.resize(numPar * numPar, 0);
55  m_maxDpar.resize(numPar, 0);
56  }
57 
58 
59  void TOPalign::setSteps(double position, double angle, double time, double refind)
60  {
61  m_steps[0] = position;
62  m_steps[1] = position;
63  m_steps[2] = position;
64  m_steps[3] = angle;
65  m_steps[4] = angle;
66  m_steps[5] = angle;
67  m_steps[6] = time;
68  m_steps[7] = refind;
69  m_maxDpar = m_steps;
70  }
71 
72 
73  void TOPalign::clearData()
74  {
75  data_clear_();
76  }
77 
78  int TOPalign::addData(int moduleID, int pixelID, double time, double timeError)
79  {
80  int status = 0;
81  moduleID--; // 0-based ID used in fortran
82  pixelID--; // 0-based ID used in fortran
83  float t = (float) time;
84  float terr = (float) timeError;
85  data_put_(&moduleID, &pixelID, &t, &terr, &status);
86  switch (status) {
87  case 0:
88  B2WARNING("addData: no space available in /TOP_DATA/");
89  return status;
90  case -1:
91  B2ERROR("addData: invalid module ID."
92  << LogVar("moduleID", moduleID + 1));
93  return status;
94  case -2:
95  B2ERROR("addData: invalid pixel ID."
96  << LogVar("pixelID", pixelID + 1));
97  return status;
98  case -3:
99  B2DEBUG(100, "addData: digit should already be masked-out (different masks used?)");
100  return status;
101  default:
102  return status;
103  }
104  }
105 
106  void TOPalign::setPhotonYields(double bkgPerModule, double scaleN0)
107  {
108  float bkg = bkgPerModule;
109  float sf = scaleN0;
110  set_top_par_(&bkg, &sf);
111  }
112 
113 
114  int TOPalign::iterate(const TOPtrack& track, const Const::ChargedStable& hypothesis)
115  {
116  m_numPhotons = -1;
117  if (track.getModuleID() != m_moduleID) return -3;
118 
119  // pass track parameters to fortran code
120  float x = track.getX();
121  float y = track.getY();
122  float z = track.getZ();
123  float t = track.getTrackLength() / Const::speedOfLight;
124  float px = track.getPx();
125  float py = track.getPy();
126  float pz = track.getPz();
127  int Q = track.getCharge();
128  int HYP = 0;
129  int REF = 0;
130  int moduleID = m_moduleID - 1;
131  rtra_clear_();
132  rtra_put_(&x, &y, &z, &t, &px, &py, &pz, &Q, &HYP, &REF, &moduleID);
133 
134  // set single mass hypothesis
135  int Num = 1;
136  float mass = hypothesis.getMass();
137  int pdg = hypothesis.getPDGCode();
138  rtra_set_hypo_(&Num, &mass);
139  rtra_set_hypid_(&Num, &pdg);
140 
141  // set PDF option
142  set_pdf_opt_(&m_opt, &m_NP, &m_NC);
143 
144  // run single iteration
145  int ier = 0;
146  int np = m_par.size();
147  std::vector<float> dpar(np, 0);
148  auto steps = m_steps;
149  for (size_t i = 0; i < steps.size(); i++) {
150  if (m_fixed[i]) steps[i] = 0;
151  }
152  top_alignment_(&np, m_par.data(), steps.data(), m_U.data(),
153  dpar.data(), m_COV.data(), &ier);
154  if (ier < 0) return ier;
155 
156  m_numTracks++;
157  int K = 1;
158  m_numPhotons = rtra_get_nfot_(&K);
159  if (ier != 0) return ier;
160 
161  for (size_t i = 0; i < dpar.size(); i++) {
162  if (fabs(dpar[i]) > m_maxDpar[i]) return ier;
163  }
164  for (size_t i = 0; i < dpar.size(); i++) {
165  m_par[i] += dpar[i];
166  }
167  m_numUsedTracks = m_numTracks;
168  m_valid = true;
169 
170  return ier;
171  }
172 
173  void TOPalign::reset()
174  {
175  m_par = m_parInit;
176  for (auto& x : m_COV) x = 0;
177  for (auto& x : m_U) x = 0;
178  m_numTracks = 0;
179  m_numUsedTracks = 0;
180  m_valid = false;
181  }
182 
183  std::vector<float> TOPalign::getErrors() const
184  {
185  std::vector<float> errors;
186  int numPar = m_par.size();
187  for (int i = 0; i < numPar; i++) {
188  errors.push_back(sqrt(m_COV[i * (numPar + 1)]));
189  }
190  return errors;
191  }
192 
193 
194  } // namespace TOP
196 } // namespace Belle2
197 
Belle2::TOP::TOPtrack
Class to hold reconstructed track, interface to fortran.
Definition: TOPtrack.h:42
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::Const::ParticleType::getMass
double getMass() const
Particle mass.
Definition: UnitConst.cc:310