Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-24 09:53:32

0001 #ifndef SimPPS_PPSPixelDigiProducer_PPSPixelDigiProducer_h
0002 #define SimPPS_PPSPixelDigiProducer_PPSPixelDigiProducer_h
0003 
0004 // -*- C++ -*-
0005 //
0006 // Package:    PPSPixelDigiProducer
0007 // Class:      CTPPSPixelDigiProducer
0008 //
0009 /**\class CTPPSPixelDigiProducer PPSPixelDigiProducer.cc SimPPS/PPSPixelDigiProducer/plugins/PPSPixelDigiProducer.cc
0010 
0011  Description: <one line class summary>
0012 
0013  Implementation:
0014      <Notes on implementation>
0015 */
0016 //
0017 // Original Author:  F.Ferro
0018 //
0019 
0020 // system include files
0021 #include <memory>
0022 #include <vector>
0023 #include <map>
0024 #include <string>
0025 
0026 // user include files
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/stream/EDProducer.h"
0029 
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/EventSetup.h"
0032 #include "FWCore/Framework/interface/MakerMacros.h"
0033 
0034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0036 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0037 
0038 //  ****  CTPPS
0039 #include "DataFormats/CTPPSDigi/interface/CTPPSPixelDigi.h"
0040 #include "DataFormats/CTPPSDigi/interface/CTPPSPixelDigiCollection.h"
0041 
0042 #include "SimPPS/PPSPixelDigiProducer/interface/RPixDetDigitizer.h"
0043 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
0044 
0045 #include "DataFormats/Common/interface/DetSet.h"
0046 
0047 // DB
0048 #include "CondFormats/DataRecord/interface/CTPPSPixelDAQMappingRcd.h"
0049 #include "CondFormats/DataRecord/interface/CTPPSPixelAnalysisMaskRcd.h"
0050 #include "CondFormats/DataRecord/interface/CTPPSPixelGainCalibrationsRcd.h"
0051 #include "CondFormats/PPSObjects/interface/CTPPSPixelDAQMapping.h"
0052 #include "CondFormats/PPSObjects/interface/CTPPSPixelAnalysisMask.h"
0053 #include "CondFormats/PPSObjects/interface/CTPPSPixelGainCalibrations.h"
0054 
0055 #include "CondFormats/PPSObjects/interface/PPSPixelTopology.h"
0056 #include "CondFormats/DataRecord/interface/PPSPixelTopologyRcd.h"
0057 // user include files
0058 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0059 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0060 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0061 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
0062 #include <cstdlib>  // I need it for random numbers
0063 //needed for the geometry:
0064 #include "FWCore/Framework/interface/EventSetup.h"
0065 #include "FWCore/Framework/interface/ESHandle.h"
0066 
0067 #include "DataFormats/Common/interface/DetSetVector.h"
0068 
0069 //Random Number
0070 #include "FWCore/ServiceRegistry/interface/Service.h"
0071 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0072 #include "FWCore/Utilities/interface/Exception.h"
0073 #include "CLHEP/Random/RandomEngine.h"
0074 
0075 namespace CLHEP {
0076   class HepRandomEngine;
0077 }
0078 
0079 class CTPPSPixelDigiProducer : public edm::stream::EDProducer<> {
0080 public:
0081   explicit CTPPSPixelDigiProducer(const edm::ParameterSet&);
0082   ~CTPPSPixelDigiProducer() override;
0083   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0084 
0085 private:
0086   void produce(edm::Event&, const edm::EventSetup&) override;
0087 
0088   // ----------member data ---------------------------
0089   std::vector<std::string> RPix_hit_containers_;
0090   typedef std::map<unsigned int, std::vector<PSimHit>> simhit_map;
0091   typedef simhit_map::iterator simhit_map_iterator;
0092 
0093   edm::ParameterSet conf_;
0094 
0095   std::map<uint32_t, std::unique_ptr<RPixDetDigitizer>> theAlgoMap;  //DetId = uint32_t
0096 
0097   CLHEP::HepRandomEngine* rndEngine_ = nullptr;
0098   int verbosity_;
0099 
0100   edm::EDGetTokenT<CrossingFrame<PSimHit>> tokenCrossingFramePPSPixel;
0101   edm::ESGetToken<CTPPSPixelGainCalibrations, CTPPSPixelGainCalibrationsRcd> gainCalibESToken_;
0102   edm::ESGetToken<PPSPixelTopology, PPSPixelTopologyRcd> thePixelTopologyToken_;
0103 };
0104 
0105 CTPPSPixelDigiProducer::CTPPSPixelDigiProducer(const edm::ParameterSet& conf)
0106     : conf_(conf), gainCalibESToken_(esConsumes()), thePixelTopologyToken_(esConsumes()) {
0107   produces<edm::DetSetVector<CTPPSPixelDigi>>();
0108 
0109   // register data to consume
0110   tokenCrossingFramePPSPixel = consumes<CrossingFrame<PSimHit>>(edm::InputTag("mix", "g4SimHitsCTPPSPixelHits"));
0111 
0112   RPix_hit_containers_.clear();
0113   RPix_hit_containers_ = conf.getParameter<std::vector<std::string>>("ROUList");
0114   verbosity_ = conf.getParameter<int>("RPixVerbosity");
0115 }
0116 
0117 CTPPSPixelDigiProducer::~CTPPSPixelDigiProducer() {}
0118 
0119 void CTPPSPixelDigiProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0120   edm::ParameterSetDescription desc;
0121   // all distances in [mm]
0122   // RPDigiProducer
0123   desc.add<std::vector<std::string>>("ROUList", {"CTPPSPixelHits"});
0124   desc.add<int>("RPixVerbosity", 0);
0125   desc.add<bool>("CTPPSPixelDigiSimHitRelationsPersistence",
0126                  false);  // save links betweend digi, clusters and OSCAR/Geant4 hits
0127 
0128   // RPDetDigitizer
0129   desc.add<double>("RPixEquivalentNoiseCharge", 1000.0);
0130   desc.add<bool>("RPixNoNoise", false);
0131 
0132   // RPDisplacementGenerator
0133   desc.add<double>("RPixGeVPerElectron", 3.61e-09);
0134   desc.add<std::vector<double>>("RPixInterSmearing", {0.011});
0135   desc.add<bool>("RPixLandauFluctuations", true);
0136   desc.add<int>("RPixChargeDivisions", 20);
0137   desc.add<double>("RPixDeltaProductionCut", 0.120425);  // [MeV]
0138 
0139   // RPixChargeShare
0140   desc.add<std::string>("ChargeMapFile2E", "SimPPS/PPSPixelDigiProducer/data/PixelChargeMap.txt");
0141   desc.add<std::string>("ChargeMapFile2E_2X", "SimPPS/PPSPixelDigiProducer/data/PixelChargeMap_2X.txt");
0142   desc.add<std::string>("ChargeMapFile2E_2Y", "SimPPS/PPSPixelDigiProducer/data/PixelChargeMap_2Y.txt");
0143   desc.add<std::string>("ChargeMapFile2E_2X2Y", "SimPPS/PPSPixelDigiProducer/data/PixelChargeMap_2X2Y.txt");
0144   desc.add<double>(
0145       "RPixCoupling",
0146       0.250);  // fraction of the remaining charge going to the closer neighbour pixel. Value = 0.135, Value = 0.0 bypass the charge map and the charge sharing approach
0147 
0148   // RPixDummyROCSimulator
0149   desc.add<double>("RPixDummyROCThreshold", 1900.0);
0150   desc.add<double>("RPixDummyROCElectronPerADC", 135.0);  // 210.0 to be verified
0151   desc.add<int>("VCaltoElectronGain", 50);                // same values as in RPixDetClusterizer
0152   desc.add<int>("VCaltoElectronOffset", -411);            //
0153   desc.add<bool>("doSingleCalibration", false);           //
0154   desc.add<double>("RPixDeadPixelProbability", 0.001);
0155   desc.add<bool>("RPixDeadPixelSimulationOn", true);
0156 
0157   desc.add<std::string>("mixLabel", "mix");
0158   desc.add<std::string>("InputCollection", "g4SimHitsCTPPSPixelHits");
0159   descriptions.add("RPixDetDigitizer", desc);
0160 }
0161 
0162 // member functions
0163 //
0164 
0165 // ------------ method called to produce the data  ------------
0166 void CTPPSPixelDigiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0167   if (!rndEngine_) {
0168     edm::Service<edm::RandomNumberGenerator> rng;
0169     if (!rng.isAvailable()) {
0170       throw cms::Exception("Configuration")
0171           << "This class requires the RandomNumberGeneratorService\n"
0172              "which is not present in the configuration file.  You must add the service\n"
0173              "in the configuration file or remove the modules that require it.";
0174     }
0175     rndEngine_ = &(rng->getEngine(iEvent.streamID()));
0176   }
0177 
0178   // get calibration DB
0179   const auto& gainCalibration = iSetup.getData(gainCalibESToken_);
0180   const auto& thePixelTopology = iSetup.getData(thePixelTopologyToken_);
0181 
0182   // Step A: Get Inputs
0183   edm::Handle<CrossingFrame<PSimHit>> cf;
0184 
0185   iEvent.getByToken(tokenCrossingFramePPSPixel, cf);
0186 
0187   if (verbosity_) {
0188     edm::LogInfo("PPS") << "PixelDigiProducer \n\n=================== Starting SimHit access"
0189                         << "  ===================";
0190 
0191     MixCollection<PSimHit> col{cf.product(), std::pair(-0, 0)};
0192     MixCollection<PSimHit>::iterator cfi;
0193     int count = 0;
0194     for (cfi = col.begin(); cfi != col.end(); cfi++) {
0195       edm::LogInfo("PPS") << "PixelDigiProducer"
0196                           << " Hit " << count << " has tof " << cfi->timeOfFlight() << " trackid " << cfi->trackId()
0197                           << " bunchcr " << cfi.bunch() << " trigger " << cfi.getTrigger()
0198                           << ", from EncodedEventId: " << cfi->eventId().bunchCrossing() << " "
0199                           << cfi->eventId().event() << " bcr from MixCol " << cfi.bunch();
0200       edm::LogInfo("PPS") << " PixelDigiProducer Hit: " << (*cfi) << "  " << cfi->exitPoint();
0201       count++;
0202     }
0203   }
0204 
0205   MixCollection<PSimHit> allRPixHits{cf.product(), std::pair(0, 0)};
0206 
0207   if (verbosity_)
0208     edm::LogInfo("PPS") << "PixelDigiProducer Input MixCollection size = " << allRPixHits.size();
0209 
0210   //Loop on PSimHit
0211   simhit_map SimHitMap;
0212   SimHitMap.clear();
0213 
0214   MixCollection<PSimHit>::iterator isim;
0215   for (isim = allRPixHits.begin(); isim != allRPixHits.end(); ++isim) {
0216     SimHitMap[(*isim).detUnitId()].push_back((*isim));
0217   }
0218 
0219   // Step B: LOOP on hits in event
0220   std::vector<edm::DetSet<CTPPSPixelDigi>> theDigiVector;
0221   theDigiVector.reserve(400);
0222   theDigiVector.clear();
0223 
0224   for (simhit_map_iterator it = SimHitMap.begin(); it != SimHitMap.end(); ++it) {
0225     edm::DetSet<CTPPSPixelDigi> digi_collector(it->first);
0226 
0227     if (theAlgoMap.find(it->first) == theAlgoMap.end()) {
0228       theAlgoMap[it->first] = std::make_unique<RPixDetDigitizer>(
0229           conf_, *rndEngine_, it->first, thePixelTopology);  //a digitizer for any detector
0230     }
0231 
0232     std::vector<int> input_links;
0233     std::vector<std::vector<std::pair<int, double>>> output_digi_links;  // links to simhits
0234 
0235     theAlgoMap.at(it->first)->run(
0236         SimHitMap[it->first], input_links, digi_collector.data, output_digi_links, &gainCalibration, &thePixelTopology);
0237 
0238     if (!digi_collector.data.empty()) {
0239       theDigiVector.push_back(digi_collector);
0240     }
0241   }
0242 
0243   std::unique_ptr<edm::DetSetVector<CTPPSPixelDigi>> digi_output(new edm::DetSetVector<CTPPSPixelDigi>(theDigiVector));
0244 
0245   if (verbosity_) {
0246     edm::LogInfo("PPS") << "PixelDigiProducer digi_output->size()=" << digi_output->size();
0247   }
0248 
0249   iEvent.put(std::move(digi_output));
0250 }
0251 
0252 DEFINE_FWK_MODULE(CTPPSPixelDigiProducer);
0253 
0254 #endif