CMS 3D CMS Logo

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