CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTCalibrationMap.cc
Go to the documentation of this file.
1 
2 /*
3  * See header file for a description of this class.
4  *
5  * \author G. Cerminara - INFN Torino
6  */
7 
9 
12 
13 #include <iostream>
14 #include <fstream>
15 
16 #include <sstream>
17 #include <algorithm>
18 #include <iterator>
19 
20 using namespace std;
21 using namespace edm;
22 
24  nFields = pset.getUntrackedParameter<int>("nFields", 5);
25  calibConstFileName = pset.getUntrackedParameter<string>("calibConstFileName", "dummy.txt");
26  calibConstGranularity = pset.getUntrackedParameter<string>("calibConstGranularity","bySL");
27 
28  // Initialize correctly the enum which specify the granularity for the calibration
29  if(calibConstGranularity == "byWire") {
30  theGranularity = byWire;
31  } else if(calibConstGranularity == "byLayer"){
32  theGranularity = byLayer;
33  } else if(calibConstGranularity == "bySL") {
34  theGranularity = bySL;
35  } else {
36  theGranularity = byChamber;
37  if(!(calibConstGranularity == "byChamber")) {
38  cout << "[DTCalibrationMap]###Warning: Check parameter calibConstGranularity: "
39  << calibConstGranularity << " options not available!" << endl;
40  }
41  }
42  readConsts(calibConstFileName);
43 }
44 
45 
46 
48 
49 
50 
51 // Return the t_trig (ns) for a particular wire
52 float DTCalibrationMap::tTrig(DTWireId wireId) const {
53  return getField(wireId, 0);
54 }
55 
56 
57 
58 // Return the sigma of the t_trig (ns) for a particular wire
60  return getField(wireId, 1);
61 }
62 
63 
64 
65 // Return the kfactor for a particular wire
66 float DTCalibrationMap::kFactor(DTWireId wireId) const {
67  return getField(wireId, 2);
68 }
69 
70 
71 
72 // Return the mean drift velocity for a particular wire (cm/ns)
74  return getField(wireId, 3);
75 }
76 
77 
78 
79 // Return the sigma of the mean drift velocity for a particular wire (cm/ns)
81  return getField(wireId, 4);
82 }
83 
84 
85 
86 // Get a key to read calibration constants for a particular wire
87 // with the given granularity
89  if (theGranularity == byChamber){
90  return Key(wireId.chamberId(), 0, 0, 0);
91  } else if (theGranularity == bySL) {
92  return Key(wireId.superlayerId(), 0, 0);
93  } else if (theGranularity == byLayer) {
94  return Key(wireId.layerId(), 0);
95  } else {
96  return Key(wireId);
97  }
98 }
99 
100 
101 
102 // Get from the map the calibration constants for a particular key
104  // Create a cache
105  static pair<Key, CalibConsts> cache;
106 
107  // Get the key
108  Key theKey = getKey(wireId);
109 
110  // Check if the result is already cached
111  if ( theKey == cache.first ) {
112  return &(cache.second);
113  }
114 
115  // Look for the given key into the map
116  map<Key, CalibConsts>::const_iterator res = theMap.find(theKey);
117  if (res != theMap.end()) {
118  cache = (*res);
119  return &((*res).second);
120  } else {
121  return 0;
122  }
123 }
124 
125 
126 
127 // Get a particular number (field) between all the calibration
128 // constants available for a particluar wire
129 float DTCalibrationMap::getField(DTWireId wireId, int field) const {
130  const CalibConsts* cals = getConsts(wireId);
131  if (cals == 0) {
132  throw cms::Exception("NoCalibConsts") << "DTCalibrationMap:" << endl
133  << "No parameters for wire: " << wireId << endl
134  << "Check the " << calibConstFileName << " file!" << endl;
135  } else {
136  return (*(cals))[field];
137  }
138 }
139 
140 
141 
142 
143 // Read the calibration consts from a file
145  ifstream file(inputFileName.c_str());
146  // Check if the file exists
147  if(!file) {
148  cout << "[DTCalibrationMap]***Warning: File: " << inputFileName
149  << " not found in current directory!!!" << endl;
150  }
151 
152  string line;
153 
154  // The numbers to be read to build the key
155  int wheel_id = 0;
156  int station_id = 0;
157  int sector_id = 0;
158  int superlayer_id = 0;
159  int layer_id = 0;
160  int wire_id = 0;
161 
162  // Read all the lines
163  while (getline(file,line)) {
164  if( line == "" || line[0] == '#' ) continue; // Skip comments and empty lines
165  stringstream linestr;
166  linestr << line;
167 
168  pair<Key, CalibConsts> wireCalib;
169 
170  linestr >> wheel_id
171  >> station_id
172  >> sector_id
173  >> superlayer_id
174  >> layer_id
175  >> wire_id;
176 
177  // Build the key
178  wireCalib.first = Key( wheel_id,
179  station_id,
180  sector_id,
181  superlayer_id,
182  layer_id,
183  wire_id);
184 
185  if(!checkGranularity(wireCalib.first))
186  cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the selected granularity!" << endl;
187 
188 
189  // Read the calibration constants
190  copy(istream_iterator<float>(linestr),
191  istream_iterator<float>(),
192  back_inserter(wireCalib.second));
193 
194  if(wireCalib.second.size() < nFields){
195  cout << "[DTCalibrationMap]***Warning: the CalibConstFile is not consistent with the number of fields!" << endl;
196  }
197 
198  theMap.insert(wireCalib);
199  }
200 }
201 
202 // Add to the map the calibration consts for a given key
203 void DTCalibrationMap::addCell(Key theKey, const CalibConsts& calibConst) {
204  if(!checkGranularity(theKey))
205  throw cms::Exception("addCell") << "DTCalibrationMap:" << endl
206  << "The added key is not compatible with the selected granularity"
207  << endl;
208 
209  theMap[theKey] = calibConst;
210 }
211 
212 // Write the calibration consts to a file
214  ofstream out(outputFileName.c_str());
215  for(map<Key,CalibConsts>::const_iterator iter = theMap.begin();
216  iter != theMap.end() ; ++iter) {
217 
218  out << (*iter).first.wheel() << ' '
219  << (*iter).first.station() << ' '
220  << (*iter).first.sector() << ' '
221  << (*iter).first.superlayer() << ' '
222  << (*iter).first.layer() << ' '
223  << (*iter).first.wire() << ' ';
224  copy((*iter).second.begin(), (*iter).second.end(),
225  ostream_iterator<float>(out, " "));
226  out << endl;
227  }
228 }
229 
230 
231  // Check the consistency of a given key with the selected granularity
233  bool ret = true;
234 
235  // Check that the key is consistent with the given granularity
236  if(theGranularity == byChamber) {
237  if(aKey.superlayer() || aKey.layer() || aKey.wire()) {
238  ret = false;
239  }
240  } else if(theGranularity == bySL) {
241  if(aKey.layer() || aKey.wire()) {
242  ret = false;
243  }
244  } else if(theGranularity == byLayer) {
245  if(aKey.wire()) {
246  ret = false;
247  }
248  } else if(theGranularity == byWire) {
249  if(aKey.wire() == 0) {
250  ret = false;
251  }
252  }
253  return ret;
254 }
T getUntrackedParameter(std::string const &, T const &) const
tuple ret
prodAgent to be discontinued
Key getKey(DTWireId wireId) const
void writeConsts(const std::string &outputFileName) const
DTCalibrationMap(const edm::ParameterSet &pset)
Constructor.
DTChamberId chamberId() const
Return the corresponding ChamberId.
float kFactor(DTWireId wireId) const
Return the kfactor for a particular wire.
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:59
std::string getKey(const DetId &det)
float meanVDrift(DTWireId wireId) const
Return the mean drift velocity for a particular wire (cm/ns)
float getField(DTWireId wireId, int field) const
std::vector< float > CalibConsts
bool checkGranularity(Key aKey) const
virtual ~DTCalibrationMap()
Destructor.
void addCell(Key wireId, const CalibConsts &calibConst)
float sigma_tTrig(DTWireId wireId) const
Return the sigma of the t_trig (ns) for a particular wire.
float tTrig(DTWireId wireId) const
Return the t_trig (ns) for a particular wire.
int wire() const
Return the wire number.
Definition: DTWireId.h:56
int superlayer() const
Return the superlayer number (deprecated method name)
float sigma_meanVDrift(DTWireId wireId) const
Return the sigma of the mean drift velocity for a particular wire (cm/ns)
const CalibConsts * getConsts(DTWireId wireId) const
DTLayerId layerId() const
Return the corresponding LayerId.
Definition: DTWireId.h:62
tuple cout
Definition: gather_cfg.py:145
void readConsts(const std::string &inputFileName)