Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpFinderEval.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EpFinderEval.cc
1 
2 #include "EpFinderEval.h"
3 
4 #include <phool/phool.h>
5 #include <phool/getClass.h>
6 
10 
12 #include <g4main/PHG4Hit.h>
14 #include <g4main/PHG4VtxPoint.h>
15 #include <g4main/PHG4Particle.h>
16 
17 #include <calobase/RawTowerDefs.h>
18 #include <calobase/RawTowerContainer.h>
19 #include <calobase/RawTower.h>
20 #include <calobase/RawTowerGeomContainer.h>
21 #include <calobase/RawTowerGeom.h>
22 #include <calobase/RawClusterContainer.h>
23 #include <calobase/RawCluster.h>
24 
27 #include <HepMC/GenEvent.h>
28 #include <HepMC/GenParticle.h>
29 #include <HepMC/HeavyIon.h>
30 
31 #include <TTree.h>
32 #include <TH2D.h>
33 #include <TVector3.h>
34 #include <TRandom3.h>
35 #include <TMath.h>
36 
37 #include <iostream>
38 #include <utility>
39 
40 #define TOWER_E_CUT 0.0
41 
42 #include "EpFinder.h"
43 
44 using namespace std;
45 
46 double XYtoPhi(double x, double y)
47 {
48  // -Pi to +Pi
49  Double_t phi = atan2(y,x);
50  if(phi<-TMath::Pi()) phi += TMath::TwoPi();
51  if(phi>=TMath::Pi()) phi -= TMath::TwoPi();
52  return phi;
53 }
54 
55 double XYtoPhi_02PI(double x, double y)
56 {
57  // 0 to 2pi
58  Double_t phi = atan2(y,x);
59  if(phi<0.0) phi += TMath::TwoPi();
60  if(phi>=TMath::TwoPi()) phi -= TMath::TwoPi();
61  return phi;
62 }
63 
64 double getEta(double pt, double pz){
65  float theta = XYtoPhi(pz,pt);
66  float eta = -log(tan(theta/2.0));
67  return eta;
68 }
69 
70 double DeltaPhi(double phi1, double phi2){
71  Double_t dphi;
72  dphi = phi1 - phi2;
73  if(dphi<-TMath::Pi()) dphi += TMath::TwoPi();
74  if(dphi>=TMath::Pi()) dphi -= TMath::TwoPi();
75  return dphi;
76 }
77 
78 //----------------------------------------------------------------------------//
79 //-- Constructor:
80 //-- simple initialization
81 //----------------------------------------------------------------------------//
82 
84  SubsysReco(name), _eval_tree_event(NULL) {
85  //initialize
86  _event = 0;
87  _outfile_name = "EpFinder_Eval.root";
88  RpFinder = NULL;
89  RpFinderL = NULL;
90  RpFinderR = NULL;
91  primRpFinder = NULL;
92  fprimRpFinder = NULL;
93 
94 }
95 
96 //----------------------------------------------------------------------------//
97 //-- Init():
98 //-- Intialize all histograms, trees, and ntuples
99 //----------------------------------------------------------------------------//
101 
102  cout << PHWHERE << " Opening file " << _outfile_name << endl;
103  PHTFileServer::get().open(_outfile_name, "RECREATE");
104 
105  _eval_tree_event = new TTree("event", "FastSim Eval => event parameters");
106  _eval_tree_event->Branch("event", &_event, "_event/I");
107  _eval_tree_event->Branch("rplane_angle", &rplane_angle, "rplane_angle/F");
108  _eval_tree_event->Branch("bimpact", &bimpact, "bimpact/F");
109  _eval_tree_event->Branch("prim_rplane_angle", &prim_rplane_angle, "prim_rplane_angle/F");
110 
111  _eval_tree_event->Branch("fprim_rplane_angle", &fprim_rplane_angle, "fprim_rplane_angle/F");
112  _eval_tree_event->Branch("fprim_phiweighted_rplane_angle", &fprim_phiweighted_rplane_angle, "fprim_phiweighted_rplane_angle/F");
113  _eval_tree_event->Branch("fprim_phiweightedandshifted_rplane_angle", &fprim_phiweightedandshifted_rplane_angle, "fprim_phiweightedandshifted_rplane_angle/F");
114 
115  _eval_tree_event->Branch("rfprim_rplane_angle", &rfprim_rplane_angle, "rfprim_rplane_angle/F");
116  _eval_tree_event->Branch("rfprim_phiweighted_rplane_angle", &rfprim_phiweighted_rplane_angle, "rfprim_phiweighted_rplane_angle/F");
117  _eval_tree_event->Branch("rfprim_phiweightedandshifted_rplane_angle", &rfprim_phiweightedandshifted_rplane_angle, "rfprim_phiweightedandshifted_rplane_angle/F");
118 
119  _eval_tree_event->Branch("femc_raw_rplane_angle", &femc_raw_rplane_angle, "femc_raw_rplane_angle/F");
120  _eval_tree_event->Branch("femc_phiweighted_rplane_angle", &femc_phiweighted_rplane_angle, "femc_phiweighted_rplane_angle/F");
121  _eval_tree_event->Branch("femc_phiweightedandshifted_rplane_angle", &femc_phiweightedandshifted_rplane_angle, "femc_phiweightedandshifted_rplane_angle/F");
122 
123  _eval_tree_event->Branch("rfemc_raw_rplane_angle", &rfemc_raw_rplane_angle, "rfemc_raw_rplane_angle/F");
124  _eval_tree_event->Branch("rfemc_phiweighted_rplane_angle", &rfemc_phiweighted_rplane_angle, "rfemc_phiweighted_rplane_angle/F");
125  _eval_tree_event->Branch("rfemc_phiweightedandshifted_rplane_angle", &rfemc_phiweightedandshifted_rplane_angle, "rfemc_phiweightedandshifted_rplane_angle/F");
126 
127  _eval_tree_event->Branch("femcL_raw_rplane_angle", &femcL_raw_rplane_angle, "femcL_raw_rplane_angle/F");
128  _eval_tree_event->Branch("femcL_phiweighted_rplane_angle", &femcL_phiweighted_rplane_angle, "femcL_phiweighted_rplane_angle/F");
129  _eval_tree_event->Branch("femcL_phiweightedandshifted_rplane_angle", &femcL_phiweightedandshifted_rplane_angle, "femcL_phiweightedandshifted_rplane_angle/F");
130 
131  _eval_tree_event->Branch("femcR_raw_rplane_angle", &femcR_raw_rplane_angle, "femcR_raw_rplane_angle/F");
132  _eval_tree_event->Branch("femcR_phiweighted_rplane_angle", &femcR_phiweighted_rplane_angle, "femcR_phiweighted_rplane_angle/F");
133  _eval_tree_event->Branch("femcR_phiweightedandshifted_rplane_angle", &femcR_phiweightedandshifted_rplane_angle, "femcR_phiweightedandshifted_rplane_angle/F");
134 
135  RpFinder = new EpFinder(4,"EpFinderCorrectionHistograms_OUTPUT.root", "EpFinderCorrectionHistograms_INPUT.root", 181, 181);
136  RpFinder->SetnMipThreshold(0.0);
137  RpFinder->SetMaxTileWeight(100.0);
138  cout << RpFinder->Report() << endl;
139 
140  rRpFinder = new EpFinder(4,"rEpFinderCorrectionHistograms_OUTPUT.root", "rEpFinderCorrectionHistograms_INPUT.root", 181, 181);
142  rRpFinder->SetMaxTileWeight(100.0);
143  cout << rRpFinder->Report() << endl;
144 
145  RpFinderL = new EpFinder(4, "L_EpFinderCorrectionHistograms_OUTPUT.root", "L_EpFinderCorrectionHistograms_INPUT.root", 181, 181);
147  RpFinderL->SetMaxTileWeight(100.0);
148  cout << RpFinderL->Report() << endl;
149 
150  RpFinderR = new EpFinder(4, "R_EpFinderCorrectionHistograms_OUTPUT.root", "R_EpFinderCorrectionHistograms_INPUT.root", 181, 181);
152  RpFinderR->SetMaxTileWeight(100.0);
153  cout << RpFinderR->Report() << endl;
154 
155  primRpFinder = new EpFinder(1, "primEpFinderCorrectionHistograms_OUTPUT.root", "primEpFinderCorrectionHistograms_INPUT.root");
156  cout << primRpFinder->Report() << endl;
157 
158  fprimRpFinder = new EpFinder(1, "fprimEpFinderCorrectionHistograms_OUTPUT.root", "fprimEpFinderCorrectionHistograms_INPUT.root",
160  cout << fprimRpFinder->Report() << endl;
161 
162  rfprimRpFinder = new EpFinder(1, "rfprimEpFinderCorrectionHistograms_OUTPUT.root", "rfprimEpFinderCorrectionHistograms_INPUT.root",
164  cout << rfprimRpFinder->Report() << endl;
165 
167 }
168 
170 
171 
173 
174 }
175 
176 //----------------------------------------------------------------------------//
177 //-- process_event():
178 //-- Call user instructions for every event.
179 //-- This function contains the analysis structure.
180 //----------------------------------------------------------------------------//
181 
183  _event++;
184 
185  GetNodes(topNode);
186 
187  fill_tree(topNode);
188 
190 }
191 
192 //----------------------------------------------------------------------------//
193 //-- End():
194 //-- End method, wrap everything up
195 //----------------------------------------------------------------------------//
197 
199 
200  _eval_tree_event->Write();
201 
202  RpFinder->Finish();
203  rRpFinder->Finish();
204  RpFinderL->Finish();
205  RpFinderR->Finish();
206  primRpFinder->Finish();
207  fprimRpFinder->Finish();
208 
209  delete RpFinder;
210  delete rRpFinder;
211  delete RpFinderL;
212  delete RpFinderR;
213  delete primRpFinder;
214  delete fprimRpFinder;
215 
217 }
218 
219 int EpFinderEval::GetPhiBin(float tphi, float numPhiDivisions)
220 {
221 
222  // determine the phi bin
223 
224  float sphi = (TMath::TwoPi()/numPhiDivisions);
225 
226  if(tphi>=TMath::TwoPi()){
227  tphi -= TMath::TwoPi();
228  }
229  else if(tphi<0.0){
230  tphi += TMath::TwoPi();
231  }
232 
233  return (int)(tphi/sphi);
234 
235 }
236 
237 int EpFinderEval::GetEtaBin(float teta, float eta_low, float eta_high, float numEtaDivisions)
238 {
239 
240  // determine the eta bin
241 
242  float seta = fabs((eta_high-eta_low)/numEtaDivisions);
243 
244  return (int)((teta-eta_low)/seta);
245 
246 }
247 
248 
249 //----------------------------------------------------------------------------//
250 //-- fill_tree():
251 //-- Fill the various trees...
252 //----------------------------------------------------------------------------//
253 
255 
256  // Read the FEMC geometry and set up the arrays for phi weighting.
257 
258  static bool first = true;
259 
260  if(first){
261 
262  for(int i=0; i<PHI_BINS; i++){
263  phi_list[i].clear();
264  rphi_list[i].clear();
265  }
266 
267  for(int i=0; i<FPRIM_PHI_BINS; i++){
268  fprim_phi_list[i].clear();
269  rfprim_phi_list[i].clear();
270  }
271 
272  // generate a list of all towers in the same phi range
273  // the phi grouping is determined by dividing phi into
274  // PHI_BINS even slices in phi
275 
277 
278  for (int twr_j = 0; twr_j< 181; twr_j++){
279  for (int twr_k = 0; twr_k< 181; twr_k++){
280  RawTowerDefs::keytype towerid = RawTowerDefs::encode_towerid( calo_id_, twr_j+500, twr_k+500 );
281  RawTowerGeom *tgeo = towergeom->get_tower_geometry(towerid);
282  if(tgeo){
283  int idx = GetPhiBin(tgeo->get_phi(), PHI_BINS);
284  std::pair<int,int> newPair(tgeo->get_column()-500,tgeo->get_row()-500);
285  phi_list[idx].push_back(newPair);
286  if(tgeo->get_eta()<2.4) rphi_list[idx].push_back(newPair);
287  }
288  }
289  }
290 
291  // For the forward primary weighting, all the eta bins are at the same phi
292 
293  for(int i=0; i<FPRIM_PHI_BINS; i++){
294  for(int j=0; j<FPRIM_ETA_BINS; j++){
295  std::pair<int,int> newPair(i,j);
296  fprim_phi_list[i].push_back(newPair);
297  }
298  for(int j=0; j<RFPRIM_ETA_BINS; j++){
299  std::pair<int,int> newPair(i,j);
300  rfprim_phi_list[i].push_back(newPair);
301  }
302  }
303 
304  first = false;
305  }
306 
307  // get the event properties
308 
309  rplane_angle = -9999.0;
310 
311  PHNodeIterator iter(topNode);
312  PHHepMCGenEventMap *genevent_map = findNode::getClass<PHHepMCGenEventMap>(topNode,"PHHepMCGenEventMap");
313  if(genevent_map){
314  // For now just take the first HEPMC event
315  PHHepMCGenEvent *genevent = (genevent_map->begin())->second;
316  if(genevent){
317  HepMC::GenEvent *event = genevent->getEvent();
318  HepMC::HeavyIon *hi = event->heavy_ion();
319  rplane_angle = hi->event_plane_angle();
320  bimpact = hi->impact_parameter();
321  }
322  }
323 
324  // -------------------------------------
325  // Primary Particle Event Plane Finder
326  // -------------------------------------
327 
328  std::vector<EpHit> phits;
329  phits.clear();
330 
331  std::vector<EpHit> fphits;
332  fphits.clear();
333 
334  std::vector<EpHit> rfphits;
335  rfphits.clear();
336 
338 
339  for (PHG4TruthInfoContainer::ConstIterator truth_itr = range.first;
340  truth_itr != range.second; ++truth_itr) {
341 
342  PHG4Particle* g4particle = truth_itr->second;
343  if(!g4particle) {
344  continue;
345  }
346 
347  // remove some particles (muons, taus, neutrinos)...
348  // 12 == nu_e
349  // 13 == muons
350  // 14 == nu_mu
351  // 15 == taus
352  // 16 == nu_tau
353  if ((abs(g4particle->get_pid()) >= 12) && (abs( g4particle->get_pid()) <= 16)) continue;
354 
355  // acceptance... _etamin,_etamax
356  if ((g4particle->get_px() == 0.0) && (g4particle->get_px() == 0.0)) continue; // avoid pt=0
357 
358  TVector3 partMom(g4particle->get_px(),g4particle->get_py(),g4particle->get_pz());
359  if ( (partMom.Eta() >= -1.0) &&
360  (partMom.Eta() <= 1.0)) {
361 
362  EpHit newHit;
363 
364  newHit.nMip = 1;
365  newHit.phi = partMom.Phi();
366  newHit.samePhi = NULL;
367 
368  phits.push_back(newHit);
369  }
370 
371  if ( (partMom.Eta() >= 1.4) &&
372  (partMom.Eta() < 4.0)) {
373 
374  EpHit newHit;
375 
376  newHit.nMip = 1;
377  newHit.phi = partMom.Phi();
378  newHit.ix = GetPhiBin(partMom.Phi(), FPRIM_PHI_BINS);
379  newHit.iy = GetEtaBin(partMom.Eta(), 1.4, 4.0, FPRIM_ETA_BINS);
380  newHit.samePhi = &fprim_phi_list[newHit.ix];
381 
382  fphits.push_back(newHit);
383  }
384 
385  if ( (partMom.Eta() >= 1.4) &&
386  (partMom.Eta() < 2.4)) {
387 
388  EpHit newHit;
389 
390  newHit.nMip = 1;
391  newHit.phi = partMom.Phi();
392  newHit.ix = GetPhiBin(partMom.Phi(), FPRIM_PHI_BINS);
393  newHit.iy = GetEtaBin(partMom.Eta(), 1.4, 2.4, RFPRIM_ETA_BINS);
394  newHit.samePhi = &rfprim_phi_list[newHit.ix];
395 
396  rfphits.push_back(newHit);
397  }
398 
399 
400  }
401 
402  EpInfo primRpResult = primRpFinder->Results(&phits,0);
403 
404  prim_rplane_angle = primRpResult.RawPsi(2);
405 
406  EpInfo fprimRpResult = fprimRpFinder->Results(&fphits,0);
407 
408  fprim_rplane_angle = fprimRpResult.RawPsi(2);
411 
412  EpInfo rfprimRpResult = rfprimRpFinder->Results(&rfphits,0);
413 
414  rfprim_rplane_angle = rfprimRpResult.RawPsi(2);
415  rfprim_phiweighted_rplane_angle = rfprimRpResult.PhiWeightedPsi(2);
417 
418  phits.clear();
419  fphits.clear();
420  rfphits.clear();
421 
422  // --------------------------------
423  // Run the FEMC event plane finder
424  // --------------------------------
425 
426  std::vector<EpHit> hits;
427  hits.clear();
428 
429  std::vector<EpHit> rhits;
430  rhits.clear();
431 
432  std::vector<EpHit> hitsL;
433  hitsL.clear();
434 
435  std::vector<EpHit> hitsR;
436  hitsR.clear();
437 
439  RawTowerContainer::ConstIterator itr = begin_end.first;
440  for (; itr != begin_end.second; ++itr) {
441  RawTowerDefs::keytype towerid = itr->first;
442  RawTower *rawtower = towers->getTower(towerid);
443  if(rawtower) {
444  if(rawtower->get_energy()>TOWER_E_CUT) {
445 
446  RawTowerGeom *tgeo = towergeom->get_tower_geometry(towerid);
447 
448  EpHit newHit;
449 
450  //newHit.nMip = 1;
451  newHit.nMip = rawtower->get_energy();
452  newHit.phi = tgeo->get_phi();
453  newHit.ix = tgeo->get_column() - 500;
454  newHit.iy = tgeo->get_row() - 500;
455 
456  int idx = GetPhiBin(tgeo->get_phi(), PHI_BINS);
457  newHit.samePhi = &phi_list[idx];
458 
459  hits.push_back(newHit);
460  if((idx>=(PHI_BINS/4))&&(idx<(3*PHI_BINS/4)))
461  hitsL.push_back(newHit);
462  else
463  hitsR.push_back(newHit);
464 
465  if( tgeo->get_eta()<2.4 ) {
466  newHit.samePhi = &rphi_list[idx];
467  rhits.push_back(newHit);
468  }
469 
470  }
471  }
472 
473  }
474 
475  // Select the event class based on the impact parameter
476 
477  int ev_class = 0;
478  if((bimpact>=0.0) && (bimpact<4.0))
479  ev_class = 0;
480  else if((bimpact>=4.0)&&(bimpact<8.0))
481  ev_class = 1;
482  else if((bimpact>=8.0)&&(bimpact<14.0))
483  ev_class = 2;
484  else
485  ev_class = 3;
486 
487  // Run the event plane finder
488 
489  EpInfo RpResult = RpFinder->Results(&hits,ev_class);
490 
491  femc_raw_rplane_angle = RpResult.RawPsi(2);
494 
495  EpInfo rRpResult = rRpFinder->Results(&rhits,ev_class);
496 
497  rfemc_raw_rplane_angle = rRpResult.RawPsi(2);
500 
501  EpInfo RpResultL = RpFinderL->Results(&hitsL,ev_class);
502 
503  femcL_raw_rplane_angle = RpResultL.RawPsi(2);
506 
507  EpInfo RpResultR = RpFinderR->Results(&hitsR,ev_class);
508 
509  femcR_raw_rplane_angle = RpResultR.RawPsi(2);
512 
513  _eval_tree_event->Fill();
514 
515  hits.clear();
516  hitsL.clear();
517  hitsR.clear();
518 
519  return;
520 
521 }
522 
523 //----------------------------------------------------------------------------//
524 //-- GetNodes():
525 //-- Get all the all the required nodes off the node tree
526 //----------------------------------------------------------------------------//
528 
529  //Truth container
530  _truth_container = findNode::getClass<PHG4TruthInfoContainer>(topNode,
531  "G4TruthInfo");
532  if (!_truth_container) {
533  cout << PHWHERE << " PHG4TruthInfoContainer node not found on node tree"
534  << endl;
536  }
537 
538  string towernodename = "TOWER_CALIB_FEMC";
539  // Grab the towers
540  towers = findNode::getClass<RawTowerContainer>(topNode, towernodename.c_str());
541  if (!towers)
542  {
543  std::cout << PHWHERE << ": Could not find node " << towernodename.c_str() << std::endl;
545  }
546  // Grab the geometry
547  string towergeomnodename = "TOWERGEOM_FEMC";
548  towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnodename.c_str());
549  if (!towergeom)
550  {
551  cout << PHWHERE << ": Could not find node " << towergeomnodename.c_str() << endl;
553  }
554 
556 }
557