Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:42:25

0001 /*
0002  *  See header file for a description of this class.
0003  *
0004  *  \author G. Mila - INFN Torino
0005  */
0006 
0007 #include "CalibMuon/DTCalibration/test/DBTools/FakeTTrig.h"
0008 
0009 // Framework
0010 #include "FWCore/Framework/interface/EventSetup.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/LuminosityBlock.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Framework/interface/ESHandle.h"
0015 #include "CalibMuon/DTCalibration/test/DBTools/DTCalibrationMap.h"
0016 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
0017 
0018 // Geometry
0019 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0020 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0021 #include "Geometry/DTGeometry/interface/DTSuperLayer.h"
0022 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
0023 #include "Geometry/DTGeometry/interface/DTTopology.h"
0024 
0025 // Database
0026 #include "CondFormats/DTObjects/interface/DTTtrig.h"
0027 #include "CondFormats/DataRecord/interface/DTTtrigRcd.h"
0028 
0029 //Random generator
0030 #include "FWCore/ServiceRegistry/interface/Service.h"
0031 #include "FWCore/Utilities/interface/Exception.h"
0032 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0033 #include "CLHEP/Random/RandGaussQ.h"
0034 
0035 // DTDigitizer
0036 #include "CalibMuon/DTDigiSync/interface/DTTTrigSyncFactory.h"
0037 #include "CalibMuon/DTDigiSync/interface/DTTTrigBaseSync.h"
0038 
0039 using namespace std;
0040 using namespace edm;
0041 
0042 FakeTTrig::FakeTTrig(const ParameterSet& pset) : dataBaseWriteWasDone(false) {
0043   cout << "[FakeTTrig] Constructor called! " << endl;
0044 
0045   // further configurable smearing
0046   smearing = pset.getUntrackedParameter<double>("smearing");
0047   ttrigToken_ =
0048       esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")));
0049   dtGeomToken_ = esConsumes<edm::Transition::BeginRun>();
0050   // get random engine
0051   edm::Service<edm::RandomNumberGenerator> rng;
0052   if (!rng.isAvailable()) {
0053     throw cms::Exception("Configuration") << "RandomNumberGeneratorService for DTFakeTTrigDB missing in cfg file";
0054   }
0055   ps = pset;
0056 }
0057 
0058 FakeTTrig::~FakeTTrig() { cout << "[FakeTTrig] Destructor called! " << endl; }
0059 
0060 void FakeTTrig::beginRun(const edm::Run&, const EventSetup& setup) {
0061   cout << "[FakeTTrig] entered into beginRun! " << endl;
0062   muonGeom = setup.getHandle(dtGeomToken_);
0063 
0064   // Get the tTrig reference map
0065   if (ps.getUntrackedParameter<bool>("readDB", true))
0066     tTrigMapRef = setup.getHandle(ttrigToken_);
0067 }
0068 
0069 void FakeTTrig::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
0070   if (!dataBaseWriteWasDone) {
0071     dataBaseWriteWasDone = true;
0072 
0073     cout << "[FakeTTrig] entered into beginLuminosityBlock! " << endl;
0074 
0075     edm::Service<edm::RandomNumberGenerator> rng;
0076     CLHEP::HepRandomEngine* engine = &rng->getEngine(lumi.index());
0077 
0078     // Get the superlayers and layers list
0079     vector<const DTSuperLayer*> dtSupLylist = muonGeom->superLayers();
0080     // Create the object to be written to DB
0081     DTTtrig tTrigMap;
0082 
0083     for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
0084       // get the time of fly
0085       double timeOfFly = tofComputation(*sl);
0086       // get the time of wire propagation
0087       double timeOfWirePropagation = wirePropComputation(*sl);
0088       // get the gaussian smearing
0089       double gaussianSmearing = CLHEP::RandGaussQ::shoot(engine, 0., smearing);
0090       // get the fake tTrig pedestal
0091       double pedestral = ps.getUntrackedParameter<double>("fakeTTrigPedestal", 500);
0092 
0093       if (ps.getUntrackedParameter<bool>("readDB", true)) {
0094         tTrigMapRef->get((*sl)->id(), tTrigRef, tTrigRMSRef, kFactorRef, DTTimeUnits::ns);
0095         // pedestral = tTrigRef;
0096         pedestral = tTrigRef + kFactorRef * tTrigRMSRef;
0097       }
0098 
0099       DTSuperLayerId slId = (*sl)->id();
0100       // if the FakeTtrig has to be smeared with a Gaussian
0101       double fakeTTrig = pedestral + timeOfFly + timeOfWirePropagation + gaussianSmearing;
0102       // if the FakeTtrig is scaled of a number of bunch crossing
0103       //  double fakeTTrig = pedestral - 75.;
0104       tTrigMap.set(slId, fakeTTrig, 0, 0, DTTimeUnits::ns);
0105     }
0106 
0107     // Write the object in the DB
0108     cout << "[FakeTTrig] Writing ttrig object to DB!" << endl;
0109     string record = "DTTtrigRcd";
0110     DTCalibDBUtils::writeToDB<DTTtrig>(record, tTrigMap);
0111   }
0112 }
0113 
0114 void FakeTTrig::endJob() { cout << "[FakeTTrig] entered into endJob! " << endl; }
0115 
0116 double FakeTTrig::tofComputation(const DTSuperLayer* superlayer) {
0117   double tof = 0;
0118   const double cSpeed = 29.9792458;  // cm/ns
0119 
0120   if (ps.getUntrackedParameter<bool>("useTofCorrection", true)) {
0121     LocalPoint localPos(0, 0, 0);
0122     double flight = superlayer->surface().toGlobal(localPos).mag();
0123     tof = flight / cSpeed;
0124   }
0125 
0126   return tof;
0127 }
0128 
0129 double FakeTTrig::wirePropComputation(const DTSuperLayer* superlayer) {
0130   double delay = 0;
0131   double theVPropWire = ps.getUntrackedParameter<double>("vPropWire", 24.4);  // cm/ns
0132 
0133   if (ps.getUntrackedParameter<bool>("useWirePropCorrection", true)) {
0134     DTLayerId lId = DTLayerId(superlayer->id(), 1);
0135     float halfL = superlayer->layer(lId)->specificTopology().cellLenght() / 2;
0136     delay = halfL / theVPropWire;
0137   }
0138 
0139   return delay;
0140 }