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