CMS 3D CMS Logo

HcalLutAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Test/HcalLutAnalyzer
4 // Class: HcalLutAnalyzer
5 //
13 //
14 // Original Author: Aleko Khukhunaishvili
15 // Created: Fri, 21 Jul 2017 08:42:05 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 #include <iostream>
23 #include <fstream>
24 
25 // user include files
33 
42 
43 #include "TString.h"
44 #include "TH1D.h"
45 #include "TH2D.h"
46 #include "TProfile.h"
47 #include "TCanvas.h"
48 #include "TROOT.h"
49 #include "TStyle.h"
50 #include "TSystem.h"
51 
52 class HcalLutAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
53  public:
54  explicit HcalLutAnalyzer(const edm::ParameterSet&);
56  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
57 
58  private:
59  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
60 
63  std::vector<std::string> tags_;
64  std::vector<std::string> quality_;
65  std::vector<std::string> pedestals_;
66  std::vector<std::string> gains_;
67  std::vector<std::string> respcorrs_;
68 
69  double Zmin;
70  double Zmax;
71  double Ymin;
72  double Ymax;
73  double Pmin;
74  double Pmax;
75 };
76 
78 {
79  inputDir = iConfig.getParameter<std::string>("inputDir");
80  plotsDir = iConfig.getParameter<std::string>("plotsDir");
81  tags_ = iConfig.getParameter<std::vector<std::string> >("tags");
82  quality_ = iConfig.getParameter<std::vector<std::string> >("quality");
83  pedestals_ = iConfig.getParameter<std::vector<std::string> >("pedestals");
84  gains_ = iConfig.getParameter<std::vector<std::string> >("gains");
85  respcorrs_ = iConfig.getParameter<std::vector<std::string> >("respcorrs");
86 
87  Zmin = iConfig.getParameter<double>("Zmin");
88  Zmax = iConfig.getParameter<double>("Zmax");
89  Ymin = iConfig.getParameter<double>("Ymin");
90  Ymax = iConfig.getParameter<double>("Ymax");
91  Pmin = iConfig.getParameter<double>("Pmin");
92  Pmax = iConfig.getParameter<double>("Pmax");
93 }
94 
95 
96 void
98 {
99  using namespace std;
100 
102  iSetup.get<HcalRecNumberingRecord>().get( topology );
103 
104  typedef std::vector<std::string> vstring;
105  typedef std::map<unsigned long int, float> LUTINPUT;
106 
107  static const int NVAR=5; //variables
108  static const int NDET=2; //detectors
109  static const int NDEP=7; //depths
110  static const int NLEV=3; //old,new,ratio
111 
112  const bool doRatio[NVAR] = {false, true, true, false, true};
113  const char* titleVar[NVAR]= {"Pedestals", "RespCorrs", "Gains", "Threshold", "LUTs"};
114  const char* titleHisR[NLEV]= {"Old", "New", "Ratio"};
115  const char* titleHisD[NLEV]= {"Old", "New", "Difference"};
116  const char* titleDet[4]= {"HBHE", "HF", "HEP17", "HO"};
117  const int DEP[NDET]={7,4};
118  const char* titleDep[NDEP]= {"depth1", "depth2", "depth3", "depth4", "depth5","depth6","depth7"};
119 
120 
121  TH2D *r[NVAR][NDET];
122  TProfile *p[NVAR][NDET];
123 
124  TH2D *h[NVAR][NLEV][NDET][NDEP];
125  TH2D *hlut[4][NLEV];
126  TH2D *hslope[2];
127  TH2D *houtput[4][2];
128 
129  for(int d=0; d<4; ++d){
130  for(int i=0; i<3; ++i){
131  hlut[d][i] = new TH2D(Form("Lut_%s_%s", titleDet[d], titleHisR[i]), Form("Input LUT, %s", (i==2?"Ratio":tags_[i].c_str())), 260, 0, 260, 240, 0, i==NLEV-1?3:2400);
132  hlut[d][i]->SetMarkerColor(d==0?kBlue:d==1? kGreen+2 : d==2?kRed : kCyan);
133  hlut[d][i]->SetXTitle("raw adc");
134  hlut[d][i]->SetYTitle("lin adc");
135  }
136  }
137 
138  for(int d=0; d<NDET; ++d){
139  hslope[d] = new TH2D(Form("GainLutScatter_%s", titleDet[d]), Form("Gain-Lutslope scatter, %s",titleDet[d]), 200, 0, 2, 200, 0, 2);
140  hslope[d]->SetXTitle("Gain x RespCorr ratio");
141  hslope[d]->SetYTitle("Lut ratio");
142 
143  for(int j=0; j<NVAR; ++j){
144  double rmin=doRatio[j]?Ymin:-6;
145  double rmax=doRatio[j]?Ymax: 6;
146  r[j][d] = new TH2D(Form("r%s_%s", titleVar[j], titleDet[d]), Form("%s, %s",titleVar[j], titleDet[d]), 83,-41.5, 41.5, 250, rmin, rmax);
147  r[j][d]->SetXTitle("iEta");
148  r[j][d]->SetYTitle(doRatio[j]?"New / Old":"New - Old");
149  p[j][d] = new TProfile(Form("p%s_%s", titleVar[j], titleDet[d]), Form("%s, %s",titleVar[j], titleDet[d]), 83,-41.5, 41.5);
150  p[j][d]->SetXTitle("iEta");
151  p[j][d]->SetYTitle(doRatio[j]?"New / Old":"New - Old");
152  p[j][d]->SetMarkerStyle(20);
153  p[j][d]->SetMarkerSize(0.9);
154  p[j][d]->SetMarkerColor(kBlue);
155 
156  for(int p=0; p<DEP[d]; ++p){
157  for(int i=0; i<NLEV; ++i){
158  const char *titHist=doRatio[j]?titleHisR[i]:titleHisD[i];
159  h[j][i][d][p] = new TH2D(Form("h%s_%s_%s_%s", titleVar[j], titHist,titleDet[d],titleDep[p]),
160  Form("%s, %s, %s, %s",titleVar[j], titHist,titleDet[d],titleDep[p]), 83, -41.5, 41.5, 72, 0.5, 72.5);
161  h[j][i][d][p]->SetXTitle("iEta");
162  h[j][i][d][p]->SetYTitle("iPhi");
163  }
164  }
165  }
166  }
167 
168  for(int i=0; i<4; ++i){
169  int color=i==0?kBlue : i==1? kViolet : i==2 ? kGreen+2 : kRed;
170  houtput[i][0] = new TH2D(Form("houtlut0_%d",i), Form("Output LUT, %s", tags_[0].c_str()), 2100,0,2100,260,0,260);
171  houtput[i][1] = new TH2D(Form("houtlut1_%d",i), Form("Output LUT, %s", tags_[1].c_str()), 2100,0,2100,260,0,260);
172  for(int j=0; j<2; ++j) {
173  houtput[i][j]->SetMarkerColor(color);
174  houtput[i][j]->SetLineColor(color);
175  }
176  }
177 
178  //FILL LUT INPUT DATA
179  LUTINPUT lutgain[2];
180  LUTINPUT lutresp[2];
181  LUTINPUT lutpede[2];
182 
183  assert(tags_.size()==2);
184  assert(gains_.size()==2);
185  assert(respcorrs_.size()==2);
186  assert(pedestals_.size()==2);
187 
188  unsigned long int iraw;
189  int ieta, iphi, idep;
190  string det, base;
191  float val1, val2, val3, val4;
192  float wid1, wid2, wid3, wid4;
193  char buffer[1024];
194 
195  std::vector<HcalDetId> BadChans[2];
196  std::vector<HcalDetId> ZeroLuts[2];
197 
198  //Read input condition files
199  for(int ii=0; ii<2; ++ii){
200  //Gains
201  std::ifstream infile(edm::FileInPath(Form("%s/Gains/Gains_Run%s.txt", inputDir.c_str(), gains_[ii].c_str())).fullPath().c_str());
202  assert(!infile.fail());
203  while(!infile.eof()){
204  infile.getline(buffer, 1024);
205  if(buffer[0]=='#') continue;
206  std::istringstream(buffer) >> ieta >> iphi >> idep >> det >> val1 >> val2 >> val3 >> val4 >> iraw ;
207  if(det!="HB" && det!="HE" && det!="HF") continue;
208 
209  float theval = (val1+val2+val3+val4)/4.0;
210 
211  HcalSubdetector subdet = det=="HB" ? HcalBarrel :
212  det=="HE" ? HcalEndcap :
213  det=="HF" ? HcalForward:
214  HcalOther;
215 
216  HcalDetId id(subdet, ieta, iphi, idep);
217  lutgain[ii].insert(LUTINPUT::value_type(id.rawId(), theval));
218  }
219 
220 
221  //Pedestals
222  std::ifstream infped(edm::FileInPath(Form("%s/Pedestals/Pedestals_Run%s.txt", inputDir.c_str(), pedestals_[ii].c_str())).fullPath().c_str());
223  assert(!infped.fail());
224  while(!infped.eof()){
225  infped.getline(buffer, 1024);
226  if(buffer[0]=='#') continue;
227  std::istringstream(buffer) >> ieta >> iphi >> idep >> det >> val1 >> val2 >> val3 >> val4 >> wid1 >> wid2 >> wid3 >> wid4 >> iraw ;
228  if(det!="HB" && det!="HE" && det!="HF") continue;
229 
230  float theval = (val1+val2+val3+val4)/4.0;
231 
232  HcalSubdetector subdet = det=="HB" ? HcalBarrel :
233  det=="HE" ? HcalEndcap :
234  det=="HF" ? HcalForward:
235  HcalOther;
236 
237  HcalDetId id(subdet, ieta, iphi, idep);
238  lutpede[ii].insert(LUTINPUT::value_type(id.rawId(), theval));
239  }
240 
241 
242 
243  //Response corrections
244  std::ifstream inresp(edm::FileInPath(Form("%s/RespCorrs/RespCorrs_Run%s.txt", inputDir.c_str(), respcorrs_[ii].c_str())).fullPath().c_str());
245  assert(!inresp.fail());
246  while(!inresp.eof()){
247  inresp.getline(buffer, 1024);
248  if(buffer[0]=='#') continue;
249  std::istringstream(buffer) >> ieta >> iphi >> idep >> det >> val1 >> iraw ;
250  if(det!="HB" && det!="HE" && det!="HF") continue;
251 
252  float theval = val1;
253 
254  HcalSubdetector subdet = det=="HB" ? HcalBarrel :
255  det=="HE" ? HcalEndcap :
256  det=="HF" ? HcalForward:
257  HcalOther;
258 
259  HcalDetId id(subdet, ieta, iphi, idep);
260  lutresp[ii].insert(LUTINPUT::value_type(id.rawId(), theval));
261  }
262 
263 
264  //ChannelQuality
265  std::ifstream inchan(edm::FileInPath(Form("%s/ChannelQuality/ChannelQuality_Run%s.txt", inputDir.c_str(), quality_[ii].c_str())).fullPath().c_str());
266  assert(!inchan.fail());
267  while(!inchan.eof()){
268  inchan.getline(buffer, 1024);
269  if(buffer[0]=='#') continue;
270  std::istringstream(buffer) >> ieta >> iphi >> idep >> det >> base >> val1 >> iraw ;
271 
272  float theval = val1;
273 
274  HcalSubdetector subdet = det=="HB" ? HcalBarrel :
275  det=="HE" ? HcalEndcap :
276  det=="HF" ? HcalForward:
277  det=="HO" ? HcalOuter:
278  HcalOther;
279 
280  HcalDetId id(subdet, ieta, iphi, idep);
281  if(theval!=0) BadChans[ii].push_back(id);
282  }
283  }
284 
285 
286  LutXml xmls1(edm::FileInPath(Form("%s/%s/%s.xml", inputDir.c_str(), tags_[0].c_str(), tags_[0].c_str())).fullPath());
287  LutXml xmls2(edm::FileInPath(Form("%s/%s/%s.xml", inputDir.c_str(), tags_[1].c_str(), tags_[1].c_str())).fullPath());
288 
289  xmls1.create_lut_map();
290  xmls2.create_lut_map();
291 
292  for (const auto& xml2 : xmls2){
293  HcalGenericDetId detid(xml2.first);
294 
296  HcalTrigTowerDetId tid(detid.rawId());
297  if(!topology->validHT(tid)) continue;
298  const auto& lut2 = xml2.second;
299 
300  int D=abs(tid.ieta())<29 ? (lut2.size()==1024 ? 0 : 3) :
301  tid.version()==0? 1: 2;
302  for(size_t i=0; i<lut2.size(); ++i){
303  if(int(i)%4==D)
304  houtput[D][1]->Fill(i,lut2[i]);
305  }
306  }
307  else if(topology->valid(detid)){
308  HcalDetId id(detid);
309  HcalSubdetector subdet=id.subdet();
310  int idet = int(subdet);
311  const auto& lut2 = xml2.second;
312  int hbhe = idet==HcalForward ? 1 :
313  idet==HcalOuter ? 3 :
314  lut2.size()==128 ? 0 : 2;
315  for(size_t i=0; i<lut2.size(); ++i) {
316  hlut[hbhe][1]->Fill(i, lut2[i]);
317  if(hbhe==2) hlut[hbhe][1]->Fill(i, lut2[i]&0x3FF);
318  }
319  }
320  }
321 
322  for (const auto& xml1 : xmls1){
323 
324  HcalGenericDetId detid(xml1.first);
325  const auto& lut1 = xml1.second;
326 
328  HcalTrigTowerDetId tid(detid.rawId());
329  if(!topology->validHT(tid)) continue;
330  int D=abs(tid.ieta())<29 ? (lut1.size()==1024 ? 0 : 3) :
331  tid.version()==0? 1: 2;
332  for(size_t i=0; i<lut1.size(); ++i){
333  if(int(i)%4==D)
334  houtput[D][0]->Fill(i,lut1[i]);
335  }
336  }else if(topology->valid(detid)){
337  HcalDetId id(detid);
338  HcalSubdetector subdet=id.subdet();
339  int idet = int(subdet);
340  const auto& lut1 = xml1.second;
341  int hbhe = idet==HcalForward ? 1 :
342  idet==HcalOuter ? 3 :
343  lut1.size()==128 ? 0 : 2;
344  for(size_t i=0; i<lut1.size(); ++i) {
345  hlut[hbhe][0]->Fill(i, lut1[i]);
346  if(hbhe==2) hlut[hbhe][0]->Fill(i, lut1[i]&0x3FF);
347  }
348  }
349 
350  auto xml2 =xmls2.find(detid.rawId());
351  if(xml2==xmls2.end()) continue;
352 
354 
355  HcalDetId id(detid);
356 
357  HcalSubdetector subdet=id.subdet();
358  int idet = int(subdet);
359  int ieta = id.ieta();
360  int iphi = id.iphi();
361  int idep = id.depth()-1;
362  unsigned long int iraw = id.rawId();
363 
364  if(!topology->valid(detid)) continue;
365 
366  int hbhe = idet==HcalForward;
367 
368  const auto& lut2 = xml2->second;
369 
370 
371  size_t size = lut1.size();
372  if(size != lut2.size()) continue;
373 
374  std::vector<unsigned int> llut1(size);
375  std::vector<unsigned int> llut2(size);
376  for(size_t i=0; i<size; ++i){
377  llut1[i]=hbhe==0? lut1[i]&0x3FF: lut1[i] ;
378  llut2[i]=hbhe==0? lut2[i]&0x3FF: lut2[i] ;
379  }
380 
381  int threshold[2]={0, 0};
382  //Thresholds
383  for(size_t i=0; i<size; ++i){
384  if(llut1[i]>0){
385  threshold[0]=i;
386  break;
387  }
388  if(i==size-1){
389  ZeroLuts[0].push_back(id);
390  }
391  }
392  for(size_t i=0; i<size; ++i){
393  if(llut2[i]>0){
394  threshold[1]=i;
395  break;
396  }
397  if(i==size-1){
398  ZeroLuts[1].push_back(id);
399  }
400  }
401 
402  if(subdet!=HcalBarrel && subdet!=HcalEndcap && subdet!=HcalForward) continue;
403 
404  //fill conditions
405 
406  double xfill=0;
407  h[0][0][hbhe][idep]->Fill(ieta, iphi,lutpede[0][iraw]);
408  h[0][1][hbhe][idep]->Fill(ieta, iphi,lutpede[1][iraw]);
409  xfill=lutpede[1][iraw]-lutpede[0][iraw];
410  h[0][2][hbhe][idep]->Fill(ieta, iphi, xfill);
411  r[0][hbhe]->Fill(ieta, xfill);
412  p[0][hbhe]->Fill(ieta, xfill);
413 
414  h[1][0][hbhe][idep]->Fill(ieta, iphi,lutresp[0][iraw]);
415  h[1][1][hbhe][idep]->Fill(ieta, iphi,lutresp[1][iraw]);
416  xfill=lutresp[1][iraw]/lutresp[0][iraw];
417  h[1][2][hbhe][idep]->Fill(ieta, iphi, xfill);
418  r[1][hbhe]->Fill(ieta, xfill);
419  p[1][hbhe]->Fill(ieta, xfill);
420 
421  h[2][0][hbhe][idep]->Fill(ieta, iphi,lutgain[0][iraw]);
422  h[2][1][hbhe][idep]->Fill(ieta, iphi,lutgain[1][iraw]);
423  xfill=lutgain[1][iraw]/lutgain[0][iraw];
424  h[2][2][hbhe][idep]->Fill(ieta, iphi, xfill);
425  r[2][hbhe]->Fill(ieta, xfill);
426  p[2][hbhe]->Fill(ieta, xfill);
427 
428  h[3][0][hbhe][idep]->Fill(ieta, iphi, threshold[0]);
429  h[3][1][hbhe][idep]->Fill(ieta, iphi, threshold[1]);
430  xfill=threshold[1]-threshold[0];
431  h[3][2][hbhe][idep]->Fill(ieta, iphi, xfill);
432  r[3][hbhe]->Fill(ieta, xfill);
433  p[3][hbhe]->Fill(ieta, xfill);
434 
435  size_t maxvalue=hbhe==0?1023:2047;
436 
437  //LutDifference
438  for(size_t i=0; i<size; ++i){
439 
440  hlut[hbhe][2]->Fill(i, llut1[i]==0?0:(double)llut2[i]/llut1[i]);
441 
442  if(i==size-1 || (llut1[i]==maxvalue || llut2[i]==maxvalue)){ //Fill with only the last one before the maximum
443  if(llut1[i-1]==0 || llut2[i-1]==0) {
444  break;
445  }
446  double condratio=lutgain[1][iraw]/lutgain[0][iraw] * lutresp[1][iraw]/lutresp[0][iraw];
447  xfill= (double)llut2[i-1]/llut1[i-1];
448  hslope[hbhe]->Fill(condratio, xfill);
449 
450  h[4][0][hbhe][idep]->Fill(ieta, iphi, (double)llut1[i-1]/(i-1));
451  h[4][1][hbhe][idep]->Fill(ieta, iphi, (double)llut2[i-1]/(i-1));
452  h[4][2][hbhe][idep]->Fill(ieta, iphi, xfill);
453  r[4][hbhe]->Fill(ieta, xfill);
454  p[4][hbhe]->Fill(ieta, xfill);
455 
456  break;
457  }
458  }
459  }
460 
461  gROOT->SetStyle("Plain");
462  gStyle->SetPalette(1);
463  gStyle->SetStatW(0.2);
464  gStyle->SetStatH(0.1);
465  gStyle->SetStatY(1.0);
466  gStyle->SetStatX(0.9);
467  gStyle->SetOptStat(110010);
468  gStyle->SetOptFit(111111);
469 
470  //Draw and Print
471  TCanvas *cc = new TCanvas("cc", "cc", 0, 0, 1600, 1200);
472  cc->SetGridy();
473  for(int j=0; j<NVAR; ++j){
474  gSystem->mkdir(TString(plotsDir)+"/_"+titleVar[j]);
475  for(int d=0; d<NDET; ++d){
476  cc->Clear();
477  r[j][d]->Draw("colz");
478  cc->Print(TString(plotsDir)+"/_"+titleVar[j]+"/"+TString(r[j][d]->GetName())+".pdf");
479 
480  cc->Clear();
481  p[j][d]->Draw();
482  if(doRatio[j]){
483  p[j][d]->SetMinimum(Pmin);
484  p[j][d]->SetMaximum(Pmax);
485  }
486  cc->Print(TString(plotsDir)+"/_"+titleVar[j]+"/"+TString(p[j][d]->GetName())+".pdf");
487 
488  for(int i=0; i<NLEV; ++i){
489  for(int p=0; p<DEP[d]; ++p){
490  cc->Clear();
491  h[j][i][d][p]->Draw("colz");
492 
493  if(i==NLEV-1){
494  if(doRatio[j]){
495  h[j][2][d][p]->SetMinimum(Zmin);
496  h[j][2][d][p]->SetMaximum(Zmax);
497  }
498  else{
499  h[j][2][d][p]->SetMinimum(-3);
500  h[j][2][d][p]->SetMaximum( 3);
501  }
502  }
503  cc->Print(TString(plotsDir)+"/_"+titleVar[j]+"/"+TString(h[j][i][d][p]->GetName())+".pdf");
504  }
505  }
506  }
507  }
508 
509  for(int i=0; i<NLEV; ++i){
510  cc->Clear();
511  hlut[0][i]->Draw();
512  hlut[1][i]->Draw("sames");
513  hlut[2][i]->Draw("sames");
514  hlut[3][i]->Draw("sames");
515  cc->Print(TString(plotsDir)+Form("LUT_%d.gif",i));
516  }
517  cc->Clear(); hslope[0]->Draw("colz"); cc->SetGridx(); cc->SetGridy(); cc->Print(TString(plotsDir)+"GainLutScatterHBHE.pdf");
518  cc->Clear(); hslope[1]->Draw("colz"); cc->SetGridx(); cc->SetGridy(); cc->Print(TString(plotsDir)+"GainLutScatterLutHF.pdf");
519 
520  for(int i=0; i<2; ++i){
521  cc->Clear();
522  houtput[0][i]->Draw("box");
523  houtput[1][i]->Draw("samebox");
524  houtput[2][i]->Draw("samebox");
525  houtput[3][i]->Draw("samebox");
526  cc->Print(TString(plotsDir)+Form("OUT_%d.gif",i));
527  }
528 }
529 
530 
531 
532 
533 
534 void
537  desc.setUnknown();
538  descriptions.addDefault(desc);
539 }
540 
size
Write out results.
T getParameter(std::string const &) const
Definition: LutXml.h:27
bool valid(const DetId &id) const override
CaloTopology const * topology(0)
vector< string > vstring
Definition: ExoticaDQM.cc:8
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int create_lut_map(void)
Definition: LutXml.cc:375
std::string plotsDir
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void addDefault(ParameterSetDescription const &psetDescription)
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< std::string > quality_
std::vector< std::string > gains_
HcalLutAnalyzer(const edm::ParameterSet &)
base
Make Sure CMSSW is Setup ##.
std::string inputDir
ii
Definition: cuy.py:588
std::vector< std::string > pedestals_
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:151
std::vector< std::string > respcorrs_
const T & get() const
Definition: EventSetup.h:55
std::vector< std::string > tags_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string fullPath() const
Definition: FileInPath.cc:184
HcalGenericSubdetector genericSubdet() const
bool validHT(const HcalTrigTowerDetId &id) const