Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-30 22:37:57

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