CMS 3D CMS Logo

DTCalibrationMap.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2007/07/11 12:20:50 $
00006  *  $Revision: 1.1 $
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", 4);
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 mean drift velocity for a particular wire (cm/ns)
00068 float DTCalibrationMap::meanVDrift(DTWireId wireId) const {
00069  return getField(wireId, 2);
00070 }
00071 
00072 
00073 
00074 // Return the sigma of the mean drift velocity for a particular wire (cm/ns)
00075 float DTCalibrationMap::sigma_meanVDrift(DTWireId wireId) const {
00076  return getField(wireId, 3);
00077 }
00078 
00079 
00080 
00081 // Get a key to read calibration constants for a particular wire
00082 // with the given granularity
00083 DTCalibrationMap::Key DTCalibrationMap::getKey(DTWireId wireId) const {
00084   if (theGranularity == byChamber){
00085     return Key(wireId.chamberId(), 0, 0, 0);
00086   } else if (theGranularity == bySL) {
00087     return Key(wireId.superlayerId(), 0, 0); 
00088   } else if (theGranularity == byLayer) {
00089     return Key(wireId.layerId(), 0);  
00090   } else {
00091     return Key(wireId);
00092   }
00093 }
00094 
00095 
00096 
00097 // Get from the map the calibration constants for a particular key
00098 const DTCalibrationMap::CalibConsts* DTCalibrationMap::getConsts(DTWireId wireId) const {
00099   // Create a cache
00100   static pair<Key, CalibConsts> cache;
00101 
00102   // Get the key
00103   Key theKey = getKey(wireId);
00104 
00105   // Check if the result is already cached
00106   if ( theKey == cache.first ) {
00107     return &(cache.second);
00108   }
00109 
00110   // Look for the given key into the map
00111   map<Key, CalibConsts>::const_iterator res = theMap.find(theKey);
00112   if (res != theMap.end()) {
00113     cache = (*res);
00114     return &((*res).second);
00115   } else {
00116     return 0;
00117   }
00118 }
00119   
00120 
00121 
00122 // Get a particular number (field) between all the calibration
00123 // constants available for a particluar wire
00124 float DTCalibrationMap::getField(DTWireId wireId, int field) const {
00125   const CalibConsts* cals = getConsts(wireId);
00126   if (cals == 0) {
00127     throw cms::Exception("NoCalibConsts") << "DTCalibrationMap:" << endl
00128                                                 << "No parameters for wire: " << wireId << endl
00129                                                 << "Check the " << calibConstFileName << " file!" << endl;
00130   } else {
00131     return (*(cals))[field];
00132   }
00133 }
00134 
00135 
00136 
00137 
00138 // Read the calibration consts from a file 
00139 void DTCalibrationMap::readConsts(const string& inputFileName) {
00140    ifstream file(inputFileName.c_str());
00141    // Check if the file exists
00142    if(!file) {
00143     cout << "[DTCalibrationMap]***Warning: File: " << inputFileName 
00144          << " not found in current directory!!!" << endl; 
00145    }
00146 
00147   string line;
00148 
00149   // The numbers to be read to build the key
00150   int wheel_id = 0;
00151   int station_id = 0;
00152   int sector_id = 0;
00153   int superlayer_id = 0;
00154   int layer_id = 0;
00155   int wire_id = 0;
00156 
00157   // Read all the lines
00158   while (getline(file,line)) {
00159     if( line == "" || line[0] == '#' ) continue; // Skip comments and empty lines
00160     stringstream linestr;
00161     linestr << line;
00162 
00163     pair<Key, CalibConsts> wireCalib;
00164 
00165     linestr >> wheel_id
00166             >> station_id
00167             >> sector_id
00168             >> superlayer_id
00169             >> layer_id
00170             >> wire_id;
00171     
00172     // Build the key
00173     wireCalib.first =  Key( wheel_id, 
00174                             station_id, 
00175                             sector_id, 
00176                             superlayer_id, 
00177                             layer_id, 
00178                             wire_id);
00179 
00180     if(!checkGranularity(wireCalib.first))
00181        cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the selected granularity!" << endl;
00182 
00183 
00184     // Read the calibration constants
00185     copy(istream_iterator<float>(linestr),
00186          istream_iterator<float>(),
00187          back_inserter(wireCalib.second));
00188     
00189     if(wireCalib.second.size() !=  nFields){
00190       cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the number of fields!" << endl;
00191     }
00192     
00193     theMap.insert(wireCalib);
00194   }
00195 }
00196 
00197 // Add to the map the calibration consts for a given key 
00198 void DTCalibrationMap::addCell(Key theKey, const CalibConsts& calibConst) {
00199   if(!checkGranularity(theKey))
00200     throw cms::Exception("addCell") << "DTCalibrationMap:" << endl
00201                                     << "The added key is not compatible with the selected granularity"
00202                                     << endl;
00203 
00204   theMap[theKey] = calibConst;
00205 }
00206 
00207 // Write the calibration consts to a file 
00208 void DTCalibrationMap::writeConsts(const string& outputFileName) const {
00209   ofstream out(outputFileName.c_str());
00210   for(map<Key,CalibConsts>::const_iterator iter = theMap.begin();
00211       iter != theMap.end() ; iter++) {
00212     
00213     out << (*iter).first.wheel() << ' '
00214       << (*iter).first.station() << ' '
00215       << (*iter).first.sector() << ' '
00216       << (*iter).first.superlayer() << ' '
00217       << (*iter).first.layer() << ' '
00218       << (*iter).first.wire() << ' ';
00219     copy((*iter).second.begin(), (*iter).second.end(),
00220          ostream_iterator<float>(out, " "));
00221     out << endl;
00222   }
00223 }
00224 
00225 
00226   // Check the consistency of a given key with the selected granularity
00227 bool DTCalibrationMap::checkGranularity(Key aKey) const {
00228   bool ret = true;
00229 
00230   // Check that the key is consistent with the given granularity
00231   if(theGranularity == byChamber) {
00232     if(aKey.superlayer() || aKey.layer() || aKey.wire()) {
00233       ret = false;
00234     }
00235   } else if(theGranularity == bySL) {
00236     if(aKey.layer() || aKey.wire()) {
00237       ret = false;
00238     }
00239   } else if(theGranularity == byLayer) {
00240     if(aKey.wire()) {
00241       ret = false;
00242     }
00243   } else if(theGranularity == byWire) {
00244     if(aKey.wire() == 0) {
00245       ret = false;
00246     }
00247   } 
00248   return ret;
00249 }

Generated on Tue Jun 9 17:25:28 2009 for CMSSW by  doxygen 1.5.4