Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pi0EtaByEta.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file pi0EtaByEta.cc
1 #include "pi0EtaByEta.h"
2 
5 
6 // Tower includes
7 #include <calobase/RawCluster.h>
8 #include <calobase/RawClusterContainer.h>
9 #include <calobase/RawClusterUtility.h>
10 #include <calobase/RawTowerGeom.h>
11 #include <calobase/RawTowerGeomContainer.h>
12 #include <calobase/TowerInfo.h>
13 #include <calobase/TowerInfoContainer.h>
14 #include <calobase/TowerInfoDefs.h>
15 
16 #include <cdbobjects/CDBTTree.h> // for CDBTTree
17 
20 #include <fun4all/SubsysReco.h> // for SubsysReco
21 
22 #include <phool/getClass.h>
23 #include <phool/phool.h>
24 
25 #include <TF1.h>
26 #include <TFile.h>
27 #include <TH1.h>
28 #include <TH2.h>
29 #include <TLorentzVector.h>
30 #include <TNtuple.h>
31 #include <TTree.h>
32 
33 #include <CLHEP/Vector/ThreeVector.h> // for Hep3Vector
34 
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 #include <utility>
39 
41  : SubsysReco(name)
42  , detector("HCALIN")
43  , outfilename(filename)
44 {
45  h_mass_eta_lt.fill(nullptr);
46 }
47 
49 {
50  delete hm;
51  delete g4hitntuple;
52  delete g4cellntuple;
53  delete towerntuple;
54  delete clusterntuple;
55 }
56 
58 {
59  hm = new Fun4AllHistoManager(Name());
60  // create and register your histos (all types) here
61 
62  outfile = new TFile(outfilename.c_str(), "RECREATE");
63 
64  // correlation plots
65  for (int i = 0; i < 96; i++)
66  {
67  h_mass_eta_lt[i] = new TH1F(Form("h_mass_eta_lt%d", i), "", 50, 0, 0.5);
68  }
69 
70  h_cemc_etaphi = new TH2F("h_cemc_etaphi", "", 96, 0, 96, 256, 0, 256);
71 
72  // 1D distributions
73  h_InvMass = new TH1F("h_InvMass", "Invariant Mass", 120, 0, 1.2);
74  h_InvMassMix = new TH1F("h_InvMassMix", "Invariant Mass", 120, 0, 1.2);
75 
76  // cluster QA
77  h_etaphi_clus = new TH2F("h_etaphi_clus", "", 140, -1.2, 1.2, 64, -1 * M_PI, M_PI);
78  h_clusE = new TH1F("h_clusE", "", 100, 0, 10);
79 
80  h_emcal_e_eta = new TH1F("h_emcal_e_eta", "", 96, 0, 96);
81 
82  h_pt1 = new TH1F("h_pt1", "", 100, 0, 5);
83  h_pt2 = new TH1F("h_pt2", "", 100, 0, 5);
84 
85  h_nclusters = new TH1F("h_nclusters", "", 1000, 0, 1000);
86 
87  return 0;
88 }
89 
91 {
92  _eventcounter++;
93 
94  process_towers(topNode);
95 
97 }
98 
100 {
101  if ((_eventcounter % 1000) == 0)
102  {
103  std::cout << _eventcounter << std::endl;
104  }
105 
106  // float emcaldownscale = 1000000 / 800;
107 
108  float emcal_hit_threshold = 0.5; // GeV
109 
110  // cuts
111  float maxDr = 1.1;
112  float maxAlpha = 0.6;
113  float clus_chisq_cut = 4;
114  float nClus_ptCut = 0.5;
115  int max_nClusCount = 300;
116 
117  //----------------------------------get vertex------------------------------------------------------//
118 
119  GlobalVertexMap* vertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
120  if (!vertexmap)
121  {
122  // std::cout << PHWHERE << " Fatal Error - GlobalVertexMap node is missing"<< std::endl;
123  std::cout << "pi0EtaByEta GlobalVertexMap node is missing" << std::endl;
124  // return Fun4AllReturnCodes::ABORTRUN;
125  }
126  float vtx_z = 0;
127  if (vertexmap && !vertexmap->empty())
128  {
129  GlobalVertex* vtx = vertexmap->begin()->second;
130  if (vtx)
131  {
132  vtx_z = vtx->get_z();
133  }
134  }
135 
136  std::vector<float> ht_eta;
137  std::vector<float> ht_phi;
138 
139  // if (!m_vtxCut || abs(vtx_z) > _vz) return Fun4AllReturnCodes::EVENT_OK;
140 
141  TowerInfoContainer* towers = findNode::getClass<TowerInfoContainer>(topNode, "TOWERINFO_CALIB_CEMC");
142  if (towers)
143  {
144  int size = towers->size(); // online towers should be the same!
145  for (int channel = 0; channel < size; channel++)
146  {
148  float offlineenergy = tower->get_energy();
149  unsigned int towerkey = towers->encode_key(channel);
150  int ieta = towers->getTowerEtaBin(towerkey);
151  int iphi = towers->getTowerPhiBin(towerkey);
152  bool isGood = !(tower->get_isBadChi2());
153  if (!isGood && offlineenergy > 0.2)
154  {
155  ht_eta.push_back(ieta);
156  ht_phi.push_back(iphi);
157  }
158  if (isGood)
159  {
160  h_emcal_e_eta->Fill(ieta, offlineenergy);
161  }
162  if (offlineenergy > emcal_hit_threshold)
163  {
164  h_cemc_etaphi->Fill(ieta, iphi);
165  }
166  }
167  }
168 
169  RawClusterContainer* clusterContainer = findNode::getClass<RawClusterContainer>(topNode, "CLUSTERINFO_CEMC2");
170  if (!clusterContainer)
171  {
172  std::cout << PHWHERE << "funkyCaloStuff::process_event - Fatal Error - CLUSTER_CEMC node is missing. " << std::endl;
173  return 0;
174  }
175 
177  // geometry for hot tower/cluster masking
178  std::string towergeomnodename = "TOWERGEOM_CEMC";
179  RawTowerGeomContainer* m_geometry = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnodename);
180  if (!m_geometry)
181  {
182  std::cout << Name() << "::"
183  << "CreateNodeTree"
184  << ": Could not find node " << towergeomnodename << std::endl;
185  throw std::runtime_error("failed to find TOWERGEOM node in RawClusterDeadHotMask::CreateNodeTree");
186  }
187 
188  RawClusterContainer::ConstRange clusterEnd = clusterContainer->getClusters();
191  int nClusCount = 0;
192  for (clusterIter = clusterEnd.first; clusterIter != clusterEnd.second; clusterIter++)
193  {
194  RawCluster* recoCluster = clusterIter->second;
195 
196  CLHEP::Hep3Vector vertex(0, 0, vtx_z);
197  CLHEP::Hep3Vector E_vec_cluster = RawClusterUtility::GetECoreVec(*recoCluster, vertex);
198 
199  float clus_pt = E_vec_cluster.perp();
200  float clus_chisq = recoCluster->get_chi2();
201 
202  if (clus_pt < nClus_ptCut)
203  {
204  continue;
205  }
206  if (clus_chisq > clus_chisq_cut)
207  {
208  continue;
209  }
210 
211  nClusCount++;
212  }
213 
214  h_nclusters->Fill(nClusCount);
215 
216  if (nClusCount > max_nClusCount)
217  {
219  }
220 
221  float ptClusMax = 5;
222  float pt1ClusCut = 1.3; // 1.3
223  float pt2ClusCut = 0.7; // 0.7
224 
225  if (nClusCount > 30)
226  {
227  pt1ClusCut += 1.4 * (nClusCount - 29) / 200.0;
228  pt2ClusCut += 1.4 * (nClusCount - 29) / 200.0;
229  }
230 
231  float pi0ptcut = 1.22 * (pt1ClusCut + pt2ClusCut);
232 
233  std::vector<float> save_pt;
234  std::vector<float> save_eta;
235  std::vector<float> save_phi;
236  std::vector<float> save_e;
237 
238  for (clusterIter = clusterEnd.first; clusterIter != clusterEnd.second; clusterIter++)
239  {
240  RawCluster* recoCluster = clusterIter->second;
241 
242  CLHEP::Hep3Vector vertex(0, 0, vtx_z);
243  CLHEP::Hep3Vector E_vec_cluster = RawClusterUtility::GetECoreVec(*recoCluster, vertex);
244 
245  float clusE = E_vec_cluster.mag();
246  float clus_eta = E_vec_cluster.pseudoRapidity();
247  float clus_phi = E_vec_cluster.phi();
248  float clus_pt = E_vec_cluster.perp();
249  float clus_chisq = recoCluster->get_chi2();
250  h_clusE->Fill(clusE);
251 
252  if (clus_chisq > clus_chisq_cut)
253  {
254  continue;
255  }
256 
257  // loop over the towers in the cluster
258  RawCluster::TowerConstRange towerCR = recoCluster->get_towers();
260  bool hotClus = false;
261  float lt_e = -1000;
262  unsigned int lt_eta = -1;
263  for (toweriter = towerCR.first; toweriter != towerCR.second; ++toweriter)
264  {
265  int towereta = m_geometry->get_tower_geometry(toweriter->first)->get_bineta();
266  int towerphi = m_geometry->get_tower_geometry(toweriter->first)->get_binphi();
267  unsigned int key = TowerInfoDefs::encode_emcal(towereta, towerphi);
268  unsigned int channel = towers->decode_key(key);
269  float energy = towers->get_tower_at_channel(channel)->get_energy();
270  if (energy > lt_e)
271  {
272  lt_e = energy;
273  lt_eta = towereta;
274  }
275 
276  for (size_t i = 0; i < ht_eta.size(); i++)
277  {
278  if (towerphi == ht_phi[i] && towereta == ht_eta[i])
279  {
280  hotClus = true;
281  }
282  }
283  }
284 
285  if (dynMaskClus && hotClus == true)
286  {
287  continue;
288  }
289 
290  h_etaphi_clus->Fill(clus_eta, clus_phi);
291 
292  TLorentzVector photon1;
293  photon1.SetPtEtaPhiE(clus_pt, clus_eta, clus_phi, clusE);
294 
295  if (clus_pt < pt1ClusCut || clus_pt > ptClusMax)
296  {
297  continue;
298  }
299 
300  for (clusterIter2 = clusterEnd.first; clusterIter2 != clusterEnd.second; clusterIter2++)
301  {
302  if (clusterIter == clusterIter2)
303  {
304  continue;
305  }
306  RawCluster* recoCluster2 = clusterIter2->second;
307 
308  CLHEP::Hep3Vector E_vec_cluster2 = RawClusterUtility::GetECoreVec(*recoCluster2, vertex);
309 
310  float clus2E = E_vec_cluster2.mag();
311  float clus2_eta = E_vec_cluster2.pseudoRapidity();
312  float clus2_phi = E_vec_cluster2.phi();
313  float clus2_pt = E_vec_cluster2.perp();
314  float clus2_chisq = recoCluster2->get_chi2();
315 
316  if (clus2_pt < pt2ClusCut || clus_pt > ptClusMax)
317  {
318  continue;
319  }
320  if (clus2_chisq > clus_chisq_cut)
321  {
322  continue;
323  }
324 
325  // loop over the towers in the cluster
326  RawCluster::TowerConstRange towerCR2 = recoCluster2->get_towers();
328  bool hotClus2 = false;
329  for (toweriter2 = towerCR2.first; toweriter2 != towerCR2.second; ++toweriter2)
330  {
331  int towereta = m_geometry->get_tower_geometry(toweriter2->first)->get_bineta();
332  int towerphi = m_geometry->get_tower_geometry(toweriter2->first)->get_binphi();
333 
334  for (size_t i = 0; i < ht_eta.size(); i++)
335  {
336  if (towerphi == ht_phi[i] && towereta == ht_phi[i])
337  {
338  hotClus2 = true;
339  }
340  }
341  }
342  if (dynMaskClus && hotClus2 == true)
343  {
344  continue;
345  }
346 
347  TLorentzVector photon2;
348  photon2.SetPtEtaPhiE(clus2_pt, clus2_eta, clus2_phi, clus2E);
349 
350  if (fabs(clusE - clus2E) / (clusE + clus2E) > maxAlpha)
351  {
352  continue;
353  }
354 
355  if (photon1.DeltaR(photon2) > maxDr)
356  {
357  continue;
358  }
359 
360  TLorentzVector pi0 = photon1 + photon2;
361  if (pi0.Pt() < pi0ptcut)
362  {
363  continue;
364  }
365 
366  h_pt1->Fill(photon1.Pt());
367  h_pt2->Fill(photon2.Pt());
368  h_InvMass->Fill(pi0.M());
369  if (lt_eta > 95)
370  {
371  continue;
372  }
373  h_mass_eta_lt[lt_eta]->Fill(pi0.M());
374  }
375  } // clus1 loop
376 
377  ht_phi.clear();
378  ht_eta.clear();
379 
381 }
382 
384 {
385  outfile->cd();
386 
387  outfile->Write();
388  outfile->Close();
389  delete outfile;
390  hm->dumpHistos(outfilename, "UPDATE");
391  return 0;
392 }
393 
395 {
396  TF1* fitFunc = new TF1("fitFunc", "[0]/[2]/2.5*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2 + [6]*x^3", h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
397 
398  fitFunc->SetParameter(0, 5);
399  fitFunc->SetParameter(1, target_pi0_mass);
400  fitFunc->SetParameter(2, 0.01);
401  fitFunc->SetParameter(3, 0.0);
402  fitFunc->SetParameter(4, 0.0);
403  fitFunc->SetParameter(5, 0.0);
404  fitFunc->SetParameter(6, 100);
405 
406  fitFunc->SetParLimits(0, 0,10);
407  fitFunc->SetParLimits(1, 0.113, 0.25);
408  fitFunc->SetParLimits(2, 0.01, 0.04);
409  fitFunc->SetParLimits(3,-2 ,1 );
410  fitFunc->SetParLimits(4,0 ,40 );
411  fitFunc->SetParLimits(5, -150,50 );
412  fitFunc->SetParLimits(6, 0,200 );
413 
414  fitFunc->SetRange(0.05, 0.7);
415 
416  // Perform the fit
417  h->Fit("fitFunc", "QN");
418 
419  return fitFunc;
420 }
421 
422 void pi0EtaByEta::fitEtaSlices(const std::string& infile, const std::string& fitOutFile, const std::string& cdbFile)
423 {
424  TFile* fin = new TFile(infile.c_str());
425  std::cout << "getting hists" << std::endl;
426  TH1F* h_peak_eta = new TH1F("h_peak_eta", "", 96, 0, 96);
427  TH1F* h_sigma_eta = new TH1F("h_sigma_eta", "", 96, 0, 96);
428  TH1F* h_p3_eta = new TH1F("h_p3_eta", "", 96, 0, 96);
429  TH1F* h_p4_eta = new TH1F("h_p4_eta", "", 96, 0, 96);
430  TH1F* h_p5_eta = new TH1F("h_p5_eta", "", 96, 0, 96);
431  TH1F* h_p6_eta = new TH1F("h_p6_eta", "", 96, 0, 96);
432  TH1F* h_p0_eta = new TH1F("h_p0_eta", "", 96, 0, 96);
433  if (!fin)
434  {
435  std::cout << "pi0EtaByEta::fitEtaSlices null fin" << std::endl;
436  exit(1);
437  }
438  TH1F* h_M_eta[96];
439  for (int i = 0; i < 96; i++)
440  {
441  h_M_eta[i] = (TH1F*) fin->Get(Form("h_mass_eta_lt%d", i));
442  h_M_eta[i]->Scale(1./h_M_eta[i]->Integral(),"width");
443  }
444 
445  TF1* fitFunOut[96];
446  for (int i = 0; i < 96; i++)
447  {
448  if (!h_M_eta[i])
449  {
450  std::cout << "pi0EtaByEta::fitEtaSlices null hist" << std::endl;
451  }
452 
453  fitFunOut[i] = fitHistogram(h_M_eta[i]);
454  fitFunOut[i]->SetName(Form("f_pi0_eta%d",i));
455  float mass_val_out = fitFunOut[i]->GetParameter(1);
456  float mass_err_out = fitFunOut[i]->GetParError(1);
457  h_peak_eta->SetBinContent(i + 1, mass_val_out);
458  if (isnan(h_M_eta[i]->GetEntries())){
459  h_peak_eta->SetBinError(i + 1, 0);
460  continue;
461  }
462  h_peak_eta->SetBinError(i + 1, mass_err_out);
463  h_sigma_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(2));
464  h_sigma_eta->SetBinError(i+1,fitFunOut[i]->GetParError(2));
465  h_p3_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(3));
466  h_p3_eta->SetBinError(i+1,fitFunOut[i]->GetParError(3));
467  h_p4_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(4));
468  h_p4_eta->SetBinError(i+1,fitFunOut[i]->GetParError(4));
469  h_p5_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(5));
470  h_p5_eta->SetBinError(i+1,fitFunOut[i]->GetParError(5));
471  h_p6_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(6));
472  h_p6_eta->SetBinError(i+1,fitFunOut[i]->GetParError(6));
473  h_p0_eta->SetBinContent(i+1,fitFunOut[i]->GetParameter(0));
474  h_p0_eta->SetBinError(i+1,fitFunOut[i]->GetParError(0));
475  }
476 
477  CDBTTree* cdbttree1 = new CDBTTree(cdbFile.c_str());
478  CDBTTree* cdbttree2 = new CDBTTree(cdbFile.c_str());
479 
480  std::string m_fieldname = "Femc_datadriven_qm1_correction";
481 
482  for (int i = 0; i < 96; i++)
483  {
484  for (int j = 0; j < 256; j++)
485  {
486  float correction = target_pi0_mass / h_peak_eta->GetBinContent(i + 1);
487  unsigned int key = TowerInfoDefs::encode_emcal(i, j);
488  float val1 = cdbttree1->GetFloatValue(key, m_fieldname);
489  cdbttree2->SetFloatValue(key, m_fieldname, val1 * correction);
490  }
491  }
492 
493  cdbttree2->Commit();
494  cdbttree2->WriteCDBTTree();
495  delete cdbttree2;
496  delete cdbttree1;
497 
498  TFile* fit_out = new TFile(fitOutFile.c_str(), "recreate");
499  fit_out->cd();
500  for (auto& i : h_M_eta)
501  {
502  i->Write();
503  delete i;
504  }
505  for (auto& i : fitFunOut)
506  {
507  i->Write();
508  delete i;
509  }
510 
511  h_p3_eta->Write();
512  h_p4_eta->Write();
513  h_p5_eta->Write();
514  h_p6_eta->Write();
515  h_p0_eta->Write();
516  h_sigma_eta->Write();
517  h_peak_eta->Write();
518  fin->Close();
519 
520  std::cout << "finish fitting suc" << std::endl;
521 
522  return;
523 }