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