Belle II Software  release-08-01-10
TOPPhotonYieldsAlgorithm.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 #include <top/calibration/TOPPhotonYieldsAlgorithm.h>
10 #include <top/dbobjects/TOPCalPhotonYields.h>
11 #include <TROOT.h>
12 #include <TFile.h>
13 #include <TProfile.h>
14 #include <string>
15 
16 using namespace std;
17 
18 namespace Belle2 {
23  namespace TOP {
24 
25  TOPPhotonYieldsAlgorithm::TOPPhotonYieldsAlgorithm():
26  CalibrationAlgorithm("TOPPhotonYieldsCollector")
27  {
28  setDescription("Calibration algorithm for photon pixel yields aimed for PMT ageing studies and for finding optically decoupled PMT's");
29  }
30 
32  {
33  gROOT->SetBatch();
34 
35  // construct file name and open output root file for storing merged histograms
36 
37  const auto& expRun = getRunList();
38  string expNo = to_string(expRun[0].first);
39  while (expNo.length() < 4) expNo.insert(0, "0");
40  string runNo = to_string(expRun[0].second);
41  while (runNo.length() < 5) runNo.insert(0, "0");
42  string outputFileName = "photonYields-e" + expNo + "-r" + runNo + ".root";
43  auto* file = TFile::Open(outputFileName.c_str(), "recreate");
44 
45  // get basic histograms
46 
47  auto numTracks = getObjectPtr<TH1F>("numTracks");
48  if (not numTracks) {
49  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'numTracks' not found");
50  file->Close();
51  return c_Failure;
52  }
53  numTracks->Write();
54 
55  auto timeStamp = getObjectPtr<TProfile>("timeStamp");
56  if (not timeStamp) {
57  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'timeStamp' not found");
58  file->Close();
59  return c_Failure;
60  }
61  timeStamp->Write();
62 
63  // construct DB object for storing the results and set the time stamp
64 
65  auto* dbobject = new TOPCalPhotonYields();
66  dbobject->setTimeStamp(timeStamp->GetBinContent(1), timeStamp->GetBinError(1));
67 
68  // for each slot determine photon pixel yields and equalized alpha ratio and store them into DB object
69 
70  int numModules = numTracks->GetNbinsX();
71  for (int slot = 1; slot <= numModules; slot++) {
72  string slotName = (slot < 10) ? "_0" + to_string(slot) : "_" + to_string(slot);
73 
74  auto signalHits = getObjectPtr<TH1F>("signalHits" + slotName);
75  if (not signalHits) {
76  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'signalHits' for slot " << slot << " not found");
77  file->Close();
78  delete dbobject;
79  return c_Failure;
80  }
81  signalHits->Write();
82 
83  auto bkgHits = getObjectPtr<TH1F>("bkgHits" + slotName);
84  if (not bkgHits) {
85  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'bkgHits' for slot " << slot << " not found");
86  file->Close();
87  delete dbobject;
88  return c_Failure;
89  }
90  bkgHits->Write();
91 
92  auto activePixels = getObjectPtr<TH1F>("activePixels" + slotName);
93  if (not activePixels) {
94  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'activePixels' for slot " << slot << " not found");
95  file->Close();
96  delete dbobject;
97  return c_Failure;
98  }
99  activePixels->Write();
100 
101  auto alphaLow = getObjectPtr<TH1F>("alphaLow" + slotName);
102  if (not alphaLow) {
103  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'alphaLow' for slot " << slot << " not found");
104  file->Close();
105  delete dbobject;
106  return c_Failure;
107  }
108  alphaLow->Sumw2();
109  alphaLow->Write();
110 
111  auto alphaHigh = getObjectPtr<TH1F>("alphaHigh" + slotName);
112  if (not alphaHigh) {
113  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'alphaHigh' for slot " << slot << " not found");
114  file->Close();
115  delete dbobject;
116  return c_Failure;
117  }
118  alphaHigh->Sumw2();
119  alphaHigh->Write();
120 
121  auto pulseHeights = getObjectPtr<TH2F>("pulseHeights" + slotName);
122  if (not pulseHeights) {
123  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'pulseHeights' for slot " << slot << " not found");
124  file->Close();
125  delete dbobject;
126  return c_Failure;
127  }
128  pulseHeights->Write();
129 
130  auto muonZ = getObjectPtr<TH1F>("muonZ" + slotName);
131  if (not muonZ) {
132  B2ERROR("TOPPhotonYieldsAlgorithm: histogram 'muonZ' for slot " << slot << " not found");
133  file->Close();
134  delete dbobject;
135  return c_Failure;
136  }
137  muonZ->Write();
138 
139  auto* photonYields = (TH1F*) signalHits->Clone("tmp1");
140  photonYields->Add(signalHits.get(), bkgHits.get(), 1, -1); // subtract background
141  for (int bin = 1; bin <= activePixels->GetNbinsX(); bin++) activePixels->SetBinError(bin, 0);
142  photonYields->Divide(activePixels.get()); // normalize
143 
144  auto* bkgYields = (TH1F*) bkgHits->Clone("tmp2");
145  bkgYields->Divide(activePixels.get()); // normalize
146 
147  auto* alphaRatio = (TH1F*) alphaHigh->Clone("tmp2");
148  alphaRatio->Divide(alphaHigh.get(), alphaLow.get());
149  equalize(alphaRatio);
150 
151  dbobject->set(slot, photonYields, bkgYields, alphaRatio, activePixels.get(), pulseHeights.get(), muonZ.get());
152 
153  delete photonYields;
154  delete bkgYields;
155  delete alphaRatio;
156  }
157 
158  file->Close();
159 
160  saveCalibration(dbobject);
161 
162  return c_OK;
163  }
164 
165 
167  {
168  // these constants are determined from fits to MC
169  const double a[] = {1.50301, 1.16907, 1.00912, 0.943571, 0.889658, 0.93418, 1.01846, 0.953715};
170  const double b[] = {-1.02531, -0.111269, 0.0731595, -0.121119, -0.79083, -1.00261, -1.41012, -1.23368};
171  const double c[] = {8.47995, 3.18639, 1.9513, 2.58279, 4.79672, 5.5659, 7.558, 6.71336};
172  const double d[] = {1.49743, 1.17224, 1.02211, 0.928502, 0.829334, 0.849355, 0.921871, 0.850427};
173 
174  int row = (bin - 1) / 64;
175  if (row < 0 or row > 7) return 1;
176 
177  int col = (bin - 1) % 64 + 1;
178  if (col == 1 or col == 64) return d[row];
179 
180  double x = (col - 32.5) / 64.;
181  return a[row] + b[row] * pow(x, 2) + c[row] * pow(x, 4);
182  }
183 
184 
186  {
187  for (int bin = 1; bin <= h->GetNbinsX(); bin++) {
188  double s = getEqualizingValue(bin);
189  h->SetBinContent(bin, h->GetBinContent(bin) / s);
190  h->SetBinError(bin, h->GetBinError(bin) / s);
191  }
192  }
193 
194  } // end namespace TOP
196 } // end namespace Belle2
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
EResult
The result of calibration.
@ c_OK
Finished successfuly =0 in Python.
@ c_Failure
Failed =3 in Python.
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
Class to store photon pixel yields for PMT ageing studies, and equalized alpha ratios for finding opt...
void equalize(TH1F *h)
Equalize alpha ratio histogram.
virtual EResult calibrate() final
algorithm implementation
double getEqualizingValue(int bin)
Returns equalizing value for alpha ratio.
Abstract base class for different kinds of events.