Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:34:56

0001 /*
0002  *  See header file for a description of this class.
0003  */
0004 
0005 #include "DTT0Correction.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/Framework/interface/ESHandle.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0013 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0014 #include "Geometry/DTGeometry/interface/DTSuperLayer.h"
0015 
0016 #include "CondFormats/DTObjects/interface/DTT0.h"
0017 #include "CondFormats/DataRecord/interface/DTT0Rcd.h"
0018 
0019 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
0020 
0021 #include "CalibMuon/DTCalibration/interface/DTT0CorrectionFactory.h"
0022 #include "CalibMuon/DTCalibration/interface/DTT0BaseCorrection.h"
0023 #include "FWCore/Framework/interface/ConsumesCollector.h"
0024 
0025 #include <iostream>
0026 #include <fstream>
0027 
0028 using namespace edm;
0029 using namespace std;
0030 
0031 DTT0Correction::DTT0Correction(const ParameterSet& pset)
0032     : correctionAlgo_{DTT0CorrectionFactory::get()->create(pset.getParameter<string>("correctionAlgo"),
0033                                                            pset.getParameter<ParameterSet>("correctionAlgoConfig"),
0034                                                            consumesCollector())},
0035       dtGeomToken_(esConsumes<edm::Transition::BeginRun>()),
0036       t0Token_(esConsumes<edm::Transition::BeginRun>()) {
0037   LogVerbatim("Calibration") << "[DTT0Correction] Constructor called" << endl;
0038 }
0039 
0040 DTT0Correction::~DTT0Correction() { LogVerbatim("Calibration") << "[DTT0Correction] Destructor called" << endl; }
0041 
0042 void DTT0Correction::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
0043   // Get t0 record from DB
0044   ESHandle<DTT0> t0H;
0045   t0H = setup.getHandle(t0Token_);
0046   t0Map_ = &setup.getData(t0Token_);
0047   LogVerbatim("Calibration") << "[DTT0Correction]: T0 version: " << t0H->version() << endl;
0048 
0049   // Get geometry from Event Setup
0050   muonGeom_ = setup.getHandle(dtGeomToken_);
0051 
0052   // Pass EventSetup to correction Algo
0053   correctionAlgo_->setES(setup);
0054 }
0055 
0056 void DTT0Correction::endJob() {
0057   // Create the object to be written to DB
0058   DTT0 t0NewMap;
0059 
0060   // Loop over all channels
0061   for (vector<const DTSuperLayer*>::const_iterator sl = muonGeom_->superLayers().begin();
0062        sl != muonGeom_->superLayers().end();
0063        ++sl) {
0064     for (vector<const DTLayer*>::const_iterator layer = (*sl)->layers().begin(); layer != (*sl)->layers().end();
0065          ++layer) {
0066       // Access layer topology
0067       const DTTopology& dtTopo = (*layer)->specificTopology();
0068       const int firstWire = dtTopo.firstChannel();
0069       const int lastWire = dtTopo.lastChannel();
0070       //const int nWires = dtTopo.channels();
0071 
0072       //Loop on wires
0073       for (int wire = firstWire; wire <= lastWire; ++wire) {
0074         DTWireId wireId((*layer)->id(), wire);
0075 
0076         // Get old value from DB
0077         float t0Mean, t0RMS;
0078         int status = t0Map_->get(wireId, t0Mean, t0RMS, DTTimeUnits::counts);
0079 
0080         // Compute new t0 for this wire
0081         try {
0082           dtCalibration::DTT0Data t0Corr = correctionAlgo_->correction(wireId);
0083           float t0MeanNew = t0Corr.mean;
0084           float t0RMSNew = t0Corr.rms;
0085           t0NewMap.set(wireId, t0MeanNew, t0RMSNew, DTTimeUnits::counts);
0086 
0087           LogVerbatim("Calibration") << "New t0 for: " << wireId << " mean from " << t0Mean << " to " << t0MeanNew
0088                                      << " rms from " << t0RMS << " to " << t0RMSNew << endl;
0089         } catch (cms::Exception& e) {
0090           LogError("Calibration") << e.explainSelf();
0091           // Set db to the old value, if it was there in the first place
0092           if (!status) {
0093             t0NewMap.set(wireId, t0Mean, t0RMS, DTTimeUnits::counts);
0094             LogVerbatim("Calibration") << "Keep old t0 for: " << wireId << " mean " << t0Mean << " rms " << t0RMS
0095                                        << endl;
0096           }
0097           continue;
0098         }
0099       }  // End of loop on wires
0100     }  // End of loop on layers
0101   }  // End of loop on superlayers
0102 
0103   //Write object to DB
0104   LogVerbatim("Calibration") << "[DTT0Correction]: Writing t0 object to DB!" << endl;
0105   string record = "DTT0Rcd";
0106   DTCalibDBUtils::writeToDB<DTT0>(record, t0NewMap);
0107 }