Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:13

0001 //
0002 // F.Ratnikov (UMd), Dec 14, 2005
0003 //
0004 #include <vector>
0005 #include <string>
0006 #include <cmath>
0007 #include <sstream>
0008 #include <iostream>
0009 #include <memory>
0010 #include <cassert>
0011 
0012 #include "CLHEP/Random/RandGauss.h"
0013 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbHardcode.h"
0014 #include "CalibFormats/HcalObjects/interface/HcalSiPMType.h"
0015 #include "DataFormats/HcalDigi/interface/HcalQIENum.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 
0018 HcalDbHardcode::HcalDbHardcode()
0019     :                                              //"generic" set of conditions
0020       theDefaultParameters_(3.0,                   //pedestal
0021                             0.5,                   //pedestal width
0022                             {0.2, 0.2},            //gains
0023                             {0.0, 0.0},            //gain widths
0024                             0,                     //ZS threshold
0025                             0,                     //QIE type
0026                             {0.0, 0.0, 0.0, 0.0},  //QIE offsets
0027                             {0.9, 0.9, 0.9, 0.9},  //QIE slopes
0028                             125,                   //MC shape
0029                             105,                   //Reco shape
0030                             0.0,                   //photoelectronsToAnalog
0031                             {0.0},                 //dark current
0032                             {0.0}                  //noise correlation
0033                             ),
0034       setHB_(false),
0035       setHE_(false),
0036       setHF_(false),
0037       setHO_(false),
0038       setHBUpgrade_(false),
0039       setHEUpgrade_(false),
0040       setHFUpgrade_(false),
0041       useHBUpgrade_(false),
0042       useHEUpgrade_(false),
0043       useHOUpgrade_(true),
0044       useHFUpgrade_(false),
0045       testHFQIE10_(false),
0046       testHEPlan1_(false) {}
0047 
0048 const HcalHardcodeParameters& HcalDbHardcode::getParameters(HcalGenericDetId fId) const {
0049   if (fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel) {
0050     if (useHBUpgrade_ && setHBUpgrade_)
0051       return theHBUpgradeParameters_;
0052     else if (!useHBUpgrade_ && setHB_)
0053       return theHBParameters_;
0054     else
0055       return theDefaultParameters_;
0056   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap) {
0057     bool b_isHEPlan1 = testHEPlan1_ ? isHEPlan1(fId) : false;
0058     if ((useHEUpgrade_ || b_isHEPlan1) && setHEUpgrade_)
0059       return theHEUpgradeParameters_;
0060     else if (!useHEUpgrade_ && !b_isHEPlan1 && setHE_)
0061       return theHEParameters_;
0062     else
0063       return theDefaultParameters_;
0064   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenForward) {
0065     if (useHFUpgrade_ && setHFUpgrade_)
0066       return theHFUpgradeParameters_;
0067     else if (testHFQIE10_ && fId.isHcalDetId()) {
0068       HcalDetId hid(fId);
0069       //special mixed case for HF 2016
0070       if (hid.iphi() == 39 && hid.zside() == 1 &&
0071           (hid.depth() >= 3 || (hid.depth() == 2 && (hid.ieta() == 30 || hid.ieta() == 34))) && setHFUpgrade_)
0072         return theHFUpgradeParameters_;
0073       else if (setHF_)
0074         return theHFParameters_;
0075       else
0076         return theDefaultParameters_;
0077     } else if (!useHFUpgrade_ && setHF_)
0078       return theHFParameters_;
0079     else
0080       return theDefaultParameters_;
0081   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0082     if (setHO_)
0083       return theHOParameters_;
0084     else
0085       return theDefaultParameters_;
0086   } else
0087     return theDefaultParameters_;
0088 }
0089 
0090 const int HcalDbHardcode::getGainIndex(HcalGenericDetId fId) const {
0091   int index = 0;
0092   if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0093     HcalDetId hid(fId);
0094     if ((hid.ieta() > -5) && (hid.ieta() < 5))
0095       index = 0;
0096     else
0097       index = 1;
0098   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenForward) {
0099     HcalDetId hid(fId);
0100     if (hid.depth() % 2 == 1)
0101       index = 0;  //depths 1,3
0102     else if (hid.depth() % 2 == 0)
0103       index = 1;  //depths 2,4
0104   }
0105   return index;
0106 }
0107 
0108 HcalPedestal HcalDbHardcode::makePedestal(
0109     HcalGenericDetId fId, bool fSmear, bool eff, const HcalTopology* topo, double intlumi) {
0110   HcalPedestalWidth width = makePedestalWidth(fId, eff, topo, intlumi);
0111   float value0 = getParameters(fId).pedestal();
0112   if (eff) {
0113     //account for dark current + crosstalk
0114     auto sipmpar = makeHardcodeSiPMParameter(fId, topo, intlumi);
0115     auto sipmchar = makeHardcodeSiPMCharacteristics();
0116     value0 += sipmpar.getDarkCurrent() * 25. / (1. - sipmchar->getCrossTalk(sipmpar.getType()));
0117   }
0118   float value[4] = {value0, value0, value0, value0};
0119   if (fSmear) {
0120     for (int i = 0; i < 4; i++) {
0121       value[i] = 0.0f;
0122       while (value[i] <= 0.0f)
0123         // ignore correlations, assume 10K pedestal run
0124         value[i] = value0 + (float)CLHEP::RandGauss::shoot(0.0, width.getWidth(i) / 100.);
0125     }
0126   }
0127   HcalPedestal result(fId.rawId(), value[0], value[1], value[2], value[3]);
0128   return result;
0129 }
0130 
0131 HcalPedestalWidth HcalDbHardcode::makePedestalWidth(HcalGenericDetId fId,
0132                                                     bool eff,
0133                                                     const HcalTopology* topo,
0134                                                     double intlumi) {
0135   float value = getParameters(fId).pedestalWidth();
0136   float width2 = value * value;
0137   // everything in fC
0138 
0139   if (eff) {
0140     //account for dark current + crosstalk
0141     auto sipmpar = makeHardcodeSiPMParameter(fId, topo, intlumi);
0142     auto sipmchar = makeHardcodeSiPMCharacteristics();
0143     //add in quadrature
0144     width2 += sipmpar.getDarkCurrent() * 25. / std::pow(1 - sipmchar->getCrossTalk(sipmpar.getType()), 3) *
0145               sipmpar.getFCByPE();
0146   }
0147 
0148   HcalPedestalWidth result(fId.rawId());
0149   for (int i = 0; i < 4; i++) {
0150     for (int j = 0; j < 4; j++) {
0151       result.setSigma(i, j, 0.0);
0152     }
0153     result.setSigma(i, i, width2);
0154   }
0155   return result;
0156 }
0157 
0158 HcalGain HcalDbHardcode::makeGain(HcalGenericDetId fId, bool fSmear) const {  // GeV/fC
0159   HcalGainWidth width = makeGainWidth(fId);
0160   float value0 = getParameters(fId).gain(getGainIndex(fId));
0161   float value[4] = {value0, value0, value0, value0};
0162   if (fSmear) {
0163     for (int i = 0; i < 4; i++) {
0164       value[i] = 0.0f;
0165       while (value[i] <= 0.0f)
0166         value[i] = value0 + (float)CLHEP::RandGauss::shoot(0.0, width.getValue(i));
0167     }
0168   }
0169   HcalGain result(fId.rawId(), value[0], value[1], value[2], value[3]);
0170   return result;
0171 }
0172 
0173 HcalGainWidth HcalDbHardcode::makeGainWidth(HcalGenericDetId fId) const {  // GeV/fC
0174   float value = getParameters(fId).gainWidth(getGainIndex(fId));
0175   HcalGainWidth result(fId.rawId(), value, value, value, value);
0176   return result;
0177 }
0178 
0179 HcalZSThreshold HcalDbHardcode::makeZSThreshold(HcalGenericDetId fId) const {
0180   int value = getParameters(fId).zsThreshold();
0181   HcalZSThreshold result(fId.rawId(), value);
0182   return result;
0183 }
0184 
0185 HcalQIECoder HcalDbHardcode::makeQIECoder(HcalGenericDetId fId) const {
0186   HcalQIECoder result(fId.rawId());
0187   // slope in ADC/fC
0188   const HcalHardcodeParameters& param(getParameters(fId));
0189   for (unsigned range = 0; range < 4; range++) {
0190     for (unsigned capid = 0; capid < 4; capid++) {
0191       result.setOffset(capid, range, param.qieOffset(range));
0192       result.setSlope(capid, range, param.qieSlope(range));
0193     }
0194   }
0195 
0196   return result;
0197 }
0198 
0199 HcalQIEType HcalDbHardcode::makeQIEType(HcalGenericDetId fId) const {
0200   HcalQIENum qieType = (HcalQIENum)(getParameters(fId).qieType());
0201   HcalQIEType result(fId.rawId(), qieType);
0202   return result;
0203 }
0204 
0205 HcalCalibrationQIECoder HcalDbHardcode::makeCalibrationQIECoder(HcalGenericDetId fId) const {
0206   HcalCalibrationQIECoder result(fId.rawId());
0207   float lowEdges[64];
0208   for (int i = 0; i < 64; i++) {
0209     lowEdges[i] = -1.5 + i;
0210   }
0211   result.setMinCharges(lowEdges);
0212   return result;
0213 }
0214 
0215 HcalQIEShape HcalDbHardcode::makeQIEShape() const { return HcalQIEShape(); }
0216 
0217 HcalMCParam HcalDbHardcode::makeMCParam(HcalGenericDetId fId) const {
0218   int r1bit[5];
0219   r1bit[0] = 9;  //  [0,9]
0220   int syncPhase = 0;
0221   r1bit[1] = 1;
0222   int binOfMaximum = 0;
0223   r1bit[2] = 4;
0224   float phase = -25.0f;                   // [-25.0,25.0]
0225   float Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0226                                           // (offset 50nsec,  0.25ns step)
0227   int Iphase = Xphase;
0228   r1bit[3] = 8;  // [0,256] offset 50ns, .25ns step
0229   int timeSmearing = 0;
0230   r1bit[4] = 1;  //  bool
0231 
0232   const HcalHardcodeParameters& hparam(getParameters(fId));
0233   int pulseShapeID = hparam.mcShape();  // a0
0234 
0235   if (fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel) {
0236     syncPhase = 1;                    // a1  bool
0237     binOfMaximum = 5;                 // a2
0238     phase = 5.0f;                     // a3  [-25.0,25.0]
0239     Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0240                                       // (offset 50nsec,  0.25ns step)
0241     Iphase = Xphase;
0242     timeSmearing = 1;  // a4
0243 
0244   }
0245 
0246   else if (fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap) {
0247     syncPhase = 1;                    // a1  bool
0248     binOfMaximum = 5;                 // a2
0249     phase = 5.0f;                     // a3  [-25.0,25.0]
0250     Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0251                                       // (offset 50nsec,  0.25ns step)
0252     Iphase = Xphase;
0253     timeSmearing = 1;  // a4
0254 
0255   }
0256 
0257   else if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0258     syncPhase = 1;                    // a1  bool
0259     binOfMaximum = 5;                 // a2
0260     phase = 5.0f;                     // a3  [-25.0,25.0]
0261     Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0262                                       // (offset 50nsec,  0.25ns step)
0263     Iphase = Xphase;
0264     timeSmearing = 0;  // a4
0265 
0266     HcalDetId cell = HcalDetId(fId);
0267     if (cell.ieta() == 1 && cell.iphi() == 1)
0268       pulseShapeID = 125;
0269 
0270   }
0271 
0272   else if (fId.genericSubdet() == HcalGenericDetId::HcalGenForward) {
0273     syncPhase = 1;                    // a1  bool
0274     binOfMaximum = 3;                 // a2
0275     phase = 14.0f;                    // a3  [-25.0,25.0]
0276     Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0277                                       // (offset 50nsec,  0.25ns step)
0278     Iphase = Xphase;
0279     timeSmearing = 0;  // a4
0280 
0281   }
0282 
0283   else if (fId.genericSubdet() == HcalGenericDetId::HcalGenZDC) {
0284     pulseShapeID = 401;               // a0
0285     syncPhase = 1;                    // a1  bool
0286     binOfMaximum = 5;                 // a2
0287     phase = -4.0f;                    // a3  [-25.0,25.0]
0288     Xphase = (phase + 32.0f) * 4.0f;  // never change this line
0289                                       // (offset 50nsec,  0.25ns step)
0290     Iphase = Xphase;
0291     timeSmearing = 0;  // a4
0292   }
0293 
0294   int rshift[7];
0295   rshift[0] = 0;
0296   for (int k = 0; k < 5; k++) {
0297     rshift[k + 1] = rshift[k] + r1bit[k];
0298   }
0299 
0300   int packingScheme = 1;  // a5
0301   unsigned int param = pulseShapeID | syncPhase << rshift[1] | (binOfMaximum << rshift[2]) | (Iphase << rshift[3]) |
0302                        (timeSmearing << rshift[4] | packingScheme << 27);
0303 
0304   HcalMCParam result(fId.rawId(), param);
0305   return result;
0306 }
0307 
0308 HcalRecoParam HcalDbHardcode::makeRecoParam(HcalGenericDetId fId) const {
0309   // Mostly comes from S.Kunori's macro
0310   int p1bit[6];
0311 
0312   // param1
0313   int containmentCorrectionFlag = 0;
0314   p1bit[0] = 1;  // bool
0315   int containmentCorrectionPreSample = 0;
0316   p1bit[1] = 1;                         // bool
0317   float phase = 13.0;                   // [-25.0,25.0]
0318   float Xphase = (phase + 32.0) * 4.0;  //never change this line
0319                                         // (offset 50nsec,  0.25ns step)
0320   int Iphase = Xphase;
0321   p1bit[2] = 8;  // [0,256]
0322                  // (offset 50ns, 0.25ns step
0323   int firstSample = 4;
0324   p1bit[3] = 4;  // [0,9]
0325   int samplesToAdd = 2;
0326   p1bit[4] = 4;  // [0,9]
0327   p1bit[5] = 9;  // [0,9]
0328 
0329   const HcalHardcodeParameters& hparam(getParameters(fId));
0330   int pulseShapeID = hparam.recoShape();  // a5
0331 
0332   int q2bit[10];
0333   //  param2.
0334   int useLeakCorrection = 0;
0335   q2bit[0] = 1;  // bool
0336   int LeakCorrectionID = 0;
0337   q2bit[1] = 4;  // [0,15]
0338   int correctForTimeslew = 0;
0339   q2bit[2] = 1;  // bool
0340   int timeCorrectionID = 0;
0341   q2bit[3] = 4;  // [0,15]
0342   int correctTiming = 0;
0343   q2bit[4] = 1;  // bool
0344   int firstAuxTS = 0;
0345   q2bit[5] = 4;  // [0,15]
0346   int specialCaseID = 0;
0347   q2bit[6] = 4;  // [0,15]
0348   int noiseFlaggingID = 0;
0349   q2bit[7] = 4;  // [0,15]
0350   int pileupCleaningID = 0;
0351   q2bit[8] = 4;  // [0,15]
0352   int packingScheme = 1;
0353   q2bit[9] = 4;
0354 
0355   if ((fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel) ||
0356       (fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap)) {
0357     //  param1.
0358     containmentCorrectionFlag = 1;       // p0
0359     containmentCorrectionPreSample = 0;  // p1
0360     float phase = 6.0;
0361     float Xphase = (phase + 32.0) * 4.0;  // never change this line
0362                                           //(offset 50nsec, 0.25ns step)
0363     Iphase = Xphase;                      // p2
0364     firstSample = 4;                      // p3
0365     samplesToAdd = 2;                     // p4
0366 
0367     //  param2.
0368     useLeakCorrection = 0;   //  q0
0369     LeakCorrectionID = 0;    //  q1
0370     correctForTimeslew = 1;  //  q2
0371     timeCorrectionID = 0;    //  q3
0372     correctTiming = 1;       //  q4
0373     firstAuxTS = 4;          //  q5
0374     specialCaseID = 0;       //  q6
0375     noiseFlaggingID = 1;     //  q7
0376     pileupCleaningID = 0;    //  q8
0377   }
0378 
0379   else if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0380     //  param1.
0381     containmentCorrectionFlag = 1;       // p0
0382     containmentCorrectionPreSample = 0;  // p1
0383     float phase = 13.0;
0384     float Xphase = (phase + 32.0) * 4.0;  // never change this line
0385                                           // (offset 50nsec,  0.25ns step)
0386     Iphase = Xphase;                      // p2
0387     firstSample = 4;                      // p3
0388     samplesToAdd = 4;                     // p4
0389 
0390     //  param2.
0391     useLeakCorrection = 0;   //  q0
0392     LeakCorrectionID = 0;    //  q1
0393     correctForTimeslew = 1;  //  q2
0394     timeCorrectionID = 0;    //  q3
0395     correctTiming = 1;       //  q4
0396     firstAuxTS = 4;          //  q5
0397     specialCaseID = 0;       //  q6
0398     noiseFlaggingID = 1;     //  q7
0399     pileupCleaningID = 0;    //  q8
0400 
0401   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenForward) {
0402     //  param1.
0403     containmentCorrectionFlag = 0;       // p0
0404     containmentCorrectionPreSample = 0;  // p1
0405     float phase = 13.0;
0406     float Xphase = (phase + 32.0) * 4.0;  // never change this line
0407                                           // (offset 50nsec,  0.25ns step)
0408     Iphase = Xphase;                      // p2
0409     firstSample = 2;                      // p3
0410     samplesToAdd = 1;                     // p4
0411 
0412     //  param2.
0413     useLeakCorrection = 0;   //  q0
0414     LeakCorrectionID = 0;    //  q1
0415     correctForTimeslew = 0;  //  q2
0416     timeCorrectionID = 0;    //  q3
0417     correctTiming = 1;       //  q4
0418     firstAuxTS = 1;          //  q5
0419     specialCaseID = 0;       //  q6
0420     noiseFlaggingID = 1;     //  q7
0421     pileupCleaningID = 0;    //  q8
0422   }
0423 
0424   // Packing parameters in two words
0425 
0426   int p1shift[7];
0427   p1shift[0] = 0;
0428   for (int k = 0; k < 6; k++) {
0429     int j = k + 1;
0430     p1shift[j] = p1shift[k] + p1bit[k];
0431     //     cout<<"  j= "<<j<<"  shift "<< p1shift[j]<<endl;
0432   }
0433   int param1 = 0;
0434   param1 = containmentCorrectionFlag | (containmentCorrectionPreSample << p1shift[1]) | (Iphase << p1shift[2]) |
0435            (firstSample << p1shift[3]) | (samplesToAdd << p1shift[4]) | (pulseShapeID << p1shift[5]);
0436 
0437   int q2shift[10];
0438   q2shift[0] = 0;
0439   for (int k = 0; k < 9; k++) {
0440     int j = k + 1;
0441     q2shift[j] = q2shift[k] + q2bit[k];
0442     //  cout<<"  j= "<<j<<"  shift "<< q2shift[j]<<endl;
0443   }
0444   int param2 = 0;
0445   param2 = useLeakCorrection | (LeakCorrectionID << q2shift[1]) | (correctForTimeslew << q2shift[2]) |
0446            (timeCorrectionID << q2shift[3]) | (correctTiming << q2shift[4]) | (firstAuxTS << q2shift[5]) |
0447            (specialCaseID << q2shift[6]) | (noiseFlaggingID << q2shift[7]) | (pileupCleaningID << q2shift[8]) |
0448            (packingScheme << q2shift[9]);
0449 
0450   HcalRecoParam result(fId.rawId(), param1, param2);
0451 
0452   return result;
0453 }
0454 
0455 HcalTimingParam HcalDbHardcode::makeTimingParam(HcalGenericDetId fId) const {
0456   int nhits = 0;
0457   float phase = 0.0;
0458   float rms = 0.0;
0459   if (fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel) {
0460     nhits = 4;
0461     phase = 4.5;
0462     rms = 6.5;
0463   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap) {
0464     nhits = 4;
0465     phase = 9.3;
0466     rms = 7.8;
0467   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0468     nhits = 4;
0469     phase = 8.6;
0470     rms = 2.3;
0471   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenForward) {
0472     nhits = 4;
0473     phase = 12.4;
0474     rms = 12.29;
0475   }
0476   HcalTimingParam result(fId.rawId(), nhits, phase, rms);
0477 
0478   return result;
0479 }
0480 
0481 #define EMAP_NHBHECR 9
0482 #define EMAP_NHFCR 3
0483 #define EMAP_NHOCR 4
0484 #define EMAP_NFBR 8
0485 #define EMAP_NFCH 3
0486 #define EMAP_NHTRS 3
0487 #define EMAP_NHSETS 4
0488 #define EMAP_NTOPBOT 2
0489 #define EMAP_NHTRSHO 4
0490 #define EMAP_NHSETSHO 3
0491 
0492 std::unique_ptr<HcalDcsMap> HcalDbHardcode::makeHardcodeDcsMap() const {
0493   HcalDcsMapAddons::Helper dcs_map_helper;
0494   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalBarrel, -16, 1, 1),
0495                                  HcalDcsDetId(HcalDcsBarrel, -1, 1, HcalDcsDetId::HV, 2));
0496   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalForward, -41, 3, 1),
0497                                  HcalDcsDetId(HcalDcsForward, -1, 1, HcalDcsDetId::DYN8, 1));
0498   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalForward, -26, 25, 2),
0499                                  HcalDcsDetId(HcalDcsForward, -1, 7, HcalDcsDetId::HV, 1));
0500   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalBarrel, -15, 68, 1),
0501                                  HcalDcsDetId(HcalDcsBarrel, -1, 18, HcalDcsDetId::HV, 3));
0502   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalOuter, -14, 1, 4),
0503                                  HcalDcsDetId(HcalDcsOuter, -2, 2, HcalDcsDetId::HV, 4));
0504   dcs_map_helper.mapGeomId2DcsId(HcalDetId(HcalForward, 41, 71, 2),
0505                                  HcalDcsDetId(HcalDcsForward, 1, 4, HcalDcsDetId::DYN8, 3));
0506   return std::make_unique<HcalDcsMap>(dcs_map_helper);
0507 }
0508 
0509 std::unique_ptr<HcalElectronicsMap> HcalDbHardcode::makeHardcodeMap(const std::vector<HcalGenericDetId>& cells) const {
0510   static const int kUTCAMask = 0x4000000;         //set bit 26 for uTCA version
0511   static const int kLinearIndexMax = 0x7FFFF;     //19 bits
0512   static const int kTriggerBitMask = 0x02000000;  //2^25
0513   uint32_t counter = 0;
0514   uint32_t counterTrig = 0;
0515   HcalElectronicsMapAddons::Helper emapHelper;
0516   for (const auto& fId : cells) {
0517     if (fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel ||
0518         fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap ||
0519         fId.genericSubdet() == HcalGenericDetId::HcalGenForward ||
0520         fId.genericSubdet() == HcalGenericDetId::HcalGenOuter || fId.genericSubdet() == HcalGenericDetId::HcalGenZDC) {
0521       ++counter;
0522       assert(counter < kLinearIndexMax);
0523       uint32_t raw = counter;
0524       raw |= kUTCAMask;
0525       HcalElectronicsId elId(raw);
0526       emapHelper.mapEId2chId(elId, fId);
0527     } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenTriggerTower) {
0528       ++counterTrig;
0529       assert(counterTrig < kLinearIndexMax);
0530       uint32_t raw = counterTrig;
0531       raw |= kUTCAMask | kTriggerBitMask;
0532       HcalElectronicsId elId(raw);
0533       emapHelper.mapEId2tId(elId, fId);
0534     }
0535   }
0536   return std::make_unique<HcalElectronicsMap>(emapHelper);
0537 }
0538 
0539 std::unique_ptr<HcalFrontEndMap> HcalDbHardcode::makeHardcodeFrontEndMap(
0540     const std::vector<HcalGenericDetId>& cells) const {
0541   HcalFrontEndMapAddons::Helper emapHelper;
0542   std::stringstream mystream;
0543   std::string detector[5] = {"XX", "HB", "HE", "HO", "HF"};
0544   for (const auto& fId : cells) {
0545     if (fId.isHcalDetId()) {
0546       HcalDetId id = HcalDetId(fId.rawId());
0547       HcalSubdetector subdet = id.subdet();
0548       int ieta = id.ietaAbs();
0549       int iside = id.zside();
0550       int iphi = id.iphi();
0551       std::string det, rbx;
0552       int irm(0);
0553       char tempbuff[30];
0554       char sidesign = (iside == -1) ? 'M' : 'P';
0555       if (subdet == HcalBarrel || subdet == HcalEndcap) {
0556         det = detector[subdet];
0557         irm = (iphi + 1) % 4 + 1;
0558         int iwedge(0);
0559         if (ieta >= 21 && (irm == 1 || irm == 3))
0560           iwedge = (iphi + 1 + irm + 1) / 4;
0561         else
0562           iwedge = (iphi + irm + 1) / 4;
0563         if (iwedge > 18)
0564           iwedge -= 18;
0565         sprintf(tempbuff, "%s%c%2.2i%c", det.c_str(), sidesign, iwedge, '\0');
0566         mystream << tempbuff;
0567         rbx = mystream.str();
0568         mystream.str("");
0569         emapHelper.loadObject(id, irm, rbx);
0570       } else if (subdet == HcalForward) {
0571         det = detector[subdet];
0572         int hfphi(0);
0573         if ((iside == 1 && ieta == 40) || (iside == -1 && ieta == 41)) {
0574           irm = ((iphi + 1) / 2) % 36 + 1;
0575           hfphi = ((iphi + 1) / 6) % 12 + 1;
0576         } else {
0577           irm = (iphi + 1) / 2;
0578           hfphi = (iphi - 1) / 6 + 1;
0579         }
0580         irm = (irm - 1) % 3 + 1;
0581         sprintf(tempbuff, "%s%c%2.2i%c", det.c_str(), sidesign, hfphi, '\0');
0582         mystream << tempbuff;
0583         rbx = mystream.str();
0584         mystream.str("");
0585         emapHelper.loadObject(id, irm, rbx);
0586       } else if (subdet == HcalOuter) {
0587         det = detector[subdet];
0588         int ring(0), sector(0);
0589         if (ieta <= 4)
0590           ring = 0;
0591         else if (ieta >= 5 && ieta <= 10)
0592           ring = 1;
0593         else
0594           ring = 2;
0595         for (int i = -2; i < iphi; i += 6)
0596           sector++;
0597         if (sector > 12)
0598           sector = 1;
0599         irm = ((iphi + 1) / 2) % 6 + 1;
0600         if (ring != 0 && sector % 2 != 0)
0601           sector++;
0602         if (ring == 0)
0603           sprintf(tempbuff, "%s%i%2.2d", det.c_str(), ring, sector);
0604         else
0605           sprintf(tempbuff, "%s%i%c%2.2d", det.c_str(), ring, sidesign, sector);
0606         mystream << tempbuff;
0607         rbx = mystream.str();
0608         mystream.str("");
0609         emapHelper.loadObject(id, irm, rbx);
0610       }
0611     }
0612   }
0613   return std::make_unique<HcalFrontEndMap>(emapHelper);
0614 }
0615 
0616 int HcalDbHardcode::getLayersInDepth(int ieta, int depth, const HcalTopology* topo) {
0617   //check for cached value
0618   auto eta_depth_pair = std::make_pair(ieta, depth);
0619   auto nLayers = theLayersInDepths_.find(eta_depth_pair);
0620   if (nLayers != theLayersInDepths_.end()) {
0621     return nLayers->second;
0622   } else {
0623     std::vector<int> segmentation;
0624     topo->getDepthSegmentation(ieta, segmentation);
0625     //assume depth segmentation vector is sorted
0626     int nLayersInDepth = std::distance(std::lower_bound(segmentation.begin(), segmentation.end(), depth),
0627                                        std::upper_bound(segmentation.begin(), segmentation.end(), depth));
0628     theLayersInDepths_.insert(std::make_pair(eta_depth_pair, nLayersInDepth));
0629     return nLayersInDepth;
0630   }
0631 }
0632 
0633 bool HcalDbHardcode::isHEPlan1(HcalGenericDetId fId) const {
0634   if (fId.isHcalDetId()) {
0635     HcalDetId hid(fId);
0636     //special mixed case for HE 2017
0637     if (hid.zside() == 1 && (hid.iphi() == 63 || hid.iphi() == 64 || hid.iphi() == 65 || hid.iphi() == 66))
0638       return true;
0639   }
0640   return false;
0641 }
0642 
0643 HcalSiPMParameter HcalDbHardcode::makeHardcodeSiPMParameter(HcalGenericDetId fId,
0644                                                             const HcalTopology* topo,
0645                                                             double intlumi) {
0646   // SiPMParameter defined for each DetId the following quantities:
0647   //  SiPM type, PhotoElectronToAnalog, Dark Current, two auxiliary words
0648   //  (the second of those containing float noise correlation coefficient
0649   //  These numbers come from some measurements done with SiPMs
0650   // rule for type: cells with >4 layers use larger device (3.3mm diameter), otherwise 2.8mm
0651   HcalSiPMType theType = HcalNoSiPM;
0652   double thePe2fC = getParameters(fId).photoelectronsToAnalog();
0653   double theDC = getParameters(fId).darkCurrent(0, intlumi);
0654   double theNoiseCN = getParameters(fId).noiseCorrelation(0);
0655   if (fId.genericSubdet() == HcalGenericDetId::HcalGenBarrel) {
0656     if (useHBUpgrade_) {
0657       HcalDetId hid(fId);
0658       int nLayersInDepth = getLayersInDepth(hid.ietaAbs(), hid.depth(), topo);
0659       if (nLayersInDepth > 4) {
0660         theType = HcalHBHamamatsu2;
0661         theDC = getParameters(fId).darkCurrent(1, intlumi);
0662         theNoiseCN = getParameters(fId).noiseCorrelation(1);
0663       } else {
0664         theType = HcalHBHamamatsu1;
0665         theDC = getParameters(fId).darkCurrent(0, intlumi);
0666         theNoiseCN = getParameters(fId).noiseCorrelation(0);
0667       }
0668     } else
0669       theType = HcalHPD;
0670   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenEndcap) {
0671     if (useHEUpgrade_ || (testHEPlan1_ && isHEPlan1(fId))) {
0672       HcalDetId hid(fId);
0673       int nLayersInDepth = getLayersInDepth(hid.ietaAbs(), hid.depth(), topo);
0674       if (nLayersInDepth > 4) {
0675         theType = HcalHEHamamatsu2;
0676         theDC = getParameters(fId).darkCurrent(1, intlumi);
0677         theNoiseCN = getParameters(fId).noiseCorrelation(1);
0678       } else {
0679         theType = HcalHEHamamatsu1;
0680         theDC = getParameters(fId).darkCurrent(0, intlumi);
0681         theNoiseCN = getParameters(fId).noiseCorrelation(0);
0682       }
0683     } else
0684       theType = HcalHPD;
0685   } else if (fId.genericSubdet() == HcalGenericDetId::HcalGenOuter) {
0686     if (useHOUpgrade_)
0687       theType = HcalHOHamamatsu;
0688     else
0689       theType = HcalHPD;
0690   }
0691 
0692   return HcalSiPMParameter(fId.rawId(), theType, thePe2fC, theDC, 0, (float)theNoiseCN);
0693 }
0694 
0695 std::unique_ptr<HcalSiPMCharacteristics> HcalDbHardcode::makeHardcodeSiPMCharacteristics() const {
0696   // SiPMCharacteristics are constants for each type of SiPM:
0697   // Type, # of pixels, 3 parameters for non-linearity, cross talk parameter, ..
0698   // Obtained from data sheet and measurements
0699   // types (in order): HcalHOZecotek=1, HcalHOHamamatsu, HcalHEHamamatsu1, HcalHEHamamatsu2, HcalHBHamamatsu1, HcalHBHamamatsu2, HcalHPD
0700   HcalSiPMCharacteristicsAddons::Helper sipmHelper;
0701   for (unsigned ip = 0; ip < theSiPMCharacteristics_.size(); ++ip) {
0702     auto& ps = theSiPMCharacteristics_[ip];
0703     sipmHelper.loadObject(ip + 1,
0704                           ps.getParameter<int>("pixels"),
0705                           ps.getParameter<double>("nonlin1"),
0706                           ps.getParameter<double>("nonlin2"),
0707                           ps.getParameter<double>("nonlin3"),
0708                           ps.getParameter<double>("crosstalk"),
0709                           0,
0710                           0);
0711   }
0712   return std::make_unique<HcalSiPMCharacteristics>(sipmHelper);
0713 }
0714 
0715 HcalTPChannelParameter HcalDbHardcode::makeHardcodeTPChannelParameter(HcalGenericDetId fId) const {
0716   // For each detId parameters for trigger primitive
0717   // mask for channel validity and self trigger information, fine grain
0718   // bit information and auxiliary words
0719   uint32_t bitInfo = ((44 << 16) | 30);
0720   return HcalTPChannelParameter(fId.rawId(), 0, bitInfo, 0, 0);
0721 }
0722 
0723 void HcalDbHardcode::makeHardcodeTPParameters(HcalTPParameters& tppar) const {
0724   // Parameters for a given TP algorithm:
0725   // FineGrain Algorithm Version for HBHE, ADC threshold fof TDC mask of HF,
0726   // TDC mask for HF, Self Trigger bits, auxiliary words
0727   tppar.loadObject(0, 0, 0xFFFFFFFFFFFFFFFF, 0, 0, 0);
0728 }