Belle II Software development
CDCDedxCorrectionModule.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#include <cdc/modules/CDCDedxCorrection/CDCDedxCorrectionModule.h>
10
11#include <cdc/geometry/CDCGeometryPar.h>
12
13#include <TMath.h>
14
15using namespace Belle2;
16using namespace CDC;
17
18REG_MODULE(CDCDedxCorrection);
19
21{
22
23 setDescription("Apply hit level corrections to the dE/dx measurements.");
24 addParam("relativeCorrections", m_relative, "If true, apply corrections relative", true);
25 addParam("momentumCor", m_momCor, "Boolean to apply momentum correction", false);
26 addParam("momentumCorFromDB", m_useDBMomCor, "Boolean to apply momentum correction from DB", false);
27 addParam("scaleCor", m_scaleCor, "Boolean to apply scale correction", false);
28 addParam("cosineCor", m_cosineCor, "Boolean to apply cosine correction", false);
29 addParam("wireGain", m_wireGain, "Boolean to apply wire gains", false);
30 addParam("runGain", m_runGain, "Boolean to apply run gain", false);
31 addParam("timeGain", m_timeGain, "Boolean to apply time gain", false);
32 addParam("twoDCell", m_twoDCell, "Boolean to apply 2D correction", false);
33 addParam("oneDCell", m_oneDCell, "Boolean to apply 1D correction", false);
34 addParam("cosineEdge", m_cosineEdge, "Boolean to apply cosine edge correction", true);
35 addParam("nonlADC", m_nonlADC, "Boolean to apply non-linear adc correction", true);
36 addParam("removeLowest", m_removeLowest, "portion of events with low dE/dx", double(0.05));
37 addParam("removeHighest", m_removeHighest, "portion of events with high dE/dx", double(0.25));
38}
39
41
43{
44 // register in datastore
45 m_cdcDedxTracks.isRequired();
46
47 // make sure the calibration constants are reasonable
48 // run gains
49 if (m_DBRunGain->getRunGain() == 0) {
50 B2WARNING("Run gain is zero for this run");
51 }
52
53 // wire gains
54 int nZeroWG = 0;
55
56 for (unsigned int i = 0; i < c_nSenseWires; ++i) {
57 if (m_DBWireGains->getWireGain(i) == 0) {
58 ++nZeroWG;
59 B2DEBUG(20, "Wire gain is zero for wire: " << i);
60 }
61 }
62
63 if (nZeroWG > 0) {
64 B2WARNING("Found " << nZeroWG << " wires with zero gain");
65 }
66
67 // cosine correction (store the bin edges for extrapolation)
68 for (unsigned int il = 0; il < c_maxNSenseLayers; ++il) {
69 unsigned int ncosbins = m_DBCosineCor->getSize(il);
70
71 for (unsigned int i = 0; i < ncosbins; ++i) {
72 double gain = m_DBCosineCor->getMean(il, i);
73 if (gain == 0)
74 B2ERROR("Cosine gain is zero for this bin " << i);
75 }
76 }
77
78 // get the hadron correction parameters
79 if (!m_DBHadronCor || m_DBHadronCor->getSize() == 0) {
80 B2WARNING("No hadron correction parameters!");
81 for (int i = 0; i < 4; ++i)
82 m_hadronpars.push_back(0.0);
83 m_hadronpars.push_back(1.0);
84 } else m_hadronpars = m_DBHadronCor->getHadronPars();
85
86 int jwire = -1;
87 B2INFO("Creating CDCGeometryPar object");
89
90 for (unsigned int il = 0; il < c_maxNSenseLayers; il++) {
91 int activewires = 0;
92 m_lgainavg[il] = 0.0;
93
94 for (unsigned int iw = 0; iw < cdcgeo.nWiresInLayer(il); ++iw) {
95 jwire++;
96 if (m_DBWireGains->getWireGain(jwire) > 0.) {
97 //active wire only
98 m_lgainavg[il] += m_DBWireGains->getWireGain(jwire);
99 activewires++;
100 }
101 }
102 if (activewires > 0) m_lgainavg[il] /= activewires;
103 else m_lgainavg[il] = 1.0;
104 }
105}
106
107
109{
110
111 // **************************************************
112 // LOOP OVER EACH DEDX MEASUREMENT (TRACK LEVEL)
113 // **************************************************
114 for (auto& dedxTrack : m_cdcDedxTracks) {
115 if (dedxTrack.size() == 0) {
116 B2WARNING("No good hits on this track...");
117 continue;
118 }
119
120 // **************************************************
121 // LOOP OVER EACH DEDX MEASUREMENT (HIT LEVEL)
122 // **************************************************
123 // hit level
124 int nhits = dedxTrack.size();
125 double costh = dedxTrack.getCosTheta();
126 std::vector<double> newLayerHits;
127 double newLayerDe = 0, newLayerDx = 0;
128
129 if (costh < TMath::Cos(150.0 * TMath::DegToRad()))continue; //-0.866
130 if (costh > TMath::Cos(17.0 * TMath::DegToRad())) continue; //0.95
131
132 double injtime = dedxTrack.getInjectionTime();
133 double injring = dedxTrack.getInjectionRing();
134
135 for (int i = 0; i < nhits; ++i) {
136
137 //pay attention to deadwire or gain uses
138 //getADCount is already corrected w/ non linear ADC payload
139 //getADCbasecount is now incorrect ADC
140 int jadcbase = dedxTrack.getADCBaseCount(i);
141 double jLayer = dedxTrack.getHitLayer(i);
142 double jWire = dedxTrack.getWire(i);
143 double jNDocaRS = dedxTrack.getDocaRS(i) / dedxTrack.getCellHalfWidth(i);
144 double jEntaRS = dedxTrack.getEntaRS(i);
145 double jPath = dedxTrack.getPath(i);
146
147 double correction = dedxTrack.m_scale * dedxTrack.m_runGain * dedxTrack.m_timeGain * dedxTrack.getCosineCorrection(
148 i) * dedxTrack.m_cosEdgeCor *
149 dedxTrack.getTwoDCorrection(i) * dedxTrack.getOneDCorrection(i) * dedxTrack.getNonLADCCorrection(i);
150 if (dedxTrack.getWireGain(i) > 0)correction *= dedxTrack.getWireGain(i); //also keep dead wire
151
152 //Modify hit level dedx
153 double newhitdedx = (m_relative) ? 1 / correction : 1.0;
154 StandardCorrection(jadcbase, jLayer, jWire, jNDocaRS, jEntaRS, jPath, costh, injring, injtime, newhitdedx);
155 dedxTrack.setDedx(i, newhitdedx);
156
157 // do track level dedx and modify after loop over hits
158 // rel const -> upto 6 from calibrated GT and 2 are direct from dedx track (no rel cal for them now)
159 // abs const -> upto 6 from calibrated GT and 2 are direct from default GT
160 if (m_relative) {
161 //prewire gains need old tracks (old bad wire) and post need track new wg (new dead wire)
162 //get same base adc + rel correction factor
163 correction *= GetCorrection(jadcbase, jLayer, jWire, jNDocaRS, jEntaRS, costh, injring, injtime);
164 if (!m_DBWireGains && dedxTrack.getWireGain(i) == 0)correction = 0;
165 } else {
166 //get modified adc + abs correction factor
167 correction = GetCorrection(jadcbase, jLayer, jWire, jNDocaRS, jEntaRS, costh, injring, injtime);
168 }
169
170 // combine hits across layers
171 if (correction != 0) {
172 newLayerDe += jadcbase / correction;
173 newLayerDx += jPath;
174 }
175
176 if (i + 1 < nhits && dedxTrack.getHitLayer(i + 1) == jLayer) {
177 continue;
178 } else {
179 if (newLayerDx != 0)newLayerHits.push_back(newLayerDe / newLayerDx * std::sqrt(1 - costh * costh));
180 newLayerDe = 0;
181 newLayerDx = 0;
182 }
183 }
184
185 // recalculate the truncated means
186 calculateMeans(&(dedxTrack.m_dedxAvg),
187 &(dedxTrack.m_dedxAvgTruncatedNoSat),
188 &(dedxTrack.m_dedxAvgTruncatedErr),
189 newLayerHits);
190
191 dedxTrack.m_dedxAvgTruncated = dedxTrack.m_dedxAvgTruncatedNoSat;
192 HadronCorrection(costh, dedxTrack.m_dedxAvgTruncated);
193 } // end loop over tracks
194}
195
197{
198
199 B2INFO("CDCDedxCorrectionModule exiting...");
200}
201
203{
204 double gain = m_DBRunGain->getRunGain();
205 if (gain != 0) {
206 dedx = dedx / gain;
207 } else dedx = 0;
208}
209
210void CDCDedxCorrectionModule::TimeGainCorrection(double& dedx, double ring, double time) const
211{
212 double gain = m_DBInjectTime->getCorrection("mean", ring, time);
213 if (gain != 0) {
214 dedx = dedx / gain;
215 } else dedx = 0;
216}
217
218void CDCDedxCorrectionModule::WireGainCorrection(int wireID, double& dedx, int layer) const
219{
220 double gain = m_DBWireGains->getWireGain(wireID);
221 if (gain != 0) dedx = dedx / gain;
222 else {
223 //rel-abs method needs all wire for cal but w/ this method post calis (e.g.final RG)
224 //will also see all hitdedx but that is not an issue for track level calibration
225 if (m_relative)dedx = 0;
226 else {
227 if (m_lgainavg.at(layer) > 0)dedx = dedx / m_lgainavg.at(layer);
228 }
229 }
230}
231
232void CDCDedxCorrectionModule::TwoDCorrection(int layer, double doca, double enta, double& dedx) const
233{
234
235 double gain = (m_DB2DCell) ? m_DB2DCell->getMean(layer, doca, enta) : 1.0;
236 if (gain != 0) dedx = dedx / gain;
237 else dedx = 0;
238}
239
240
241void CDCDedxCorrectionModule::OneDCorrection(int layer, double enta, double& dedx) const
242{
243 double gain = (m_DB1DCell) ? m_DB1DCell->getMean(layer, enta) : 1.0;
244 if (gain != 0) dedx = dedx / gain;
245 else dedx = 0;
246}
247
248void CDCDedxCorrectionModule::CosineCorrection(unsigned int layer, double costh, double& dedx) const
249{
250 double coscor = m_DBCosineCor->getMean(layer, costh);
251 if (coscor != 0) dedx = dedx / coscor;
252 else dedx = 0;
253}
254
255void CDCDedxCorrectionModule::CosineEdgeCorrection(double costh, double& dedx) const
256{
257
258 double cosedgecor = m_DBCosEdgeCor->getMean(costh);
259 if (cosedgecor != 0) dedx = dedx / cosedgecor;
260 else dedx = 0;
261}
262
263
264void CDCDedxCorrectionModule::HadronCorrection(double costheta, double& dedx) const
265{
266 dedx = D2I(costheta, I2D(costheta, 1.00) / 1.00 * dedx);
267}
268
269void CDCDedxCorrectionModule::StandardCorrection(int adc, int layer, int wireID, double doca, double enta, double length,
270 double costheta, double ring, double time, double& dedx) const
271{
272
273 if (!m_relative && m_nonlADC)
274 adc = m_DBNonlADC->getCorrectedADC(adc, layer);
275
276 dedx *= adc * std::sqrt(1 - costheta * costheta) / length;
277
278 if (m_scaleCor) {
279 double scale = m_DBScaleFactor->getScaleFactor();
280 if (scale != 0) dedx = dedx / scale;
281 else dedx = 0;
282 }
283
284 if (m_runGain)
285 RunGainCorrection(dedx);
286
287 if (m_timeGain)
288 TimeGainCorrection(dedx, ring, time);
289
290 if (m_wireGain)
291 WireGainCorrection(wireID, dedx, layer);
292
293 if (m_cosineCor)
294 CosineCorrection(layer, costheta, dedx);
295
296 //only if constants are abs are for specific costh
297 if (!m_relative && m_cosineEdge && (costheta <= -0.850 || costheta >= 0.950))
298 CosineEdgeCorrection(costheta, dedx);
299
300 if (m_twoDCell)
301 TwoDCorrection(layer, doca, enta, dedx);
302
303 if (m_oneDCell)
304 OneDCorrection(layer, enta, dedx);
305}
306
307
308double CDCDedxCorrectionModule::GetCorrection(int& adc, int layer, int wireID, double doca, double enta, double costheta,
309 double ring, double time) const
310{
311 double correction = 1.0;
312 if (m_scaleCor) correction *= m_DBScaleFactor->getScaleFactor();
313 if (m_runGain) correction *= m_DBRunGain->getRunGain();
314 if (m_timeGain) correction *= m_DBInjectTime->getCorrection("mean", ring, time);
315 if (m_wireGain) correction *= m_DBWireGains->getWireGain(wireID);
316 if (m_twoDCell) correction *= m_DB2DCell->getMean(layer, doca, enta);
317 if (m_oneDCell) correction *= m_DB1DCell->getMean(layer, enta);
318 if (m_cosineCor) correction *= m_DBCosineCor->getMean(layer, costheta);
319
320 //these last two are only for abs constant
321 if (!m_relative) {
322 if (m_nonlADC)adc = m_DBNonlADC->getCorrectedADC(adc, layer);
323 if (m_cosineEdge && (costheta <= -0.850 || costheta >= 0.950)) {
324 correction *= m_DBCosEdgeCor->getMean(costheta);
325 }
326 }
327 return correction;
328}
329
330
331double CDCDedxCorrectionModule::D2I(const double cosTheta, const double D) const
332{
333 double absCosTheta = fabs(cosTheta);
334 double projection = pow(absCosTheta, m_hadronpars[3]) + m_hadronpars[2];
335 if (projection == 0) {
336 B2WARNING("Something wrong with dE/dx hadron constants!");
337 return D;
338 }
339
340 double chargeDensity = D / projection;
341 double numerator = 1 + m_hadronpars[0] * chargeDensity;
342 double denominator = 1 + m_hadronpars[1] * chargeDensity;
343
344 if (denominator == 0) {
345 B2WARNING("Something wrong with dE/dx hadron constants!");
346 return D;
347 }
348
349 double I = D * m_hadronpars[4] * numerator / denominator;
350 return I;
351}
352
353double CDCDedxCorrectionModule::I2D(const double cosTheta, const double I) const
354{
355 double absCosTheta = fabs(cosTheta);
356 double projection = pow(absCosTheta, m_hadronpars[3]) + m_hadronpars[2];
357
358 if (projection == 0 || m_hadronpars[4] == 0) {
359 B2WARNING("Something wrong with dE/dx hadron constants!");
360 return I;
361 }
362
363 double a = m_hadronpars[0] / projection;
364 double b = 1 - m_hadronpars[1] / projection * (I / m_hadronpars[4]);
365 double c = -1.0 * I / m_hadronpars[4];
366
367 if (b == 0 && a == 0) {
368 B2WARNING("both a and b coefficiants for hadron correction are 0");
369 return I;
370 }
371
372 double discr = b * b - 4.0 * a * c;
373 if (discr < 0) {
374 B2WARNING("negative discriminant; return uncorrectecd value");
375 return I;
376 }
377
378 double D = (a != 0) ? (-b + sqrt(discr)) / (2.0 * a) : -c / b;
379 if (D < 0) {
380 B2WARNING("D is less 0! will try another solution");
381 D = (a != 0) ? (-b - sqrt(discr)) / (2.0 * a) : -c / b;
382 if (D < 0) {
383 B2WARNING("D is still less 0! just return uncorrectecd value");
384 return I;
385 }
386 }
387
388 return D;
389}
390
391
392void CDCDedxCorrectionModule::calculateMeans(double* mean, double* truncatedMean, double* truncatedMeanErr,
393 const std::vector<double>& dedx) const
394{
395 // Calculate the truncated average by skipping the lowest & highest
396 // events in the array of dE/dx values
397 std::vector<double> sortedDedx = dedx;
398 std::sort(sortedDedx.begin(), sortedDedx.end());
399 sortedDedx.erase(std::remove(sortedDedx.begin(), sortedDedx.end(), 0), sortedDedx.end());
400 sortedDedx.shrink_to_fit();
401
402 double truncatedMeanTmp = 0.0;
403 double meanTmp = 0.0;
404 double sumOfSquares = 0.0;
405 int numValuesTrunc = 0;
406 const int numDedx = sortedDedx.size();
407
408 // add a factor of 0.51 here to make sure we are rounding appropriately...
409 const int lowEdgeTrunc = int(numDedx * m_removeLowest + 0.51);
410 const int highEdgeTrunc = int(numDedx * (1 - m_removeHighest) + 0.51);
411 for (int i = 0; i < numDedx; i++) {
412 meanTmp += sortedDedx[i];
413 if (i >= lowEdgeTrunc and i < highEdgeTrunc) {
414 truncatedMeanTmp += sortedDedx[i];
415 sumOfSquares += sortedDedx[i] * sortedDedx[i];
416 numValuesTrunc++;
417 }
418 }
419
420 if (numDedx != 0) {
421 meanTmp /= numDedx;
422 }
423 if (numValuesTrunc != 0) {
424 truncatedMeanTmp /= numValuesTrunc;
425 } else {
426 truncatedMeanTmp = meanTmp;
427 }
428
429 *mean = meanTmp;
430 *truncatedMean = truncatedMeanTmp;
431
432 if (numValuesTrunc > 1) {
433 *truncatedMeanErr = sqrt(sumOfSquares / double(numValuesTrunc) - truncatedMeanTmp * truncatedMeanTmp) / double(
434 numValuesTrunc - 1);
435 } else {
436 *truncatedMeanErr = 0;
437 }
438}
bool m_twoDCell
boolean to apply 2D correction
double D2I(const double cosTheta, const double D) const
Saturation correction: convert the measured ionization (D) to actual ionization (I)
bool m_runGain
boolean to apply run gains
std::vector< double > m_hadronpars
hadron saturation parameters
bool m_cosineEdge
boolean to apply cosine edge
DBObjPtr< CDCDedxRunGain > m_DBRunGain
Run gain DB object.
bool m_useDBMomCor
boolean to apply momentum correction from DB
void TwoDCorrection(int layer, double doca, double enta, double &dedx) const
Perform a 2D correction.
bool m_momCor
boolean to apply momentum correction
virtual void initialize() override
Initialize the Module.
bool m_scaleCor
boolean to apply scale factor
DBObjPtr< CDCDedxHadronCor > m_DBHadronCor
hadron saturation parameters
virtual void event() override
This method is called for each event.
double m_removeHighest
upper bound for truncated mean
DBObjPtr< CDCDedxCosineEdge > m_DBCosEdgeCor
cosine edge calibration
double I2D(const double cosTheta, const double I) const
Saturation correction: convert the actual ionization (I) to measured ionization (D)
std::array< double, 56 > m_lgainavg
average calibration factor for the layer
void OneDCorrection(int layer, double enta, double &dedx) const
Perform a wire gain correction.
DBObjPtr< CDCDedxADCNonLinearity > m_DBNonlADC
hadron saturation non linearity
DBObjPtr< CDCDedx1DCell > m_DB1DCell
1D correction DB object
virtual void terminate() override
End of the event processing.
bool m_oneDCell
boolean to apply 1D correction
void HadronCorrection(double costheta, double &dedx) const
Perform a hadron saturation correction.
void calculateMeans(double *mean, double *truncatedMean, double *truncatedMeanErr, const std::vector< double > &dedx) const
Recalculate the dE/dx mean values after corrections.
void StandardCorrection(int adc, int layer, int wireID, double doca, double enta, double length, double costheta, double ring, double time, double &dedx) const
Perform a standard set of corrections.
DBObjPtr< CDCDedx2DCell > m_DB2DCell
2D correction DB object
bool m_nonlADC
boolean to apply non linear ADC
bool m_relative
boolean to apply relative or absolute correction
void CosineCorrection(unsigned int superlayer, double costheta, double &dedx) const
Perform the cosine correction.
DBObjPtr< CDCDedxCosineCor > m_DBCosineCor
Electron saturation correction DB object.
StoreArray< CDCDedxTrack > m_cdcDedxTracks
Store array: CDCDedxTrack.
bool m_timeGain
boolean to apply injection time gains
void CosineEdgeCorrection(double costh, double &dedx) const
Perform the cosine edge correction.
DBObjPtr< CDCDedxWireGain > m_DBWireGains
Wire gain DB object.
void WireGainCorrection(int wireID, double &dedx, int layer) const
Perform a wire gain correction.
bool m_cosineCor
boolean to apply cosine correction
void RunGainCorrection(double &dedx) const
Perform a run gain correction.
double GetCorrection(int &adc, int layer, int wireID, double doca, double enta, double costheta, double ring, double time) const
Get the standard set of corrections.
double m_removeLowest
lower bound for truncated mean
DBObjPtr< CDCDedxInjectionTime > m_DBInjectTime
time gain/reso DB object
DBObjPtr< CDCDedxScaleFactor > m_DBScaleFactor
Scale factor to make electrons ~1.
void TimeGainCorrection(double &dedx, double ring, double time) const
Perform a injection time gain correction.
bool m_wireGain
boolean to apply wire gains
The Class for CDC Geometry Parameters.
unsigned nWiresInLayer(int layerId) const
Returns wire numbers in a layer.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.