Belle II Software  release-08-01-10
eclAutocovarianceCalibrationC4Collector.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 //This module`
10 #include <ecl/modules/eclAutocovarianceCalibrationC4Collector/eclAutocovarianceCalibrationC4Collector.h>
11 
12 //Root
13 #include <TDecompChol.h>
14 
15 //Framework
16 #include <framework/dataobjects/EventMetaData.h>
17 
18 //ECL
19 #include <ecl/dataobjects/ECLDsp.h>
20 #include <ecl/dbobjects/ECLCrystalCalib.h>
21 
22 using namespace std;
23 using namespace Belle2;
24 
25 //-----------------------------------------------------------------
26 // Register the Modules
27 //-----------------------------------------------------------------
28 REG_MODULE(eclAutocovarianceCalibrationC4Collector);
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
34 // constructor
35 eclAutocovarianceCalibrationC4CollectorModule::eclAutocovarianceCalibrationC4CollectorModule() : CalibrationCollectorModule(),
36  m_ECLAutocovarianceCalibrationC1Threshold("ECLAutocovarianceCalibrationC1Threshold"),
37  m_ECLAutocovarianceCalibrationC3Autocovariances("ECLAutoCovariance")
38 {
39  // Set module properties
40  setDescription("Module to test covariance matrix calibrations using waveforms from delayed Bhabha events");
42 }
43 
45 {
46 
51  registerObject<TH2F>("Chi2VsCrysID", Chi2VsCrysID);
52 
53  m_eclDsps.isRequired();
54 
55 }
56 
58 {
60  B2INFO("eclAutocovarianceCalibrationC4Collector: Experiment = " << m_evtMetaData->getExperiment() << " run = " <<
61  m_evtMetaData->getRun());
62 
64 
65  std::vector<double> buf(m_numberofADCPoints);
66 
68 
69  for (int id = 0; id < ECLElementNumbers::c_NCrystals; id++) {
70  m_ECLAutocovarianceCalibrationC3Autocovariances->getAutoCovariance(id + 1, buf.data());
72  for (int i = 0; i < m_numberofADCPoints; i++) {
73  for (int j = 0; j < m_numberofADCPoints; j++) {
74  m_NoiseMatrix[id](i, j) = buf[abs(i - j)];
75  }
76  }
77  TDecompChol dc(m_NoiseMatrix[id]);
78  bool InvertStatus = dc.Invert(m_NoiseMatrix[id]);
79 
80  if (InvertStatus == 0) {
81  B2INFO("Invert Failed for " << id);
82  }
83 
84  }
85 
86 }
87 
88 
90 {
91 
92  const int NumDsp = m_eclDsps.getEntries();
93 
94  //Random Trigger Events have waveform for each crystal
95  if (NumDsp == ECLElementNumbers::c_NCrystals) {
96 
97  for (auto& aECLDsp : m_eclDsps) {
98 
99  const int id = aECLDsp.getCellId() - 1;
100 
101  //Peak to peak amplitude used to gauge noise level
102  float PeakToPeak = (float) aECLDsp.computePeaktoPeakAmp();
103 
104  if (PeakToPeak < m_PeakToPeakThresholds[id]) {
105 
106  //Computing baselinw subtracted waveform. aECLDsp.getDspA() is the raw ECLDsp waveform.
107  vector<double> waveform(m_numberofADCPoints);
108  //for (int i = 0; i < m_numberofADCPoints; i++) waveform[i] = aECLDsp.getDspA()[i] - baseline;
109  for (int i = 0; i < m_numberofADCPoints; i++) waveform[i] = aECLDsp.getDspA()[i];
110 
111  //After subtraccting baseline, expected value is 0. Assuming noise is properly modelld by the cov. mat. (computed by this calibration) then a "fit" to a constant should give a good chi2. Below computes the resulting chi2 for a fit to 0 and used the calibrated cov. mat. to model the noise.
112 
113  // chi^2(B) = a*B^2 - 2*B*b + c
114  double aconst = 0, bconst = 0, cconst = 0;
115  for (int i = 0; i < 31; i++) {
116  double sum = 0, sumQ = 0;
117  for (int j = 0; j < 31; j++) {
118  sum += m_NoiseMatrix[id](i, j);
119  sumQ += m_NoiseMatrix[id](i, j) * waveform[j];
120  }
121  aconst += sum;
122  bconst += sum * waveform[i];
123  cconst += sumQ * waveform[i];
124  }
125 
126  const double Bconst = bconst / aconst;
127  const double chi2val = ((aconst * Bconst - 2 * bconst) * Bconst + cconst);
128 
129  Chi2VsCrysID->Fill(id, chi2val);
130  }
131 
132  }
133  }
134 }
135 
137 {
138  for (int i = 0; i < ECLElementNumbers::c_NCrystals; i++) {
139  for (int j = 0; j < m_NbinsForChi2Histogram; j++) {
140  getObjectPtr<TH2>("Chi2VsCrysID")->SetBinContent(i + 1, j + 1, Chi2VsCrysID->GetBinContent(i + 1, j + 1));
141  }
142  }
143 }
Calibration collector module base class.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
std::vector< TMatrixDSym > m_NoiseMatrix
Stores noise matrix derived from the input Autocovariances.
StoreArray< ECLDsp > m_eclDsps
Required input array of ECLDSPs.
DBObjPtr< ECLCrystalCalib > m_ECLAutocovarianceCalibrationC1Threshold
thresholds obtained from C1 stage
void collect() override
Select events and crystals and accumulate histograms.
StoreObjPtr< EventMetaData > m_evtMetaData
dataStore EventMetaData
std::vector< float > m_PeakToPeakThresholds
vector of thresholds obtained from DB object
void closeRun() override
Transfer fom array container to ROOT histogram.
void prepare() override
Define histograms and read payloads from DB.
DBObjPtr< ECLAutoCovariance > m_ECLAutocovarianceCalibrationC3Autocovariances
Autocovariances obtained from C3 stage.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.