CMS 3D CMS Logo

EcalPedOffset.cc
Go to the documentation of this file.
1 // TODO: fix header here?
11 #include <fstream>
12 #include <iostream>
13 #include <memory>
14 
16 
18 
24 
26 
29 
33 
34 using namespace cms;
35 using namespace edm;
36 
39  : m_barrelDigiCollection(paramSet.getParameter<edm::InputTag>("EBdigiCollection")),
40  m_endcapDigiCollection(paramSet.getParameter<edm::InputTag>("EEdigiCollection")),
41  m_headerCollection(paramSet.getParameter<edm::InputTag>("headerCollection")),
42  m_xmlFile(paramSet.getParameter<std::string>("xmlFile")),
43  m_DACmin(paramSet.getUntrackedParameter<int>("DACmin", 0)),
44  m_DACmax(paramSet.getUntrackedParameter<int>("DACmax", 256)),
45  m_RMSmax(paramSet.getUntrackedParameter<double>("RMSmax", 2)),
46  m_bestPed(paramSet.getUntrackedParameter<int>("bestPed", 200)),
47  m_dbHostName(paramSet.getUntrackedParameter<std::string>("dbHostName", "0")),
48  m_dbName(paramSet.getUntrackedParameter<std::string>("dbName", "0")),
49  m_dbUserName(paramSet.getUntrackedParameter<std::string>("dbUserName")),
50  m_dbPassword(paramSet.getUntrackedParameter<std::string>("dbPassword")),
51  m_dbHostPort(paramSet.getUntrackedParameter<int>("dbHostPort", 1521)),
52  m_create_moniov(paramSet.getUntrackedParameter<bool>("createMonIOV", false)),
53  m_location(paramSet.getUntrackedParameter<std::string>("location", "H4")),
54  m_run(-1),
55  m_plotting(paramSet.getParameter<std::string>("plotting")),
56  m_maxSlopeAllowed_(paramSet.getUntrackedParameter<double>("maxSlopeAllowed", -29)),
57  m_minSlopeAllowed_(paramSet.getUntrackedParameter<double>("minSlopeAllowed", -18)),
58  m_maxChi2OverNDFAllowed_(paramSet.getUntrackedParameter<double>("maxChi2OverNDF", 5)) {
59  edm::LogInfo("EcalPedOffset") << " reading "
60  << " m_DACmin: " << m_DACmin << " m_DACmax: " << m_DACmax << " m_RMSmax: " << m_RMSmax
61  << " m_bestPed: " << m_bestPed;
62 }
63 
64 // -----------------------------------------------------------------------------
65 
68  for (std::map<int, TPedValues *>::iterator mapIt = m_pedValues.begin(); mapIt != m_pedValues.end(); ++mapIt)
69  delete mapIt->second;
70  for (std::map<int, TPedResult *>::iterator mapIt = m_pedResult.begin(); mapIt != m_pedResult.end(); ++mapIt)
71  delete mapIt->second;
72 }
73 
74 // -----------------------------------------------------------------------------
75 
77 void EcalPedOffset::beginRun(Run const &, EventSetup const &eventSetup) {
78  LogDebug("EcalPedOffset") << "entering beginRun...";
79 
81  eventSetup.get<EcalMappingRcd>().get(handle);
82  ecalElectronicsMap_ = handle.product();
83 }
84 
85 // -----------------------------------------------------------------------------
86 
88 void EcalPedOffset::analyze(Event const &event, EventSetup const &eventSetup) {
89  LogDebug("EcalPedOffset") << "entering analyze ...";
90 
91  // get the headers
92  // (one header for each supermodule)
94  event.getByLabel(m_headerCollection, DCCHeaders);
95 
96  std::map<int, int> DACvalues;
97 
98  if (m_run == -1)
99  m_run = event.id().run();
100 
101  // loop over the headers
102  for (EcalRawDataCollection::const_iterator headerItr = DCCHeaders->begin(); headerItr != DCCHeaders->end();
103  ++headerItr) {
104  EcalDCCHeaderBlock::EcalDCCEventSettings settings = headerItr->getEventSettings();
105  int FEDid = 600 + headerItr->id();
106  DACvalues[FEDid] = settings.ped_offset;
107  LogDebug("EcalPedOffset") << "Found FED: " << FEDid << " in DCC header";
108  }
109 
110  bool barrelDigisFound = true;
111  bool endcapDigisFound = true;
112  // get the barrel digis
113  // (one digi for each crystal)
115  event.getByLabel(m_barrelDigiCollection, barrelDigis);
116  if (!barrelDigis.isValid()) {
117  edm::LogError("EcalPedOffset") << "Error! can't get the product " << m_barrelDigiCollection
118  << "; not reading barrel digis";
119  barrelDigisFound = false;
120  }
121 
122  if (barrelDigis->empty()) {
123  edm::LogInfo("EcalPedOffset") << "Size of EBDigiCollection is zero;"
124  << " not reading barrel digis";
125  barrelDigisFound = false;
126  }
127 
128  // get the endcap digis
129  // (one digi for each crystal)
131  event.getByLabel(m_endcapDigiCollection, endcapDigis);
132  if (!endcapDigis.isValid()) {
133  edm::LogError("EcalPedOffset") << "Error! can't get the product " << m_endcapDigiCollection
134  << "; not reading endcap digis";
135  endcapDigisFound = false;
136  }
137 
138  if (endcapDigis->empty()) {
139  edm::LogInfo("EcalPedOffset") << "Size of EEDigiCollection is zero;"
140  << " not reading endcap digis";
141  endcapDigisFound = false;
142  }
143 
144  if (barrelDigisFound)
145  readDACs(barrelDigis, DACvalues);
146  if (endcapDigisFound)
147  readDACs(endcapDigis, DACvalues);
148  if (!barrelDigisFound && !endcapDigisFound)
149  edm::LogError("EcalPedOffset") << "No digis found in the event!";
150 }
151 
152 // -----------------------------------------------------------------------------
153 
154 void EcalPedOffset::readDACs(const edm::Handle<EBDigiCollection> &pDigis, const std::map<int, int> &_DACvalues) {
155  std::map<int, int> DACvalues = _DACvalues;
156  // loop over the digis
157  for (EBDigiCollection::const_iterator itdigi = pDigis->begin(); itdigi != pDigis->end(); ++itdigi) {
158  int gainId = ((EBDataFrame)(*itdigi)).sample(0).gainId();
159  EBDetId detId = EBDetId(itdigi->id());
161  int FEDid = 600 + elecId.dccId();
162  int crystalId = detId.ic();
163 
164  // TODO: Behavior here
165  if (DACvalues.find(FEDid) == DACvalues.end()) {
166  edm::LogError("EcalPedOffset") << "Error! EB DCCid of digi does not "
167  "match any DCCid found in DCC headers"
168  << FEDid;
169  }
170 
171  if (!m_pedValues.count(FEDid)) {
172  LogDebug("EcalPedOffset") << "Inserting new TPedValues object for FED:" << FEDid;
173  m_pedValues[FEDid] = new TPedValues(m_RMSmax, m_bestPed);
174  }
175 
176  // loop over the samples
177  for (int iSample = 0; iSample < EcalDataFrame::MAXSAMPLES; ++iSample) {
178  m_pedValues[FEDid]->insert(
179  gainId, crystalId, DACvalues[FEDid], ((EBDataFrame)(*itdigi)).sample(iSample).adc(), crystalId);
180  }
181 
182  } // end loop over digis
183 }
184 
185 // -----------------------------------------------------------------------------
186 
187 void EcalPedOffset::readDACs(const edm::Handle<EEDigiCollection> &pDigis, const std::map<int, int> &_DACvalues) {
188  std::map<int, int> DACvalues = _DACvalues;
189  // loop over the digis
190  for (EEDigiCollection::const_iterator itdigi = pDigis->begin(); itdigi != pDigis->end(); ++itdigi) {
191  int gainId = ((EEDataFrame)(*itdigi)).sample(0).gainId();
192  // int gainId = itdigi->sample(0).gainId();
193  EEDetId detId = EEDetId(itdigi->id());
195  int FEDid = 600 + elecId.dccId();
196  int crystalId = 25 * elecId.towerId() + 5 * elecId.stripId() + elecId.xtalId();
197  int endcapCrystalId = 100 * elecId.towerId() + 5 * (elecId.stripId() - 1) + elecId.xtalId();
198 
199  // TODO: Behavior here
200  if (DACvalues.find(FEDid) == DACvalues.end()) {
201  edm::LogError("EcalPedOffset") << "Error! EE DCCid of digi does not "
202  "match any DCCid found in DCC headers: "
203  << FEDid;
204  }
205 
206  if (!m_pedValues.count(FEDid)) {
207  LogDebug("EcalPedOffset") << "Inserting new TPedValues object for FED:" << FEDid;
208  m_pedValues[FEDid] = new TPedValues(m_RMSmax, m_bestPed);
209  }
210 
211  // loop over the samples
212  for (int iSample = 0; iSample < EcalDataFrame::MAXSAMPLES; ++iSample) {
213  m_pedValues[FEDid]->insert(
214  gainId, crystalId, DACvalues[FEDid], ((EEDataFrame)(*itdigi)).sample(iSample).adc(), endcapCrystalId);
215  }
216 
217  } // end loop over digis
218 }
219 
220 // -----------------------------------------------------------------------------
221 
224  for (std::map<int, TPedValues *>::const_iterator smPeds = m_pedValues.begin(); smPeds != m_pedValues.end();
225  ++smPeds) {
226  m_pedResult[smPeds->first] = new TPedResult((smPeds->second)->terminate(m_DACmin, m_DACmax));
227  }
228  edm::LogInfo("EcalPedOffset") << " results map size " << m_pedResult.size();
230 
231  if (m_plotting != '0')
232  makePlots();
233  if (m_dbHostName != '0')
234  writeDb();
235 }
236 
237 // -----------------------------------------------------------------------------
238 
242  LogDebug("EcalPedOffset") << " entering writeDb ...";
243 
244  // connect to the database
245  EcalCondDBInterface *DBconnection;
246  try {
247  LogInfo("EcalPedOffset") << "Opening DB connection with TNS_ADMIN ...";
249  } catch (std::runtime_error &e) {
250  LogError("EcalPedOffset") << e.what();
251  if (!m_dbHostName.empty()) {
252  try {
253  LogInfo("EcalPedOffset") << "Opening DB connection without TNS_ADMIN ...";
255  } catch (std::runtime_error &e) {
256  LogError("EcalPedOffset") << e.what();
257  return;
258  }
259  } else
260  return;
261  }
262 
263  // define the query for RunIOV to get the right place in the database
264  RunTag runtag;
265  LocationDef locdef;
266  RunTypeDef rundef;
267  locdef.setLocation(m_location);
268 
269  runtag.setGeneralTag("PEDESTAL-OFFSET");
270  rundef.setRunType("PEDESTAL-OFFSET");
271  // rundef.setRunType ("TEST");
272  // runtag.setGeneralTag ("TEST");
273 
274  runtag.setLocationDef(locdef);
275  runtag.setRunTypeDef(rundef);
276 
277  run_t run = m_run; // FIXME dal config file
278  // RunIOV runiov = DBconnection->fetchRunIOV (&runtag, run);
279  RunIOV runiov = DBconnection->fetchRunIOV(m_location, run);
280 
281  // MonRunIOV
282  MonVersionDef monverdef;
283  monverdef.setMonitoringVersion("test01");
284  MonRunTag montag;
285  montag.setMonVersionDef(monverdef);
286  montag.setGeneralTag("CMSSW");
287 
288  subrun_t subrun = 1; // hardcoded!
289 
290  MonRunIOV moniov;
291 
292  try {
293  runtag = runiov.getRunTag();
294  moniov = DBconnection->fetchMonRunIOV(&runtag, &montag, run, subrun);
295  } catch (std::runtime_error &e) {
296  if (m_create_moniov) {
297  // if not already in the DB create a new MonRunIOV
298  Tm startSubRun;
299  startSubRun.setToCurrentGMTime();
300  // setup the MonIOV
301  moniov.setRunIOV(runiov);
302  moniov.setSubRunNumber(subrun);
303  moniov.setSubRunStart(startSubRun);
304  moniov.setMonRunTag(montag);
305  LogDebug("EcalPedOffset") << " creating a new MonRunIOV";
306  } else {
307  edm::LogError("EcalPedOffset") << " no MonRunIOV existing in the DB";
308  edm::LogError("EcalPedOffset") << " the result will not be stored into the DB";
309  if (DBconnection) {
310  delete DBconnection;
311  }
312  return;
313  }
314  }
315 
316  // create the table to be filled and the map to be inserted
317  EcalLogicID ecid;
318  std::map<EcalLogicID, MonPedestalOffsetsDat> DBdataset;
319  MonPedestalOffsetsDat DBtable;
320 
321  // fill the table
322 
323  // loop over the super-modules
324  for (std::map<int, TPedResult *>::const_iterator result = m_pedResult.begin(); result != m_pedResult.end();
325  ++result) {
326  // loop over the crystals
327  for (int xtal = 0; xtal < 1700; ++xtal) {
328  DBtable.setDACG1(result->second->m_DACvalue[2][xtal]);
329  DBtable.setDACG6(result->second->m_DACvalue[1][xtal]);
330  DBtable.setDACG12(result->second->m_DACvalue[0][xtal]);
331  DBtable.setTaskStatus(true); // FIXME to be set correctly
332 
333  // fill the table
334  if (DBconnection) {
335  try {
336  int fedid = result->first;
337  int eid = m_pedValues[fedid]->getCrystalNumber(xtal);
338  // If eid is zero, that crystal was not present in digis
339  if (eid == 0)
340  continue;
341 
342  if (fedid >= 601 && fedid <= 609) {
343  // Add the FEDid part in for DB
344  eid = eid + 10000 * (fedid - 600);
345  ecid = DBconnection->getEcalLogicID("EE_elec_crystal_number", eid);
346  } else if (fedid >= 610 && fedid <= 627) {
347  ecid = DBconnection->getEcalLogicID("EB_crystal_number", fedid - 610 + 19, eid);
348  } else if (fedid >= 628 && fedid <= 645) {
349  ecid = DBconnection->getEcalLogicID("EB_crystal_number", fedid - 628 + 1, eid);
350  } else if (fedid >= 646 && fedid <= 654) {
351  // Add the FEDid part in for DB
352  eid = eid + 10000 * (fedid - 600);
353  ecid = DBconnection->getEcalLogicID("EE_elec_crystal_number", eid);
354  } else
355  LogError("EcalPedOffset") << "FEDid is out of range 601-654";
356 
357  DBdataset[ecid] = DBtable;
358  } catch (std::runtime_error &e) {
359  edm::LogError("EcalPedOffset") << e.what();
360  }
361  }
362  } // loop over the crystals
363  } // loop over the super-modules
364 
365  // insert the map of tables in the database
366  if (DBconnection) {
367  try {
368  LogDebug("EcalPedOffset") << "Inserting dataset ... " << std::flush;
369  if (!DBdataset.empty())
370  DBconnection->insertDataSet(&DBdataset, &moniov);
371  LogDebug("EcalPedOffset") << "done.";
372  } catch (std::runtime_error &e) {
373  edm::LogError("EcalPedOffset") << e.what();
374  }
375  }
376 
377  if (DBconnection) {
378  delete DBconnection;
379  }
380 }
381 
382 // -----------------------------------------------------------------------------
383 
386  // loop over the super-modules
387  for (std::map<int, TPedResult *>::const_iterator smRes = m_pedResult.begin(); smRes != m_pedResult.end(); ++smRes) {
388  std::string thisSMFileName = fileName;
389  // open the output stream
390  thisSMFileName += "_";
391  thisSMFileName += intToString(smRes->first);
392  thisSMFileName += ".xml";
393  std::ofstream xml_outfile;
394  xml_outfile.open(thisSMFileName.c_str());
395 
396  // write the header file
397  xml_outfile << "<offsets>" << std::endl;
398  xml_outfile << "<PEDESTAL_OFFSET_RELEASE VERSION_ID = \"SM1_VER1\"> \n";
399  xml_outfile << " <RELEASE_ID>RELEASE_1</RELEASE_ID>\n";
400  xml_outfile << " <SUPERMODULE>";
401  xml_outfile << smRes->first;
402  xml_outfile << "</SUPERMODULE>\n";
403  xml_outfile << " <TIME_STAMP> 070705 </TIME_STAMP>" << std::endl;
404 
405  // loop over the crystals
406  for (int xtal = 0; xtal < 1700; ++xtal) {
407  int crystalNumber = m_pedValues[smRes->first]->getCrystalNumber(xtal);
408  if (crystalNumber == 0)
409  continue;
410  xml_outfile << " <PEDESTAL_OFFSET>\n";
411  xml_outfile << " <HIGH>" << ((smRes->second)->m_DACvalue)[0][xtal] << "</HIGH>\n";
412  xml_outfile << " <MED>" << ((smRes->second)->m_DACvalue)[1][xtal] << "</MED>\n";
413  xml_outfile << " <LOW>" << ((smRes->second)->m_DACvalue)[2][xtal] << "</LOW>\n";
414  xml_outfile << " <CRYSTAL> " << crystalNumber << " </CRYSTAL>\n";
415  xml_outfile << " </PEDESTAL_OFFSET>" << std::endl;
416  }
417 
418  // close the open tags
419  xml_outfile << " </PEDESTAL_OFFSET_RELEASE>" << std::endl;
420  xml_outfile << "</offsets>" << std::endl;
421  xml_outfile.close();
422  } // loop over the super-modules
423 }
424 
425 // -----------------------------------------------------------------------------
426 
429  LogDebug("EcalPedOffset") << " entering makePlots ...";
430 
431  edm::LogInfo("EcalPedOffset") << " map size: " << m_pedValues.size();
432 
433  // create the ROOT file
434  m_plotting += ".root";
435 
436  TFile *rootFile = new TFile(m_plotting.c_str(), "RECREATE");
437 
438  // loop over the supermodules
439  for (std::map<int, TPedValues *>::const_iterator smPeds = m_pedValues.begin(); smPeds != m_pedValues.end();
440  ++smPeds) {
441  // make a folder in the ROOT file
442  char folderName[120];
443  sprintf(folderName, "FED%02d", smPeds->first);
444  rootFile->mkdir(folderName);
445  smPeds->second->makePlots(rootFile, folderName, m_maxSlopeAllowed_, m_minSlopeAllowed_, m_maxChi2OverNDFAllowed_);
446  }
447 
448  rootFile->Close();
449  delete rootFile;
450 
451  LogDebug("EcalPedOffset") << " DONE";
452 }
453 
454 // -----------------------------------------------------------------------------
455 
456 // convert an int to a string
458  // outputs the number into the string stream and then flushes
459  // the buffer (makes sure the output is put into the stream)
460  std::ostringstream myStream;
461  myStream << num << std::flush;
462  return (myStream.str()); // returns the string form of the stringstream object
463 }
#define LogDebug(id)
void setTaskStatus(bool status)
double m_maxChi2OverNDFAllowed_
max chi2/ndf allowed for linearity test
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
int run_t
Definition: CaliIOV.h:11
edm::InputTag m_barrelDigiCollection
secondary name given to collection of digis
Definition: EcalPedOffset.h:61
std::string m_dbHostName
database host name
Definition: EcalPedOffset.h:76
MonRunIOV fetchMonRunIOV(RunTag *runtag, MonRunTag *montag, run_t run, subrun_t monrun) noexcept(false)
EcalLogicID getEcalLogicID(std::string name, int id1=EcalLogicID::NULLID, int id2=EcalLogicID::NULLID, int id3=EcalLogicID::NULLID, std::string mapsTo="") noexcept(false)
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
Definition: RunTag.h:13
std::string m_dbName
database name
Definition: EcalPedOffset.h:78
void setRunIOV(const RunIOV &iov)
Definition: MonRunIOV.cc:36
std::string intToString(int num)
std::vector< T >::const_iterator const_iterator
~EcalPedOffset() override
Destructor.
std::string m_xmlFile
name of the xml file to be saved
Definition: EcalPedOffset.h:65
const_iterator begin() const
double m_minSlopeAllowed_
min slope (in magnitude) allowed for linearity test
Definition: EcalPedOffset.h:98
void setToCurrentGMTime()
Definition: Tm.cc:141
void setGeneralTag(std::string tag)
Definition: MonRunTag.cc:21
std::string m_dbPassword
database user password
Definition: EcalPedOffset.h:82
std::string m_plotting
the root file where to store the detail plots
Definition: EcalPedOffset.h:94
void endJob(void) override
EndJob.
int m_dbHostPort
database
Definition: EcalPedOffset.h:84
std::string m_dbUserName
database user name
Definition: EcalPedOffset.h:80
RunTag getRunTag() const
Definition: RunIOV.cc:58
EcalElectronicsId getElectronicsId(const DetId &id) const
Get the electronics id for this det id.
void writeDb()
WriteDB.
void readDACs(const edm::Handle< EBDigiCollection > &pDigis, const std::map< int, int > &DACvalues)
RunIOV fetchRunIOV(RunTag *tag, run_t run) noexcept(false)
int subrun_t
Definition: MODRunIOV.h:11
void makePlots()
create the plots of the DAC pedestal trend
void setSubRunNumber(subrun_t subrun)
Definition: MonRunIOV.cc:45
constexpr int adc(sample_type sample)
get the ADC sample (12 bits)
void setMonVersionDef(const MonVersionDef &ver)
Definition: MonRunTag.cc:30
bool m_create_moniov
Definition: EcalPedOffset.h:87
void setSubRunStart(const Tm &start)
Definition: MonRunIOV.cc:54
bool isValid() const
Definition: HandleBase.h:70
int dccId() const
get the DCC (Ecal Local DCC value not global one) id
void beginRun(edm::Run const &, edm::EventSetup const &eventSetup) override
BeginRun.
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
std::map< int, TPedResult * > m_pedResult
Definition: EcalPedOffset.h:68
int ic() const
get ECAL/crystal number inside SM
Definition: EBDetId.cc:41
Namespace of DDCMS conversion namespace.
void writeXMLFiles(std::string fileName)
write the results into xml format
const_iterator end() const
EcalPedOffset(const edm::ParameterSet &ps)
Constructor.
edm::InputTag m_headerCollection
name of module/plugin/producer making headers
Definition: EcalPedOffset.h:63
int m_run
run number
Definition: EcalPedOffset.h:91
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:21
void setMonRunTag(const MonRunTag &tag)
Definition: MonRunIOV.cc:27
std::map< int, TPedValues * > m_pedValues
Definition: EcalPedOffset.h:67
boost::transform_iterator< IterHelp, boost::counting_iterator< int > > const_iterator
void setMonitoringVersion(std::string ver)
const_iterator end() const
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
const EcalElectronicsMapping * ecalElectronicsMap_
Definition: EcalPedOffset.h:55
HLT enums.
void insertDataSet(const std::map< EcalLogicID, DATT > *data, IOVT *iov) noexcept(false)
T get() const
Definition: EventSetup.h:73
void setLocation(std::string loc)
Definition: LocationDef.cc:20
std::string m_location
Definition: EcalPedOffset.h:89
static constexpr int MAXSAMPLES
Definition: EcalDataFrame.h:48
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
double m_maxSlopeAllowed_
max slope (in magnitude) allowed for linearity test
Definition: EcalPedOffset.h:96
edm::InputTag m_endcapDigiCollection
secondary name given to collection of digis
Definition: EcalPedOffset.h:62
Definition: RunIOV.h:13
T const * product() const
Definition: ESHandle.h:86
Definition: Tm.h:13
const_iterator begin() const
Definition: event.py:1
Definition: Run.h:45
void analyze(edm::Event const &event, edm::EventSetup const &eventSetup) override
! Analyze