Belle II Software  release-08-01-10
eclWaveformTemplateCalibrationC1Collector.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/eclWaveformTemplateCalibrationC1Collector/eclWaveformTemplateCalibrationC1Collector.h>
11 
12 //Framework
13 #include <framework/dataobjects/EventMetaData.h>
14 
15 //ECL
16 #include <ecl/dataobjects/ECLDigit.h>
17 #include <ecl/dataobjects/ECLDsp.h>
18 #include <ecl/dbobjects/ECLCrystalCalib.h>
19 
20 using namespace std;
21 using namespace Belle2;
22 
23 //-----------------------------------------------------------------
24 // Register the Modules
25 //-----------------------------------------------------------------
26 REG_MODULE(eclWaveformTemplateCalibrationC1Collector);
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 // constructor
32 eclWaveformTemplateCalibrationC1CollectorModule::eclWaveformTemplateCalibrationC1CollectorModule()
33 {
34  // Set module properties
35  setDescription("Module to export histogram of baseline noise of energetic waveforms from ee->gg events. First step in photon template calculation.");
36  addParam("MinEnergyThreshold", m_MinEnergyThreshold, "Minimum energy threshold of online fit result for Fitting Waveforms (GeV).",
37  2.0);
38  addParam("MaxEnergyThreshold", m_MaxEnergyThreshold, "Maximum energy threshold of online fit result for Fitting Waveforms (GeV).",
39  4.0);
40  addParam("baselineLimit", m_baselineLimit, "Number of ADC points used to define baseline.", 12);
41  addParam("maxResvsCrysIDHistogramLimit", m_maxResvsCrysIDHistogramLimit, "upper limit of histogram.", 100);
42  addParam("maxResvsCrysIDHistogramNBins", m_maxResvsCrysIDHistogramNBins, "histogram number of bins.", 1000);
43  addParam("ADCFloorThreshold", m_ADCFloorThreshold, "Used to determine if waveform hit ADC floor", 10);
44  setPropertyFlags(c_ParallelProcessingCertified);
45 }
46 
47 void eclWaveformTemplateCalibrationC1CollectorModule::prepare()
48 {
49 
52  maxResvsCrysID = new TH2F("maxResvsCrysID", "", ECLElementNumbers::c_NCrystals, 0, ECLElementNumbers::c_NCrystals,
53  m_maxResvsCrysIDHistogramNBins, 0, m_maxResvsCrysIDHistogramLimit);
54  registerObject<TH2F>("maxResvsCrysID", maxResvsCrysID);
55 
56  m_eclDsps.isRequired();
57  m_eclDigits.isRequired();
58 
59 }
60 
61 void eclWaveformTemplateCalibrationC1CollectorModule::startRun()
62 {
64  B2INFO("eclWaveformTemplateCalibrationC1Collector: Experiment = " << m_evtMetaData->getExperiment() << " run = " <<
65  m_evtMetaData->getRun());
66 
67  // Loading crystal calibration constants from database
68  m_ADCtoEnergy.resize(ECLElementNumbers::c_NCrystals);
69  if (m_CrystalElectronics.isValid()) {
70  for (int i = 0; i < ECLElementNumbers::c_NCrystals; i++)
71  m_ADCtoEnergy[i] = m_CrystalElectronics->getCalibVector()[i];
72  }
73  if (m_CrystalEnergy.isValid()) {
74  for (int i = 0; i < ECLElementNumbers::c_NCrystals; i++)
75  m_ADCtoEnergy[i] *= m_CrystalEnergy->getCalibVector()[i];
76  }
77 
78 }
79 
80 
81 void eclWaveformTemplateCalibrationC1CollectorModule::collect()
82 {
83 
84  for (auto& aECLDsp : m_eclDsps) {
85 
86  const int id = aECLDsp.getCellId() - 1;
87 
88  //estimating crystal energy
89  double energy = 0.0;
90  for (const auto& aECLDigit : m_eclDigits) {
91  if (aECLDigit.getCellId() - 1 == id) {
92  energy = aECLDigit.getAmp() * m_ADCtoEnergy[id];
93  break;
94  }
95  }
96 
97  // only select high energy crystals
98  if (energy < m_MinEnergyThreshold) continue;
99  // avoid very high energy crystals due to potential photodiode interactions
100  if (energy > m_MaxEnergyThreshold) continue;
101 
102  // check if waveform has point below m_ADCFloorThreshold (indicates waveform hit ADC floor)
103  bool skipWaveform = false;
104  for (int i = 0; i < m_numberofADCPoints; i++) {
105  if (aECLDsp.getDspA()[i] < m_ADCFloorThreshold) skipWaveform = true;
106  }
107  if (skipWaveform) continue;
108 
109  //compute mean of baseline
110  float baseline = 0.0;
111  for (int i = 0; i < m_baselineLimit; i++) baseline += aECLDsp.getDspA()[i];
112  baseline /= ((float) m_baselineLimit);
113 
114  //compute max residual in baseline
115  float maxRes = 0.0;
116  for (int i = 0; i < m_baselineLimit; i++) {
117  float temp = fabs(aECLDsp.getDspA()[i] - baseline);
118  if (temp > maxRes) maxRes = temp;
119  }
120 
121  //save result to histogram
122  maxResvsCrysID->Fill(id, maxRes);
123 
124  }
125 
126 }
127 
128 void eclWaveformTemplateCalibrationC1CollectorModule::closeRun()
129 {
130  for (int i = 0; i < ECLElementNumbers::c_NCrystals; i++) {
131  for (int j = 0; j < m_maxResvsCrysIDHistogramNBins; j++) {
132  getObjectPtr<TH2>("maxResvsCrysID")->SetBinContent(i + 1, j + 1, maxResvsCrysID->GetBinContent(i + 1, j + 1));
133  }
134  }
135 }
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.