CMS 3D CMS Logo

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