Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-17 23:35:52

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author S. Maselli - INFN Torino
0006  */
0007 
0008 #include "CalibMuon/DTCalibration/plugins/DTTTrigCorrectionFirst.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 DTTTrigCorrectionFirst::DTTTrigCorrectionFirst(const ParameterSet& pset)
0029     : dtGeomToken_(esConsumes<edm::Transition::BeginRun>()),
0030       ttrigToken_(
0031           esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")))) {
0032   debug = pset.getUntrackedParameter<bool>("debug", false);
0033   ttrigMin = pset.getUntrackedParameter<double>("ttrigMin", 0);
0034   ttrigMax = pset.getUntrackedParameter<double>("ttrigMax", 5000);
0035   rmsLimit = pset.getUntrackedParameter<double>("rmsLimit", 5.);
0036 }
0037 
0038 DTTTrigCorrectionFirst::~DTTTrigCorrectionFirst() {}
0039 
0040 void DTTTrigCorrectionFirst::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
0041   ESHandle<DTTtrig> tTrig;
0042   tTrig = setup.getHandle(ttrigToken_);
0043   tTrigMap = &setup.getData(ttrigToken_);
0044   cout << "[DTTTrigCorrection]: TTrig version: " << tTrig->version() << endl;
0045   muonGeom = setup.getHandle(dtGeomToken_);
0046 }
0047 
0048 void DTTTrigCorrectionFirst::endJob() {
0049   // Create the object to be written to DB
0050   DTTtrig tTrigNewMap;
0051   //Get the superlayers list
0052   vector<const DTSuperLayer*> dtSupLylist = muonGeom->superLayers();
0053 
0054   //Loop on all superlayers to compute the mean
0055   double average = 0.;
0056   double average2 = 0.;
0057   double rms = 0.;
0058   double averageSigma = 0.;
0059   double average2Sigma = 0.;
0060   double counter = 0.;
0061   double averagekfactor = 0;
0062   float kfactor = 0;
0063 
0064   for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
0065     float ttrigMean = 0;
0066     float ttrigSigma = 0;
0067     tTrigMap->get((*sl)->id(), ttrigMean, ttrigSigma, kfactor, DTTimeUnits::ns);
0068     if (ttrigMean < ttrigMax && ttrigMean > ttrigMin) {
0069       average += ttrigMean;
0070       averageSigma += ttrigSigma;
0071       if (ttrigMean > 0)
0072         counter += 1.;
0073     }
0074   }  //End of loop on superlayers
0075 
0076   average = average / counter;
0077   averageSigma = averageSigma / counter;
0078   averagekfactor = kfactor;
0079 
0080   //  cout << " average counter "<< average << " "<< counter <<endl;
0081 
0082   for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
0083     float ttrigMean = 0;
0084     float ttrigSigma = 0;
0085     float kfactor = 0;
0086     tTrigMap->get((*sl)->id(), ttrigMean, ttrigSigma, kfactor, DTTimeUnits::ns);
0087     if (ttrigMean < ttrigMax && ttrigMean > ttrigMin) {
0088       average2 += (ttrigMean - average) * (ttrigMean - average);
0089       average2Sigma += (ttrigSigma - averageSigma) * (ttrigSigma - averageSigma);
0090     }
0091   }  //End of loop on superlayers
0092 
0093   rms = average2 / (counter - 1);
0094   rms = sqrt(rms);
0095   cout << "average averageSigma counter rms " << average << " " << averageSigma << " " << counter << " " << rms << endl;
0096 
0097   for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
0098     //Compute new ttrig
0099     double newTTrigMean = 0;
0100     double newTTrigSigma = 0;
0101     double newkfactor = 0;
0102     float tempttrigMean = 0;
0103     float tempttrigSigma = 0;
0104     float tempkfactor = 0;
0105     float ttrigMean = 0;
0106     float ttrigSigma = 0;
0107     float kfactor = 0;
0108     tTrigMap->get((*sl)->id(), ttrigMean, ttrigSigma, kfactor, DTTimeUnits::ns);
0109     int chamber = (*sl)->id().chamberId().station();
0110 
0111     //check if ttrigMean is similar to the mean
0112     if (abs(ttrigMean - average) < rmsLimit * rms) {
0113       newTTrigMean = ttrigMean;
0114       newTTrigSigma = ttrigSigma;
0115       newkfactor = kfactor;
0116     } else {
0117       // do not consider if ttrig == 0
0118       if (ttrigMean > 0) {
0119         //cout << "ttrig chamber " << ttrigMean <<" "<<chamber<<endl;
0120         if (((*sl)->id().superlayer()) == 1) {
0121           //cout << " superlayer " << ((*sl)->id().superlayer()) <<endl;
0122           DTSuperLayerId slId((*sl)->id().chamberId(), 3);
0123           tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0124           if (abs(tempttrigMean - average) < rmsLimit * rms) {
0125             newTTrigMean = tempttrigMean;
0126             newTTrigSigma = tempttrigSigma;
0127             newkfactor = tempkfactor;
0128             cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0129                  << " -> takes value of sl 3 " << newTTrigMean << endl;
0130           } else if (chamber == 4) {
0131             cout << "No correction possible within same chamber (sl1) " << endl;
0132             newTTrigMean = average;
0133             newTTrigSigma = averageSigma;
0134             newkfactor = averagekfactor;
0135             cout << "####### Bad SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0136           } else if (chamber != 4) {
0137             DTSuperLayerId slId((*sl)->id().chamberId(), 2);
0138             tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0139             if (abs(tempttrigMean - average) < rmsLimit * rms) {
0140               newTTrigMean = tempttrigMean;
0141               newTTrigSigma = tempttrigSigma;
0142               newkfactor = tempkfactor;
0143               cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0144                    << " -> takes value of sl 2 " << newTTrigMean << endl;
0145             } else {
0146               cout << "No correction possible within same chamber (sl1) " << endl;
0147               newTTrigMean = average;
0148               newTTrigSigma = averageSigma;
0149               newkfactor = averagekfactor;
0150               cout << "####### Bad SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0151             }
0152           }
0153         } else if (((*sl)->id().superlayer()) == 2) {
0154           //cout << " superlayer " << ((*sl)->id().superlayer()) <<endl;
0155           DTSuperLayerId slId((*sl)->id().chamberId(), 1);
0156           tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0157           if (abs(tempttrigMean - average) < rmsLimit * rms) {
0158             newTTrigMean = tempttrigMean;
0159             newTTrigSigma = tempttrigSigma;
0160             newkfactor = tempkfactor;
0161             cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0162                  << " -> takes value of sl 1 " << newTTrigMean << endl;
0163           } else {
0164             DTSuperLayerId slId((*sl)->id().chamberId(), 3);
0165             tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0166             if (abs(tempttrigMean - average) < rmsLimit * rms) {
0167               newTTrigMean = tempttrigMean;
0168               newTTrigSigma = tempttrigSigma;
0169               newkfactor = tempkfactor;
0170               cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0171                    << "-> takes value of sl 3 " << newTTrigMean << endl;
0172             } else {
0173               cout << "No correction possible within same chamber (sl2)  " << endl;
0174               newTTrigMean = average;
0175               newTTrigSigma = averageSigma;
0176               newkfactor = averagekfactor;
0177               cout << "####### Bad SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0178             }
0179           }
0180 
0181         } else if (((*sl)->id().superlayer()) == 3) {
0182           //cout << " superlayer " << ((*sl)->id().superlayer()) <<endl;
0183           DTSuperLayerId slId((*sl)->id().chamberId(), 1);
0184           tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0185           if (abs(tempttrigMean - average) < rmsLimit * rms) {
0186             newTTrigMean = tempttrigMean;
0187             newTTrigSigma = tempttrigSigma;
0188             newkfactor = tempkfactor;
0189             cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0190                  << " -> takes value of sl 1 " << newTTrigMean << endl;
0191           } else if (chamber == 4) {
0192             cout << "No correction possible within same chamber (sl3)" << endl;
0193             newTTrigMean = average;
0194             newTTrigSigma = averageSigma;
0195             newkfactor = averagekfactor;
0196             cout << "####### Bad SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0197           } else if (chamber != 4) {
0198             DTSuperLayerId slId((*sl)->id().chamberId(), 2);
0199             tTrigMap->get(slId, tempttrigMean, tempttrigSigma, tempkfactor, DTTimeUnits::ns);
0200             if (abs(tempttrigMean - average) < rmsLimit * rms) {
0201               newTTrigMean = tempttrigMean;
0202               newTTrigSigma = tempttrigSigma;
0203               newkfactor = tempkfactor;
0204               cout << "Chamber " << chamber << " sl " << ((*sl)->id().superlayer()) << "has ttrig " << ttrigMean
0205                    << " -> takes value of sl 2 " << newTTrigMean << endl;
0206             } else {
0207               cout << "No correction possible within same chamber (sl3) " << endl;
0208               newTTrigMean = average;
0209               newTTrigSigma = averageSigma;
0210               newkfactor = averagekfactor;
0211               cout << "####### Bad SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0212             }
0213           }
0214         }
0215       } else {
0216         //  cout << "SL not present " << ttrigMean << " " <<((*sl)->id().superlayer()) <<endl;
0217         newTTrigMean = average;
0218         newTTrigSigma = averageSigma;
0219         newkfactor = averagekfactor;
0220         cout << "####### NotPresent SL: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0221       }
0222     }
0223 
0224     //Store new ttrig in the new map
0225     tTrigNewMap.set((*sl)->id(), newTTrigMean, newTTrigSigma, newkfactor, DTTimeUnits::ns);
0226     if (debug) {
0227       cout << "New tTrig: " << (*sl)->id() << " from " << ttrigMean << " to " << newTTrigMean << endl;
0228       cout << "New tTrigSigma: " << (*sl)->id() << " from " << ttrigSigma << " to " << newTTrigSigma << endl;
0229     }
0230   }  //End of loop on superlayers
0231 
0232   //Write object to DB
0233   cout << "[DTTTrigCorrection]: Writing ttrig object to DB!" << endl;
0234   string record = "DTTtrigRcd";
0235   DTCalibDBUtils::writeToDB<DTTtrig>(record, tTrigNewMap);
0236 }