Belle II Software  release-08-01-10
EKLMAlignmentAlongStripsAlgorithm.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 <klm/eklm/calibration/EKLMAlignmentAlongStripsAlgorithm.h>
11 
12 /* KLM headers. */
13 #include <klm/eklm/geometry/GeometryData.h>
14 
15 /* ROOT headers. */
16 #include <TFile.h>
17 #include <TTree.h>
18 
19 using namespace Belle2;
20 
21 static bool compareSegmentSignificance(
22  const std::pair<int, double>& first, const std::pair<int, double>& second)
23 {
24  return first.second > second.second;
25 }
26 
28  CalibrationAlgorithm("EKLMAlignmentAlongStripsCollector"),
29  m_OutputFile("")
30 {
31 }
32 
34 {
35 }
36 
38 {
39  /* cppcheck-suppress variableScope */
40  int i, n, nSections, nSectors, nLayers, nDetectorLayers, nPlanes, nSegments;
41  /* cppcheck-suppress variableScope */
42  int segment, segmentGlobal;
43  std::vector<std::pair<int, double> > segmentSignificance;
44  std::vector<std::pair<int, double> >::iterator it;
45  int iSection, iLayer, iSector, iPlane, iSegment;
46  int**** *nHits, *** *averageHits;
47  double nHitsSegment, nHitsAverage, nSigma;
48  bool found;
49  const EKLMElementNumbers* elementNumbers = &(EKLMElementNumbers::Instance());
51  struct Event* event = nullptr;
52  std::shared_ptr<TTree> t_in = getObjectPtr<TTree>("calibration_data");
53  t_in->SetBranchAddress("event", &event);
54  nSections = geoDat->getNSections();
55  nSectors = geoDat->getNSectors();
56  nLayers = geoDat->getNLayers();
57  nPlanes = geoDat->getNPlanes();
58  nSegments = geoDat->getNSegments();
59  nHits = new int**** [nSections];
60  averageHits = new int** *[nSections];
61  for (iSection = 0; iSection < nSections; iSection++) {
62  nHits[iSection] = new int** *[nLayers];
63  averageHits[iSection] = new int** [nLayers];
64  for (iLayer = 0; iLayer < nLayers; iLayer++) {
65  nHits[iSection][iLayer] = new int** [nSectors];
66  averageHits[iSection][iLayer] = new int* [nPlanes];
67  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
68  averageHits[iSection][iLayer][iPlane] = new int[nSegments];
69  for (iSegment = 0; iSegment < nSegments; iSegment++) {
70  averageHits[iSection][iLayer][iPlane][iSegment] = 0;
71  }
72  }
73  for (iSector = 0; iSector < nSectors; iSector++) {
74  nHits[iSection][iLayer][iSector] = new int* [nPlanes];
75  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
76  nHits[iSection][iLayer][iSector][iPlane] = new int[nSegments];
77  for (iSegment = 0; iSegment < nSegments; iSegment++) {
78  nHits[iSection][iLayer][iSector][iPlane][iSegment] = 0;
79  }
80  }
81  }
82  }
83  }
84  n = t_in->GetEntries();
85  for (i = 0; i < n; i++) {
86  t_in->GetEntry(i);
87  segment = (event->strip - 1) / elementNumbers->getNStripsSegment();
88  nHits[event->section - 1][event->layer - 1][event->sector - 1]
89  [event->plane - 1][segment]++;
90  averageHits[event->section - 1][event->layer - 1]
91  [getAveragedPlane(event->sector, event->plane)][segment]++;
92  }
93  for (iSection = 0; iSection < nSections; iSection++) {
94  for (iLayer = 0; iLayer < nLayers; iLayer++) {
95  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
96  for (iSegment = 0; iSegment < nSegments; iSegment++) {
97  averageHits[iSection][iLayer][iPlane][iSegment] /= nSectors;
98  }
99  }
100  }
101  }
102  for (iSection = 0; iSection < nSections; iSection++) {
103  nDetectorLayers = geoDat->getNDetectorLayers(iSection + 1);
104  for (iLayer = 0; iLayer < nDetectorLayers; iLayer++) {
105  for (iSector = 0; iSector < nSectors; iSector++) {
106  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
107  for (iSegment = 0; iSegment < nSegments; iSegment++) {
108  segmentGlobal = elementNumbers->segmentNumber(
109  iSection + 1, iLayer + 1, iSector + 1, iPlane + 1,
110  iSegment + 1);
111  nHitsSegment = nHits[iSection][iLayer][iSector][iPlane][iSegment];
112  nHitsAverage = averageHits[iSection][iLayer]
113  [getAveragedPlane(iSector + 1, iPlane + 1)]
114  [iSegment];
115  nSigma = (nHitsSegment - nHitsAverage) /
116  sqrt(nHitsSegment + nHitsAverage -
117  2.0 / nSectors * sqrt(nHitsSegment * nHitsAverage));
118  segmentSignificance.push_back(
119  std::pair<int, double>(segmentGlobal, nSigma));
120  }
121  }
122  }
123  }
124  }
125  sort(segmentSignificance.begin(), segmentSignificance.end(),
126  compareSegmentSignificance);
127  printf("Checking for significantly shifted segments:\n");
128  found = false;
129  for (it = segmentSignificance.begin(); it != segmentSignificance.end();
130  ++it) {
131  if (it->second < 3.0)
132  break;
133  elementNumbers->segmentNumberToElementNumbers(
134  it->first, &iSection, &iLayer, &iSector, &iPlane, &iSegment);
135  printf("Segment %d (section %d, layer %d, sector %d, plane %d, segment %d):"
136  " %.1f sigma\n", it->first, iSection, iLayer, iSector, iPlane,
137  iSegment, it->second);
138  found = true;
139  }
140  if (!found)
141  printf("none found.\n");
142  for (iSection = 0; iSection < nSections; iSection++) {
143  for (iLayer = 0; iLayer < nLayers; iLayer++) {
144  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
145  delete[] averageHits[iSection][iLayer][iPlane];
146  }
147  for (iSector = 0; iSector < nSectors; iSector++) {
148  for (iPlane = 0; iPlane < nPlanes; iPlane++) {
149  delete[] nHits[iSection][iLayer][iSector][iPlane];
150  }
151  delete[] nHits[iSection][iLayer][iSector];
152  }
153  delete[] nHits[iSection][iLayer];
154  delete[] averageHits[iSection][iLayer];
155  }
156  delete[] nHits[iSection];
157  delete[] averageHits[iSection];
158  }
159  delete[] nHits;
160  delete[] averageHits;
161  if (m_OutputFile != "") {
162  TFile* f_out = new TFile(m_OutputFile.c_str(), "recreate");
163  TTree* t_out = new TTree("tree", "");
164  t_out->Branch("event", event);
165  n = t_in->GetEntries();
166  for (i = 0; i < n; i++) {
167  t_in->GetEntry(i);
168  t_out->Fill();
169  }
170  f_out->cd();
171  t_out->Write();
172  delete t_out;
173  delete f_out;
174  }
176 }
177 
179 {
180  m_OutputFile = outputFile;
181 }
182 
184 getAveragedPlane(int sector, int plane) const
185 {
186  if (sector == 1 || sector == 4)
187  return plane - 1;
188  return 2 - plane;
189 }
190 
Base class for calibration algorithms.
EResult
The result of calibration.
@ c_OK
Finished successfuly =0 in Python.
void setOutputFile(const char *outputFile)
Set output file name.
CalibrationAlgorithm::EResult calibrate() override
Calibration.
int getAveragedPlane(int sector, int plane) const
Get plane number for average values (0-based).
EKLM element numbers.
static const EKLMElementNumbers & Instance()
Instantiation.
static constexpr int getNStripsSegment()
Get number of strips in a segment.
int segmentNumber(int section, int layer, int sector, int plane, int segment) const
Get segment number.
void segmentNumberToElementNumbers(int segmentGlobal, int *section, int *layer, int *sector, int *plane, int *segment) const
Get element numbers by segment global number.
int getNPlanes() const
Get number of planes.
int getNSections() const
Get number of sections.
int getNLayers() const
Get number of layers.
int getNDetectorLayers(int section) const
Get number of detector layers.
int getNSegments() const
Get number of segments.
int getNSectors() const
Get number of sectors.
EKLM geometry data.
Definition: GeometryData.h:38
static const GeometryData & Instance(enum DataSource dataSource=c_Database, const GearDir *gearDir=nullptr)
Instantiation.
Definition: GeometryData.cc:33
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.