Belle II Software  release-08-01-10
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/ECLAutoCovariance.h>
15 
16 /* ROOT headers. */
17 #include <TFile.h>
18 #include <TGraph.h>
19 #include <TH2I.h>
20 #include"TMatrixDSym.h"
21 #include"TDecompChol.h"
22 
23 using namespace Belle2;
24 using namespace ECL;
25 
28  CalibrationAlgorithm("eclAutocovarianceCalibrationC3Collector")
29 {
31  "Computes the covariance matrix for each crystal"
32  );
33 }
34 
36 {
37 
38 
40  gROOT->SetBatch();
41 
43  std::vector<double> cryIDs;
44  std::vector<double> invertStatusVector;
45  std::vector<double> noiseMatrix00Vector;
46  std::vector<double> totalCountsVector;
47 
50  auto CovarianceMatrixInfoVsCrysID = getObjectPtr<TH2F>("CovarianceMatrixInfoVsCrysID");
51 
52  ECLAutoCovariance* Autocovariances = new ECLAutoCovariance();
53 
54  for (int ID = 0; ID < ECLElementNumbers::c_NCrystals; ID++) {
55 
56  float totalCounts = CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 1,
57  m_numberofADCPoints + 1));
58 
59  if (totalCounts < m_TotalCountsThreshold) {
60  B2INFO("eclAutocovarianceCalibrationC3Algorithm: warning total entries for cell ID " << ID + 1 << " is only: " << totalCounts <<
61  " Requirement is m_TotalCountsThreshold: " << m_TotalCountsThreshold);
63  return c_NotEnoughData;
64  }
65 
66  TMatrixDSym NoiseMatrix;
67  NoiseMatrix.ResizeTo(m_numberofADCPoints, m_numberofADCPoints);
68  for (int i = 0; i < m_numberofADCPoints; i++) {
69  for (int j = 0; j < m_numberofADCPoints; j++) {
70  int index = abs(i - j);
71  NoiseMatrix(i, j) = float(CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 1,
72  index + 1))) / (totalCounts - 1) / (float(m_numberofADCPoints - index));
73  }
74  }
75 
76  double tempAutoCov[m_numberofADCPoints];
77  for (int i = 0; i < m_numberofADCPoints; i++) tempAutoCov[i] = NoiseMatrix(0, i);
78 
79  TDecompChol dc(NoiseMatrix);
80  bool InvertStatus = dc.Invert(NoiseMatrix);
81  if (InvertStatus == false && ID > 0) {
82  B2INFO("eclAutocovarianceCalibrationC3Algorithm iD InvertStatus [0][0] totalCounts: " << ID << " " << InvertStatus << " " <<
83  NoiseMatrix(0, 0) << " " << totalCounts);
84  B2INFO("eclAutocovarianceCalibrationC3Algorithm Setting ID " << ID << " Autocovariance to Autocovariance from ID " << ID - 1);
85  B2INFO("eclAutocovarianceCalibrationC3Algorithm B: " << tempAutoCov[0]);
86  Autocovariances->getAutoCovariance(ID + 1 - 1, tempAutoCov);
87  B2INFO("eclAutocovarianceCalibrationC3Algorithm A: " << tempAutoCov[0]);
88  }
89 
90  Autocovariances->setAutoCovariance(ID + 1, tempAutoCov);
91 
92  cryIDs.push_back(ID + 1);
93  invertStatusVector.push_back(InvertStatus);
94  noiseMatrix00Vector.push_back(NoiseMatrix(0, 0));
95  totalCountsVector.push_back(totalCounts);
96 
97  }
98 
100  saveCalibration(Autocovariances, "ECLAutoCovariance");
101 
103  auto ginvertStatusVector = new TGraph(cryIDs.size(), cryIDs.data(), invertStatusVector.data());
104  ginvertStatusVector->SetName("ginvertStatusVector");
105  ginvertStatusVector->SetMarkerStyle(20);
106  auto gnoiseMatrix00Vector = new TGraph(cryIDs.size(), cryIDs.data(), noiseMatrix00Vector.data());
107  gnoiseMatrix00Vector->SetName("gnoiseMatrix00Vector");
108  gnoiseMatrix00Vector->SetMarkerStyle(20);
109  auto gtotalCountsVector = new TGraph(cryIDs.size(), cryIDs.data(), totalCountsVector.data());
110  gtotalCountsVector->SetName("gtotalCountsVector");
111  gtotalCountsVector->SetMarkerStyle(20);
112 
114  TString fName = m_outputName;
115  TDirectory::TContext context;
116  TFile* histfile = new TFile(fName, "recreate");
117  histfile->cd();
118  ginvertStatusVector->Write();
119  gnoiseMatrix00Vector->Write();
120  gtotalCountsVector->Write();
121  histfile->Close();
122  delete histfile;
123 
124  return c_OK;
125 }
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 getAutoCovariance(const int cellID, double acov[31]) const
Get auto covariance for a channel.
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.