Belle II Software  release-05-02-19
SVDClusterQualityEstimatorCalibration.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: James Webb *
7  * *
8  **************************************************************************/
9 
10 #include <svd/modules/svdClusterQualityEstimator/ClusterQualityHelperFunctions.h>
11 #include <svd/modules/svdClusterQualityEstimator/SVDClusterQualityEstimatorCalibrationModule.h>
12 
13 #include <TFile.h>
14 
15 #include <vector>
16 #include <math.h>
17 
18 using namespace std;
19 using namespace Belle2;
20 
21 
22 REG_MODULE(SVDClusterQualityEstimatorCalibration)
23 
25  Module()
26 {
27 
28  setDescription("Generate PDFs used in assigning quality to clusterss.");
29  setPropertyFlags(c_ParallelProcessingCertified);
30 
31  // 1. Collections.
32  addParam("SVDClusters", m_svdClustersName,
33  "SVDCluster collection name", string(""));
34 
35  addParam("RecoTracks", m_recoTracksName,
36  "RecoTracks collection name", string(""));
37 
38  // 2.Modification parameters:
39  addParam("NameOfInstance", m_nameOfInstance,
40  "allows the user to set an identifier for this module. Usefull if one wants to use several instances of that module", string(""));
41  addParam("binSizeCharge", m_binSizeCharge, "Number of bins in charge distribtuion.",
42  int(100));
43  addParam("binSizeTime", m_binSizeTime, "Number of bins in charge distribtuion.",
44  int(40));
45 
46  addParam("maxClusterSize", m_maxClusterSize, "Max number of strips the PDF are separated into.",
47  int(5));
48 
49 
50  addParam("outputFileName", m_outputFileName,
51  "Name of output file containing pdfs", std::string("clusterQICalibration.root"));
52 
53  addParam("useLegacyNaming", m_useLegacyNaming,
54  "Use old PDF name convention?", bool(true));
55 }
56 
57 
58 
59 void SVDClusterQualityEstimatorCalibrationModule::initialize()
60 {
61  // prepare all store- and relationArrays:
62  m_svdClusters.isRequired(m_svdClustersName);
63  m_recoTracks.isRequired(m_recoTracksName);
64 
65 
66 
67  VXD::GeoCache& geo = VXD::GeoCache::getInstance();
68  for (auto& layers : geo.getLayers(VXD::SensorInfoBase::SVD)) {
69  for (auto& ladders : geo.getLadders(layers)) {
70  for (auto& sensors : geo.getSensors(ladders)) {
71 
72  for (int side = 0; side <= 1; side++) {
73  for (int size = 1; size <= m_maxClusterSize; size++) {
74  std::string sensorName;
75  std::string errorNameString;
76 
77 
78  clusterPDFName(sensors, size, side, m_maxClusterSize, sensorName, errorNameString, m_useLegacyNaming);
79 
80  std::string signalHistName = sensorName + "signal";
81  std::string backgroundHistName = sensorName + "background";
82 
83  if (signalHistMap.count(sensorName) == 0) {
84  TH2F* sigHist = new TH2F(signalHistName.c_str(), "", m_binSizeTime, -100, 100, m_binSizeCharge, 0, 200000);
85  TH2F* bkgHist = new TH2F(backgroundHistName.c_str(), "", m_binSizeTime, -100, 100, m_binSizeCharge, 0, 200000);
86 
87  signalHistMap[sensorName] = sigHist;
88  backgroundHistMap[sensorName] = bkgHist;
89  }
90  }
91  }
92  }
93  }
94  }
95 
96 
97 
98 }
99 
100 
101 
102 void SVDClusterQualityEstimatorCalibrationModule::event()
103 {
104  for (auto& track : m_recoTracks) {
105  if (track.wasFitSuccessful() == 1) {
106  if (track.hasSVDHits() == 1) {
107  for (auto& svdHit : track.getSVDHitList()) {
108  std::string pdfName;
109  std::string errorNameString;
110  clusterPDFName(svdHit->getSensorID(), svdHit->getSize(), svdHit->isUCluster(), m_maxClusterSize, pdfName, errorNameString,
111  m_useLegacyNaming);
112 
113  auto sigHist = signalHistMap.at(pdfName);
114  sigHist->Fill(svdHit->getClsTime(), svdHit->getCharge());
115 
116 
117  }
118  }
119  }
120  }
121 
122 
123  for (auto& cluster : m_svdClusters) {
124  std::string pdfName;
125  std::string errorNameString;
126  clusterPDFName(cluster.getSensorID(), cluster.getSize(), cluster.isUCluster(), m_maxClusterSize, pdfName, errorNameString,
127  m_useLegacyNaming);
128 
129  auto bkgHist = backgroundHistMap.at(pdfName);
130  bkgHist->Fill(cluster.getClsTime(), cluster.getCharge());
131  }
132 }
133 
134 
135 
136 void SVDClusterQualityEstimatorCalibrationModule::terminate()
137 {
138  //Open rootfile
139  TFile* f = new TFile(m_outputFileName.c_str(), "RECREATE");
140  f->cd();
141 
142  std::vector<std::string> usedSensors; //Store names to avoid double counting
143 
144  VXD::GeoCache& geo = VXD::GeoCache::getInstance();
145  for (auto& layers : geo.getLayers(VXD::SensorInfoBase::SVD)) {
146  for (auto& ladders : geo.getLadders(layers)) {
147  for (auto& sensors : geo.getSensors(ladders)) {
148  for (int side = 0; side <= 1; side++) {
149  for (int size = 1; size <= m_maxClusterSize; size++) {
150  std::string sensorName;
151  std::string errorName;
152 
153  clusterPDFName(sensors, size, side, m_maxClusterSize, sensorName, errorName, m_useLegacyNaming);
154 
155  if (std::find(usedSensors.begin(), usedSensors.end(), sensorName.c_str()) == usedSensors.end()) {
156  usedSensors.push_back(sensorName);
157  TH2F* probHist = new TH2F(sensorName.c_str(), "", m_binSizeTime, -100, 100, m_binSizeCharge, 0, 200000);
158  TH2F* errorHist = new TH2F(errorName.c_str(), "", m_binSizeTime, -100, 100, m_binSizeCharge, 0, 200000);
159  calculateProb(signalHistMap[sensorName], backgroundHistMap[sensorName], probHist);
160  calculateError(signalHistMap[sensorName], backgroundHistMap[sensorName], errorHist);
161 
162  probHist->Write();
163  errorHist->Write();
164  }
165  }
166  }
167  }
168  }
169  }
170  f->Close();
171 }
172 
173 
174 void SVDClusterQualityEstimatorCalibrationModule::calculateProb(TH2F* signal, TH2F* background, TH2F* probability)
175 {
176  signal->Divide(background);
177  probability->Add(signal);
178 
179 }
180 void SVDClusterQualityEstimatorCalibrationModule::calculateError(TH2F* signal, TH2F* background, TH2F* error)
181 {
182  int imax = signal->GetXaxis()->GetNbins();
183  int jmax = signal->GetYaxis()->GetNbins();
184  for (int i = 1; i <= imax; i++) {
185  for (int j = 1; j <= jmax; j++) {
186  int bkg = background->GetBinContent(i, j);
187  int sig = signal->GetBinContent(i, j);
188  double var = ((sig + 1) * (sig + 2)) / ((bkg + 2) * (bkg + 3)) -
189  ((sig + 1) * (sig + 1)) / ((bkg + 2) * (bkg + 2));
190  double err = sqrt(var);
191  error->SetBinContent(i, j, err);
192  }
193  }
194 }
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::VXD::GeoCache::getLayers
const std::set< Belle2::VxdID > getLayers(SensorInfoBase::SensorType sensortype=SensorInfoBase::VXD)
Return a set of all known Layers.
Definition: GeoCache.cc:177
Belle2::VXD::GeoCache::getSensors
const std::set< Belle2::VxdID > & getSensors(Belle2::VxdID ladder) const
Return a set of all sensor IDs belonging to a given ladder.
Definition: GeoCache.cc:205
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::VXD::GeoCache
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:41
Belle2::VXD::GeoCache::getLadders
const std::set< Belle2::VxdID > & getLadders(Belle2::VxdID layer) const
Return a set of all ladder IDs belonging to a given layer.
Definition: GeoCache.cc:194
Belle2::clusterPDFName
void clusterPDFName(const VxdID &sensor, int size, int side, int maxClusterSize, std::string &PDFName, std::string &errorPDFName, bool useLegacyNaming)
Function to set name of PDF for cluster quality estimation.
Definition: ClusterQualityHelperFunctions.h:34
Belle2::SVDClusterQualityEstimatorCalibrationModule
Generate PDFs for assigning probability that cluster generated from signal particle.
Definition: SVDClusterQualityEstimatorCalibrationModule.h:41