Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:39

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author G. Cerminara - INFN Torino
0006  */
0007 
0008 #include "CalibMuon/DTCalibration/plugins/DTCalibrationMap.h"
0009 
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 
0013 #include <iostream>
0014 #include <fstream>
0015 
0016 #include <sstream>
0017 #include <algorithm>
0018 #include <iterator>
0019 
0020 using namespace std;
0021 using namespace edm;
0022 
0023 DTCalibrationMap::DTCalibrationMap(const ParameterSet& pset) {
0024   nFields = pset.getUntrackedParameter<int>("nFields", 5);
0025   calibConstFileName = pset.getUntrackedParameter<string>("calibConstFileName", "dummy.txt");
0026   calibConstGranularity = pset.getUntrackedParameter<string>("calibConstGranularity", "bySL");
0027 
0028   // Initialize correctly the enum which specify the granularity for the calibration
0029   if (calibConstGranularity == "byWire") {
0030     theGranularity = byWire;
0031   } else if (calibConstGranularity == "byLayer") {
0032     theGranularity = byLayer;
0033   } else if (calibConstGranularity == "bySL") {
0034     theGranularity = bySL;
0035   } else {
0036     theGranularity = byChamber;
0037     if (!(calibConstGranularity == "byChamber")) {
0038       cout << "[DTCalibrationMap]###Warning: Check parameter calibConstGranularity: " << calibConstGranularity
0039            << " options not available!" << endl;
0040     }
0041   }
0042   readConsts(calibConstFileName);
0043 }
0044 
0045 DTCalibrationMap::~DTCalibrationMap() {}
0046 
0047 // Return the t_trig (ns) for a particular wire
0048 float DTCalibrationMap::tTrig(DTWireId wireId) const { return getField(wireId, 0); }
0049 
0050 // Return the sigma of the t_trig (ns) for a particular wire
0051 float DTCalibrationMap::sigma_tTrig(DTWireId wireId) const { return getField(wireId, 1); }
0052 
0053 // Return the kfactor for a particular wire
0054 float DTCalibrationMap::kFactor(DTWireId wireId) const { return getField(wireId, 2); }
0055 
0056 // Return the mean drift velocity for a particular wire (cm/ns)
0057 float DTCalibrationMap::meanVDrift(DTWireId wireId) const { return getField(wireId, 3); }
0058 
0059 // Return the sigma of the mean drift velocity for a particular wire (cm/ns)
0060 float DTCalibrationMap::sigma_meanVDrift(DTWireId wireId) const { return getField(wireId, 4); }
0061 
0062 // Get a key to read calibration constants for a particular wire
0063 // with the given granularity
0064 DTCalibrationMap::Key DTCalibrationMap::getKey(DTWireId wireId) const {
0065   if (theGranularity == byChamber) {
0066     return Key(wireId.chamberId(), 0, 0, 0);
0067   } else if (theGranularity == bySL) {
0068     return Key(wireId.superlayerId(), 0, 0);
0069   } else if (theGranularity == byLayer) {
0070     return Key(wireId.layerId(), 0);
0071   } else {
0072     return Key(wireId);
0073   }
0074 }
0075 
0076 // Get from the map the calibration constants for a particular key
0077 const DTCalibrationMap::CalibConsts* DTCalibrationMap::getConsts(DTWireId wireId) const {
0078   // Create a cache
0079   static pair<Key, CalibConsts> cache;
0080 
0081   // Get the key
0082   Key theKey = getKey(wireId);
0083 
0084   // Check if the result is already cached
0085   if (theKey == cache.first) {
0086     return &(cache.second);
0087   }
0088 
0089   // Look for the given key into the map
0090   map<Key, CalibConsts>::const_iterator res = theMap.find(theKey);
0091   if (res != theMap.end()) {
0092     cache = (*res);
0093     return &((*res).second);
0094   } else {
0095     return nullptr;
0096   }
0097 }
0098 
0099 // Get a particular number (field) between all the calibration
0100 // constants available for a particluar wire
0101 float DTCalibrationMap::getField(DTWireId wireId, int field) const {
0102   const CalibConsts* cals = getConsts(wireId);
0103   if (cals == nullptr) {
0104     throw cms::Exception("NoCalibConsts") << "DTCalibrationMap:" << endl
0105                                           << "No parameters for wire: " << wireId << endl
0106                                           << "Check the " << calibConstFileName << " file!" << endl;
0107   } else {
0108     return (*(cals))[field];
0109   }
0110 }
0111 
0112 // Read the calibration consts from a file
0113 void DTCalibrationMap::readConsts(const string& inputFileName) {
0114   ifstream file(inputFileName.c_str());
0115   // Check if the file exists
0116   if (!file) {
0117     cout << "[DTCalibrationMap]***Warning: File: " << inputFileName << " not found in current directory!!!" << endl;
0118   }
0119 
0120   string line;
0121 
0122   // The numbers to be read to build the key
0123   int wheel_id = 0;
0124   int station_id = 0;
0125   int sector_id = 0;
0126   int superlayer_id = 0;
0127   int layer_id = 0;
0128   int wire_id = 0;
0129 
0130   // Read all the lines
0131   while (getline(file, line)) {
0132     if (line.empty() || line[0] == '#')
0133       continue;  // Skip comments and empty lines
0134     stringstream linestr;
0135     linestr << line;
0136 
0137     pair<Key, CalibConsts> wireCalib;
0138 
0139     linestr >> wheel_id >> station_id >> sector_id >> superlayer_id >> layer_id >> wire_id;
0140 
0141     // Build the key
0142     wireCalib.first = Key(wheel_id, station_id, sector_id, superlayer_id, layer_id, wire_id);
0143 
0144     if (!checkGranularity(wireCalib.first))
0145       cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the selected granularity!"
0146            << endl;
0147 
0148     // Read the calibration constants
0149     copy(istream_iterator<float>(linestr), istream_iterator<float>(), back_inserter(wireCalib.second));
0150 
0151     if (wireCalib.second.size() < nFields) {
0152       cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the number of fields!" << endl;
0153     }
0154 
0155     theMap.insert(wireCalib);
0156   }
0157 }
0158 
0159 // Add to the map the calibration consts for a given key
0160 void DTCalibrationMap::addCell(Key theKey, const CalibConsts& calibConst) {
0161   if (!checkGranularity(theKey))
0162     throw cms::Exception("addCell") << "DTCalibrationMap:" << endl
0163                                     << "The added key is not compatible with the selected granularity" << endl;
0164 
0165   theMap[theKey] = calibConst;
0166 }
0167 
0168 // Write the calibration consts to a file
0169 void DTCalibrationMap::writeConsts(const string& outputFileName) const {
0170   ofstream out(outputFileName.c_str());
0171   for (map<Key, CalibConsts>::const_iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
0172     out << (*iter).first.wheel() << ' ' << (*iter).first.station() << ' ' << (*iter).first.sector() << ' '
0173         << (*iter).first.superlayer() << ' ' << (*iter).first.layer() << ' ' << (*iter).first.wire() << ' ';
0174     copy((*iter).second.begin(), (*iter).second.end(), ostream_iterator<float>(out, " "));
0175     out << endl;
0176   }
0177 }
0178 
0179 // Check the consistency of a given key with the selected granularity
0180 bool DTCalibrationMap::checkGranularity(Key aKey) const {
0181   bool ret = true;
0182 
0183   // Check that the key is consistent with the given granularity
0184   if (theGranularity == byChamber) {
0185     if (aKey.superlayer() || aKey.layer() || aKey.wire()) {
0186       ret = false;
0187     }
0188   } else if (theGranularity == bySL) {
0189     if (aKey.layer() || aKey.wire()) {
0190       ret = false;
0191     }
0192   } else if (theGranularity == byLayer) {
0193     if (aKey.wire()) {
0194       ret = false;
0195     }
0196   } else if (theGranularity == byWire) {
0197     if (aKey.wire() == 0) {
0198       ret = false;
0199     }
0200   }
0201   return ret;
0202 }