CMS 3D CMS Logo

EcalPedestalHistory.cc
Go to the documentation of this file.
1 
8 //
9 // $Id: EcalEcalPedestalHistory.cc,v 0.0 2016/05/02 jean fay Exp $
10 //
11 //
12 
13 #include "EcalPedestalHistory.h"
14 
17 
18 //#include<fstream>
19 
20 #include "TFile.h"
21 #include "TTree.h"
22 #include "TBranch.h"
23 #include "TH1.h"
24 #include "TH2.h"
25 #include "TF1.h"
26 #include "TProfile.h"
27 
28 #include <iostream>
29 #include <iomanip>
30 #include <string>
31 #include <stdexcept>
32 
33 using namespace edm;
34 using namespace cms;
35 using namespace std;
36 
37 //
38 // constants, enums and typedefs
39 //
40 //const Int_t kSample=10;
41 //
42 // static data member definitions
43 //
44 //int gainValues[3] = {12, 6, 1};
45 
46 //
47 // constructors and destructor
48 //
49 
50 //====================================================================
52  //====================================================================
53  //now do what ever initialization is needed
54  // EBDigiCollection_ = iConfig.getParameter<edm::InputTag>("EBDigiCollection");
55  runnumber_ = iConfig.getUntrackedParameter<int>("runnumber", -1);
56  ECALType_ = iConfig.getParameter<std::string>("ECALType");
57  runType_ = iConfig.getParameter<std::string>("runType");
58  startevent_ = iConfig.getUntrackedParameter<unsigned int>("startevent", 1);
59 
60  std::cout << "EcalPedestals Source handler constructor\n" << std::endl;
61  m_firstRun = static_cast<unsigned int>(atoi(iConfig.getParameter<std::string>("firstRun").c_str()));
62  m_lastRun = static_cast<unsigned int>(atoi(iConfig.getParameter<std::string>("lastRun").c_str()));
63  m_sid = iConfig.getParameter<std::string>("OnlineDBSID");
64  m_user = iConfig.getParameter<std::string>("OnlineDBUser");
65  m_pass = iConfig.getParameter<std::string>("OnlineDBPassword");
66  m_locationsource = iConfig.getParameter<std::string>("LocationSource");
67  m_location = iConfig.getParameter<std::string>("Location");
68  m_gentag = iConfig.getParameter<std::string>("GenTag");
69  std::cout << m_sid << "/" << m_user << "/" << m_pass << "/" << m_location << "/" << m_gentag << std::endl;
70 
71  vector<int> listDefaults;
72  listDefaults.push_back(-1);
73 
74  cnt_evt_ = 0;
75  // cout << "Exiting constructor" << endl;
76 } //constructor
77 
78 //========================================================================
80  //========================================================================
81  cout << "ANALYSIS FINISHED" << endl;
82 } //destructor
83 
84 //========================================================================
87 
88  cout << "Entering beginRun" << endl;
89  /* do not use any tag...
90  edm::ESHandle<EcalChannelStatus> pChannelStatus;
91  c.get<EcalChannelStatusRcd>().get(pChannelStatus);
92  const EcalChannelStatus* chStatus = pChannelStatus.product();
93  EcalChannelStatusMap::const_iterator chit;
94  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
95  EBDetId id = EBDetId::unhashIndex(iChannel);
96  chit = chStatus->getMap().find(id.rawId());
97  if( chit != chStatus->getMap().end() ) {
98  EcalChannelStatusCode ch_code = (*chit);
99  uint16_t statusCode = ch_code.getStatusCode() & 31;
100  if(statusCode == 1 || (statusCode > 7 && statusCode < 12))
101  maskedChannels_.push_back(iChannel);
102  }
103  }
104  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
105  EEDetId id = EEDetId::unhashIndex(iChannel);
106  chit = chStatus->getMap().find(id.rawId());
107  if( chit != chStatus->getMap().end() ) {
108  EcalChannelStatusCode ch_code = (*chit);
109  uint16_t statusCode = ch_code.getStatusCode() & 31;
110  if(statusCode == 1 || (statusCode > 7 && statusCode < 12))
111  maskedEEChannels_.push_back(iChannel);
112  }
113  }
114  */
115  TH1F** hMean = new TH1F*[15];
116  TH1F** hRMS = new TH1F*[15];
117  TFile f("PedHist.root", "RECREATE");
118 
119  typedef struct {
120  int iChannel;
121  int ix;
122  int iy;
123  int iz;
124  } Chan_t;
125  Chan_t Chan;
126  Chan.iChannel = -1;
127  Chan.ix = -1;
128  Chan.iy = -1;
129  Chan.iz = -1;
130 
131  TTree* tPedChan = new TTree("PedChan", "Channels"); // Output tree for channels
132  tPedChan->Branch("Channels", &Chan.iChannel, "iChannel/I");
133  tPedChan->Branch("x", &Chan.ix, "ix/I");
134  tPedChan->Branch("y", &Chan.iy, "iy/I");
135  tPedChan->Branch("z", &Chan.iz, "iz/I");
136  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
137  Chan.iChannel = iChannel;
138  EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
139  Chan.ix = myEBDetId.ieta(); // -85:-1,1:85
140  Chan.iy = myEBDetId.iphi(); // 1:360
141  Chan.iz = 0;
142  if (iChannel % 10000 == 0)
143  cout << " EB channel " << iChannel << " eta " << Chan.ix << " phi " << Chan.iy << endl;
144  tPedChan->Fill();
145  }
146  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
147  Chan.iChannel = iChannel;
148  EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
149  Chan.ix = myEEDetId.ix();
150  Chan.iy = myEEDetId.iy();
151  Chan.iz = myEEDetId.zside();
152  if (iChannel % 1000 == 0)
153  cout << " EE channel " << iChannel << " x " << Chan.ix << " y " << Chan.iy << " z " << Chan.iz << endl;
154  tPedChan->Fill();
155  }
156  tPedChan->Write();
157  tPedChan->Print();
158 
159  typedef struct {
160  int Run;
161  double Mean[kChannels];
162  double RMS[kChannels];
163  } Ped_t;
164  Ped_t PedVal;
165  PedVal.Run = -1; // initialization
166  for (int iChannel = 0; iChannel < kChannels; iChannel++) {
167  PedVal.Mean[iChannel] = -1.;
168  PedVal.RMS[iChannel] = -1.;
169  }
170  TTree* tPedHist = new TTree("PedHist", "Pedestal History"); // Output tree for pedestal mean/rms
171  tPedHist->Branch("Pedestals", &PedVal.Run, "Run/I");
172  tPedHist->Branch("Mean", PedVal.Mean, "Mean[75848]/D");
173  tPedHist->Branch("RMS", PedVal.RMS, "RMS[75848]/D");
174 
175  // here we retrieve all the runs after the last from online DB
176  std::cout << "Retrieving run list from ONLINE DB ... " << std::endl;
177  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
178  std::cout << "Connection done" << std::endl;
179  if (!econn) {
180  std::cout << " Problem with OMDS: connection parameters " << m_sid << "/" << m_user << "/" << m_pass << std::endl;
181  throw cms::Exception("OMDS not available");
182  }
183 
184  // these are the online conditions DB classes
185  RunList my_runlist;
186  RunTag my_runtag;
189 
190  my_locdef.setLocation(m_location);
191  my_rundef.setRunType("PEDESTAL");
192  my_runtag.setLocationDef(my_locdef);
193  my_runtag.setRunTypeDef(my_rundef);
194  my_runtag.setGeneralTag(m_gentag);
195 
196  // here we retrieve the Monitoring run records
197  MonVersionDef monverdef;
198  monverdef.setMonitoringVersion("test01");
199  MonRunTag mon_tag;
200  // mon_tag.setGeneralTag("CMSSW");
201  mon_tag.setGeneralTag("CMSSW-offline-private");
202  mon_tag.setMonVersionDef(monverdef);
203  MonRunList mon_list;
204  mon_list.setMonRunTag(mon_tag);
205  mon_list.setRunTag(my_runtag);
206  // mon_list=econn->fetchMonRunList(my_runtag, mon_tag);
207  unsigned int min_run = 0, max_since = 0;
208  if (m_firstRun < max_since) {
209  min_run = max_since + 1; // we have to add 1 to the last transferred one
210  } else {
211  min_run = m_firstRun;
212  }
213 
214  unsigned int max_run = m_lastRun;
215  mon_list = econn->fetchMonRunList(my_runtag, mon_tag, min_run, max_run);
216 
217  std::vector<MonRunIOV> mon_run_vec = mon_list.getRuns();
218  int mon_runs = mon_run_vec.size();
219  std::cout << "number of Mon runs is : " << mon_runs << std::endl;
220 
221  if (mon_runs > 0) {
222  int NbChan = 0;
223  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
224  if (iChannel % 10000 == 1) {
225  hMean[NbChan] = new TH1F(Form("Mean_%i", NbChan), Form("Mean EB %i", iChannel), mon_runs, 0., mon_runs);
226  hRMS[NbChan] = new TH1F(Form("RMS_%i", NbChan), Form("RMS EB %i", iChannel), mon_runs, 0., mon_runs);
227  NbChan++;
228  }
229  }
230  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
231  if (iChannel % 2000 == 1) {
232  hMean[NbChan] = new TH1F(Form("Mean_%i", NbChan), Form("Mean EE %i", iChannel), mon_runs, 0., mon_runs);
233  hRMS[NbChan] = new TH1F(Form("RMS_%i", NbChan), Form("RMS EE %i", iChannel), mon_runs, 0., mon_runs);
234  NbChan++;
235  }
236  }
237 
238  // int krmax = std::min(mon_runs, 30);
239  int krmax = mon_runs;
240  for (int kr = 0; kr < krmax; kr++) {
241  std::cout << "-kr------: " << kr << std::endl;
242 
243  unsigned int irun = static_cast<unsigned int>(mon_run_vec[kr].getRunIOV().getRunNumber());
244  std::cout << "retrieve the data for run number: " << irun << std::endl;
245  if (mon_run_vec[kr].getSubRunNumber() <= 1) {
246  // retrieve the data for a given run
247  RunIOV runiov_prime = mon_run_vec[kr].getRunIOV();
248  // retrieve the pedestals from OMDS for this run
249  std::map<EcalLogicID, MonPedestalsDat> dataset_mon;
250  econn->fetchDataSet(&dataset_mon, &mon_run_vec[kr]);
251  std::cout << "OMDS record for run " << irun << " is made of " << dataset_mon.size() << std::endl;
252  int nEB = 0, nEE = 0;
253  typedef std::map<EcalLogicID, MonPedestalsDat>::const_iterator CImon;
254  EcalLogicID ecid_xt;
255  MonPedestalsDat rd_ped;
256 
257  // this to validate ...
258  int nbad = 0;
259  for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
260  ecid_xt = p->first;
261  rd_ped = p->second;
262  int sm_num = ecid_xt.getID1();
263  int xt_num = ecid_xt.getID2();
264  int yt_num = ecid_xt.getID3();
265 
266  //checkPedestal
267  bool result = true;
268  if (rd_ped.getPedRMSG12() > 3 || rd_ped.getPedRMSG12() <= 0 || rd_ped.getPedRMSG6() > 2 ||
269  rd_ped.getPedRMSG12() <= 0 || rd_ped.getPedRMSG1() > 1 || rd_ped.getPedRMSG1() <= 0 ||
270  rd_ped.getPedMeanG12() > 300 || rd_ped.getPedMeanG12() <= 100 || rd_ped.getPedMeanG6() > 300 ||
271  rd_ped.getPedMeanG6() <= 100 || rd_ped.getPedMeanG1() > 300 || rd_ped.getPedMeanG6() <= 100)
272  result = false;
273 
274  // here we check and count how many bad channels we have
275  if (!result) {
276  nbad++;
277  if (nbad < 10)
278  std::cout << "BAD LIST: channel " << sm_num << "/" << xt_num << "/" << yt_num << "ped/rms "
279  << rd_ped.getPedMeanG12() << "/" << rd_ped.getPedRMSG12() << std::endl;
280  }
281  if (ecid_xt.getName() == "EB_crystal_number") {
282  nEB++;
283  } else {
284  nEE++;
285  }
286  } // end loop over pedestal data
287  // ok or bad? A bad run is for more than 5% bad channels
288 
289  // if(nbad<(dataset_mon.size()*0.1)){
290  if (nbad < (dataset_mon.size() * 0.05) &&
291  (nEB > 10200 || nEE > 2460)) { // this is good run, fill histo and tree
292  PedVal.Run = irun;
293  int NbChan = 0;
294  for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
295  ecid_xt = p->first;
296  rd_ped = p->second;
297  int sm_num = ecid_xt.getID1();
298  int xt_num = ecid_xt.getID2();
299  int yt_num = ecid_xt.getID3();
300 
301  if (ecid_xt.getName() == "EB_crystal_number") { // Barrel
302  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
303  int iChannel = ebdetid.hashedIndex();
304  if (iChannel < 0 || iChannel > 61200)
305  cout << " SM " << sm_num << " Chan in SM " << xt_num << " IChannel " << iChannel << endl;
306  if (iChannel % 10000 == 1) {
307  hMean[NbChan]->Fill(kr, rd_ped.getPedMeanG12());
308  hRMS[NbChan]->Fill(kr, rd_ped.getPedRMSG12());
309  NbChan++;
310  }
311  PedVal.Mean[iChannel] = rd_ped.getPedMeanG12();
312  PedVal.RMS[iChannel] = rd_ped.getPedRMSG12();
313  if (iChannel % 10000 == 0)
314  cout << " channel " << iChannel << " mean " << PedVal.Mean[iChannel] << " RMS " << PedVal.RMS[iChannel]
315  << endl;
316  } else { // Endcaps
317  if (EEDetId::validDetId(xt_num, yt_num, sm_num)) {
318  EEDetId eedetid(xt_num, yt_num, sm_num);
319  int iChannel = eedetid.hashedIndex();
320  if (iChannel < 0 || iChannel > 14648)
321  cout << " x " << sm_num << " y " << xt_num << " z " << yt_num << " IChannel " << iChannel << endl;
322  if (iChannel % 2000 == 1) {
323  hMean[NbChan]->Fill(kr, rd_ped.getPedMeanG12());
324  hRMS[NbChan]->Fill(kr, rd_ped.getPedRMSG12());
325  NbChan++;
326  }
327  int iChanEE = kEBChannels + iChannel;
328  // cout << " channel EE " << iChanEE << endl;
329  PedVal.Mean[iChanEE] = rd_ped.getPedMeanG12();
330  PedVal.RMS[iChanEE] = rd_ped.getPedRMSG12();
331  } // valid ee Id
332  } // Endcaps
333  } // loop over channels
334  tPedHist->Fill();
335  cout << " We got a good run " << irun << endl;
336  } // good run
337  } // mon_run_vec
338  } // loop over all runs
339  } // number of runs > 0
340  cout << "Exiting beginRun" << endl;
341  for (int NbChan = 0; NbChan < 15; NbChan++) {
342  if (hMean[NbChan]->GetEntries() > 0.) { // save only when filled!
343  hMean[NbChan]->Write();
344  hRMS[NbChan]->Write();
345  }
346  }
347  tPedHist->Write();
348  tPedHist->Print();
349  f.Close();
350 
351 } //beginRun
352 
353 //========================================================================
355  //========================================================================
356 
357 } //endRun
358 
359 //========================================================================
362 
363 } //beginJob
364 
365 //========================================================================
367  //========================================================================
368 
369 } //endJob
370 
371 //
372 // member functions
373 //
374 
375 //========================================================================
377  //========================================================================
378 
379  if (cnt_evt_ == 0) {
380  if (ECALType_ == "EB" || ECALType_ == "EA") {
381  cout << " Barrel data : nb channels " << kEBChannels << endl;
382  } else if (ECALType_ == "EE" || ECALType_ == "EA") {
383  cout << " End cap data : nb channels " << kEEChannels << endl;
384  } else {
385  cout << " strange ECALtype : " << ECALType_ << " abort " << endl;
386  return;
387  }
388  /*
389  int NbOfmaskedChannels = maskedChannels_.size();
390  cout << " Nb masked EB channels " << NbOfmaskedChannels << endl;
391  for (vector<int>::iterator iter = maskedChannels_.begin(); iter != maskedChannels_.end(); ++iter)
392  cout<< " : masked channel " << *(iter) << endl;
393  NbOfmaskedChannels = maskedEEChannels_.size();
394  cout << " Nb masked EE channels " << NbOfmaskedChannels << endl;
395  for (vector<int>::iterator iter = maskedEEChannels_.begin(); iter != maskedEEChannels_.end(); ++iter)
396  cout<< " : masked channel " << *(iter) << endl;
397  */
398  }
399  cnt_evt_++;
400 
401 } //analyze
402 
403 //define this as a plug-in
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
int getID1() const
Definition: EcalLogicID.cc:30
std::string getName() const
Definition: EcalLogicID.cc:26
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
Definition: RunTag.h:13
int getID2() const
Definition: EcalLogicID.cc:32
int ix() const
Definition: EEDetId.h:77
float getPedRMSG6() const
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:65
void setGeneralTag(std::string tag)
Definition: MonRunTag.cc:21
float getPedRMSG1() const
void setRunTag(const RunTag &tag)
Definition: MonRunList.cc:18
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
float getPedMeanG1() const
EcalPedestalHistory(const edm::ParameterSet &)
float getPedMeanG6() const
void endRun(edm::Run const &, edm::EventSetup const &) override
double f[11][100]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void setMonVersionDef(const MonVersionDef &ver)
Definition: MonRunTag.cc:30
float getPedRMSG12() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Namespace of DDCMS conversion namespace.
std::vector< MonRunIOV > getRuns()
Definition: MonRunList.cc:32
int zside() const
Definition: EEDetId.h:71
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
void setMonRunTag(const MonRunTag &tag)
Definition: MonRunList.cc:23
float getPedMeanG12() const
void setMonitoringVersion(std::string ver)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:110
int getID3() const
Definition: EcalLogicID.cc:34
HLT enums.
int hashedIndex() const
get a compact index for arrays
Definition: EBDetId.h:82
void analyze(const edm::Event &, const edm::EventSetup &) override
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
int hashedIndex() const
Definition: EEDetId.h:183
void beginRun(edm::Run const &, edm::EventSetup const &) override
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
Definition: RunIOV.h:13
Definition: Run.h:45
int iy() const
Definition: EEDetId.h:83
const Int_t kChannels