CMS 3D CMS Logo

DeDxTools.cc
Go to the documentation of this file.
3 
5 
6 #include <vector>
7 #include <numeric>
8 
9 namespace deDxTools {
10  using namespace std;
11  using namespace reco;
12 
13  bool shapeSelection(const SiStripCluster& clus) {
14  // ---------------- Counting the number of maxima --------------------------
15  //----------------------------------------------------------------------------
16  auto const& ampls = clus.amplitudes();
17  Int_t NofMax = 0;
18  Int_t recur255 = 1;
19  Int_t recur254 = 1;
20  bool MaxOnStart = false;
21  bool MaxInMiddle = false, MaxOnEnd = false;
22  Int_t MaxPos = 0;
23 
24  // Start with max
25  if (ampls.size() != 1 &&
26  ((ampls[0] > ampls[1]) ||
27  (ampls.size() > 2 && ampls[0] == ampls[1] && ampls[1] > ampls[2] && ampls[0] != 254 && ampls[0] != 255) ||
28  (ampls.size() == 2 && ampls[0] == ampls[1] && ampls[0] != 254 && ampls[0] != 255))) {
29  NofMax = NofMax + 1;
30  MaxOnStart = true;
31  }
32 
33  // Maximum reached
34  if (ampls.size() > 2) {
35  for (unsigned int i = 1; i < ampls.size() - 1U; i++) {
36  if ((ampls[i] > ampls[i - 1] && ampls[i] > ampls[i + 1]) ||
37  (ampls.size() > 3 && i > 0 && i < ampls.size() - 2U && ampls[i] == ampls[i + 1] &&
38  ampls[i] > ampls[i - 1] && ampls[i] > ampls[i + 2] && ampls[i] != 254 && ampls[i] != 255)) {
39  NofMax = NofMax + 1;
40  MaxInMiddle = true;
41  MaxPos = i;
42  }
43  if (ampls[i] == 255 && ampls[i] == ampls[i - 1]) {
44  recur255 = recur255 + 1;
45  MaxPos = i - (recur255 / 2);
46  if (ampls[i] > ampls[i + 1]) {
47  NofMax = NofMax + 1;
48  MaxInMiddle = true;
49  }
50  }
51  if (ampls[i] == 254 && ampls[i] == ampls[i - 1]) {
52  recur254 = recur254 + 1;
53  MaxPos = i - (recur254 / 2);
54  if (ampls[i] > ampls[i + 1]) {
55  NofMax = NofMax + 1;
56  MaxInMiddle = true;
57  }
58  }
59  }
60  }
61 
62  // End with a max
63  if (ampls.size() > 1) {
64  if (ampls[ampls.size() - 1] > ampls[ampls.size() - 2] ||
65  (ampls.size() > 2 && ampls[ampls.size() - 1] == ampls[ampls.size() - 2] &&
66  ampls[ampls.size() - 2] > ampls[ampls.size() - 3]) ||
67  ampls[ampls.size() - 1] == 255) {
68  NofMax = NofMax + 1;
69  MaxOnEnd = true;
70  }
71  }
72 
73  // If only one strip touched
74  if (ampls.size() == 1) {
75  NofMax = 1;
76  }
77 
78  // -------------- SHAPE-BASED SELECTION FOR UNIQUE MAXIMAS --------------
79  //------------------------------------------------------------------------
80  /*
81  ____
82  | |____
83  ____| | |
84  | | | |____
85  ____| | | | |
86  | | | | | |____
87  __|____|____|____|____|____|____|__
88  C_Mnn C_Mn C_M C_D C_Dn C_Dnn
89  */
90  bool shapecdtn = false;
91 
92  Float_t C_M = 0.0;
93  Float_t C_D = 0.0;
94  Float_t C_Mn = 10000;
95  Float_t C_Dn = 10000;
96  Float_t C_Mnn = 10000;
97  Float_t C_Dnn = 10000;
98  Int_t CDPos;
99  Float_t coeff1 = 1.7;
100  Float_t coeff2 = 2.0;
101  Float_t coeffn = 0.10;
102  Float_t coeffnn = 0.02;
103  Float_t noise = 4.0;
104 
105  if (NofMax == 1) {
106  if (MaxOnStart == true) {
107  C_M = (Float_t)ampls[0];
108  C_D = (Float_t)ampls[1];
109  if (ampls.size() < 3)
110  shapecdtn = true;
111  else if (ampls.size() == 3) {
112  C_Dn = (Float_t)ampls[2];
113  if (C_Dn <= coeff1 * coeffn * C_D + coeff2 * coeffnn * C_M + 2 * noise || C_D == 255)
114  shapecdtn = true;
115  } else if (ampls.size() > 3) {
116  C_Dn = (Float_t)ampls[2];
117  C_Dnn = (Float_t)ampls[3];
118  if ((C_Dn <= coeff1 * coeffn * C_D + coeff2 * coeffnn * C_M + 2 * noise || C_D == 255) &&
119  C_Dnn <= coeff1 * coeffn * C_Dn + coeff2 * coeffnn * C_D + 2 * noise) {
120  shapecdtn = true;
121  }
122  }
123  }
124 
125  if (MaxOnEnd == true) {
126  C_M = (Float_t)ampls[ampls.size() - 1];
127  C_D = (Float_t)ampls[ampls.size() - 2];
128  if (ampls.size() < 3)
129  shapecdtn = true;
130  else if (ampls.size() == 3) {
131  C_Dn = (Float_t)ampls[0];
132  if (C_Dn <= coeff1 * coeffn * C_D + coeff2 * coeffnn * C_M + 2 * noise || C_D == 255)
133  shapecdtn = true;
134  } else if (ampls.size() > 3) {
135  C_Dn = (Float_t)ampls[ampls.size() - 3];
136  C_Dnn = (Float_t)ampls[ampls.size() - 4];
137  if ((C_Dn <= coeff1 * coeffn * C_D + coeff2 * coeffnn * C_M + 2 * noise || C_D == 255) &&
138  C_Dnn <= coeff1 * coeffn * C_Dn + coeff2 * coeffnn * C_D + 2 * noise) {
139  shapecdtn = true;
140  }
141  }
142  }
143 
144  if (MaxInMiddle == true) {
145  C_M = (Float_t)ampls[MaxPos];
146  int LeftOfMaxPos = MaxPos - 1;
147  if (LeftOfMaxPos <= 0)
148  LeftOfMaxPos = 0;
149  int RightOfMaxPos = MaxPos + 1;
150  if (RightOfMaxPos >= (int)ampls.size())
151  RightOfMaxPos = ampls.size() - 1;
152  if (ampls[LeftOfMaxPos] < ampls[RightOfMaxPos]) {
153  C_D = (Float_t)ampls[RightOfMaxPos];
154  C_Mn = (Float_t)ampls[LeftOfMaxPos];
155  CDPos = RightOfMaxPos;
156  } else {
157  C_D = (Float_t)ampls[LeftOfMaxPos];
158  C_Mn = (Float_t)ampls[RightOfMaxPos];
159  CDPos = LeftOfMaxPos;
160  }
161  if (C_Mn < coeff1 * coeffn * C_M + coeff2 * coeffnn * C_D + 2 * noise || C_M == 255) {
162  if (ampls.size() == 3)
163  shapecdtn = true;
164  else if (ampls.size() > 3) {
165  if (CDPos > MaxPos) {
166  if (ampls.size() - CDPos - 1 == 0) {
167  C_Dn = 0;
168  C_Dnn = 0;
169  }
170  if (ampls.size() - CDPos - 1 == 1) {
171  C_Dn = (Float_t)ampls[CDPos + 1];
172  C_Dnn = 0;
173  }
174  if (ampls.size() - CDPos - 1 > 1) {
175  C_Dn = (Float_t)ampls[CDPos + 1];
176  C_Dnn = (Float_t)ampls[CDPos + 2];
177  }
178  if (MaxPos >= 2) {
179  C_Mnn = (Float_t)ampls[MaxPos - 2];
180  } else if (MaxPos < 2)
181  C_Mnn = 0;
182  }
183  if (CDPos < MaxPos) {
184  if (CDPos == 0) {
185  C_Dn = 0;
186  C_Dnn = 0;
187  }
188  if (CDPos == 1) {
189  C_Dn = (Float_t)ampls[0];
190  C_Dnn = 0;
191  }
192  if (CDPos > 1) {
193  C_Dn = (Float_t)ampls[CDPos - 1];
194  C_Dnn = (Float_t)ampls[CDPos - 2];
195  }
196  if (ampls.size() - LeftOfMaxPos > 1 && MaxPos + 2 < (int)(ampls.size()) - 1) {
197  C_Mnn = (Float_t)ampls[MaxPos + 2];
198  } else
199  C_Mnn = 0;
200  }
201  if ((C_Dn <= coeff1 * coeffn * C_D + coeff2 * coeffnn * C_M + 2 * noise || C_D == 255) &&
202  C_Mnn <= coeff1 * coeffn * C_Mn + coeff2 * coeffnn * C_M + 2 * noise &&
203  C_Dnn <= coeff1 * coeffn * C_Dn + coeff2 * coeffnn * C_D + 2 * noise) {
204  shapecdtn = true;
205  }
206  }
207  }
208  }
209  }
210  if (ampls.size() == 1) {
211  shapecdtn = true;
212  }
213 
214  return shapecdtn;
215  }
216 
217  int getCharge(const SiStripCluster* cluster,
218  int& nSatStrip,
219  const GeomDetUnit& detUnit,
220  const std::vector<std::vector<float> >& calibGains,
221  const unsigned int& offsetDU_) {
222  const auto& Ampls = cluster->amplitudes();
223 
224  nSatStrip = 0;
225  int charge = 0;
226 
227  if (calibGains.empty()) {
228  for (unsigned int i = 0; i < Ampls.size(); i++) {
229  int calibratedCharge = Ampls[i];
230  charge += calibratedCharge;
231  if (calibratedCharge >= 254)
232  nSatStrip++;
233  }
234  } else {
235  for (unsigned int i = 0; i < Ampls.size(); i++) {
236  int calibratedCharge = Ampls[i];
237 
238  auto& gains = calibGains[detUnit.index() - offsetDU_];
239  calibratedCharge = (int)(calibratedCharge / gains[(cluster->firstStrip() + i) / 128]);
240  if (calibratedCharge >= 255) {
241  if (calibratedCharge >= 1025)
242  calibratedCharge = 255;
243  else
244  calibratedCharge = 254;
245  }
246 
247  charge += calibratedCharge;
248  if (calibratedCharge >= 254)
249  nSatStrip++;
250  }
251  }
252  return charge;
253  }
254 
255  void makeCalibrationMap(const std::string& m_calibrationPath,
256  const TrackerGeometry& tkGeom,
257  std::vector<std::vector<float> >& calibGains,
258  const unsigned int& offsetDU_) {
259  auto const& dus = tkGeom.detUnits();
260  calibGains.resize(dus.size() - offsetDU_);
261 
262  TChain* t1 = new TChain("SiStripCalib/APVGain");
263  t1->Add(m_calibrationPath.c_str());
264 
265  unsigned int tree_DetId;
266  unsigned char tree_APVId;
267  double tree_Gain;
268  t1->SetBranchAddress("DetId", &tree_DetId);
269  t1->SetBranchAddress("APVId", &tree_APVId);
270  t1->SetBranchAddress("Gain", &tree_Gain);
271 
272  for (unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
273  t1->GetEntry(ientry);
274  auto& gains = calibGains[tkGeom.idToDetUnit(DetId(tree_DetId))->index() - offsetDU_];
275  if (gains.size() < (size_t)(tree_APVId + 1)) {
276  gains.resize(tree_APVId + 1);
277  }
278  gains[tree_APVId] = tree_Gain;
279  }
280  t1->Delete();
281  }
282 
284  if (Record == "SiStripDeDxMip_3D_Rcd") {
286  }
287  if (Record == "SiStripDeDxPion_3D_Rcd") {
289  }
290  if (Record == "SiStripDeDxKaon_3D_Rcd") {
292  }
293  if (Record == "SiStripDeDxProton_3D_Rcd") {
295  }
296  if (Record == "SiStripDeDxElectron_3D_Rcd") {
298  }
299  throw cms::Exception("WrongRecord for dEdx") << "The record : " << Record << "is unknown\n";
300  }
301 
303  ESGetTokenH3DDVariant const& iToken) {
304  switch (iToken.index()) {
305  case 0:
306  return iES.getData(std::get<0>(iToken));
307  case 1:
308  return iES.getData(std::get<1>(iToken));
309  case 2:
310  return iES.getData(std::get<2>(iToken));
311  case 3:
312  return iES.getData(std::get<3>(iToken));
313  case 4:
314  return iES.getData(std::get<4>(iToken));
315  }
316  throw cms::Exception("HistogramD3DTokenUnset");
317  }
318 
321  TH3F*& Prob_ChargePath) {
322  float xmin = deDxMap.rangeX().min;
323  float xmax = deDxMap.rangeX().max;
324  float ymin = deDxMap.rangeY().min;
325  float ymax = deDxMap.rangeY().max;
326  float zmin = deDxMap.rangeZ().min;
327  float zmax = deDxMap.rangeZ().max;
328 
329  if (Prob_ChargePath)
330  delete Prob_ChargePath;
331  Prob_ChargePath = new TH3F("Prob_ChargePath",
332  "Prob_ChargePath",
333  deDxMap.numberOfBinsX(),
334  xmin,
335  xmax,
336  deDxMap.numberOfBinsY(),
337  ymin,
338  ymax,
339  deDxMap.numberOfBinsZ(),
340  zmin,
341  zmax);
342 
343  if (strcmp(ProbabilityMode.c_str(), "Accumulation") == 0) {
344  for (int i = 0; i <= Prob_ChargePath->GetXaxis()->GetNbins() + 1; i++) {
345  for (int j = 0; j <= Prob_ChargePath->GetYaxis()->GetNbins() + 1; j++) {
346  float Ni = 0;
347  for (int k = 0; k <= Prob_ChargePath->GetZaxis()->GetNbins() + 1; k++) {
348  Ni += deDxMap.binContent(i, j, k);
349  }
350  for (int k = 0; k <= Prob_ChargePath->GetZaxis()->GetNbins() + 1; k++) {
351  float tmp = 0;
352  for (int l = 0; l <= k; l++) {
353  tmp += deDxMap.binContent(i, j, l);
354  }
355  if (Ni > 0) {
356  Prob_ChargePath->SetBinContent(i, j, k, tmp / Ni);
357  } else {
358  Prob_ChargePath->SetBinContent(i, j, k, 0);
359  }
360  }
361  }
362  }
363  } else if (strcmp(ProbabilityMode.c_str(), "Integral") == 0) {
364  for (int i = 0; i <= Prob_ChargePath->GetXaxis()->GetNbins() + 1; i++) {
365  for (int j = 0; j <= Prob_ChargePath->GetYaxis()->GetNbins() + 1; j++) {
366  float Ni = 0;
367  for (int k = 0; k <= Prob_ChargePath->GetZaxis()->GetNbins() + 1; k++) {
368  Ni += deDxMap.binContent(i, j, k);
369  }
370  for (int k = 0; k <= Prob_ChargePath->GetZaxis()->GetNbins() + 1; k++) {
371  float tmp = deDxMap.binContent(i, j, k);
372  if (Ni > 0) {
373  Prob_ChargePath->SetBinContent(i, j, k, tmp / Ni);
374  } else {
375  Prob_ChargePath->SetBinContent(i, j, k, 0);
376  }
377  }
378  }
379  }
380  } else {
381  throw cms::Exception("WrongConfig for dEdx") << "The ProbabilityMode: " << ProbabilityMode << "is unknown\n";
382  }
383  }
384 
385  bool isSpanningOver2APV(unsigned int FirstStrip, unsigned int ClusterSize) {
386  if (FirstStrip == 0)
387  return true;
388  if (FirstStrip == 128)
389  return true;
390  if (FirstStrip == 256)
391  return true;
392  if (FirstStrip == 384)
393  return true;
394  if (FirstStrip == 512)
395  return true;
396  if (FirstStrip == 640)
397  return true;
398 
399  if (FirstStrip <= 127 && FirstStrip + ClusterSize > 127)
400  return true;
401  if (FirstStrip <= 255 && FirstStrip + ClusterSize > 255)
402  return true;
403  if (FirstStrip <= 383 && FirstStrip + ClusterSize > 383)
404  return true;
405  if (FirstStrip <= 511 && FirstStrip + ClusterSize > 511)
406  return true;
407  if (FirstStrip <= 639 && FirstStrip + ClusterSize > 639)
408  return true;
409 
410  if (FirstStrip + ClusterSize == 127)
411  return true;
412  if (FirstStrip + ClusterSize == 255)
413  return true;
414  if (FirstStrip + ClusterSize == 383)
415  return true;
416  if (FirstStrip + ClusterSize == 511)
417  return true;
418  if (FirstStrip + ClusterSize == 639)
419  return true;
420  if (FirstStrip + ClusterSize == 767)
421  return true;
422 
423  return false;
424  }
425 
426  bool isFarFromBorder(const TrajectoryStateOnSurface& trajState, const GeomDetUnit* it) {
427  if (dynamic_cast<const StripGeomDetUnit*>(it) == nullptr && dynamic_cast<const PixelGeomDetUnit*>(it) == nullptr) {
428  edm::LogInfo("deDxTools") << "this detID doesn't seem to belong to the Tracker" << std::endl;
429  return false;
430  }
431 
432  LocalPoint HitLocalPos = trajState.localPosition();
433  LocalError HitLocalError = trajState.localError().positionError();
434 
435  const BoundPlane plane = it->surface();
436  const TrapezoidalPlaneBounds* trapezoidalBounds(dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
437  const RectangularPlaneBounds* rectangularBounds(dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
438 
439  if (!trapezoidalBounds && !rectangularBounds)
440  return false;
441 
442  double DistFromBorder = 1.0;
443  double HalfLength =
444  trapezoidalBounds ? (*trapezoidalBounds).parameters()[3] : it->surface().bounds().length() / 2.0;
445 
446  if (fabs(HitLocalPos.y()) + HitLocalError.yy() >= (HalfLength - DistFromBorder))
447  return false;
448 
449  return true;
450  }
451 
452 } // namespace deDxTools
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
uint16_t firstStrip() const
virtual float length() const =0
const TrackerGeomDet * idToDetUnit(DetId) const override
Return the pointer to the GeomDetUnit corresponding to a given DetId.
const LocalTrajectoryError & localError() const
PhysicsTools::Calibration::HistogramD3D const & getHistogramD3D(edm::EventSetup const &, ESGetTokenH3DDVariant const &)
Definition: DeDxTools.cc:302
int index() const
Definition: GeomDet.h:83
PhysicsTools::Calibration::HistogramD3D H3DD
Definition: DeDxTools.h:53
Value_t binContent(int bin) const
Definition: Histogram3D.h:93
int getCharge(const SiStripCluster *cluster, int &nSatStrip, const GeomDetUnit &detUnit, const std::vector< std::vector< float >> &calibGains, const unsigned int &m_off)
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
LocalError positionError() const
std::variant< edm::ESGetToken< H3DD, SiStripDeDxMip_3D_Rcd >, edm::ESGetToken< H3DD, SiStripDeDxPion_3D_Rcd >, edm::ESGetToken< H3DD, SiStripDeDxKaon_3D_Rcd >, edm::ESGetToken< H3DD, SiStripDeDxProton_3D_Rcd >, edm::ESGetToken< H3DD, SiStripDeDxElectron_3D_Rcd > > ESGetTokenH3DDVariant
Definition: DeDxTools.h:58
float yy() const
Definition: LocalError.h:24
SiStripCluster const & amplitudes() const
bool isFarFromBorder(const TrajectoryStateOnSurface &trajState, const GeomDetUnit *it)
Definition: DeDxTools.cc:426
bool getData(T &iHolder) const
Definition: EventSetup.h:122
constexpr float gains[NGAINS]
Definition: EcalConstants.h:11
Log< level::Info, false > LogInfo
Definition: DetId.h:17
void makeCalibrationMap(const std::string &m_calibrationPath, const TrackerGeometry &tkGeom, std::vector< std::vector< float >> &calibGains, const unsigned int &m_off)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
bool shapeSelection(const SiStripCluster &ampls)
Definition: DeDxTools.cc:13
bool isSpanningOver2APV(unsigned int FirstStrip, unsigned int ClusterSize)
Definition: DeDxTools.cc:385
void buildDiscrimMap(PhysicsTools::Calibration::HistogramD3D const &, std::string const &ProbabilityMode, TH3F *&Prob_ChargePath)
Definition: DeDxTools.cc:319
fixed size matrix
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::LocalCoordinateSystemTag > LocalPoint
point in local coordinate system
Definition: Point3D.h:15
tmp
align.sh
Definition: createJobs.py:716
const Bounds & bounds() const
Definition: Surface.h:87