CMS 3D CMS Logo

DTNoiseComputation.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author G. Mila - INFN Torino
5  */
6 
7 
10 
11 // Framework
17 
18 // Geometry
23 
24 // Digis
29 
30 #include "TH1F.h"
31 #include "TH2F.h"
32 #include "TFile.h"
33 #include "TF1.h"
34 #include "TProfile.h"
35 #include "TPostScript.h"
36 #include "TCanvas.h"
37 #include "TLegend.h"
38 
39 using namespace edm;
40 using namespace std;
41 
42 
44 
45  cout << "[DTNoiseComputation]: Constructor" <<endl;
46 
47  // Get the debug parameter for verbose output
48  debug = ps.getUntrackedParameter<bool>("debug");
49 
50  // The analysis type
51  fastAnalysis = ps.getUntrackedParameter<bool>("fastAnalysis", true);
52 
53  // The root file which contain the histos
54  string rootFileName = ps.getUntrackedParameter<string>("rootFileName");
55  theFile = new TFile(rootFileName.c_str(), "READ");
56 
57  // The new root file which contain the histos
58  string newRootFileName = ps.getUntrackedParameter<string>("newRootFileName");
59  theNewFile = new TFile(newRootFileName.c_str(), "RECREATE");
60 
61  // The maximum number of events to analyze
62  MaxEvents = ps.getUntrackedParameter<int>("MaxEvents");
63 
64 }
65 
67 {
68  // Get the DT Geometry
69  setup.get<MuonGeometryRecord>().get(dtGeom);
70 
71  static int count = 0;
72 
73  if(count == 0){
74  string CheckHistoName;
75 
76  TH1F *hOccHisto;
77  TH1F *hAverageNoiseHisto;
78  TH1F *hAverageNoiseIntegratedHisto;
79  TH1F *hAverageNoiseHistoPerCh;
80  TH1F *hAverageNoiseIntegratedHistoPerCh;
81  TH2F *hEvtHisto;
82  string HistoName;
83  string Histo2Name;
84  string AverageNoiseName;
85  string AverageNoiseIntegratedName;
86  string AverageNoiseNamePerCh;
87  string AverageNoiseIntegratedNamePerCh;
88  TH1F *hnoisyC;
89  TH1F *hsomeHowNoisyC;
90 
91  // Loop over all the chambers
92  vector<const DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
93  vector<const DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
94  // Loop over the SLs
95  for (; ch_it != ch_end; ++ch_it) {
96  DTChamberId ch = (*ch_it)->id();
97  vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();
98  vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
99  // Loop over the SLs
100  for(; sl_it != sl_end; ++sl_it) {
101  // DTSuperLayerId sl = (*sl_it)->id();
102  vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin();
103  vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
104  // Loop over the Ls
105  for(; l_it != l_end; ++l_it) {
106  DTLayerId dtLId = (*l_it)->id();
107 
108  //check if the layer has digi
109  theFile->cd();
110  CheckHistoName = "DigiOccupancy_" + getLayerName(dtLId);
111  TH1F *hCheckHisto = (TH1F *) theFile->Get(CheckHistoName.c_str());
112  if(hCheckHisto){
113  delete hCheckHisto;
114  stringstream wheel; wheel << ch.wheel();
115  stringstream station; station << ch.station();
116 
117  if(someHowNoisyC.find(make_pair(ch.wheel(),ch.station())) == someHowNoisyC.end()) {
118  TString histoName_someHowNoisy = "somehowNoisyCell_W"+wheel.str()+"_St"+station.str();
119  hsomeHowNoisyC = new TH1F(histoName_someHowNoisy,histoName_someHowNoisy,getMaxNumBins(ch),1,getMaxNumBins(ch)+1);
120  someHowNoisyC[make_pair(ch.wheel(),ch.station())]=hsomeHowNoisyC;
121  }
122 
123  if(noisyC.find(make_pair(ch.wheel(),ch.station())) == noisyC.end()) {
124  TString histoName_noisy = "noisyCell_W"+wheel.str()+"_St"+station.str();
125  hnoisyC = new TH1F(histoName_noisy,histoName_noisy,getMaxNumBins(ch),1,getMaxNumBins(ch)+1);
126  noisyC[make_pair(ch.wheel(),ch.station())]=hnoisyC;
127  }
128 
129  //to fill a map with the average noise per wire and fill new noise histo
130  if(AvNoisePerSuperLayer.find(dtLId.superlayerId()) == AvNoisePerSuperLayer.end()) {
131  AverageNoiseName = "AverageNoise_" + getSuperLayerName(dtLId.superlayerId());
132  hAverageNoiseHisto = new TH1F(AverageNoiseName.c_str(), AverageNoiseName.c_str(), 200, 0, 10000);
133  AverageNoiseIntegratedName = "AverageNoiseIntegrated_" + getSuperLayerName(dtLId.superlayerId());
134  hAverageNoiseIntegratedHisto = new TH1F(AverageNoiseIntegratedName.c_str(), AverageNoiseIntegratedName.c_str(), 200, 0, 10000);
135  AvNoisePerSuperLayer[dtLId.superlayerId()] = hAverageNoiseHisto;
136  AvNoiseIntegratedPerSuperLayer[dtLId.superlayerId()] = hAverageNoiseIntegratedHisto;
137  if(debug){
138  cout << " New Average Noise Histo per SuperLayer : " << hAverageNoiseHisto->GetName() << endl;
139  cout << " New Average Noise Integrated Histo per SuperLayer : " << hAverageNoiseHisto->GetName() << endl;
140  }
141  }
142  if(AvNoisePerChamber.find(dtLId.superlayerId().chamberId()) == AvNoisePerChamber.end()) {
143  AverageNoiseNamePerCh = "AverageNoise_" + getChamberName(dtLId);
144  hAverageNoiseHistoPerCh = new TH1F(AverageNoiseNamePerCh.c_str(), AverageNoiseNamePerCh.c_str(), 200, 0, 10000);
145  AverageNoiseIntegratedNamePerCh = "AverageNoiseIntegrated_" + getChamberName(dtLId);
146  hAverageNoiseIntegratedHistoPerCh = new TH1F(AverageNoiseIntegratedNamePerCh.c_str(), AverageNoiseIntegratedNamePerCh.c_str(), 200, 0, 10000);
147  AvNoisePerChamber[dtLId.superlayerId().chamberId()] = hAverageNoiseHistoPerCh;
148  AvNoiseIntegratedPerChamber[dtLId.superlayerId().chamberId()] = hAverageNoiseIntegratedHistoPerCh;
149  if(debug)
150  cout << " New Average Noise Histo per chamber : " << hAverageNoiseHistoPerCh->GetName() << endl;
151  }
152 
153  HistoName = "DigiOccupancy_" + getLayerName(dtLId);
154  theFile->cd();
155  hOccHisto = (TH1F *) theFile->Get(HistoName.c_str());
156  int numBin = hOccHisto->GetXaxis()->GetNbins();
157  for (int bin=1; bin<=numBin; bin++) {
158  DTWireId wireID(dtLId, bin);
159  theAverageNoise[wireID]= hOccHisto->GetBinContent(bin);
160  if(theAverageNoise[wireID] != 0) {
161  AvNoisePerSuperLayer[dtLId.superlayerId()]->Fill(theAverageNoise[wireID]);
162  AvNoisePerChamber[dtLId.superlayerId().chamberId()]->Fill(theAverageNoise[wireID]);
163  }
164  }
165 
166  //to compute the average noise per layer (excluding the noisy cells)
167  double numCell=0;
168  double AvNoise=0;
169  HistoName = "DigiOccupancy_" + getLayerName(dtLId);
170  theFile->cd();
171  hOccHisto = (TH1F *) theFile->Get(HistoName.c_str());
172  numBin = hOccHisto->GetXaxis()->GetNbins();
173  for (int bin=1; bin<=numBin; bin++) {
174  DTWireId wireID(dtLId, bin);
175  theAverageNoise[wireID]= hOccHisto->GetBinContent(bin);
176  if(hOccHisto->GetBinContent(bin)<100){
177  numCell++;
178  AvNoise += hOccHisto->GetBinContent(bin);
179  }
180  if(hOccHisto->GetBinContent(bin)>100 && hOccHisto->GetBinContent(bin)<500){
181  someHowNoisyC[make_pair(ch.wheel(),ch.station())]->Fill(bin);
182  cout<<"filling somehow noisy cell"<<endl;
183  }
184  if(hOccHisto->GetBinContent(bin)>500){
185  noisyC[make_pair(ch.wheel(),ch.station())]->Fill(bin);
186  cout<<"filling noisy cell"<<endl;
187  }
188  }
189  AvNoise = AvNoise/numCell;
190  cout<<"theAverageNoise for layer "<<getLayerName(dtLId)<<" is : "<<AvNoise << endl;
191 
192 
193  // book the digi event plots every 1000 events
194  int updates = MaxEvents/1000;
195  for(int evt=0; evt<updates; evt++){
196  stringstream toAppend; toAppend << evt;
197  Histo2Name = "DigiPerWirePerEvent_" + getLayerName(dtLId) + "_" + toAppend.str();
198  theFile->cd();
199  hEvtHisto = (TH2F *) theFile->Get(Histo2Name.c_str());
200  if(hEvtHisto){
201  if(debug)
202  cout << " New Histo with the number of events per evt per wire: " << hEvtHisto->GetName() << endl;
203  theEvtMap[dtLId].push_back(hEvtHisto);
204  }
205  }
206 
207  }// done if the layer has digi
208  }// loop over layers
209  }// loop over superlayers
210  }// loop over chambers
211 
212  count++;
213  }
214 
215 }
216 
218 
219  cout << "[DTNoiseComputation] endjob called!" <<endl;
220  TH1F *hEvtDistance=0;
221  TF1 *ExpoFit = new TF1("ExpoFit","expo", 0.5, 1000.5);
222  ExpoFit->SetMarkerColor();//just silence gcc complaining about unused vars
223  TF1 *funct=0;
224  TProfile *theNoiseHisto = new TProfile("theNoiseHisto","Time Constant versus Average Noise",100000,0,100000);
225 
226 
227  //compute the time constant
228  for(map<DTLayerId, vector<TH2F*> >::const_iterator lHisto = theEvtMap.begin();
229  lHisto != theEvtMap.end();
230  ++lHisto) {
231  for(int bin=1; bin<(*lHisto).second[0]->GetYaxis()->GetNbins(); bin++){
232  int distanceEvt = 1;
233  DTWireId wire((*lHisto).first, bin);
234  for(int i=0; i<int((*lHisto).second.size()); i++){
235  for(int evt=1; evt<=(*lHisto).second[i]->GetXaxis()->GetNbins(); evt++){
236  if((*lHisto).second[i]->GetBinContent(evt,bin) == 0) distanceEvt++;
237  else {
238  if(toDel.find(wire) == toDel.end()) {
239  toDel[wire] = false;
240  stringstream toAppend; toAppend << bin;
241  string Histo = "EvtDistancePerWire_" + getLayerName((*lHisto).first) + "_" + toAppend.str();
242  hEvtDistance = new TH1F(Histo.c_str(),Histo.c_str(), 50000,0.5,50000.5);
243  }
244  hEvtDistance->Fill(distanceEvt);
245  distanceEvt=1;
246  }
247  }
248  }
249  if(toDel.find(wire) != toDel.end()){
250  theHistoEvtDistancePerWire[wire] = hEvtDistance;
251  theNewFile->cd();
252  theHistoEvtDistancePerWire[wire]->Fit("ExpoFit","R");
253  funct = theHistoEvtDistancePerWire[wire]->GetFunction("ExpoFit");
254  double par0 = funct->GetParameter(0);
255  double par1 = funct->GetParameter(1);
256  cout<<"par0: "<<par0<<" par1: "<<par1<<endl;
257  double chi2rid = funct->GetChisquare()/funct->GetNDF();
258  if(chi2rid>10)
259  theTimeConstant[wire]=1;
260  else
261  theTimeConstant[wire]=par1;
262  toDel[wire] = true;
263  theHistoEvtDistancePerWire[wire]->Write();
264  delete hEvtDistance;
265  }
266  }
267  }
268 
269  if(!fastAnalysis){
270  //fill the histo with the time constant as a function of the average noise
271  for(map<DTWireId, double>::const_iterator AvNoise = theAverageNoise.begin();
272  AvNoise != theAverageNoise.end();
273  ++AvNoise) {
274  DTWireId wire = (*AvNoise).first;
275  theNoiseHisto->Fill((*AvNoise).second, theTimeConstant[wire]);
276  cout<<"Layer: "<<getLayerName(wire.layerId())<<" wire: "<<wire.wire()<<endl;
277  cout<<"The Average noise: "<<(*AvNoise).second<<endl;
278  cout<<"The time constant: "<<theTimeConstant[wire]<<endl;
279  }
280  theNewFile->cd();
281  theNoiseHisto->Write();
282  }
283 
284 
285  // histos with the integrated noise per layer
286  int numBin;
287  double integratedNoise, bin, halfBin, maxBin;
288  for(map<DTSuperLayerId, TH1F*>::const_iterator AvNoiseHisto = AvNoisePerSuperLayer.begin();
289  AvNoiseHisto != AvNoisePerSuperLayer.end();
290  ++AvNoiseHisto) {
291  integratedNoise=0;
292  numBin = (*AvNoiseHisto).second->GetXaxis()->GetNbins();
293  maxBin = (*AvNoiseHisto).second->GetXaxis()->GetXmax();
294  bin= double(maxBin/numBin);
295  halfBin=double(bin/2);
296  theNewFile->cd();
297  (*AvNoiseHisto).second->Write();
298  for(int i=1; i<numBin; i++){
299  integratedNoise+=(*AvNoiseHisto).second->GetBinContent(i);
300  AvNoiseIntegratedPerSuperLayer[(*AvNoiseHisto).first]->Fill(halfBin,integratedNoise);
301  halfBin+=bin;
302  }
303  theNewFile->cd();
304  AvNoiseIntegratedPerSuperLayer[(*AvNoiseHisto).first]->Write();
305  }
306  // histos with the integrated noise per chamber
307  for(map<DTChamberId, TH1F*>::const_iterator AvNoiseHisto = AvNoisePerChamber.begin();
308  AvNoiseHisto != AvNoisePerChamber.end();
309  ++AvNoiseHisto) {
310  integratedNoise=0;
311  numBin = (*AvNoiseHisto).second->GetXaxis()->GetNbins();
312  maxBin = (*AvNoiseHisto).second->GetXaxis()->GetXmax();
313  bin= maxBin/numBin;
314  halfBin=bin/2;
315  theNewFile->cd();
316  (*AvNoiseHisto).second->Write();
317  for(int i=1; i<numBin; i++){
318  integratedNoise+=(*AvNoiseHisto).second->GetBinContent(i);
319  AvNoiseIntegratedPerChamber[(*AvNoiseHisto).first]->Fill(halfBin,integratedNoise);
320  halfBin+=bin;
321  }
322  theNewFile->cd();
323  AvNoiseIntegratedPerChamber[(*AvNoiseHisto).first]->Write();
324  }
325 
326 
327  //overimpose the average noise histo
328  bool histo=false;
329  vector<const DTChamber*>::const_iterator chamber_it = dtGeom->chambers().begin();
330  vector<const DTChamber*>::const_iterator chamber_end = dtGeom->chambers().end();
331  // Loop over the chambers
332  for (; chamber_it != chamber_end; ++chamber_it) {
333  vector<const DTSuperLayer*>::const_iterator sl_it = (*chamber_it)->superLayers().begin();
334  vector<const DTSuperLayer*>::const_iterator sl_end = (*chamber_it)->superLayers().end();
335  // Loop over the SLs
336  for(; sl_it != sl_end; ++sl_it) {
337  DTSuperLayerId sl = (*sl_it)->id();
338  vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin();
339  vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
340 
341  string canvasName = "c" + getSuperLayerName(sl);
342  TCanvas c1(canvasName.c_str(),canvasName.c_str(),600,780);
343  TLegend *leg=new TLegend(0.5,0.6,0.7,0.8);
344  for(; l_it != l_end; ++l_it) {
345  DTLayerId layerId = (*l_it)->id();
346  string HistoName = "DigiOccupancy_" + getLayerName(layerId);
347  theFile->cd();
348  TH1F *hOccHisto = (TH1F *) theFile->Get(HistoName.c_str());
349  if(hOccHisto){
350  string TitleHisto = "AverageNoise_" + getSuperLayerName(sl);
351  cout<<"TitleHisto : "<<TitleHisto<<endl;
352  hOccHisto->SetTitle(TitleHisto.c_str());
353  stringstream layer; layer << layerId.layer();
354  string legendHisto = "layer " + layer.str();
355  leg->AddEntry(hOccHisto,legendHisto.c_str(),"L");
356  hOccHisto->SetMaximum(getYMaximum(sl));
357  histo=true;
358  if(layerId.layer() == 1)
359  hOccHisto->Draw();
360  else
361  hOccHisto->Draw("same");
362  hOccHisto->SetLineColor(layerId.layer());
363  }
364  }
365  if(histo){
366  leg->Draw("same");
367  theNewFile->cd();
368  c1.Write();
369  }
370  }
371  histo=false;
372  }
373 
374  //write on file the noisy plots
375  for(map<pair<int,int>, TH1F*>::const_iterator nCell = noisyC.begin();
376  nCell != noisyC.end();
377  ++nCell) {
378  theNewFile->cd();
379  (*nCell).second->Write();
380  }
381  for(map<pair<int,int>, TH1F*>::const_iterator somehownCell = someHowNoisyC.begin();
382  somehownCell != someHowNoisyC.end();
383  ++somehownCell) {
384  theNewFile->cd();
385  (*somehownCell).second->Write();
386  }
387 
388 }
389 
390 
392 
393  theFile->Close();
394  theNewFile->Close();
395 
396 }
397 
398 
399 string DTNoiseComputation::getLayerName(const DTLayerId& lId) const {
400 
401  const DTSuperLayerId dtSLId = lId.superlayerId();
402  const DTChamberId dtChId = dtSLId.chamberId();
403  stringstream Layer; Layer << lId.layer();
404  stringstream superLayer; superLayer << dtSLId.superlayer();
405  stringstream wheel; wheel << dtChId.wheel();
406  stringstream station; station << dtChId.station();
407  stringstream sector; sector << dtChId.sector();
408 
409  string LayerName =
410  "W" + wheel.str()
411  + "_St" + station.str()
412  + "_Sec" + sector.str()
413  + "_SL" + superLayer.str()
414  + "_L" + Layer.str();
415 
416  return LayerName;
417 
418 }
419 
420 
422 
423  const DTChamberId dtChId = dtSLId.chamberId();
424  stringstream superLayer; superLayer << dtSLId.superlayer();
425  stringstream wheel; wheel << dtChId.wheel();
426  stringstream station; station << dtChId.station();
427  stringstream sector; sector << dtChId.sector();
428 
429  string SuperLayerName =
430  "W" + wheel.str()
431  + "_St" + station.str()
432  + "_Sec" + sector.str()
433  + "_SL" + superLayer.str();
434 
435  return SuperLayerName;
436 
437 }
438 
439 
441 
442  const DTSuperLayerId dtSLId = lId.superlayerId();
443  const DTChamberId dtChId = dtSLId.chamberId();
444  stringstream wheel; wheel << dtChId.wheel();
445  stringstream station; station << dtChId.station();
446  stringstream sector; sector << dtChId.sector();
447 
448  string ChamberName =
449  "W" + wheel.str()
450  + "_St" + station.str()
451  + "_Sec" + sector.str();
452 
453  return ChamberName;
454 
455 }
456 
457 
459 
460  int maximum=0;
461 
462  for(int SL=1; SL<=3; SL++){
463  if(!(chId.station()==4 && SL==2)){
464  for (int L=1; L<=4; L++){
465  DTLayerId layerId = DTLayerId(chId, SL, L);
466  string HistoName = "DigiOccupancy_" + getLayerName(layerId);
467  theFile->cd();
468  TH1F *hOccHisto = (TH1F *) theFile->Get(HistoName.c_str());
469  if(hOccHisto){
470  if (hOccHisto->GetXaxis()->GetXmax()>maximum)
471  maximum = hOccHisto->GetXaxis()->GetNbins();
472  }
473  }
474  }
475  }
476  return maximum;
477 }
478 
479 
481 
482  double maximum=0;
483  double dummy = pow(10.,10.);
484 
485  for (int L=1; L<=4; L++){
486  DTLayerId layerId = DTLayerId(slId, L);
487  string HistoName = "DigiOccupancy_" + getLayerName(layerId);
488  theFile->cd();
489  TH1F *hOccHisto = (TH1F *) theFile->Get(HistoName.c_str());
490  if(hOccHisto){
491  if (hOccHisto->GetMaximum(dummy)>maximum)
492  maximum = hOccHisto->GetMaximum(dummy);
493  }
494  }
495  return maximum;
496 }
497 
498 
499 
DTNoiseComputation(const edm::ParameterSet &ps)
Constructor.
T getUntrackedParameter(std::string const &, T const &) const
int getMaxNumBins(const DTChamberId &chId) const
Definition: Abs.h:5
double getYMaximum(const DTSuperLayerId &slId) const
DTChamberId chamberId() const
Return the corresponding ChamberId.
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:59
std::string getChamberName(const DTLayerId &lId) const
Get the name of the chamber.
SeedingLayerSetsHits::SeedingLayer Layer
Definition: LayerTriplets.h:14
void endJob() override
Endjob.
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
bin
set the eta bin as selection string.
int superlayer() const
Return the superlayer number (deprecated method name)
~DTNoiseComputation() override
Destructor.
#define debug
Definition: HDRShower.cc:19
const T & get() const
Definition: EventSetup.h:55
std::string HistoName
std::string getLayerName(const DTLayerId &lId) const
Get the name of the layer.
HLT enums.
int sector() const
Definition: DTChamberId.h:61
std::string getSuperLayerName(const DTSuperLayerId &slId) const
Get the name of the superLayer.
int station() const
Return the station number.
Definition: DTChamberId.h:51
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
void beginRun(const edm::Run &, const edm::EventSetup &setup) override
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: Run.h:43