Belle II Software release-09-00-01
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
23using namespace Belle2;
24using 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> noiseMatrix00Vector;
45 std::vector<double> totalCountsVector;
46 std::vector<double> invertAttempts;
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 double totalCounts = CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 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) = double(CovarianceMatrixInfoVsCrysID->GetBinContent(CovarianceMatrixInfoVsCrysID->GetBin(ID + 1,
72 index + 1))) / (totalCounts - 1) / (double(m_numberofADCPoints - index));
73 }
74 }
75
76 TMatrixDSym NoiseMatrixReduced(m_numberofADCPoints);
77 for (int i = 0; i < m_numberofADCPoints; i++) {
78 for (int j = 0; j < m_numberofADCPoints; j++) {
79 NoiseMatrixReduced(i, j) = (NoiseMatrix(0, abs(i - j)));
80 }
81 }
82
83 bool invert_successful = 0;
84 int invert_attempt = 0;
85 double tempAutoCov[m_numberofADCPoints];
86 for (int i = 0; i < m_numberofADCPoints; i++) tempAutoCov[i] = NoiseMatrixReduced(0, i);
87 std::vector<double> buf(m_numberofADCPoints);
88 while (invert_successful == 0) {
89
90 Autocovariances->setAutoCovariance(ID + 1, tempAutoCov);
91 Autocovariances->getAutoCovariance(ID + 1, buf.data());
92
93 TMatrixDSym NoiseMatrix_check(m_numberofADCPoints);
94 for (int i = 0; i < m_numberofADCPoints; i++) {
95 for (int j = 0; j < m_numberofADCPoints; j++) {
96 NoiseMatrix_check(i, j) = buf[abs(i - j)];
97 }
98 }
99
100 TDecompChol dc(NoiseMatrix_check);
101 invert_successful = dc.Invert(NoiseMatrix_check);
102 if (invert_successful == 0) {
103
104 if (invert_attempt > 4) {
105 B2INFO("eclAutocovarianceCalibrationC3Algorithm iD " << ID << " invert_attempt limit reached " << invert_attempt);
106 B2INFO("eclAutocovarianceCalibrationC3Algorithm setting m_u2 to zero");
107 m_u2 = 0.0;
108 }
109
110 B2INFO("eclAutocovarianceCalibrationC3Algorithm iD " << ID << " invert_attempt " << invert_attempt);
111
112 for (int i = 0; i < m_numberofADCPoints; i++) B2INFO("old[" << i << "] = " << tempAutoCov[i]);
113 for (int i = 1; i < m_numberofADCPoints; i++) tempAutoCov[i] *= (m_u2 / (1. + exp((i - m_u0) / m_u1)));
114 for (int i = 0; i < m_numberofADCPoints; i++) B2INFO("new[" << i << "] = " << tempAutoCov[i]);
115
116 }
117 invert_attempt++;
118 }
119
120 cryIDs.push_back(ID + 1);
121 noiseMatrix00Vector.push_back(tempAutoCov[0]);
122 totalCountsVector.push_back(totalCounts);
123 invertAttempts.push_back(invert_attempt);
124
125 }
126
128 saveCalibration(Autocovariances, "ECLAutoCovariance");
129
131 auto gnoiseMatrix00Vector = new TGraph(cryIDs.size(), cryIDs.data(), noiseMatrix00Vector.data());
132 gnoiseMatrix00Vector->SetName("gnoiseMatrix00Vector");
133 gnoiseMatrix00Vector->SetMarkerStyle(20);
134 auto gtotalCountsVector = new TGraph(cryIDs.size(), cryIDs.data(), totalCountsVector.data());
135 gtotalCountsVector->SetName("gtotalCountsVector");
136 gtotalCountsVector->SetMarkerStyle(20);
137 auto ginvertAttempts = new TGraph(cryIDs.size(), cryIDs.data(), invertAttempts.data());
138 ginvertAttempts->SetName("ginvertAttempts");
139 ginvertAttempts->SetMarkerStyle(20);
140
142 TString fName = m_outputName;
143 TDirectory::TContext context;
144 TFile* histfile = new TFile(fName, "recreate");
145 histfile->cd();
146 gnoiseMatrix00Vector->Write();
147 gtotalCountsVector->Write();
148 ginvertAttempts->Write();
149 histfile->Close();
150 delete histfile;
151
152 return c_OK;
153}
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.