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 
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 
51 //====================================================================
53 //====================================================================
54  //now do what ever initialization is needed
55  // EBDigiCollection_ = iConfig.getParameter<edm::InputTag>("EBDigiCollection");
56  runnumber_ = iConfig.getUntrackedParameter<int>("runnumber",-1);
57  ECALType_ = iConfig.getParameter<std::string>("ECALType");
58  runType_ = iConfig.getParameter<std::string>("runType");
59  startevent_ = iConfig.getUntrackedParameter<unsigned int>("startevent", 1);
60 
61  std::cout << "EcalPedestals Source handler constructor\n" << std::endl;
62  m_firstRun = static_cast<unsigned int>(atoi(iConfig.getParameter<std::string>("firstRun").c_str()));
63  m_lastRun = static_cast<unsigned int>(atoi(iConfig.getParameter<std::string>("lastRun").c_str()));
64  m_sid = iConfig.getParameter<std::string>("OnlineDBSID");
65  m_user = iConfig.getParameter<std::string>("OnlineDBUser");
66  m_pass = iConfig.getParameter<std::string>("OnlineDBPassword");
67  m_locationsource = iConfig.getParameter<std::string>("LocationSource");
68  m_location = iConfig.getParameter<std::string>("Location");
69  m_gentag = iConfig.getParameter<std::string>("GenTag");
70  std::cout << m_sid<<"/"<<m_user<<"/"<<m_pass<<"/"<<m_location<<"/"<<m_gentag << std::endl;
71 
72  vector<int> listDefaults;
73  listDefaults.push_back(-1);
74 
75  cnt_evt_ = 0;
76  // cout << "Exiting constructor" << endl;
77 }//constructor
78 
79 
80 //========================================================================
82 //========================================================================
83  cout << "ANALYSIS FINISHED" << endl;
84 }//destructor
85 
86 //========================================================================
89 
90  cout << "Entering beginRun" << endl;
91  /* do not use any tag...
92  edm::ESHandle<EcalChannelStatus> pChannelStatus;
93  c.get<EcalChannelStatusRcd>().get(pChannelStatus);
94  const EcalChannelStatus* chStatus = pChannelStatus.product();
95  EcalChannelStatusMap::const_iterator chit;
96  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
97  EBDetId id = EBDetId::unhashIndex(iChannel);
98  chit = chStatus->getMap().find(id.rawId());
99  if( chit != chStatus->getMap().end() ) {
100  EcalChannelStatusCode ch_code = (*chit);
101  uint16_t statusCode = ch_code.getStatusCode() & 31;
102  if(statusCode == 1 || (statusCode > 7 && statusCode < 12))
103  maskedChannels_.push_back(iChannel);
104  }
105  }
106  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
107  EEDetId id = EEDetId::unhashIndex(iChannel);
108  chit = chStatus->getMap().find(id.rawId());
109  if( chit != chStatus->getMap().end() ) {
110  EcalChannelStatusCode ch_code = (*chit);
111  uint16_t statusCode = ch_code.getStatusCode() & 31;
112  if(statusCode == 1 || (statusCode > 7 && statusCode < 12))
113  maskedEEChannels_.push_back(iChannel);
114  }
115  }
116  */
117  TH1F** hMean = new TH1F*[15];
118  TH1F** hRMS = new TH1F*[15];
119  TFile f("PedHist.root","RECREATE");
120 
121  typedef struct {
122  int iChannel;
123  int ix;
124  int iy;
125  int iz;
126  } Chan_t;
127  Chan_t Chan;
128  Chan.iChannel = -1;
129  Chan.ix = -1;
130  Chan.iy = -1;
131  Chan.iz = -1;
132 
133  TTree* tPedChan = new TTree("PedChan", "Channels"); // Output tree for channels
134  tPedChan->Branch("Channels", &Chan.iChannel, "iChannel/I");
135  tPedChan->Branch("x", &Chan.ix, "ix/I");
136  tPedChan->Branch("y", &Chan.iy, "iy/I");
137  tPedChan->Branch("z", &Chan.iz, "iz/I");
138  for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
139  Chan.iChannel = iChannel;
140  EBDetId myEBDetId = EBDetId::unhashIndex(iChannel);
141  Chan.ix = myEBDetId.ieta(); // -85:-1,1:85
142  Chan.iy = myEBDetId.iphi(); // 1:360
143  Chan.iz = 0;
144  if(iChannel%10000 == 0) cout << " EB channel " << iChannel << " eta " << Chan.ix << " phi " << Chan.iy << endl;
145  tPedChan->Fill();
146  }
147  for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
148  Chan.iChannel = iChannel;
149  EEDetId myEEDetId = EEDetId::unhashIndex(iChannel);
150  Chan.ix = myEEDetId.ix();
151  Chan.iy = myEEDetId.iy();
152  Chan.iz = myEEDetId.zside();
153  if(iChannel%1000 == 0) 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;
187  LocationDef my_locdef;
188  RunTypeDef my_rundef;
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 
247  // retrieve the data for a given run
248  RunIOV runiov_prime = mon_run_vec[kr].getRunIOV();
249  // retrieve the pedestals from OMDS for this run
250  std::map<EcalLogicID, MonPedestalsDat> dataset_mon;
251  econn->fetchDataSet(&dataset_mon, &mon_run_vec[kr]);
252  std::cout <<"OMDS record for run "<<irun <<" is made of "<< dataset_mon.size() << std::endl;
253  int nEB = 0, nEE = 0, nEBbad = 0, nEEbad =0;
254  typedef std::map<EcalLogicID, MonPedestalsDat>::const_iterator CImon;
255  EcalLogicID ecid_xt;
256  MonPedestalsDat rd_ped;
257 
258  // this to validate ...
259  int nbad = 0;
260  for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
261  ecid_xt = p->first;
262  rd_ped = p->second;
263  int sm_num = ecid_xt.getID1();
264  int xt_num = ecid_xt.getID2();
265  int yt_num = ecid_xt.getID3();
266 
267  //checkPedestal
268  bool result=true;
269  if(rd_ped.getPedRMSG12() > 3 || rd_ped.getPedRMSG12()<= 0 || rd_ped.getPedRMSG6() >2 || rd_ped.getPedRMSG12() <= 0
270  || rd_ped.getPedRMSG1() > 1 || rd_ped.getPedRMSG1() <= 0
271  || rd_ped.getPedMeanG12() > 300 || rd_ped.getPedMeanG12() <= 100
272  || rd_ped.getPedMeanG6() > 300 || rd_ped.getPedMeanG6() <= 100
273  || rd_ped.getPedMeanG1() > 300 || rd_ped.getPedMeanG6() <= 100) result=false;
274 
275  // here we check and count how many bad channels we have
276  if(!result ) {
277  nbad++;
278  if(nbad < 10) std::cout <<"BAD LIST: channel " << sm_num << "/" << xt_num << "/"<< yt_num
279  << "ped/rms "<< rd_ped.getPedMeanG12() << "/"<< rd_ped.getPedRMSG12() << std::endl;
280  }
281  if(ecid_xt.getName()=="EB_crystal_number") {
282  nEB++;
283  if(!result ) nEBbad++;
284  }
285  else {
286  nEE++;
287  if(!result ) nEEbad++;
288  }
289  } // end loop over pedestal data
290  // ok or bad? A bad run is for more than 5% bad channels
291 
292  // if(nbad<(dataset_mon.size()*0.1)){
293  if(nbad < (dataset_mon.size()*0.05) && (nEB > 10200 || nEE > 2460)) { // this is good run, fill histo and tree
294  PedVal.Run = irun;
295  int NbChan = 0;
296  for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
297  ecid_xt = p->first;
298  rd_ped = p->second;
299  int sm_num = ecid_xt.getID1();
300  int xt_num = ecid_xt.getID2();
301  int yt_num = ecid_xt.getID3();
302 
303  if(ecid_xt.getName()=="EB_crystal_number") { // Barrel
304  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
305  int iChannel = ebdetid.hashedIndex();
306  if(iChannel < 0 || iChannel > 61200) cout << " SM " << sm_num << " Chan in SM " << xt_num
307  << " IChannel " << iChannel << endl;
308  if(iChannel%10000 == 1) {
309  hMean[NbChan]->Fill(kr, rd_ped.getPedMeanG12());
310  hRMS[NbChan]->Fill(kr, rd_ped.getPedRMSG12());
311  NbChan++;
312  }
313  PedVal.Mean[iChannel] = rd_ped.getPedMeanG12();
314  PedVal.RMS[iChannel] = rd_ped.getPedRMSG12();
315  if(iChannel%10000 == 0) cout << " channel " << iChannel << " mean " << PedVal.Mean[iChannel] << " RMS " << PedVal.RMS[iChannel] << endl;
316  }
317  else { // Endcaps
318  if(EEDetId::validDetId(xt_num, yt_num, sm_num)) {
319  EEDetId eedetid(xt_num,yt_num,sm_num);
320  int iChannel = eedetid.hashedIndex();
321  if(iChannel < 0 || iChannel > 14648) cout << " x " << sm_num << " y " << xt_num << " z " << yt_num
322  << " IChannel " << iChannel << endl;
323  if(iChannel%2000 == 1) {
324  hMean[NbChan]->Fill(kr, rd_ped.getPedMeanG12());
325  hRMS[NbChan]->Fill(kr, rd_ped.getPedRMSG12());
326  NbChan++;
327  }
328  int iChanEE = kEBChannels + iChannel;
329  // cout << " channel EE " << iChanEE << endl;
330  PedVal.Mean[iChanEE] = rd_ped.getPedMeanG12();
331  PedVal.RMS[iChanEE] = rd_ped.getPedRMSG12();
332  } // valid ee Id
333  } // Endcaps
334  } // loop over channels
335  tPedHist->Fill();
336  cout << " We got a good run " << irun << endl;
337  } // good run
338  } // mon_run_vec
339  } // loop over all runs
340  } // number of runs > 0
341  cout << "Exiting beginRun" << endl;
342  for(int NbChan = 0; NbChan < 15; NbChan++) {
343  if(hMean[NbChan]->GetEntries() > 0.) { // save only when filled!
344  hMean[NbChan]->Write();
345  hRMS[NbChan]->Write();
346  }
347  }
348  tPedHist->Write();
349  tPedHist->Print();
350  f.Close();
351 
352 }//beginRun
353 
354 //========================================================================
357 
358  cout << "Entering beginJob" << endl;
359 
360  cout << "Exiting beginJob" << endl;
361 }//beginJob
362 
363 //========================================================================
364 void
366 //========================================================================
367 
368  cout << "Entering endJob" << endl;
369  cout << "Exiting endJob" << endl;
370 }//endJob
371 
372 //
373 // member functions
374 //
375 
376 //========================================================================
377 void
379 //========================================================================
380 
381  if(cnt_evt_ == 0) {
382  if(ECALType_ == "EB" || ECALType_ == "EA") {
383  cout << " Barrel data : nb channels " << kEBChannels << endl;
384  }
385  else if(ECALType_ == "EE" || ECALType_ == "EA") {
386  cout << " End cap data : nb channels " << kEEChannels << endl;
387  }
388  else {
389  cout << " strange ECALtype : " << ECALType_ << " abort " << endl;
390  return;
391  }
392  /*
393  int NbOfmaskedChannels = maskedChannels_.size();
394  cout << " Nb masked EB channels " << NbOfmaskedChannels << endl;
395  for (vector<int>::iterator iter = maskedChannels_.begin(); iter != maskedChannels_.end(); ++iter)
396  cout<< " : masked channel " << *(iter) << endl;
397  NbOfmaskedChannels = maskedEEChannels_.size();
398  cout << " Nb masked EE channels " << NbOfmaskedChannels << endl;
399  for (vector<int>::iterator iter = maskedEEChannels_.begin(); iter != maskedEEChannels_.end(); ++iter)
400  cout<< " : masked channel " << *(iter) << endl;
401  */
402  }
403  cnt_evt_++;
404 
405  cout << "Exiting analyze" << endl;
406 }//analyze
407 
408 //define this as a plug-in
T getParameter(std::string const &) const
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:70
T getUntrackedParameter(std::string const &, T const &) const
int hashedIndex() const
get a compact index for arrays
Definition: EBDetId.h:86
int ix() const
Definition: EEDetId.h:76
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Definition: RunTag.h:13
float getPedRMSG1() const
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
void setGeneralTag(std::string tag)
Definition: MonRunTag.cc:33
void setRunTag(const RunTag &tag)
Definition: MonRunList.cc:23
int getID2() const
Definition: EcalLogicID.cc:51
int iphi() const
get the crystal iphi
Definition: EBDetId.h:53
std::string getName() const
Definition: EcalLogicID.cc:36
int iEvent
Definition: GenABIO.cc:230
float getPedMeanG12() const
EcalPedestalHistory(const edm::ParameterSet &)
float getPedRMSG12() const
int zside() const
Definition: EEDetId.h:70
double f[11][100]
int getID1() const
Definition: EcalLogicID.cc:46
int iy() const
Definition: EEDetId.h:82
int ieta() const
get the crystal ieta
Definition: EBDetId.h:51
void setMonVersionDef(const MonVersionDef &ver)
Definition: MonRunTag.cc:49
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
std::vector< MonRunIOV > getRuns()
Definition: MonRunList.cc:46
int hashedIndex() const
Definition: EEDetId.h:182
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
void setMonRunTag(const MonRunTag &tag)
Definition: MonRunList.cc:29
float getPedMeanG1() const
void setMonitoringVersion(std::string ver)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:114
HLT enums.
virtual void beginRun(edm::Run const &, edm::EventSetup const &)
float getPedMeanG6() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
virtual void analyze(const edm::Event &, const edm::EventSetup &)
int getID3() const
Definition: EcalLogicID.cc:56
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
static const int SMCRYSTALMODE
Definition: EBDetId.h:167
Definition: RunIOV.h:13
float getPedRMSG6() const
Definition: Run.h:42