CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/CalibMuon/DTCalibration/plugins/DTCalibrationMap.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2008/12/09 22:43:38 $
00006  *  $Revision: 1.2 $
00007  *  \author G. Cerminara - INFN Torino
00008  */
00009 
00010 #include "CalibMuon/DTCalibration/plugins/DTCalibrationMap.h"
00011 
00012 #include "FWCore/Utilities/interface/Exception.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 
00015 #include <iostream>
00016 #include <fstream>
00017 
00018 #include <sstream>
00019 #include <algorithm>
00020 #include <iterator>
00021 
00022 using namespace std;
00023 using namespace edm;
00024 
00025 DTCalibrationMap::DTCalibrationMap(const ParameterSet& pset) {
00026   nFields =  pset.getUntrackedParameter<int>("nFields", 5);
00027   calibConstFileName = pset.getUntrackedParameter<string>("calibConstFileName", "dummy.txt");
00028   calibConstGranularity = pset.getUntrackedParameter<string>("calibConstGranularity","bySL");
00029 
00030   // Initialize correctly the enum which specify the granularity for the calibration
00031   if(calibConstGranularity == "byWire") {
00032     theGranularity = byWire;
00033   } else if(calibConstGranularity == "byLayer"){
00034     theGranularity = byLayer;
00035   } else if(calibConstGranularity == "bySL") {
00036     theGranularity = bySL;
00037   } else {
00038     theGranularity = byChamber;
00039     if(!(calibConstGranularity == "byChamber")) {
00040       cout << "[DTCalibrationMap]###Warning: Check parameter calibConstGranularity: "
00041            << calibConstGranularity << " options not available!" << endl;
00042     }
00043   }
00044   readConsts(calibConstFileName);
00045 }
00046 
00047 
00048 
00049 DTCalibrationMap::~DTCalibrationMap(){}
00050 
00051 
00052 
00053 // Return the t_trig (ns) for a particular wire
00054 float DTCalibrationMap::tTrig(DTWireId wireId) const {
00055  return getField(wireId, 0);
00056 }
00057 
00058 
00059 
00060 // Return the sigma of the t_trig (ns) for a particular wire
00061 float DTCalibrationMap::sigma_tTrig(DTWireId wireId) const {
00062  return getField(wireId, 1);
00063 }
00064 
00065 
00066 
00067 // Return the kfactor for a particular wire
00068 float DTCalibrationMap::kFactor(DTWireId wireId) const {
00069   return getField(wireId, 2);
00070 }
00071 
00072  
00073 
00074 // Return the mean drift velocity for a particular wire (cm/ns)
00075 float DTCalibrationMap::meanVDrift(DTWireId wireId) const {
00076  return getField(wireId, 3);
00077 }
00078 
00079 
00080 
00081 // Return the sigma of the mean drift velocity for a particular wire (cm/ns)
00082 float DTCalibrationMap::sigma_meanVDrift(DTWireId wireId) const {
00083  return getField(wireId, 4);
00084 }
00085 
00086 
00087 
00088 // Get a key to read calibration constants for a particular wire
00089 // with the given granularity
00090 DTCalibrationMap::Key DTCalibrationMap::getKey(DTWireId wireId) const {
00091   if (theGranularity == byChamber){
00092     return Key(wireId.chamberId(), 0, 0, 0);
00093   } else if (theGranularity == bySL) {
00094     return Key(wireId.superlayerId(), 0, 0); 
00095   } else if (theGranularity == byLayer) {
00096     return Key(wireId.layerId(), 0);  
00097   } else {
00098     return Key(wireId);
00099   }
00100 }
00101 
00102 
00103 
00104 // Get from the map the calibration constants for a particular key
00105 const DTCalibrationMap::CalibConsts* DTCalibrationMap::getConsts(DTWireId wireId) const {
00106   // Create a cache
00107   static pair<Key, CalibConsts> cache;
00108 
00109   // Get the key
00110   Key theKey = getKey(wireId);
00111 
00112   // Check if the result is already cached
00113   if ( theKey == cache.first ) {
00114     return &(cache.second);
00115   }
00116 
00117   // Look for the given key into the map
00118   map<Key, CalibConsts>::const_iterator res = theMap.find(theKey);
00119   if (res != theMap.end()) {
00120     cache = (*res);
00121     return &((*res).second);
00122   } else {
00123     return 0;
00124   }
00125 }
00126   
00127 
00128 
00129 // Get a particular number (field) between all the calibration
00130 // constants available for a particluar wire
00131 float DTCalibrationMap::getField(DTWireId wireId, int field) const {
00132   const CalibConsts* cals = getConsts(wireId);
00133   if (cals == 0) {
00134     throw cms::Exception("NoCalibConsts") << "DTCalibrationMap:" << endl
00135                                                 << "No parameters for wire: " << wireId << endl
00136                                                 << "Check the " << calibConstFileName << " file!" << endl;
00137   } else {
00138     return (*(cals))[field];
00139   }
00140 }
00141 
00142 
00143 
00144 
00145 // Read the calibration consts from a file 
00146 void DTCalibrationMap::readConsts(const string& inputFileName) {
00147    ifstream file(inputFileName.c_str());
00148    // Check if the file exists
00149    if(!file) {
00150     cout << "[DTCalibrationMap]***Warning: File: " << inputFileName 
00151          << " not found in current directory!!!" << endl; 
00152    }
00153 
00154   string line;
00155 
00156   // The numbers to be read to build the key
00157   int wheel_id = 0;
00158   int station_id = 0;
00159   int sector_id = 0;
00160   int superlayer_id = 0;
00161   int layer_id = 0;
00162   int wire_id = 0;
00163 
00164   // Read all the lines
00165   while (getline(file,line)) {
00166     if( line == "" || line[0] == '#' ) continue; // Skip comments and empty lines
00167     stringstream linestr;
00168     linestr << line;
00169 
00170     pair<Key, CalibConsts> wireCalib;
00171 
00172     linestr >> wheel_id
00173             >> station_id
00174             >> sector_id
00175             >> superlayer_id
00176             >> layer_id
00177             >> wire_id;
00178     
00179     // Build the key
00180     wireCalib.first =  Key( wheel_id, 
00181                             station_id, 
00182                             sector_id, 
00183                             superlayer_id, 
00184                             layer_id, 
00185                             wire_id);
00186 
00187     if(!checkGranularity(wireCalib.first))
00188        cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the selected granularity!" << endl;
00189 
00190 
00191     // Read the calibration constants
00192     copy(istream_iterator<float>(linestr),
00193          istream_iterator<float>(),
00194          back_inserter(wireCalib.second));
00195     
00196     if(wireCalib.second.size() !=  nFields){
00197       cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the number of fields!" << endl;
00198     }
00199     
00200     theMap.insert(wireCalib);
00201   }
00202 }
00203 
00204 // Add to the map the calibration consts for a given key 
00205 void DTCalibrationMap::addCell(Key theKey, const CalibConsts& calibConst) {
00206   if(!checkGranularity(theKey))
00207     throw cms::Exception("addCell") << "DTCalibrationMap:" << endl
00208                                     << "The added key is not compatible with the selected granularity"
00209                                     << endl;
00210 
00211   theMap[theKey] = calibConst;
00212 }
00213 
00214 // Write the calibration consts to a file 
00215 void DTCalibrationMap::writeConsts(const string& outputFileName) const {
00216   ofstream out(outputFileName.c_str());
00217   for(map<Key,CalibConsts>::const_iterator iter = theMap.begin();
00218       iter != theMap.end() ; iter++) {
00219     
00220     out << (*iter).first.wheel() << ' '
00221       << (*iter).first.station() << ' '
00222       << (*iter).first.sector() << ' '
00223       << (*iter).first.superlayer() << ' '
00224       << (*iter).first.layer() << ' '
00225       << (*iter).first.wire() << ' ';
00226     copy((*iter).second.begin(), (*iter).second.end(),
00227          ostream_iterator<float>(out, " "));
00228     out << endl;
00229   }
00230 }
00231 
00232 
00233   // Check the consistency of a given key with the selected granularity
00234 bool DTCalibrationMap::checkGranularity(Key aKey) const {
00235   bool ret = true;
00236 
00237   // Check that the key is consistent with the given granularity
00238   if(theGranularity == byChamber) {
00239     if(aKey.superlayer() || aKey.layer() || aKey.wire()) {
00240       ret = false;
00241     }
00242   } else if(theGranularity == bySL) {
00243     if(aKey.layer() || aKey.wire()) {
00244       ret = false;
00245     }
00246   } else if(theGranularity == byLayer) {
00247     if(aKey.wire()) {
00248       ret = false;
00249     }
00250   } else if(theGranularity == byWire) {
00251     if(aKey.wire() == 0) {
00252       ret = false;
00253     }
00254   } 
00255   return ret;
00256 }