CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ESTrivialConditionRetriever.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 
8 
10 
11 //#include "DataFormats/Provenance/interface/Timestamp.h"
12 
14 
15 using namespace edm;
16 
18  // initilize parameters used to produce cond DB objects
19  adcToGeVLowConstant_ = ps.getUntrackedParameter<double>("adcToGeVLowConstant", 1.0);
20  adcToGeVHighConstant_ = ps.getUntrackedParameter<double>("adcToGeVHighConstant", 1.0);
21 
22  intercalibConstantMean_ = ps.getUntrackedParameter<double>("intercalibConstantMean", 1.0);
23  intercalibConstantSigma_ = ps.getUntrackedParameter<double>("intercalibConstantSigma", 0.0);
24 
25  // intercalibErrorMean_ = ps.getUntrackedParameter<double>("IntercalibErrorMean",0.0);
26 
27  ESpedMean_ = ps.getUntrackedParameter<double>("ESpedMean", 200.);
28  ESpedRMS_ = ps.getUntrackedParameter<double>("ESpedRMS", 1.00);
29 
30  getWeightsFromFile_ = ps.getUntrackedParameter<bool>("getWeightsFromFile", false);
31 
32  std::string path = "CalibCalorimetry/EcalTrivialCondModules/data/";
33  std::string weightType;
34  std::ostringstream str;
35 
36  weightType = str.str();
37 
38  amplWeightsFile_ = ps.getUntrackedParameter<std::string>("amplWeightsFile", path + "ampWeightsES" + weightType);
39 
40  // default weights for MGPA shape after pedestal subtraction
41  getWeightsFromConfiguration(ps);
42 
43  producedESPedestals_ = ps.getUntrackedParameter<bool>("producedESPedestals", true);
44  producedESWeights_ = ps.getUntrackedParameter<bool>("producedESWeights", true);
45 
46  producedESADCToGeVConstant_ = ps.getUntrackedParameter<bool>("producedESADCToGeVConstant", true);
47 
48  verbose_ = ps.getUntrackedParameter<int>("verbose", 0);
49 
50  //Tell Producer what we produce
51  //setWhatproduce(this);
52  if (producedESPedestals_)
54 
55  if (producedESWeights_) {
58  }
59 
60  if (producedESADCToGeVConstant_)
62 
63  // intercalibration constants
64  producedESIntercalibConstants_ = ps.getUntrackedParameter<bool>("producedESIntercalibConstants", true);
65  intercalibConstantsFile_ = ps.getUntrackedParameter<std::string>("intercalibConstantsFile", "");
66 
67  if (producedESIntercalibConstants_) { // user asks to produce constants
68  if (intercalibConstantsFile_.empty()) { // if file provided read constants
69  // setWhatProduced (this, &ESTrivialConditionRetriever::getIntercalibConstantsFromConfiguration ) ;
70  // } else { // set all constants to 1. or smear as specified by user
72  }
73  findingRecord<ESIntercalibConstantsRcd>();
74  }
75 
76  // intercalibration constants errors
77  /* producedESIntercalibErrors_ = ps.getUntrackedParameter<bool>("producedESIntercalibErrors",true);
78  intercalibErrorsFile_ = ps.getUntrackedParameter<std::string>("intercalibErrorsFile","") ;
79 
80  if (producedESIntercalibErrors_) { // user asks to produce constants
81  if(intercalibErrorsFile_ != "") { // if file provided read constants
82  setWhatProduced (this, &ESTrivialConditionRetriever::getIntercalibErrorsFromConfiguration ) ;
83  } else { // set all constants to 1. or smear as specified by user
84  setWhatProduced (this, &ESTrivialConditionRetriever::produceESIntercalibErrors ) ;
85  }
86  findingRecord<ESIntercalibErrorsRcd> () ;
87  }
88  */
89 
90  // channel status
91  producedESChannelStatus_ = ps.getUntrackedParameter<bool>("producedESChannelStatus", true);
92  channelStatusFile_ = ps.getUntrackedParameter<std::string>("channelStatusFile", "");
93 
94  if (producedESChannelStatus_) {
95  if (!channelStatusFile_.empty()) { // if file provided read channel map
97  } else { // set all channels to working -- FIXME might be changed
99  }
100  findingRecord<ESChannelStatusRcd>();
101  }
102 
103  //Tell Finder what records we find
104  if (producedESPedestals_)
105  findingRecord<ESPedestalsRcd>();
106 
107  if (producedESWeights_) {
108  findingRecord<ESWeightStripGroupsRcd>();
109  findingRecord<ESTBWeightsRcd>();
110  }
111 
112  if (producedESADCToGeVConstant_)
113  findingRecord<ESADCToGeVConstantRcd>();
114 }
115 
117 
118 //
119 // member functions
120 //
122  const edm::IOVSyncValue& iTime,
123  edm::ValidityInterval& oValidity) {
124  if (verbose_ >= 1)
125  std::cout << "ESTrivialConditionRetriever::setIntervalFor(): record key = " << rk.name()
126  << "\ttime: " << iTime.time().value() << std::endl;
127  //For right now, we will just use an infinite interval of validity
129 }
130 
131 //produce methods
133  std::cout << " producing pedestals" << std::endl;
134  auto peds = std::make_unique<ESPedestals>();
135  ESPedestals::Item ESitem;
136  ESitem.mean = ESpedMean_;
137  ESitem.rms = ESpedRMS_;
138 
139  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
140  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
141  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
142  for (int iplane = 1; iplane <= 2; iplane++) {
143  for (int izeta = -1; izeta <= 1; ++izeta) {
144  if (izeta == 0)
145  continue;
146  try {
147  //ESDetId Plane iplane Zside izeta
148  ESDetId aPositiveId(istrip, ix, iy, iplane, izeta);
149  peds->insert(std::make_pair(aPositiveId.rawId(), ESitem));
150  } catch (cms::Exception& e) {
151  }
152  }
153  }
154  }
155  }
156  }
157  //return std::unique_ptr<ESPedestals>( peds );
158  std::cout << " produced pedestals" << std::endl;
159  return peds;
160 }
161 
162 std::unique_ptr<ESWeightStripGroups> ESTrivialConditionRetriever::produceESWeightStripGroups(
163  const ESWeightStripGroupsRcd&) {
164  auto xtalGroups = std::make_unique<ESWeightStripGroups>();
165  ESStripGroupId defaultGroupId(1);
166  std::cout << "entering produce weight groups" << std::endl;
167  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
168  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
169  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
170  for (int iplane = 1; iplane <= 2; iplane++) {
171  for (int izeta = -1; izeta <= 1; ++izeta) {
172  if (izeta == 0)
173  continue;
174  try {
175  //ESDetId Plane iplane Zside izeta
176  ESDetId anESId(istrip, ix, iy, iplane, izeta);
177  // xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
178  xtalGroups->setValue(anESId.rawId(), defaultGroupId); // define rings in eta
179  } catch (cms::Exception& e) {
180  }
181  }
182  }
183  }
184  }
185  }
186  std::cout << "done with produce weight groups" << std::endl;
187 
188  return xtalGroups;
189 }
190 
191 std::unique_ptr<ESIntercalibConstants> ESTrivialConditionRetriever::produceESIntercalibConstants(
192  const ESIntercalibConstantsRcd&) {
193  auto ical = std::make_unique<ESIntercalibConstants>();
194  std::cout << "entring produce intercalib " << std::endl;
195 
196  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
197  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
198  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
199  for (int iplane = 1; iplane <= 2; iplane++) {
200  for (int izeta = -1; izeta <= 1; ++izeta) {
201  if (izeta == 0)
202  continue;
203  try {
204  //ESDetId Plane iplane Zside izeta
205  ESDetId anESId(istrip, ix, iy, iplane, izeta);
206  double r = (double)std::rand() / (double(RAND_MAX) + double(1));
207  ical->setValue(anESId.rawId(), intercalibConstantMean_ + r * intercalibConstantSigma_);
208  } catch (cms::Exception& e) {
209  }
210  }
211  }
212  }
213  }
214  }
215  std::cout << "done produce intercalib" << std::endl;
216 
217  return ical;
218 }
219 
220 std::unique_ptr<ESADCToGeVConstant> ESTrivialConditionRetriever::produceESADCToGeVConstant(
221  const ESADCToGeVConstantRcd&) {
222  return std::make_unique<ESADCToGeVConstant>(adcToGeVLowConstant_, adcToGeVHighConstant_);
223 }
224 
226  // create weights for the test-beam
227  auto tbwgt = std::make_unique<ESTBWeights>();
228 
229  int igrp = 1;
230  ESWeightSet wgt = ESWeightSet(amplWeights_);
231  // ESWeightSet::ESWeightMatrix& mat1 = wgt.getWeights();
232 
233  tbwgt->setValue(igrp, wgt);
234 
235  return tbwgt;
236 }
237 
240 
241  if (!getWeightsFromFile_) {
242  // vampl.set(1.);
243 
244  // amplwgtv[0]= ps.getUntrackedParameter< std::vector<double> >("amplWeights", vampl);
245  } else if (getWeightsFromFile_) {
246  edm::LogInfo("ESTrivialConditionRetriever")
247  << "Reading amplitude weights from file " << edm::FileInPath(amplWeightsFile_).fullPath().c_str();
248  std::ifstream amplFile(edm::FileInPath(amplWeightsFile_).fullPath().c_str());
249  while (!amplFile.eof()) {
250  for (int j = 0; j < 2; ++j) {
251  std::vector<float> vec(3);
252  for (int k = 0; k < 3; ++k) {
253  float ww;
254  amplFile >> ww;
255  vec[k] = ww;
256  }
257  // vampl.putRow(vec);
258  }
259  }
260  } else {
261  //Not supported
262  edm::LogError("ESTrivialConditionRetriever") << "Configuration not supported. Exception is raised ";
263  throw cms::Exception("WrongConfig");
264  }
265 
266  amplWeights_ = ESWeightSet(vampl);
267 }
268 
269 // --------------------------------------------------------------------------------
270 
272  const ESChannelStatusRcd&) {
273  auto ecalStatus = std::make_unique<ESChannelStatus>();
274 
275  // start by setting all statuses to 0
276 
277  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
278  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
279  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
280  for (int iplane = 1; iplane <= 2; iplane++) {
281  for (int izeta = -1; izeta <= 1; ++izeta) {
282  if (izeta == 0)
283  continue;
284  try {
285  //ESDetId Plane iplane Zside izeta
286  ESDetId anESId(istrip, ix, iy, iplane, izeta);
287  // xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
288  ecalStatus->setValue(anESId, 0);
289  } catch (cms::Exception& e) {
290  }
291  }
292  }
293  }
294  }
295  }
296 
297  // overwrite the statuses which are in the file
298 
299  edm::LogInfo("ESTrivialConditionRetriever")
300  << "Reading channel statuses from file " << edm::FileInPath(channelStatusFile_).fullPath().c_str();
301  std::ifstream statusFile(edm::FileInPath(channelStatusFile_).fullPath().c_str());
302  if (!statusFile.good()) {
303  edm::LogError("ESTrivialConditionRetriever") << "*** Problems opening file: " << channelStatusFile_;
304  throw cms::Exception("Cannot open ECAL channel status file");
305  }
306 
307  std::string ESSubDet;
309  int hashIndex(0);
310  int status(0);
311 
312  while (!statusFile.eof()) {
313  statusFile >> ESSubDet;
314  if (ESSubDet != std::string("ES")) {
315  std::getline(statusFile, str);
316  continue;
317  } else {
318  statusFile >> hashIndex >> status;
319  }
320  // std::cout << ESSubDet << " " << hashIndex << " " << status;
321 
322  if (ESSubDet == std::string("ES")) {
323  ESDetId esid = ESDetId::unhashIndex(hashIndex);
324  ecalStatus->setValue(esid, status);
325  } else {
326  edm::LogError("ESTrivialConditionRetriever") << " *** " << ESSubDet << " is not ES ";
327  }
328  }
329  // the file is supposed to be in the form
330  // ES hashed_index status
331  // ES 132332 1 --> higher than 0 means bad
332 
333  statusFile.close();
334  return ecalStatus;
335 }
336 
338  auto ical = std::make_unique<ESChannelStatus>();
339  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
340  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
341  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
342  for (int iplane = 1; iplane <= 2; iplane++) {
343  for (int izeta = -1; izeta <= 1; ++izeta) {
344  if (izeta == 0)
345  continue;
346  try {
347  //ESDetId Plane iplane Zside izeta
348  ESDetId anESId(istrip, ix, iy, iplane, izeta);
349  // xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
350  ical->setValue(anESId, 0);
351  } catch (cms::Exception& e) {
352  }
353  }
354  }
355  }
356  }
357  }
358 
359  return ical;
360 }
361 
362 // --------------------------------------------------------------------------------
T getUntrackedParameter(std::string const &, T const &) const
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
virtual std::unique_ptr< ESIntercalibConstants > produceESIntercalibConstants(const ESIntercalibConstantsRcd &)
virtual std::unique_ptr< ESChannelStatus > produceESChannelStatus(const ESChannelStatusRcd &)
static const int IY_MAX
Definition: ESDetId.h:67
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:82
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
list status
Definition: mps_update.py:107
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:17
static const int ISTRIP_MAX
Definition: ESDetId.h:69
Log< level::Error, false > LogError
virtual std::unique_ptr< ESChannelStatus > getChannelStatusFromConfiguration(const ESChannelStatusRcd &)
virtual std::unique_ptr< ESTBWeights > produceESTBWeights(const ESTBWeightsRcd &)
virtual std::unique_ptr< ESPedestals > produceESPedestals(const ESPedestalsRcd &)
static const IOVSyncValue & beginOfTime()
Definition: IOVSyncValue.cc:88
ESTrivialConditionRetriever(const edm::ParameterSet &pset)
void getWeightsFromConfiguration(const edm::ParameterSet &ps)
virtual std::unique_ptr< ESWeightStripGroups > produceESWeightStripGroups(const ESWeightStripGroupsRcd &)
float mean
Definition: ESPedestals.h:16
math::Matrix< 2, 3 >::type ESWeightMatrix
Definition: ESWeightSet.h:12
Log< level::Info, false > LogInfo
static const int IY_MIN
Definition: ESDetId.h:65
virtual std::unique_ptr< ESADCToGeVConstant > produceESADCToGeVConstant(const ESADCToGeVConstantRcd &)
static const int IX_MAX
Definition: ESDetId.h:66
std::string fullPath() const
Definition: FileInPath.cc:161
const Timestamp & time() const
Definition: IOVSyncValue.h:42
tuple cout
Definition: gather_cfg.py:144
static ESDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: ESDetId.cc:32
#define str(s)
static const int ISTRIP_MIN
Definition: ESDetId.h:68
TimeValue_t value() const
Definition: Timestamp.h:45
static const int IX_MIN
Definition: ESDetId.h:64