CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalLaserAnalyzerYousi.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EcalLaserAnalyzerYousi
4 // Class: EcalLaserAnalyzerYousi
5 //
13 //
14 // Original Author: Yousi Ma
15 // Created: Tue Jun 19 23:06:36 CEST 2007
16 // $Id: EcalLaserAnalyzerYousi.cc,v 1.1 2009/06/02 12:55:19 malcles Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 #include <iostream>
24 #include <fstream>
25 
26 
27 // user include files
30 
33 
36 
42 
43 #include "TFile.h"
44 #include "TH1F.h"
45 #include "TH2F.h"
46 #include "TROOT.h"
47 #include "TF1.h"
48 #include "TNtuple.h"
49 #include "TDirectory.h"
50 
51 
52 
53 //
54 // class decleration
55 //
56 
58  public:
61 
62 
63  private:
64  virtual void beginJob();
65  virtual void analyze(const edm::Event&, const edm::EventSetup&);
66  virtual void endJob();
67 
68  // ----------member data ---------------------------
69  // Declare histograms and ROOT trees, etc.
70  TH1F *C_APD[1700];
71  TH1F *C_APDPN[1700];
72  TH1F *C_PN[1700];
73  TH1F *C_J[1700];
74  TH2F *C_APDPN_J[1700];
75 
76  TH1F *peakAPD[2];
77  TH1F *peakAPDPN[2];
78  TH1F *APD_LM[9];
79  TH1F *APDPN_LM[9];
80  TH2F *APDPN_J_LM[9];
81  TH2F *APDPN_J_H[2];
82 
83 
84  //fixme make declare and init separate
85  TH2F *APD;
86  TH2F *APD_RMS;
87  TH2F *APDPN;
88  TH2F *APDPN_RMS;
89  TH2F *PN;
90  TH2F *APDPN_J;
91  TH2F *APDPN_C;
92 
93  TH1F *FitHist;
94  TH2F *Count;
95 
96 
97  TFile *fPN;
98  TFile *fAPD;
99  TFile *fROOT;
100 
101  TNtuple *C_Tree[1700];
102 
103  //parameters
104 
105  std::string hitCollection_ ;
106  std::string hitProducer_ ;
107  // std::string PNFileName_ ;
108  // std::string ABFileName_ ;
109  std::string outFileName_ ;
110  std::string SM_ ;
111  std::string Run_ ;
112  std::string digiProducer_ ;
113  std::string PNdigiCollection_ ;
114 
115 
116 };
117 
118 //
119 // constants, enums and typedefs
120 //
121 
122 //
123 // static data member definitions
124 //
125 
126 //
127 // constructors and destructor
128 //
130 {
131  //now do what ever initialization is needed
132  //get the PN and AB file names
133  //get the output file names, digi producers, etc
134 
135  hitCollection_ = iConfig.getUntrackedParameter<std::string>("hitCollection");
136  hitProducer_ = iConfig.getUntrackedParameter<std::string>("hitProducer");
137  // PNFileName_ = iConfig.getUntrackedParameter<std::string>("PNFileName");
138  // ABFileName_ = iConfig.getUntrackedParameter<std::string>("ABFileName");
139  outFileName_ = iConfig.getUntrackedParameter<std::string>("outFileName");
140  SM_ = iConfig.getUntrackedParameter<std::string>("SM");
141  Run_ = iConfig.getUntrackedParameter<std::string>("Run");
142  digiProducer_ = iConfig.getUntrackedParameter<std::string>("digiProducer");
143  PNdigiCollection_ = iConfig.getUntrackedParameter<std::string>("PNdigiCollection");
144 
145 
146 
147 
148 }
149 
150 
152 {
153 
154  // do anything here that needs to be done at desctruction time
155  // (e.g. close files, deallocate resources etc.)
156 
157 
158 
159 }
160 
161 
162 //
163 // member functions
164 //
165 
166 // ------------ method called to for each event ------------
167 void
169 {
170  using namespace edm;
171 
172  // if ( fPN->IsOpen() ) { edm::LogInfo("EcalLaserAnalyzerYousi") <<"fPN is open in analyze OKAAAAAAAAYYY \n\n"; }
173 
174 
175 
177  iEvent.getByLabel(digiProducer_, DCCHeaders);
178 
179  EcalDCCHeaderBlock::EcalDCCEventSettings settings = DCCHeaders->begin()->getEventSettings();
180 
181  int wavelength = settings.wavelength;
182 
183  // std::cout<<"wavelength: "<<wavelength<<"\n\n";
184 
185  if ( wavelength !=0 ) { return; }//only process blue laser
186 
188 
189  try{
190  iEvent.getByLabel(hitProducer_ , hitCollection_ , hits);
191  // iEvent.getByType(hits);
192  } catch ( std::exception& ex ) {
193  LogError("EcalLaserAnalyzerYousi") << "Cannot get product: EBRecHitCollection from: "
194  << hitCollection_ << " - returning.\n\n";
195  // return;
196  }
197 
199 
200  try{
201  // iEvent.getByLabel(hitProducer_, hits);
202  iEvent.getByLabel(digiProducer_, PNdigiCollection_, pndigis);
203  //iEvent.getByType( pndigis );
204  } catch ( std::exception& ex ) {
205  LogError("EcalLaserAnalyzerYousi") << "Cannot get product: EBdigiCollection from: "
206  << "getbytype" << " - returning.\n\n";
207  // return;
208  }
209 
210 
211  Float_t PN_amp[5];
212 
213  //do some averaging over each pair of PNs
214  for (int j = 0; j < 5; ++j) {
215  PN_amp[j] = 0;
216  for (int z = 0; z<2; ++z) {
217  FitHist->Reset();
218  TF1 peakFit("peakFit", "[0] +[1]*x +[2]*x^2", 30, 50);
219  TF1 pedFit("pedFit","[0]",0,5);
220 
221  for(int k = 0; k < 50; k++){
222  FitHist->SetBinContent(k, (*pndigis)[j+z*5].sample(k).adc() );
223  }
224  pedFit.SetParameter(0,750);
225  FitHist->Fit(&pedFit,"RQI");
226  Float_t ped = pedFit.GetParameter(0);
227 
228  Int_t maxbin = FitHist->GetMaximumBin();
229  peakFit.SetRange(FitHist->GetBinCenter(maxbin)-4*FitHist->GetBinWidth(maxbin),
230  FitHist->GetBinCenter(maxbin)+4*FitHist->GetBinWidth(maxbin));
231  peakFit.SetParameters(750,4,-.05);
232  FitHist->Fit(&peakFit,"RQI");
233  Float_t max = peakFit.Eval(-peakFit.GetParameter(1)/(2*peakFit.GetParameter(2)));
234  if(ped != max){
235  PN_amp[j] = PN_amp[j] + max - ped;
236  } else {
237  PN_amp[j] = PN_amp[j] + max;
238  }
239 
240  }//end of z loop
241  PN_amp[j] = PN_amp[j]/2.0;
242  }//end of j loop
243 
244 
245 
246 
247  //do some real PN, APD calculations
248 
249 
250 
251  //FIXME. previously used .info files to get time, what to do now?
252 
253  // TNtuple *Time = new TNtuple("Time", "Time", "Time");
254  // Int_t iTime = Get_Time(Input_File);
255  // Time->Fill(iTime);
256 
257 
258  Float_t fTree[7];
259 
260 
261  Int_t cx, cy;
262  // b->GetEntry(EVT);
263  EBDetId ID;
264  Float_t theAPD;
265  Float_t thePN;
266  Float_t Jitter;
267  Float_t Chi2;
268  Int_t CN = hits->size();
269  // cout<<"num CN: "<<CN<<endl;
270  for(int j = 0; j < CN; j++){
271  ID = (*hits)[j].id();
272  cy = 20-(ID.ic()-1)%20;
273  cx = 85-(ID.ic()-1)/20;
274  theAPD = (*hits)[j].amplitude();
275  Jitter = (*hits)[j].jitter();
276  Chi2 = (*hits)[j].chi2();
277  thePN = PN_amp[ (ID.ic() + 299)/400 ];
278 
279  // cout<<"THE APD: "<<theAPD<<endl;
280  // cout<<"THE PN: "<<thePN<<endl;
281 
282  C_APD[ID.ic()-1]->Fill(theAPD);
283  C_APDPN[ID.ic()-1]->Fill(theAPD/thePN);
284  C_PN[ID.ic()-1]->Fill(thePN);
285  C_J[ID.ic()-1]->Fill(Jitter);
286  C_APDPN_J[ID.ic()-1]->Fill(Jitter, theAPD/thePN);
287  APDPN_J->Fill(Jitter, theAPD/thePN);
288  APDPN_C->Fill(Chi2, theAPD/thePN);
289  fTree[0] = theAPD;
290  fTree[1] = thePN;
291  fTree[2] = theAPD/thePN;
292  fTree[3] = Jitter;
293  fTree[4] = Chi2;
294  fTree[5] = (*hits)[j].pedestal();
295  fTree[6] = iEvent.id().event();
296  C_Tree[ID.ic()-1]->Fill(fTree);
297  if(((ID.ic()-1)%20 > 9)||((ID.ic()-1)<100)){
298  peakAPD[0]->Fill(theAPD);
299  peakAPDPN[0]->Fill(theAPD/thePN);
300  APDPN_J_H[0]->Fill(Jitter, theAPD/thePN);
301  } else {
302  peakAPD[1]->Fill(theAPD);
303  peakAPDPN[1]->Fill(theAPD/thePN);
304  APDPN_J_H[1]->Fill(Jitter, theAPD/thePN);
305  }
306  if((ID.ic()-1) < 100){
307  APD_LM[0]->Fill(theAPD);
308  APDPN_LM[0]->Fill(theAPD/thePN);
309  APDPN_J_LM[0]->Fill(Jitter, theAPD/thePN);
310  }
311  else {
312  Int_t index;
313  if(((ID.ic()-1)%20) < 10){
314  index = ((ID.ic()-101)/400)*2 + 1;
315  APD_LM[index]->Fill(theAPD);
316  APDPN_LM[index]->Fill(theAPD/thePN);
317  APDPN_J_LM[index]->Fill(Jitter, theAPD/thePN);
318  }
319  else {
320  index = ((ID.ic()-101)/400)*2 + 2;
321  APD_LM[index]->Fill(theAPD);
322  APDPN_LM[index]->Fill(theAPD/thePN);
323  APDPN_J_LM[index]->Fill(Jitter, theAPD/thePN);
324  }
325  }
326  }//end of CN loop
327 
328 
329 
330 
331  //now that you got the PN and APD's, make the ntuples. done
332 
333  //vec from ROOT version should correspond to hits_itr or something similar. done
334 
335  //check WL from PNdiodedigi, should be ==0, o.w (blue data only). don't process. done
336 
337  //get PN pulse, and do fitting of pulse. i.e. fill hist with PN.apd() or equivalent. done
338 
339  //fit to first 5 for PED, and 30-50 bins for pulse (poly2 for the moment). done
340 
341 
342 
343 
344 }
345 
346 
347 // ------------ method called once each job just before starting event loop ------------
348 void
350 {
351 
352  edm::LogInfo("EcalLaserAnalyzerYousi") << "running laser analyzer \n\n";
353 
354  fROOT = new TFile(outFileName_.c_str(), "RECREATE");
355  fROOT->cd();
356 
357  //init all the histos and files?
358  APD = new TH2F("APD", "APD", 85, 0., 85., 20, 0., 20.);
359  APD_RMS = new TH2F("APD_RMS", "APD_RMS", 85, 0., 85., 20, 0., 20.);
360  APDPN = new TH2F("APDPN", "APDPN", 85, 0., 85., 20, 0., 20.);
361  APDPN_RMS = new TH2F("APDPN_RMS", "APDPN_RMS", 85, 0., 85., 20, 0., 20.);
362  PN = new TH2F("PN", "PN", 85, 0., 85., 20, 0., 20.);
363  APDPN_J = new TH2F("JittervAPDPN", "JittervAPDPN",250, 3., 7., 250, 1., 2.);
364  APDPN_C = new TH2F("Chi2vAPDPN", "Chi2vAPDPN", 250, 0., 50., 250, 0., 5.0);
365  FitHist = new TH1F("FitHist", "FitHist", 50, 0, 50);
366  Count = new TH2F("Count", "Count", 85, 0., 1., 20, 0., 1.);
367 
368 
369  for(int i = 0; i < 1700; i++){
370  std::ostringstream name_1;
371  std::ostringstream name_2;
372  std::ostringstream name_3;
373  std::ostringstream name_4;
374  std::ostringstream name_5;
375  name_1 << "C_APD_" << i+1;
376  name_2 << "C_APDPN_" << i+1;
377  name_3 << "C_PN_" << i+1;
378  name_4 << "C_J_" << i+1;
379  name_5 << "C_APDPN_J_" << i+1;
380  C_APD[i] = new TH1F(name_1.str().c_str(), name_1.str().c_str(), 2500, 0., 5000.);
381  C_APDPN[i] = new TH1F(name_2.str().c_str(), name_2.str().c_str(), 20000, 0., 25.);
382  C_PN[i] = new TH1F(name_3.str().c_str(), name_3.str().c_str(), 1000, 0., 4000.);
383  C_J[i] = new TH1F(name_4.str().c_str(), name_4.str().c_str(), 250, 3.0, 7.);
384  C_APDPN_J[i] = new TH2F(name_5.str().c_str(), name_5.str().c_str(), 250, 3.0, 6., 250, 1., 2.2);
385  }
386 
387  for(int i = 0; i < 2; i++){
388  std::ostringstream aname_1;
389  std::ostringstream aname_2;
390  std::ostringstream aname_3;
391  aname_1 << "peakAPD_" << i;
392  aname_2 << "peakAPDPN_" << i;
393  aname_3 << "JittervAPDPN_Half_" << i;
394  peakAPD[i] = new TH1F(aname_1.str().c_str(), aname_1.str().c_str(), 1000, 0., 5000.);
395  peakAPDPN[i] = new TH1F(aname_2.str().c_str(), aname_2.str().c_str(), 1000, 0., 8.);
396  APDPN_J_H[i] = new TH2F(aname_3.str().c_str(), aname_3.str().c_str(), 250, 3., 7., 250, 1., 2.2);
397  }
398 
399  for(int i = 0; i < 9; i++){
400  std::ostringstream bname_1;
401  std::ostringstream bname_2;
402  std::ostringstream bname_3;
403  bname_1 << "APD_LM_" << i;
404  bname_2 << "APDPN_LM_" << i;
405  bname_3 << "APDPN_J_LM_" << i;
406  APD_LM[i] = new TH1F(bname_1.str().c_str(), bname_1.str().c_str(), 500, 0., 5000.);
407  APDPN_LM[i] = new TH1F(bname_2.str().c_str(), bname_2.str().c_str(), 500, 0., 8.);
408  APDPN_J_LM[i] = new TH2F(bname_3.str().c_str(), bname_3.str().c_str(), 250, 3., 7., 250, 1., 2.2);
409  }
410 
411  //get the PN file. or don't get, and read from event.
412 
413 
414  //don't need to get AB, it will be read in via framework poolsource = ???
415 
416  //configure the final NTuple
417  std::ostringstream varlist;
418  varlist << "APD:PN:APDPN:Jitter:Chi2:ped:EVT";
419  for(int i = 0; i < 1700; i++){
420  std::ostringstream name;
421  name << "C_Tree_" << i+1;
422  C_Tree[i] = (TNtuple*) new TNtuple(name.str().c_str(), name.str().c_str(),
423  varlist.str().c_str());
424  }
425 
426 }
427 
428 // ------------ method called once each job just after ending the event loop ------------
429 void
431 
432  //write the file (get ouput file name first).
433  TFile *fROOT = (TFile*) new TFile(outFileName_.c_str(), "RECREATE");
434 
435  // TDirectory *DIR = fROOT->Get(Run_.c_str());
436  TDirectory *DIR;
437  // if(DIR == NULL){
438  DIR = fROOT->mkdir(Run_.c_str());
439  // }
440  DIR->cd();
441  for(int j = 0; j < 1700; j++){
442  Float_t min_r, max_r;
443  Float_t RMS, Sigma, K;
444  Int_t iCount;
445  TF1 *gs1;
446  TF1 *gs2;
447  TF1 *gs3;
448 
449  RMS = C_APD[j]->GetRMS();
450  APD_RMS->SetBinContent(85-(j/20), 20-(j%20), RMS);
451  Sigma = 999999;
452  K = 2.5;
453  iCount = 0;
454  while(Sigma > RMS){
455  min_r = C_APD[j]->GetBinCenter(C_APD[j]->GetMaximumBin()) - K*RMS;
456  max_r = C_APD[j]->GetBinCenter(C_APD[j]->GetMaximumBin()) + K*RMS;
457  gs1 = new TF1("gs1", "gaus", min_r, max_r);
458  C_APD[j]->Fit("gs1", "RQI");
459  Sigma = gs1->GetParameter(2);
460  K = K*1.5;
461  iCount++;
462  if(iCount > 2){
463  C_APD[j]->Fit("gaus", "QI");
464  gs1 = C_APD[j]->GetFunction("gaus");
465  break;
466  }
467  }
468 
469  RMS = C_APDPN[j]->GetRMS();
470  APDPN_RMS->SetBinContent(85-(j/20), 20-(j%20), RMS);
471  Sigma = 999999;
472  K = 2.5;
473  iCount = 0;
474  while(Sigma > RMS){
475  min_r = C_APDPN[j]->GetBinCenter(C_APDPN[j]->GetMaximumBin()) - K*RMS;
476  max_r = C_APDPN[j]->GetBinCenter(C_APDPN[j]->GetMaximumBin()) + K*RMS;
477  gs2 = new TF1("gs2", "gaus", min_r, max_r);
478  C_APDPN[j]->Fit("gs2", "RQI");
479  Sigma = gs2->GetParameter(2);
480  K = K*1.5;
481  iCount++;
482  if(iCount > 2){
483  C_APDPN[j]->Fit("gaus", "QI");
484  gs2 = C_APDPN[j]->GetFunction("gaus");
485  break;
486  }
487  }
488 
489  TF1 *newgs1;
490  TF1 *newgs2;
491 
492  C_PN[j]->Fit("gaus", "Q");
493  C_APD[j]->Fit("gaus","QI");
494  C_APDPN[j]->Fit("gaus","QI");
495  C_APD[j]->Write("", TObject::kOverwrite);
496  C_APDPN[j]->Write("", TObject::kOverwrite);
497  C_PN[j]->Write("", TObject::kOverwrite);
498  C_J[j]->Write("", TObject::kOverwrite);
499  C_APDPN_J[j]->Write("", TObject::kOverwrite);
500  newgs1 = C_APD[j]->GetFunction("gaus");
501  newgs2 = C_APDPN[j]->GetFunction("gaus");
502  gs3 = C_PN[j]->GetFunction("gaus");
503  Float_t theAPD = newgs1->GetParameter(1);
504  APD->SetBinContent(85-(j/20), 20-(j%20), theAPD);
505  Float_t theAPDPN = newgs2->GetParameter(1);
506  APDPN->SetBinContent(85-(j/20), 20-(j%20), theAPDPN);
507  Float_t thePN = gs3->GetParameter(1);
508  // cout<<"LOOK HERE thePN = "<< thePN<<endl;
509  PN->SetBinContent(85-(j/20), 20-(j%20), thePN);
510  C_Tree[j]->Write("", TObject::kOverwrite);
511  }
512 
513  for(int i = 0; i < 9; i++){
514  APD_LM[i]->Write("", TObject::kOverwrite);
515  APDPN_LM[i]->Write("", TObject::kOverwrite);
516  APDPN_J_LM[i]->Write("", TObject::kOverwrite);
517  }
518 
519 
520  // Time->Write("", TObject::kOverwrite);
521  APD->Write("", TObject::kOverwrite);
522  APD_RMS->Write("", TObject::kOverwrite);
523  APDPN_RMS->Write("", TObject::kOverwrite);
524  APDPN->Write("", TObject::kOverwrite);
525  APDPN_J->Write("", TObject::kOverwrite);
526  APDPN_C->Write("", TObject::kOverwrite);
527  PN->Write("", TObject::kOverwrite);
528  peakAPD[0]->Write("", TObject::kOverwrite);
529  peakAPD[1]->Write("", TObject::kOverwrite);
530  peakAPDPN[0]->Write("", TObject::kOverwrite);
531  peakAPDPN[1]->Write("", TObject::kOverwrite);
532  APDPN_J_H[0]->Write("", TObject::kOverwrite);
533  APDPN_J_H[1]->Write("", TObject::kOverwrite);
534 
535 
536 
537  // don't Make plots
538  // fROOT->Close();
539 
540 // fPN->Close();
541 // fAPD->Close();
542 
543 
544  fROOT->Write();
545 // fROOT->Close();
546 
547 }
548 
549 //define this as a plug-in
int adc(sample_type sample)
get the ADC sample (12 bits)
EventNumber_t event() const
Definition: EventID.h:44
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
Definition: FitHist.h:8
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
uint32_t ID
Definition: Definitions.h:26
virtual void analyze(const edm::Event &, const edm::EventSetup &)
int iEvent
Definition: GenABIO.cc:243
Definition: DDAxes.h:10
const T & max(const T &a, const T &b)
int j
Definition: DBlmapReader.cc:9
static const char * Sigma
Definition: FitHist_fwd.h:8
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
int k[5][pyjets_maxn]
list DIR
Definition: linker.py:124
edm::EventID id() const
Definition: EventBase.h:56
Definition: Chi2.h:17
EcalLaserAnalyzerYousi(const edm::ParameterSet &)