CMS 3D CMS Logo

EcalPedOffset.cc
Go to the documentation of this file.
1 //TODO: fix header here?
11 #include <memory>
12 #include <iostream>
13 #include <fstream>
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 {
60  edm::LogInfo ("EcalPedOffset") << " reading "
61  << " m_DACmin: " << m_DACmin
62  << " m_DACmax: " << m_DACmax
63  << " m_RMSmax: " << m_RMSmax
64  << " m_bestPed: " << m_bestPed ;
65 }
66 
67 
68 // -----------------------------------------------------------------------------
69 
70 
73 {
74  for (std::map<int,TPedValues*>::iterator mapIt = m_pedValues.begin ();
75  mapIt != m_pedValues.end ();
76  ++mapIt)
77  delete mapIt->second ;
78  for (std::map<int,TPedResult*>::iterator mapIt = m_pedResult.begin ();
79  mapIt != m_pedResult.end ();
80  ++mapIt)
81  delete mapIt->second ;
82 }
83 
84 
85 // -----------------------------------------------------------------------------
86 
87 
89 void EcalPedOffset::beginRun (Run const &, EventSetup const& eventSetup)
90 {
91  LogDebug ("EcalPedOffset") << "entering beginRun..." ;
92 
94  eventSetup.get< EcalMappingRcd >().get(handle);
95  ecalElectronicsMap_ = handle.product();
96 
97 }
98 
99 
100 // -----------------------------------------------------------------------------
101 
102 
105  EventSetup const& eventSetup)
106 {
107  LogDebug ("EcalPedOffset") << "entering analyze ...";
108 
109  // get the headers
110  // (one header for each supermodule)
112  event.getByLabel(m_headerCollection, DCCHeaders);
113 
114  std::map <int,int> DACvalues;
115 
116  if(m_run==-1)
117  m_run = event.id().run();
118 
119  // loop over the headers
120  for (EcalRawDataCollection::const_iterator headerItr= DCCHeaders->begin();
121  headerItr != DCCHeaders->end ();
122  ++headerItr)
123  {
124  EcalDCCHeaderBlock::EcalDCCEventSettings settings = headerItr->getEventSettings();
125  int FEDid = 600+headerItr->id();
126  DACvalues[FEDid] = settings.ped_offset;
127  LogDebug("EcalPedOffset") << "Found FED: " << FEDid << " in DCC header";
128  }
129 
130  bool barrelDigisFound = true;
131  bool endcapDigisFound = true;
132  // get the barrel digis
133  // (one digi for each crystal)
135  event.getByLabel(m_barrelDigiCollection, barrelDigis);
136  if(!barrelDigis.isValid())
137  {
138  edm::LogError ("EcalPedOffset") << "Error! can't get the product "
139  << m_barrelDigiCollection << "; not reading barrel digis";
140  barrelDigisFound = false;
141  }
142 
143  if(barrelDigis->size()==0)
144  {
145  edm::LogInfo("EcalPedOffset") << "Size of EBDigiCollection is zero;"
146  << " not reading barrel digis";
147  barrelDigisFound = false;
148  }
149 
150  // get the endcap digis
151  // (one digi for each crystal)
153  event.getByLabel(m_endcapDigiCollection, endcapDigis);
154  if(!endcapDigis.isValid())
155  {
156  edm::LogError ("EcalPedOffset") << "Error! can't get the product "
157  << m_endcapDigiCollection << "; not reading endcap digis";
158  endcapDigisFound = false;
159  }
160 
161  if(endcapDigis->size()==0)
162  {
163  edm::LogInfo("EcalPedOffset") << "Size of EEDigiCollection is zero;"
164  << " not reading endcap digis";
165  endcapDigisFound = false;
166  }
167 
168 
169  if(barrelDigisFound)
170  readDACs(barrelDigis, DACvalues);
171  if(endcapDigisFound)
172  readDACs(endcapDigis, DACvalues);
173  if(!barrelDigisFound && !endcapDigisFound)
174  edm::LogError ("EcalPedOffset") << "No digis found in the event!";
175 
176 }
177 
178 
179 // -----------------------------------------------------------------------------
180 
181 
183  const std::map<int,int>& _DACvalues)
184 {
185  std::map<int,int> DACvalues = _DACvalues;
186  // loop over the digis
187  for (EBDigiCollection::const_iterator itdigi = pDigis->begin();
188  itdigi != pDigis->end();
189  ++itdigi)
190  {
191  int gainId = ((EBDataFrame)(*itdigi)).sample(0).gainId();
192  EBDetId detId = EBDetId(itdigi->id());
194  int FEDid = 600+elecId.dccId();
195  int crystalId = detId.ic();
196 
197  //TODO: Behavior here
198  if(DACvalues.find(FEDid)==DACvalues.end())
199  {
200  edm::LogError("EcalPedOffset")
201  << "Error! EB DCCid of digi does not match any DCCid found in DCC headers" << FEDid;
202  }
203 
204  if (!m_pedValues.count(FEDid))
205  {
206  LogDebug("EcalPedOffset") << "Inserting new TPedValues object for FED:" << FEDid;
208  }
209 
210  // loop over the samples
211  for (int iSample = 0; iSample < EcalDataFrame::MAXSAMPLES; ++iSample)
212  {
213  m_pedValues[FEDid]->insert(gainId,
214  crystalId,
215  DACvalues[FEDid],
216  ((EBDataFrame)(*itdigi)).sample(iSample).adc(),
217  crystalId);
218  }
219 
220  } //end loop over digis
221 }
222 
223 // -----------------------------------------------------------------------------
224 
225 
227  const std::map<int,int>& _DACvalues)
228 {
229  std::map<int,int> DACvalues = _DACvalues;
230  // loop over the digis
231  for (EEDigiCollection::const_iterator itdigi = pDigis->begin();
232  itdigi != pDigis->end();
233  ++itdigi)
234  {
235  int gainId = ((EEDataFrame)(*itdigi)).sample(0).gainId();
236  //int gainId = itdigi->sample(0).gainId();
237  EEDetId detId = EEDetId(itdigi->id());
239  int FEDid = 600+elecId.dccId();
240  int crystalId = 25*elecId.towerId()+5*elecId.stripId()+elecId.xtalId();
241  int endcapCrystalId = 100*elecId.towerId()+5*(elecId.stripId()-1)+elecId.xtalId();
242 
243  //TODO: Behavior here
244  if(DACvalues.find(FEDid)==DACvalues.end())
245  {
246  edm::LogError("EcalPedOffset")
247  << "Error! EE DCCid of digi does not match any DCCid found in DCC headers: " << FEDid;
248  }
249 
250  if (!m_pedValues.count(FEDid))
251  {
252  LogDebug("EcalPedOffset") << "Inserting new TPedValues object for FED:" << FEDid;
254  }
255 
256  // loop over the samples
257  for (int iSample = 0; iSample < EcalDataFrame::MAXSAMPLES; ++iSample)
258  {
259  m_pedValues[FEDid]->insert(gainId,
260  crystalId,
261  DACvalues[FEDid],
262  ((EEDataFrame)(*itdigi)).sample(iSample).adc(),
263  endcapCrystalId);
264  }
265 
266  } //end loop over digis
267 }
268 
269 // -----------------------------------------------------------------------------
270 
271 
274 {
275  for (std::map<int,TPedValues*>::const_iterator smPeds = m_pedValues.begin ();
276  smPeds != m_pedValues.end ();
277  ++smPeds)
278  {
279  m_pedResult[smPeds->first] =
280  new TPedResult ((smPeds->second)->terminate (m_DACmin, m_DACmax));
281  }
282  edm::LogInfo ("EcalPedOffset") << " results map size "
283  << m_pedResult.size ();
285 
286  if (m_plotting != '0') makePlots ();
287  if (m_dbHostName != '0') writeDb ();
288 }
289 
290 
291 // -----------------------------------------------------------------------------
292 
293 
297 {
298  LogDebug ("EcalPedOffset") << " entering writeDb ..." ;
299 
300  // connect to the database
301  EcalCondDBInterface* DBconnection ;
302  try
303  {
304  LogInfo("EcalPedOffset") << "Opening DB connection with TNS_ADMIN ...";
306  } catch (std::runtime_error &e) {
307  LogError("EcalPedOffset") << e.what();
308  if ( m_dbHostName.size() != 0 )
309  {
310  try
311  {
312  LogInfo("EcalPedOffset") << "Opening DB connection without TNS_ADMIN ...";
313  DBconnection = new EcalCondDBInterface(m_dbHostName, m_dbName,
315  } catch (std::runtime_error &e) {
316  LogError("EcalPedOffset") << e.what();
317  return;
318  }
319  }
320  else
321  return;
322  }
323 
324  // define the query for RunIOV to get the right place in the database
325  RunTag runtag ;
326  LocationDef locdef ;
327  RunTypeDef rundef ;
328  locdef.setLocation (m_location);
329 
330  runtag.setGeneralTag ("PEDESTAL-OFFSET");
331  rundef.setRunType ("PEDESTAL-OFFSET");
332  //rundef.setRunType ("TEST");
333  //runtag.setGeneralTag ("TEST");
334 
335  runtag.setLocationDef (locdef);
336  runtag.setRunTypeDef (rundef);
337 
338 
339  run_t run = m_run ; //FIXME dal config file
340  //RunIOV runiov = DBconnection->fetchRunIOV (&runtag, run);
341  RunIOV runiov = DBconnection->fetchRunIOV(m_location, run);
342 
343  // MonRunIOV
344  MonVersionDef monverdef ;
345  monverdef.setMonitoringVersion ("test01");
346  MonRunTag montag ;
347  montag.setMonVersionDef (monverdef);
348  montag.setGeneralTag ("CMSSW");
349 
350  subrun_t subrun = 1 ; //hardcoded!
351 
352  MonRunIOV moniov ;
353 
354  try{
355  runtag = runiov.getRunTag();
356  moniov = DBconnection->fetchMonRunIOV(&runtag, &montag, run, subrun);
357  }
358  catch (std::runtime_error &e) {
359  if(m_create_moniov){
360  //if not already in the DB create a new MonRunIOV
361  Tm startSubRun;
362  startSubRun.setToCurrentGMTime();
363  // setup the MonIOV
364  moniov.setRunIOV(runiov);
365  moniov.setSubRunNumber(subrun);
366  moniov.setSubRunStart(startSubRun);
367  moniov.setMonRunTag(montag);
368  LogDebug ("EcalPedOffset") <<" creating a new MonRunIOV" ;
369  }
370  else{
371  edm::LogError ("EcalPedOffset") << " no MonRunIOV existing in the DB" ;
372  edm::LogError ("EcalPedOffset") << " the result will not be stored into the DB" ;
373  if ( DBconnection ) {delete DBconnection;}
374  return;
375  }
376  }
377 
378  // create the table to be filled and the map to be inserted
379  EcalLogicID ecid ;
380  std::map<EcalLogicID, MonPedestalOffsetsDat> DBdataset ;
381  MonPedestalOffsetsDat DBtable ;
382 
383  // fill the table
384 
385  // loop over the super-modules
386  for (std::map<int,TPedResult*>::const_iterator result = m_pedResult.begin ();
387  result != m_pedResult.end ();
388  ++result)
389  {
390  // loop over the crystals
391  for (int xtal = 0 ; xtal<1700 ; ++xtal)
392  {
393  DBtable.setDACG1 (result->second->m_DACvalue[2][xtal]);
394  DBtable.setDACG6 (result->second->m_DACvalue[1][xtal]);
395  DBtable.setDACG12 (result->second->m_DACvalue[0][xtal]);
396  DBtable.setTaskStatus (1); //FIXME to be set correctly
397 
398  // fill the table
399  if ( DBconnection )
400  {
401  try
402  {
403  int fedid = result->first;
404  int eid = m_pedValues[fedid]->getCrystalNumber(xtal);
405  // If eid is zero, that crystal was not present in digis
406  if(eid==0)
407  continue;
408 
409  if (fedid >= 601 && fedid <= 609)
410  {
411  // Add the FEDid part in for DB
412  eid = eid+10000*(fedid-600);
413  ecid = DBconnection->getEcalLogicID("EE_elec_crystal_number", eid);
414  }
415  else if (fedid >= 610 && fedid <= 627)
416  {
417  ecid = DBconnection->getEcalLogicID("EB_crystal_number", fedid-610+19,
418  eid);
419  }
420  else if (fedid >= 628 && fedid <= 645)
421  {
422  ecid = DBconnection->getEcalLogicID("EB_crystal_number", fedid-628+1,
423  eid);
424  }
425  else if (fedid >= 646 && fedid <= 654)
426  {
427  // Add the FEDid part in for DB
428  eid = eid+10000*(fedid-600);
429  ecid = DBconnection->getEcalLogicID("EE_elec_crystal_number", eid);
430  }
431  else
432  LogError("EcalPedOffset") << "FEDid is out of range 601-654";
433 
434  DBdataset[ecid] = DBtable ;
435  } catch (std::runtime_error &e) {
436  edm::LogError ("EcalPedOffset") << e.what();
437  }
438  }
439  } // loop over the crystals
440  } // loop over the super-modules
441 
442  // insert the map of tables in the database
443  if ( DBconnection ) {
444  try {
445  LogDebug ("EcalPedOffset") << "Inserting dataset ... " << std::flush;
446  if ( DBdataset.size() != 0 ) DBconnection->insertDataSet (&DBdataset, &moniov);
447  LogDebug ("EcalPedOffset") << "done." ;
448  } catch (std::runtime_error &e) {
449  edm::LogError ("EcalPedOffset") << e.what ();
450  }
451  }
452 
453  if ( DBconnection ) {delete DBconnection;}
454 }
455 
456 
457 // -----------------------------------------------------------------------------
458 
459 
462 {
463  // loop over the super-modules
464  for (std::map<int,TPedResult*>::const_iterator smRes = m_pedResult.begin();
465  smRes != m_pedResult.end();
466  ++smRes)
467  {
468  std::string thisSMFileName = fileName;
469  // open the output stream
470  thisSMFileName+="_";
471  thisSMFileName+=intToString(smRes->first);
472  thisSMFileName+=".xml";
473  std::ofstream xml_outfile;
474  xml_outfile.open(thisSMFileName.c_str());
475 
476  // write the header file
477  xml_outfile<<"<offsets>"<<std::endl;
478  xml_outfile << "<PEDESTAL_OFFSET_RELEASE VERSION_ID = \"SM1_VER1\"> \n";
479  xml_outfile << " <RELEASE_ID>RELEASE_1</RELEASE_ID>\n";
480  xml_outfile << " <SUPERMODULE>";
481  xml_outfile << smRes->first;
482  xml_outfile << "</SUPERMODULE>\n";
483  xml_outfile << " <TIME_STAMP> 070705 </TIME_STAMP>" << std::endl;
484 
485  // loop over the crystals
486  for (int xtal = 0 ; xtal < 1700 ; ++xtal)
487  {
488  int crystalNumber = m_pedValues[smRes->first]->getCrystalNumber(xtal);
489  if(crystalNumber==0)
490  continue;
491  xml_outfile << " <PEDESTAL_OFFSET>\n";
492  xml_outfile << " <HIGH>" << ((smRes->second)->m_DACvalue)[0][xtal] << "</HIGH>\n";
493  xml_outfile << " <MED>" << ((smRes->second)->m_DACvalue)[1][xtal] << "</MED>\n";
494  xml_outfile << " <LOW>" << ((smRes->second)->m_DACvalue)[2][xtal] << "</LOW>\n";
495  xml_outfile << " <CRYSTAL> "<< crystalNumber << " </CRYSTAL>\n";
496  xml_outfile << " </PEDESTAL_OFFSET>" << std::endl;
497  }
498 
499  // close the open tags
500  xml_outfile << " </PEDESTAL_OFFSET_RELEASE>" << std::endl;
501  xml_outfile << "</offsets>" << std::endl;
502  xml_outfile.close ();
503  } // loop over the super-modules
504 
505 
506 }
507 
508 
509 // -----------------------------------------------------------------------------
510 
511 
514 {
515  LogDebug ("EcalPedOffset") << " entering makePlots ..." ;
516 
517  edm::LogInfo ("EcalPedOffset") << " map size: "
518  << m_pedValues.size();
519 
520  // create the ROOT file
521  m_plotting+=".root";
522 
523  TFile * rootFile = new TFile(m_plotting.c_str(),"RECREATE");
524 
525  // loop over the supermodules
526  for (std::map<int,TPedValues*>::const_iterator smPeds = m_pedValues.begin();
527  smPeds != m_pedValues.end();
528  ++smPeds)
529  {
530  // make a folder in the ROOT file
531  char folderName[120] ;
532  sprintf (folderName,"FED%02d",smPeds->first);
533  rootFile->mkdir(folderName);
534  smPeds->second->makePlots(rootFile,folderName,m_maxSlopeAllowed_,
536  }
537 
538  rootFile->Close();
539  delete rootFile;
540 
541  LogDebug ("EcalPedOffset") << " DONE";
542 }
543 
544 // -----------------------------------------------------------------------------
545 
546 // convert an int to a string
548 {
549 
550  // outputs the number into the string stream and then flushes
551  // the buffer (makes sure the output is put into the stream)
552  std::ostringstream myStream;
553  myStream << num << std::flush;
554  return(myStream.str()); //returns the string form of the stringstream object
555 }
int adc(sample_type sample)
get the ADC sample (12 bits)
#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:70
int run_t
Definition: CaliIOV.h:11
edm::InputTag m_barrelDigiCollection
secondary name given to collection of digis
Definition: EcalPedOffset.h:66
std::string m_dbHostName
database host name
Definition: EcalPedOffset.h:81
MonRunIOV fetchMonRunIOV(RunTag *runtag, MonRunTag *montag, run_t run, subrun_t monrun) noexcept(false)
boost::transform_iterator< IterHelp, boost::counting_iterator< int > > const_iterator
int gainId(sample_type sample)
get the gainId (2 bits)
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::map< int, TPedValues * > m_pedValues
Definition: EcalPedOffset.h:72
std::string m_dbName
database name
Definition: EcalPedOffset.h:83
void setRunIOV(const RunIOV &iov)
Definition: MonRunIOV.cc:53
std::string intToString(int num)
std::vector< EcalDCCHeaderBlock >::const_iterator const_iterator
std::string m_xmlFile
name of the xml file to be saved
Definition: EcalPedOffset.h:70
const_iterator begin() const
double m_minSlopeAllowed_
min slope (in magnitude) allowed for linearity test
void setToCurrentGMTime()
Definition: Tm.cc:177
void setGeneralTag(std::string tag)
Definition: MonRunTag.cc:33
std::string m_dbPassword
database user password
Definition: EcalPedOffset.h:87
std::string m_plotting
the root file where to store the detail plots
Definition: EcalPedOffset.h:99
void beginRun(edm::Run const &, edm::EventSetup const &eventSetup)
BeginRun.
int m_dbHostPort
database
Definition: EcalPedOffset.h:89
std::string m_dbUserName
database user name
Definition: EcalPedOffset.h:85
RunTag getRunTag() const
Definition: RunIOV.cc:96
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)
void analyze(edm::Event const &event, edm::EventSetup const &eventSetup)
! Analyze
void endJob(void)
EndJob.
int subrun_t
Definition: MODRunIOV.h:11
void makePlots()
create the plots of the DAC pedestal trend
virtual ~EcalPedOffset()
Destructor.
void setSubRunNumber(subrun_t subrun)
Definition: MonRunIOV.cc:67
void setMonVersionDef(const MonVersionDef &ver)
Definition: MonRunTag.cc:49
bool m_create_moniov
Definition: EcalPedOffset.h:92
void setSubRunStart(const Tm &start)
Definition: MonRunIOV.cc:84
bool isValid() const
Definition: HandleBase.h:74
int dccId() const
get the DCC (Ecal Local DCC value not global one) id
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
int ic() const
get ECAL/crystal number inside SM
Definition: EBDetId.cc:46
void writeXMLFiles(std::string fileName)
write the results into xml format
const_iterator end() const
std::map< int, TPedResult * > m_pedResult
Definition: EcalPedOffset.h:73
EcalPedOffset(const edm::ParameterSet &ps)
Constructor.
edm::InputTag m_headerCollection
name of module/plugin/producer making headers
Definition: EcalPedOffset.h:68
int m_run
run number
Definition: EcalPedOffset.h:96
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
const T & get() const
Definition: EventSetup.h:55
void setMonRunTag(const MonRunTag &tag)
Definition: MonRunIOV.cc:36
void setMonitoringVersion(std::string ver)
const_iterator end() const
const EcalElectronicsMapping * ecalElectronicsMap_
Definition: EcalPedOffset.h:60
HLT enums.
void insertDataSet(const std::map< EcalLogicID, DATT > *data, IOVT *iov) noexcept(false)
void setLocation(std::string loc)
Definition: LocationDef.cc:33
std::string m_location
Definition: EcalPedOffset.h:94
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
double m_maxSlopeAllowed_
max slope (in magnitude) allowed for linearity test
edm::InputTag m_endcapDigiCollection
secondary name given to collection of digis
Definition: EcalPedOffset.h:67
Definition: RunIOV.h:13
T const * product() const
Definition: ESHandle.h:86
Definition: Tm.h:13
static const int MAXSAMPLES
Definition: EcalDataFrame.h:48
const_iterator begin() const
Definition: event.py:1
Definition: Run.h:42