Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:31

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author G. Cerminara - INFN Torino
0006  */
0007 
0008 #include "ShiftTTrigDB.h"
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 
0012 #include "FWCore/Framework/interface/ESHandle.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0015 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0016 #include "Geometry/DTGeometry/interface/DTSuperLayer.h"
0017 
0018 #include "CondFormats/DTObjects/interface/DTTtrig.h"
0019 #include "CondFormats/DataRecord/interface/DTTtrigRcd.h"
0020 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
0021 
0022 #include <iostream>
0023 #include <fstream>
0024 
0025 using namespace edm;
0026 using namespace std;
0027 
0028 ShiftTTrigDB::ShiftTTrigDB(const ParameterSet& pset) {
0029   //Read the ttrig shifts
0030   shifts = pset.getParameter<vector<double> >("shifts");
0031   //Read the chambers to be shifted
0032   vector<ParameterSet> parameters = pset.getParameter<vector<ParameterSet> >("chambers");
0033 
0034   ttrigToken_ =
0035       esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")));
0036   dtGeomToken_ = esConsumes<edm::Transition::BeginRun>();
0037 
0038   int counter = 0;
0039   for (vector<ParameterSet>::iterator parameter = parameters.begin(); parameter != parameters.end(); ++parameter) {
0040     vector<int> chAddress;
0041     chAddress.push_back(parameter->getParameter<int>("wheel"));
0042     chAddress.push_back(parameter->getParameter<int>("sector"));
0043     chAddress.push_back(parameter->getParameter<int>("station"));
0044     chambers.push_back(chAddress);
0045     //Map the ttrig shift with the chamber addresses
0046     mapShiftsByChamber[chAddress] = shifts[counter];
0047     counter++;
0048   }
0049 
0050   debug = pset.getUntrackedParameter<bool>("debug", false);
0051   if (chambers.size() != shifts.size()) {
0052     cout << "[ShiftTTrigDB]: Wrong configuration: number of chambers different from number of shifts!! Aborting."
0053          << endl;
0054     abort();
0055   }
0056 }
0057 
0058 ShiftTTrigDB::~ShiftTTrigDB() {}
0059 
0060 void ShiftTTrigDB::beginRun(const edm::Run&, const EventSetup& setup) {
0061   ESHandle<DTTtrig> tTrig = setup.getHandle(ttrigToken_);
0062   tTrigMap = &*tTrig;
0063   cout << "[ShiftTTrigDB]: TTrig version: " << tTrig->version() << endl;
0064   muonGeom = setup.getHandle(dtGeomToken_);
0065 }
0066 
0067 void ShiftTTrigDB::endJob() {
0068   // Create the object to be written to DB
0069   DTTtrig tTrigNewMap;
0070   //Get the superlayers list
0071   vector<const DTSuperLayer*> dtSupLylist = muonGeom->superLayers();
0072 
0073   //Loop on all superlayers
0074   for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
0075     float ttrigMean = 0;
0076     float ttrigSigma = 0;
0077     float kFactor = 0;
0078     tTrigMap->get((*sl)->id(), ttrigMean, ttrigSigma, kFactor, DTTimeUnits::ns);
0079     bool ttrigShifted = false;
0080     //Loop on the chambers with ttrig to be shifted
0081     for (vector<vector<int> >::const_iterator ch = chambers.begin(); ch != chambers.end(); ++ch) {
0082       //Check the chamber address format
0083       if ((*ch).size() != 3) {
0084         cout << "[ShiftTTrigDB]: Wrong configuration: use three integer to indicate each chamber. Aborting." << endl;
0085         abort();
0086       }
0087       if (((*sl)->id().wheel() == (*ch)[0]) || (*ch)[0] == 999) {
0088         if (((*sl)->id().sector() == (*ch)[1]) || (*ch)[1] == 999) {
0089           if (((*sl)->id().station() == (*ch)[2]) || (*ch)[2] == 999) {
0090             //Compute new ttrig
0091             double newTTrigMean = ttrigMean + mapShiftsByChamber[(*ch)];
0092             //Store new ttrig in the new map
0093             tTrigNewMap.set((*sl)->id(), newTTrigMean, ttrigSigma, kFactor, DTTimeUnits::ns);
0094             ttrigShifted = true;
0095             if (debug) {
0096               cout << "Shifting SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0097             }
0098           }
0099         }
0100       }
0101     }  //End loop on chambers to be shifted
0102     if (!ttrigShifted) {
0103       //Store old ttrig in the new map
0104       tTrigNewMap.set((*sl)->id(), ttrigMean, ttrigSigma, kFactor, DTTimeUnits::ns);
0105       if (debug) {
0106         cout << "Copying  SL: " << (*sl)->id() << " ttrig " << ttrigMean << endl;
0107       }
0108     }
0109   }  //End of loop on superlayers
0110 
0111   //Write object to DB
0112   cout << "[ShiftTTrigDB]: Writing ttrig object to DB!" << endl;
0113   string record = "DTTtrigRcd";
0114   DTCalibDBUtils::writeToDB<DTTtrig>(record, tTrigNewMap);
0115 }