Belle II Software  release-05-02-19
TOPconfigure.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - 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/TOPconfigure.h>
12 #include <top/geometry/TOPGeometryPar.h>
13 #include <top/reconstruction/TOPf77fun.h>
14 
15 // framework aux
16 #include <framework/gearbox/Unit.h>
17 #include <framework/logging/Logger.h>
18 #include <framework/geometry/BFieldManager.h>
19 
20 extern "C" {
21  void top_geo_dump_();
22  void set_mirror_refl_(int*, float*);
23  void set_surface_refl_(int*, float*);
24  void set_alignment_(int*, float*, float*, float*, float*, float*, float*);
25  int set_tts_type_(int*, int*, float*, float*, float*);
26  int set_pmt_type_(int*, int*, int*);
27 }
28 
29 using namespace std;
30 
31 namespace Belle2 {
36  namespace TOP {
37 
38  bool TOPconfigure::m_configured(false);
39  DBObjPtr<TOPCalModuleAlignment> TOPconfigure::m_alignment;
40  OptionalDBArray<TOPPmtInstallation> TOPconfigure::m_pmtInstalled;
41 
42  TOPconfigure::TOPconfigure()
43  {
44  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
45 
46  // space for TOP modules
47  m_R1 = geo->getInnerRadius();
48  m_R2 = geo->getOuterRadius();
49  m_Z1 = geo->getBackwardZ();
50  m_Z2 = geo->getForwardZ();
51 
52  // TDC time range
53  const auto& tdc = geo->getNominalTDC();
54  m_timeRange = tdc.getOverflowValue() * tdc.getBinWidth();
55 
56  if (m_configured) return;
57 
58  // space for the modules
59  setTOPvolume(m_R1, m_R2, m_Z1, m_Z2);
60 
61  // magnetic field at TOP
62  TVector3 Bfield = BFieldManager::getField(0, geo->getRadius(), 0) / Unit::T;
63  setBfield(-Bfield.Z());
64 
65  // PMT dimensions: NOTE reconstruction assumes all modules have the same PMT's
66  const auto& pmt = geo->getModule(1).getPMTArray().getPMT();
67  setPMT(pmt.getSizeX(), pmt.getSizeY(),
68  pmt.getSensSizeX(), pmt.getSensSizeY(),
69  pmt.getNumColumns(), pmt.getNumRows());
70 
71  // Nominal TTS parameterization
72  const auto& tts = geo->getNominalTTS().getTTS();
73  std::vector<float> frac, mean, sigma;
74  for (const auto& gauss : tts) {
75  frac.push_back(gauss.fraction);
76  mean.push_back(gauss.position);
77  sigma.push_back(gauss.sigma);
78  }
79  setTTS(tts.size(), frac.data(), mean.data(), sigma.data());
80 
81  // PMT dependent TTS-es
82  setPMTDependentTTSes();
83 
84  // pixel table of PMT types
85  setPMTTypes();
86  m_pmtInstalled.addCallback(&setPMTTypes);
87 
88  // quantum efficiency
89  const auto& nominalQE = geo->getNominalQE();
90  std::vector<float> wavelength;
91  for (unsigned i = 0; i < nominalQE.getQE().size(); i++) {
92  float wl = nominalQE.getLambdaFirst() + nominalQE.getLambdaStep() * i;
93  wavelength.push_back(wl);
94  }
95  auto QE = nominalQE.getQE();
96  // multiply QE with filter transmittance
97  const auto& wavelengthFilter = geo->getWavelengthFilter();
98  for (unsigned i = 0; i < QE.size(); i++) {
99  QE[i] *= wavelengthFilter.getBulkTransmittance(wavelength[i]);
100  }
101  setQE(wavelength.data(), QE.data(), QE.size(),
102  nominalQE.getCE() * tdc.getEfficiency());
103 
104  // time to digit conversion
105  setTDC(tdc.getNumBits(), tdc.getBinWidth(), tdc.getOffset());
106  setCFD(tdc.getPileupTime(), tdc.getDoubleHitResolution());
107 
108  setEdgeRoughness(0); // No edge roughness
109 
110  // geometry of modules
111  for (unsigned i = 0; i < geo->getNumModules(); i++) {
112  const auto& module = geo->getModule(i + 1);
113  double A = module.getBarWidth();
114  double B = module.getBarThickness();
115  double z1 = module.getBackwardZ();
116  double z2 = module.getForwardZ();
117  double R = module.getInnerRadius();
118  double phi = module.getPhi() - M_PI / 2;
119  int id = setQbar(A, B, z1, z2, R, 0, phi, c_PMT, c_SphericM);
120  setSurfaceReflectivity(id, module.getSurfaceReflectivity(3.0));
121 
122  const auto& mirror = module.getMirrorSegment();
123  setMirrorRadius(id, mirror.getRadius());
124  setMirrorCenter(id, mirror.getXc(), mirror.getYc());
125  setMirrorReflectivity(id, mirror.getMirrorReflectivity(3.0));
126 
127  const auto& prism = module.getPrism();
128  double prismLength = prism.getLength();
129  double prismFlat = prism.getFlatLength();
130  double prismExit = prism.getExitThickness();
131  double prismWidth = prism.getWidth();
132  addExpansionVolume(id, c_Left, c_Prism, prismLength - prismFlat,
133  B / 2, B / 2 - prismExit, 0, 0, prismWidth);
134 
135  const auto& pmtArray = module.getPMTArray();
136  double pmtWindow = pmtArray.getPMT().getWinThickness();
137  // a call to prism.getFilterThickness is added for backward compatibility
138  double filterThickness = pmtArray.getFilterThickness() + prism.getFilterThickness();
139  double cookieThickness = pmtArray.getCookieThickness();
140  setBBoxWindow(id, prismFlat + cookieThickness + filterThickness + pmtWindow);
141 
142  double x0 = module.getPMTArrayDisplacement().getX();
143  double y0 = module.getPMTArrayDisplacement().getY();
144  arrangePMT(id, c_Left, pmtArray.getSizeX(), pmtArray.getSizeY(), x0, y0);
145  }
146 
147  // alignment
148  setAlignmentConstants();
149  m_alignment.addCallback(&setAlignmentConstants);
150 
151  m_configured = TOPfinalize(0);
152  if (!m_configured) B2ERROR("TOPconfigure: configuration failed");
153 
154  }
155 
156  void TOPconfigure::setAlignmentConstants()
157  {
158  if (!m_alignment.isValid()) {
159  B2ERROR("TOPconfigure: No valid alignment for TOP found in database");
160  return;
161  }
162 
163  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
164  for (unsigned i = 0; i < geo->getNumModules(); i++) {
165  int moduleID = i + 1;
166  if (m_alignment->isCalibrated(moduleID)) {
167  int id = i;
168  float dx = m_alignment->getX(moduleID);
169  float dy = m_alignment->getY(moduleID);
170  float dz = m_alignment->getZ(moduleID);
171  float alfa = m_alignment->getAlpha(moduleID);
172  float beta = m_alignment->getBeta(moduleID);
173  float gama = m_alignment->getGamma(moduleID);
174  set_alignment_(&id, &dx, &dy, &dz, &alfa, &beta, &gama);
175  }
176  }
177 
178  B2INFO("TOPconfigure: Alignment constants are set in reconstruction");
179 
180  }
181 
182  void TOPconfigure::setPMTDependentTTSes()
183  {
184  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
185  const auto& ttses = geo->getTTSes();
186  for (const auto& tts : ttses) {
187  int type = tts.second.getPMTType();
188  std::vector<float> frac, mean, sigma;
189  for (const auto& gauss : tts.second.getTTS()) {
190  frac.push_back(gauss.fraction);
191  mean.push_back(gauss.position);
192  sigma.push_back(gauss.sigma);
193  }
194  int size = frac.size();
195  int err = set_tts_type_(&type, &size, frac.data(), mean.data(), sigma.data());
196  if (err == 1) {
197  B2ERROR("TOPconfigure: sum of TTS fractions is zero."
198  << LogVar("TTS type", type));
199  } else if (err == 2) {
200  B2ERROR("TOPconfigure: TTS type is out of range."
201  << LogVar("TTS type", type));
202  }
203  }
204  }
205 
206 
207  void TOPconfigure::setPMTTypes()
208  {
209  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
210 
211  for (const auto& module : geo->getModules()) {
212  auto moduleID = module.getModuleID();
213  const auto& pmtArray = module.getPMTArray();
214  const auto& pmt = pmtArray.getPMT();
215  for (unsigned pmtID = 1; pmtID <= pmtArray.getSize(); pmtID++) {
216  int pmtType = TOPGeometryPar::Instance()->getPMTType(moduleID, pmtID);
217  for (unsigned pmtPixel = 1; pmtPixel <= pmt.getNumPixels(); pmtPixel++) {
218  auto pixelID = pmtArray.getPixelID(pmtID, pmtPixel);
219  int ich = pixelID - 1;
220  int mdn = moduleID - 1;
221  int err = set_pmt_type_(&mdn, &ich, &pmtType);
222  if (err == 1) {
223  B2ERROR("TOPconfigure: PMT type is out of range."
224  << LogVar("PMT type", pmtType));
225  } else if (err == 2) {
226  B2ERROR("TOPconfigure: invalid slot ID or pixel ID."
227  << LogVar("slot ID", moduleID)
228  << LogVar("pixel ID", pixelID));
229  }
230  }
231  }
232  }
233 
234  B2INFO("TOPconfigure: new map of PMT types has been passed to reconstruction");
235 
236  }
237 
238  void TOPconfigure::setTOPvolume(double R1, double R2, double Z1, double Z2)
239  {
240  float r1 = (float) R1; float r2 = (float) R2;
241  float z1 = (float) Z1; float z2 = (float) Z2;
242  set_topvol_(&r1, &r2, &z1, &z2);
243  }
244 
245  void TOPconfigure::setBfield(double B)
246  {
247  float b = (float) B;
248  set_bfield_(&b);
249  }
250 
251  void TOPconfigure::setEdgeRoughness(double R)
252  {
253  float r = (float) R;
254  set_qbar_redg_(&r);
255  }
256 
257  void TOPconfigure::setPMT(double A, double B, double Asens, double Bsens,
258  int Nx, int Ny, double TTS)
259  {
260  float a = (float) A; float b = (float) B;
261  float aa = (float) Asens; float bb = (float) Bsens;
262  float tts = (float) TTS;
263  set_pmt_(&a, &b, &aa, &bb, &Nx, &Ny, &tts);
264  }
265 
266  void TOPconfigure::setTTS(int ng, float Frac[], float Mean[], float Sigma[])
267  {
268  set_tts_(&ng, Frac, Mean, Sigma);
269  }
270 
271  void TOPconfigure::setQE(float Wavelength[], float QE[], int Size, double CE)
272  {
273  float ce = (float) CE;
274  set_qeffi_(Wavelength, QE, &Size, &ce);
275  }
276 
277  int TOPconfigure::setQbar(double A, double B, double Z1, double Z2, double R,
278  double Dx, double Phi, int Lside, int Rside)
279  {
280  float a = (float) A; float b = (float) B;
281  float z1 = (float) Z1; float z2 = (float) Z2; float r = (float) R;
282  float dx = (float) Dx; float phi = (float) Phi;
283  int id = set_qbar_(&a, &b, &z1, &z2, &r, &dx, &phi, &Lside, &Rside);
284  return id;
285  }
286 
287  void TOPconfigure::setBBoxWindow(int moduleID, double thickness)
288  {
289  float d = thickness;
290  set_bbwin_(&moduleID, &d);
291  }
292 
293  void TOPconfigure::addExpansionVolume(int moduleID, int LR, int Shape, double Dz,
294  double Yup, double Ydown,
295  double YupIn, double YdownIn,
296  double Width, double DzRefl, double Refl)
297  {
298  float dz = Dz; float yup = Yup; float ydn = Ydown; float a = Width;
299  float yupi = YupIn; float ydni = YdownIn;
300  float dzrefl = DzRefl; float refl = Refl;
301  set_extvol_(&moduleID, &LR, &Shape, &dz, &yup, &ydn, &a, &yupi, &ydni,
302  &dzrefl, &refl);
303  }
304 
305  void TOPconfigure::arrangePMT(int moduleID, double sizX, double sizY,
306  double Dx, double Dy)
307  {
308  float sizx = (float) sizX; float sizy = (float) sizY;
309  float dx = (float) Dx; float dy = (float) Dy;
310  int LR = 0;
311  arrange_pmt_(&moduleID, &LR, &sizx, &sizy, &dx, &dy);
312  LR = 1;
313  arrange_pmt_(&moduleID, &LR, &sizx, &sizy, &dx, &dy);
314  }
315 
316  void TOPconfigure::arrangePMT(int moduleID, int LR, double sizX, double sizY,
317  double Dx, double Dy)
318  {
319  float sizx = (float) sizX; float sizy = (float) sizY;
320  float dx = (float) Dx; float dy = (float) Dy;
321  arrange_pmt_(&moduleID, &LR, &sizx, &sizy, &dx, &dy);
322  }
323 
324  void TOPconfigure::setMirrorRadius(int moduleID, double R)
325  {
326  float r = (float) R;
327  set_rmi_(&moduleID, &r);
328  }
329 
330  void TOPconfigure::setMirrorCenter(int moduleID, double Xc, double Yc)
331  {
332  float xc = (float) Xc; float yc = (float) Yc;
333  set_xyc_(&moduleID, &xc, &yc);
334  }
335 
336  void TOPconfigure::setMirrorReflectivity(int moduleID, double reflectivity)
337  {
338  float refl = (float) reflectivity;
339  if (refl > 1) {
340  refl = 1;
341  B2WARNING("TOPconfigure: mirror reflectivity greater than 1 -> set to 1 in reconstruction");
342  }
343  set_mirror_refl_(&moduleID, &refl);
344  }
345 
346  void TOPconfigure::setSurfaceReflectivity(int moduleID, double reflectivity)
347  {
348  float refl = (float) reflectivity;
349  if (refl > 1) {
350  refl = 1;
351  B2WARNING("TOPconfigure: surface reflectivity greater than 1 -> set to 1 in reconstruction");
352  }
353  set_surface_refl_(&moduleID, &refl);
354  }
355 
356  void TOPconfigure::setTDC(int NBIT, double ChWid, double Offset)
357  {
358  float chwid = (float) ChWid; float offset = (float) Offset;
359  set_tdc_(&NBIT, &chwid, &offset);
360  }
361 
362  void TOPconfigure::setCFD(double delTpileup, double delTdoubleHit)
363  {
364  float dt = delTpileup; float delt = delTdoubleHit;
365  set_cfd_(&dt, &delt);
366  }
367 
368  bool TOPconfigure::TOPfinalize(int Dump)
369  {
370  return top_geo_finalize_(&Dump);
371  }
372 
373  void TOPconfigure::print() const
374  {
375  top_geo_dump_();
376  }
377 
378  void TOPconfigure::getQbar(int moduleID, double& A, double& B, double& C,
379  int& Lside, int& Rside, int& LexpvID, int& RexpvID)
380  {
381  moduleID--;
382  float a, b, c;
383  get_qbar_(&moduleID, &a, &b, &c, &Lside, &Rside, &LexpvID, &RexpvID);
384  A = a; B = b; C = c;
385  }
386 
387  void TOPconfigure::getExpansionVolume(int expvID, double& Dz, double& Yup, double& Ydown,
388  double& YupIn, double& YdownIn,
389  double& Width, int& Shape, int& LR)
390  {
391  float dz, yup, ydn, a, yupi, ydni;
392  get_expvol_(&expvID, &dz, &yup, &ydn, &yupi, &ydni, &a, &Shape, &LR);
393  Dz = dz; Yup = yup; Ydown = ydn; YupIn = yupi; YdownIn = ydni; Width = a;
394  }
395 
396  void TOPconfigure::getMirror(int moduleID, double& R, double& Xc, double& Yc, double& Zc,
397  double& F)
398  {
399  moduleID--;
400  float r, xc, yc, zc, f;
401  get_mirror_(&moduleID, &r, &xc, &yc, &zc, &f);
402  R = r; Xc = xc; Yc = yc; Zc = zc; F = f;
403  }
404 
405  double TOPconfigure::getBBoxWindow(int moduleID)
406  {
407  moduleID--;
408  return get_bbwin_(&moduleID);
409  }
410 
411  } // top namespace
413 } // Belle2 namespace
414 
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