Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 22:54:23

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author G. Cerminara - INFN Torino
0006  */
0007 
0008 #include "DumpDBToFile.h"
0009 #include "DTCalibrationMap.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/Utilities/interface/Exception.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 
0018 #include <iostream>
0019 #include <fstream>
0020 #include <algorithm>
0021 #include <iterator>
0022 
0023 using namespace edm;
0024 using namespace std;
0025 
0026 DumpDBToFile::DumpDBToFile(const ParameterSet& pset) {
0027   theCalibFile = new DTCalibrationMap(pset.getUntrackedParameter<ParameterSet>("calibFileConfig"));
0028   theOutputFileName = pset.getUntrackedParameter<string>("outputFileName");
0029 
0030   dbToDump = pset.getUntrackedParameter<string>("dbToDump", "TTrigDB");
0031   dbLabel = pset.getUntrackedParameter<string>("dbLabel", "");
0032   format = pset.getUntrackedParameter<string>("dbFormat", "Legacy");
0033 
0034   if (dbToDump != "VDriftDB" && dbToDump != "TTrigDB" && dbToDump != "TZeroDB" && dbToDump != "NoiseDB" &&
0035       dbToDump != "DeadDB" && dbToDump != "ChannelsDB" && dbToDump != "RecoUncertDB")
0036     throw cms::Exception("IncorrectSetup") << "Parameter dbToDump is not valid, check the cfg file" << endl;
0037 
0038   if (format != "Legacy" && format != "DTRecoConditions")
0039     throw cms::Exception("IncorrectSetup") << "Parameter format is not valid, check the cfg file" << endl;
0040 
0041   auto esConsumesWithLabel = [&](auto& token) {
0042     token = esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", dbLabel));
0043   };
0044 
0045   if (dbToDump == "VDriftDB") {
0046     if (format == "Legacy") {
0047       esConsumesWithLabel(mTimeMapToken_);
0048     } else {
0049       esConsumesWithLabel(vDriftToken_);
0050     }
0051   } else if (dbToDump == "TTrigDB") {
0052     if (format == "Legacy") {
0053       esConsumesWithLabel(tTrigMapToken_);
0054     } else {
0055       esConsumesWithLabel(tTrigToken_);
0056     }
0057   } else if (dbToDump == "TZeroDB") {
0058     esConsumesWithLabel(t0MapToken_);
0059   } else if (dbToDump == "NoiseDB") {
0060     esConsumesWithLabel(statusMapToken_);
0061   } else if (dbToDump == "DeadDB") {
0062     esConsumesWithLabel(deadMapToken_);
0063   } else if (dbToDump == "ChannelsDB") {
0064     esConsumesWithLabel(readOutMapToken_);
0065   } else if (dbToDump == "RecoUncertDB") {
0066     esConsumesWithLabel(uncertToken_);
0067   }
0068 }
0069 
0070 DumpDBToFile::~DumpDBToFile() {}
0071 
0072 void DumpDBToFile::beginRun(const edm::Run&, const EventSetup& setup) {
0073   // Read the right DB accordingly to the parameter dbToDump
0074   if (dbToDump == "VDriftDB") {
0075     if (format == "Legacy") {
0076       mTimeMap = &setup.getData(mTimeMapToken_);
0077     } else if (format == "DTRecoConditions") {
0078       rconds = &setup.getData(vDriftToken_);
0079     }
0080   } else if (dbToDump == "TTrigDB") {
0081     if (format == "Legacy") {
0082       tTrigMap = &setup.getData(tTrigMapToken_);
0083     } else if (format == "DTRecoConditions") {
0084       rconds = &setup.getData(tTrigToken_);
0085     }
0086   } else if (dbToDump == "TZeroDB") {
0087     tZeroMap = &setup.getData(t0MapToken_);
0088   } else if (dbToDump == "NoiseDB") {
0089     statusMap = &setup.getData(statusMapToken_);
0090   } else if (dbToDump == "DeadDB") {
0091     deadMap = &setup.getData(deadMapToken_);
0092   } else if (dbToDump == "ChannelsDB") {
0093     channelsMap = &setup.getData(readOutMapToken_);
0094   } else if (dbToDump == "RecoUncertDB") {
0095     rconds = &setup.getData(uncertToken_);
0096   }
0097 }
0098 
0099 void DumpDBToFile::endJob() {
0100   if (dbToDump != "ChannelsDB") {
0101     //---------- VDrifts
0102     if (dbToDump == "VDriftDB") {
0103       if (format == "Legacy") {
0104         int version = 1;
0105         int type = 1;  //ie constant
0106         int nfields = 1;
0107         cout << "[DumpDBToFile] MTime version: " << mTimeMap->version() << endl;
0108         for (DTMtime::const_iterator mtime = mTimeMap->begin(); mtime != mTimeMap->end(); ++mtime) {
0109           DTWireId wireId(
0110               (*mtime).first.wheelId, (*mtime).first.stationId, (*mtime).first.sectorId, (*mtime).first.slId, 0, 0);
0111           float vdrift;
0112           float reso;
0113           DetId detId(wireId.rawId());
0114           // vdrift is cm/ns , resolution is cm
0115           mTimeMap->get(detId, vdrift, reso, DTVelocityUnits::cm_per_ns);
0116           //      cout << "Wh: " << (*mtime).first.wheelId
0117           //           << " St: " << (*mtime).first.stationId
0118           //           << " Sc: " << (*mtime).first.sectorId
0119           //           << " Sl: " << (*mtime).first.slId
0120           //           << " VDrift (cm/ns): " << vdrift
0121           //           << " Hit reso (cm): " << reso << endl;
0122           vector<float> consts = {
0123               -1, -1, -1, vdrift, reso, -1, -1, -1, -1, -1, float(version * 1000 + type * 100 + nfields), vdrift};
0124           theCalibFile->addCell(wireId, consts);
0125         }
0126       } else if (format == "DTRecoConditions") {
0127         int version = rconds->version();
0128         int type = 1;  // i.e. expr = "[0]"
0129         string expr = rconds->getFormulaExpr();
0130         cout << "[DumpDBToFile] DTRecoConditions (vdrift) version: " << version << " expression: " << expr << endl;
0131         if (version != 1 || expr != "[0]")
0132           throw cms::Exception("Configuration") << "only version 1, type 1 is presently supported for VDriftDB";
0133         for (DTRecoConditions::const_iterator irc = rconds->begin(); irc != rconds->end(); ++irc) {
0134           DTWireId wireId(irc->first);
0135           const vector<double>& data = irc->second;
0136           int nfields = data.size();  // FIXME check size
0137           float vdrift = data[0];
0138           float reso = 0;
0139           vector<float> consts(11 + nfields, -1);
0140           consts[3] = vdrift;
0141           consts[4] = reso;
0142           consts[10] = float(version * 1000 + type * 100 + nfields);
0143           std::copy(data.begin(), data.end(), consts.begin() + 11);
0144           theCalibFile->addCell(wireId, consts);
0145         }
0146       }
0147 
0148       //---------- TTrigs
0149     } else if (dbToDump == "TTrigDB") {
0150       if (format == "Legacy") {
0151         int version = 1;
0152         int type = 1;  //ie constant
0153         int nfields = 1;
0154         cout << "[DumpDBToFile] TTrig version: " << tTrigMap->version() << endl;
0155         for (DTTtrig::const_iterator ttrig = tTrigMap->begin(); ttrig != tTrigMap->end(); ++ttrig) {
0156           DTWireId wireId(
0157               (*ttrig).first.wheelId, (*ttrig).first.stationId, (*ttrig).first.sectorId, (*ttrig).first.slId, 0, 0);
0158           DetId detId(wireId.rawId());
0159           float tmea;
0160           float trms;
0161           float kFactor;
0162           // ttrig and rms are ns
0163           tTrigMap->get(detId, tmea, trms, kFactor, DTTimeUnits::ns);
0164           //      cout << "Wh: " << (*ttrig).first.wheelId
0165           //           << " St: " << (*ttrig).first.stationId
0166           //           << " Sc: " << (*ttrig).first.sectorId
0167           //           << " Sl: " << (*ttrig).first.slId
0168           //           << " TTrig mean (ns): " << tmea
0169           //           << " TTrig sigma (ns): " << trms << endl;
0170 
0171           // note that in the free fields we write one single time = tmea+trms*kFactor, as per current use:
0172           // https://github.com/cms-sw/cmssw/blob/CMSSW_7_5_X/CalibMuon/DTDigiSync/src/DTTTrigSyncFromDB.cc#L197
0173           vector<float> consts = {tmea,
0174                                   trms,
0175                                   kFactor,
0176                                   -1,
0177                                   -1,
0178                                   -1,
0179                                   -1,
0180                                   -1,
0181                                   -1,
0182                                   -1,
0183                                   float(version * 1000 + type * 100 + nfields),
0184                                   tmea + trms * kFactor};
0185           theCalibFile->addCell(wireId, consts);
0186         }
0187       } else if (format == "DTRecoConditions") {
0188         int version = rconds->version();
0189         int type = 1;  // i.e. expr = "[0]"
0190         string expr = rconds->getFormulaExpr();
0191         if (version != 1 || expr != "[0]")
0192           throw cms::Exception("Configuration") << "only version 1, type 1 is presently supported for TTrigDB";
0193 
0194         cout << "[DumpDBToFile] DTRecoConditions (ttrig) version: " << rconds->version() << " expression: " << expr
0195              << endl;
0196         for (DTRecoConditions::const_iterator irc = rconds->begin(); irc != rconds->end(); ++irc) {
0197           DTWireId wireId(irc->first);
0198           const vector<double>& data = irc->second;
0199           int nfields = data.size();  // FIXME check size (should be 1)
0200           float ttrig = data[0];
0201           float sigma = 0;  // Unused in DTRecoConditions
0202           float kappa = 0;
0203 
0204           vector<float> consts(11 + nfields, -1);
0205           consts[0] = ttrig;
0206           consts[1] = sigma;
0207           consts[2] = kappa;
0208           consts[10] = float(version * 1000 + type * 100 + nfields);
0209           std::copy(data.begin(), data.end(), consts.begin() + 11);
0210           theCalibFile->addCell(wireId, consts);
0211         }
0212       }
0213 
0214       //---------- T0, noise, dead
0215     } else if (dbToDump == "TZeroDB") {
0216       cout << "[DumpDBToFile] T0 version: " << tZeroMap->version() << endl;
0217       for (DTT0::const_iterator tzero = tZeroMap->begin(); tzero != tZeroMap->end(); ++tzero) {
0218         // @@@ NEW DTT0 FORMAT
0219         //  DTWireId wireId((*tzero).first.wheelId,
0220         //          (*tzero).first.stationId,
0221         //          (*tzero).first.sectorId,
0222         //          (*tzero).first.slId,
0223         //          (*tzero).first.layerId,
0224         //          (*tzero).first.cellId);
0225         int channelId = tzero->channelId;
0226         if (channelId == 0)
0227           continue;
0228         DTWireId wireId(channelId);
0229         // @@@ NEW DTT0 END
0230         float t0mean;
0231         float t0rms;
0232         // t0s and rms are TDC counts
0233         tZeroMap->get(wireId, t0mean, t0rms, DTTimeUnits::counts);
0234         //        cout << wireId << " TZero mean (TDC counts): " << t0mean << " TZero RMS (TDC counts): " << t0rms << endl;
0235         vector<float> consts;
0236         consts.push_back(-1);
0237         consts.push_back(-1);
0238         consts.push_back(-1);
0239         consts.push_back(-1);
0240         consts.push_back(-1);
0241         consts.push_back(t0mean);
0242         consts.push_back(t0rms);
0243 
0244         theCalibFile->addCell(wireId, consts);
0245       }
0246     } else if (dbToDump == "NoiseDB") {
0247       for (DTStatusFlag::const_iterator statusFlag = statusMap->begin(); statusFlag != statusMap->end(); ++statusFlag) {
0248         DTWireId wireId((*statusFlag).first.wheelId,
0249                         (*statusFlag).first.stationId,
0250                         (*statusFlag).first.sectorId,
0251                         (*statusFlag).first.slId,
0252                         (*statusFlag).first.layerId,
0253                         (*statusFlag).first.cellId);
0254         cout << wireId << " Noisy Flag: " << (*statusFlag).second.noiseFlag << endl;
0255         vector<float> consts;
0256         consts.push_back(-1);
0257         consts.push_back(-1);
0258         consts.push_back(-1);
0259         consts.push_back(-1);
0260         consts.push_back(-1);
0261         consts.push_back(-9999999);
0262         consts.push_back(-9999999);
0263         consts.push_back((*statusFlag).second.noiseFlag);
0264 
0265         theCalibFile->addCell(wireId, consts);
0266       }
0267     } else if (dbToDump == "DeadDB") {
0268       for (DTDeadFlag::const_iterator deadFlag = deadMap->begin(); deadFlag != deadMap->end(); ++deadFlag) {
0269         DTWireId wireId((*deadFlag).first.wheelId,
0270                         (*deadFlag).first.stationId,
0271                         (*deadFlag).first.sectorId,
0272                         (*deadFlag).first.slId,
0273                         (*deadFlag).first.layerId,
0274                         (*deadFlag).first.cellId);
0275         cout << wireId << " Dead Flag: " << (*deadFlag).second.dead_TP << endl;
0276         vector<float> consts;
0277         consts.push_back(-1);
0278         consts.push_back(-1);
0279         consts.push_back(-1);
0280         consts.push_back(-1);
0281         consts.push_back(-1);
0282         consts.push_back(-9999999);
0283         consts.push_back(-9999999);
0284         consts.push_back((*deadFlag).second.dead_TP);
0285 
0286         theCalibFile->addCell(wireId, consts);
0287       }
0288 
0289       //---------- Uncertainties
0290     } else if (dbToDump == "RecoUncertDB") {
0291       int version = rconds->version();
0292       string expr = rconds->getFormulaExpr();
0293       int type = 2;  // par[step]
0294       if (version != 1 || expr != "par[step]")
0295         throw cms::Exception("Configuration") << "only version 1, type 2 is presently supported for RecoUncertDB";
0296 
0297       cout << "[DumpDBToFile] DTRecoConditions (uncerts) version: " << rconds->version() << " expression: " << expr
0298            << endl;
0299 
0300       for (DTRecoConditions::const_iterator irc = rconds->begin(); irc != rconds->end(); ++irc) {
0301         DTWireId wireId(irc->first);
0302         const vector<double>& data = irc->second;
0303         int nfields = data.size();
0304         vector<float> consts(11 + nfields, -1);
0305         consts[10] = float(version * 1000 + type * 100 + nfields);
0306         std::copy(data.begin(), data.end(), consts.begin() + 11);
0307         theCalibFile->addCell(wireId, consts);
0308       }
0309     }
0310     //Write constants into file
0311     theCalibFile->writeConsts(theOutputFileName);
0312   }
0313 
0314   else if (dbToDump == "ChannelsDB") {
0315     ofstream out(theOutputFileName.c_str());
0316     for (DTReadOutMapping::const_iterator roLink = channelsMap->begin(); roLink != channelsMap->end(); ++roLink) {
0317       out << roLink->dduId << ' ' << roLink->rosId << ' ' << roLink->robId << ' ' << roLink->tdcId << ' '
0318           << roLink->channelId << ' ' << roLink->wheelId << ' ' << roLink->stationId << ' ' << roLink->sectorId << ' '
0319           << roLink->slId << ' ' << roLink->layerId << ' ' << roLink->cellId << endl;
0320 
0321       cout << "ddu " << roLink->dduId << ' ' << "ros " << roLink->rosId << ' ' << "rob " << roLink->robId << ' '
0322            << "tdc " << roLink->tdcId << ' ' << "channel " << roLink->channelId << ' ' << "->" << ' ' << "wheel "
0323            << roLink->wheelId << ' ' << "station " << roLink->stationId << ' ' << "sector " << roLink->sectorId << ' '
0324            << "superlayer " << roLink->slId << ' ' << "layer " << roLink->layerId << ' ' << "wire " << roLink->cellId
0325            << ' ' << endl;
0326     }
0327   }
0328 }