Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include "CalibTracker/SiStripESProducers/plugins/fake/SiStripApvGainBuilderFromTag.h"
#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

#include "SiStripFakeAPVParameters.h"

SiStripApvGainBuilderFromTag::SiStripApvGainBuilderFromTag(const edm::ParameterSet& iConfig)
    : printdebug_(iConfig.getUntrackedParameter<uint32_t>("printDebug", 1)), pset_(iConfig) {
  tTopoToken_ = esConsumes<TrackerTopology, TrackerTopologyRcd>();
  tGeomToken_ = esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>();
  inputApvGainToken_ = esConsumes<SiStripApvGain, SiStripApvGainRcd>();
}

void SiStripApvGainBuilderFromTag::analyze(const edm::Event& evt, const edm::EventSetup& iSetup) {
  const auto& tTopo = iSetup.getData(tTopoToken_);
  const auto& tGeom = iSetup.getData(tGeomToken_);

  //   unsigned int run=evt.id().run();

  std::string genMode = pset_.getParameter<std::string>("genMode");
  bool applyTuning = pset_.getParameter<bool>("applyTuning");

  double meanGain_ = pset_.getParameter<double>("MeanGain");
  double sigmaGain_ = pset_.getParameter<double>("SigmaGain");
  double minimumPosValue_ = pset_.getParameter<double>("MinPositiveGain");

  uint32_t printdebug_ = pset_.getUntrackedParameter<uint32_t>("printDebug", 5);

  //parameters for layer/disk level correction; not used if applyTuning=false
  SiStripFakeAPVParameters correct{pset_, "correct"};

  // Read the gain from the given tag
  const auto& inputApvGain = iSetup.getData(inputApvGainToken_);
  std::vector<uint32_t> inputDetIds;
  inputApvGain.getDetIds(inputDetIds);

  // Prepare the new object
  SiStripApvGain obj;

  uint32_t count = 0;
  for (const auto det : tGeom.detUnits()) {
    const StripGeomDetUnit* stripDet = dynamic_cast<const StripGeomDetUnit*>(det);
    if (stripDet != nullptr) {
      const DetId detid = stripDet->geographicalId();
      // Find if this DetId is in the input tag and if so how many are the Apvs for which it contains information
      SiStripApvGain::Range inputRange;
      size_t inputRangeSize = 0;
      if (find(inputDetIds.begin(), inputDetIds.end(), detid) != inputDetIds.end()) {
        inputRange = inputApvGain.getRange(detid);
        inputRangeSize = distance(inputRange.first, inputRange.second);
      }

      std::vector<float> theSiStripVector;
      for (unsigned short j = 0; j < (stripDet->specificTopology().nstrips() / 128); j++) {
        double gainValue = meanGain_;

        if (j < inputRangeSize) {
          gainValue = inputApvGain.getApvGain(j, inputRange);
          // cout << "Gain = " << gainValue <<" from input tag for DetId = " << detid.rawId() << " and apv = " << j << endl;
        }
        // else {
        //   cout << "No gain in input tag for DetId = " << detid << " and apv = " << j << " using value from cfg = " << gainValue << endl;
        // }

        // corrections at layer/disk level:
        SiStripFakeAPVParameters::index sl = SiStripFakeAPVParameters::getIndex(&tTopo, detid);
        //unsigned short nApvs = (stripDet->specificTopology().nstrips()/128);
        if (applyTuning) {
          double correction = correct.get(sl);
          gainValue *= correction;
        }

        // smearing:
        if (genMode == "gaussian") {
          gainValue = CLHEP::RandGauss::shoot(gainValue, sigmaGain_);
          if (gainValue <= minimumPosValue_)
            gainValue = minimumPosValue_;
        } else if (genMode != "default") {
          LogDebug("SiStripApvGain") << "ERROR: wrong genMode specifier : " << genMode
                                     << ", please select one of \"default\" or \"gaussian\"" << std::endl;
          exit(1);
        }

        if (count < printdebug_) {
          edm::LogInfo("SiStripApvGainGeneratorFromTag")
              << "detid: " << detid.rawId() << " Apv: " << j << " gain: " << gainValue << std::endl;
        }
        theSiStripVector.push_back(gainValue);
      }
      count++;
      SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
      if (!obj.put(detid, range))
        edm::LogError("SiStripApvGainGeneratorFromTag") << " detid already exists" << std::endl;
    }
  }

  //End now write data in DB
  edm::Service<cond::service::PoolDBOutputService> mydbservice;

  if (mydbservice.isAvailable()) {
    if (mydbservice->isNewTagRequest("SiStripApvGainRcd2")) {
      mydbservice->createOneIOV<SiStripApvGain>(obj, mydbservice->beginOfTime(), "SiStripApvGainRcd2");
    } else {
      mydbservice->appendOneIOV<SiStripApvGain>(obj, mydbservice->currentTime(), "SiStripApvGainRcd2");
    }
  } else {
    edm::LogError("SiStripApvGainBuilderFromTag") << "Service is unavailable" << std::endl;
  }
}