Belle II Software  release-08-00-05
eclAutocovarianceCalibrationC3Algorithm.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 <ecl/calibration/eclAutocovarianceCalibrationC3Algorithm.h>
11 
12 /* ECL headers. */
13 #include <ecl/dataobjects/ECLElementNumbers.h>
14 #include <ecl/dbobjects/ECLCrystalCalib.h>
15 
16 #include <framework/database/DBImportObjPtr.h>
17 #include <ecl/dbobjects/ECLAutoCovariance.h>
18 
19 /* ROOT headers. */
20 #include <TFile.h>
21 #include <TGraph.h>
22 #include <TH2I.h>
23 #include"TMatrixDSym.h"
24 #include"TDecompChol.h"
25 
26 using namespace Belle2;
27 using namespace ECL;
28 
31  CalibrationAlgorithm("eclAutocovarianceCalibrationC3Collector")
32 {
34  "Computes the covariance matrix for each crystal"
35  );
36 }
37 
39 {
40 
41 
43  gROOT->SetBatch();
44 
46  std::vector<double> cryIDs;
47  std::vector<double> invertStatusVector;
48  std::vector<double> noiseMatrix00Vector;
49  std::vector<double> totalCountsVector;
50 
53  auto CovarianceMatrixInfoVsCrysID = getObjectPtr<TH2F>("CovarianceMatrixInfoVsCrysID");
54 
55  ECLAutoCovariance* Autocovariances = new ECLAutoCovariance();
56 
57  for (int ID = 0; ID < ECLElementNumbers::c_NCrystals; ID++) {
58 
59  float totalCounts = CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 1,
60  m_numberofADCPoints + 1));
61 
62  if (totalCounts < m_TotalCountsThreshold) {
63  B2INFO("eclAutocovarianceCalibrationC3Algorithm: warning total entries for cell ID " << ID + 1 << " is only: " << totalCounts <<
64  " Requirement is m_TotalCountsThreshold: " << m_TotalCountsThreshold);
66  return c_NotEnoughData;
67  }
68 
69  TMatrixDSym NoiseMatrix;
70  NoiseMatrix.ResizeTo(m_numberofADCPoints, m_numberofADCPoints);
71  for (int i = 0; i < m_numberofADCPoints; i++) {
72  for (int j = 0; j < m_numberofADCPoints; j++) {
73  int index = abs(i - j);
74  NoiseMatrix(i, j) = float(CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 1,
75  index + 1))) / (totalCounts - 1) / (float(m_numberofADCPoints - index));
76  }
77  }
78 
79  double tempAutoCov[m_numberofADCPoints];
80 
81  for (int i = 0; i < m_numberofADCPoints; i++) tempAutoCov[i] = NoiseMatrix(0, i);
82  Autocovariances->setAutoCovariance(ID + 1, tempAutoCov);
83 
84  TDecompChol dc(NoiseMatrix);
85  bool InvertStatus = dc.Invert(NoiseMatrix);
86  if (InvertStatus == false) B2INFO("eclAutocovarianceCalibrationC3Algorithm ID InvertStatus [0][0] totalCounts: " << ID <<
87  " " << InvertStatus << " " << NoiseMatrix(0, 0) << " " << totalCounts);
88  cryIDs.push_back(ID + 1);
89  invertStatusVector.push_back(InvertStatus);
90  noiseMatrix00Vector.push_back(NoiseMatrix(0, 0));
91  totalCountsVector.push_back(totalCounts);
92 
93  }
94 
96  saveCalibration(Autocovariances, "ECLAutoCovariance");
97 
99  auto ginvertStatusVector = new TGraph(cryIDs.size(), cryIDs.data(), invertStatusVector.data());
100  ginvertStatusVector->SetName("ginvertStatusVector");
101  ginvertStatusVector->SetMarkerStyle(20);
102  auto gnoiseMatrix00Vector = new TGraph(cryIDs.size(), cryIDs.data(), noiseMatrix00Vector.data());
103  gnoiseMatrix00Vector->SetName("gnoiseMatrix00Vector");
104  gnoiseMatrix00Vector->SetMarkerStyle(20);
105  auto gtotalCountsVector = new TGraph(cryIDs.size(), cryIDs.data(), totalCountsVector.data());
106  gtotalCountsVector->SetName("gtotalCountsVector");
107  gtotalCountsVector->SetMarkerStyle(20);
108 
110  TString fName = m_outputName;
111  TDirectory::TContext context;
112  TFile* histfile = new TFile(fName, "recreate");
113  histfile->cd();
114  ginvertStatusVector->Write();
115  gnoiseMatrix00Vector->Write();
116  gtotalCountsVector->Write();
117  histfile->Close();
118  delete histfile;
119 
120  return c_OK;
121 }
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_NotEnoughData
Needs more data =2 in Python.
Covariance matrices for offline ECL waveform fit.
void setAutoCovariance(const int cellID, const double acov[31])
Set auto covariance for a channel.
int m_TotalCountsThreshold
min number of counts needed to compute calibration
virtual EResult calibrate() override
..Run algorithm on events
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.