CMS 3D CMS Logo

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