Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
caloTreeGen.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file caloTreeGen.cc
1 #include "caloTreeGen.h"
2 
4 
6 
7 //Fun4All
11 #include <phool/PHCompositeNode.h>
12 #include <phool/getClass.h>
13 #include <phool/phool.h>
14 #include <ffaobjects/EventHeader.h>
15 
16 //ROOT stuff
17 #include <TH1F.h>
18 #include <TH2F.h>
19 #include <TH3F.h>
20 #include <TFile.h>
21 #include <TLorentzVector.h>
22 #include <TTree.h>
23 
24 //For clusters and geometry
25 #include <calobase/RawCluster.h>
26 #include <calobase/RawClusterContainer.h>
27 #include <calobase/RawClusterUtility.h>
28 #include <calobase/RawTowerGeomContainer.h>
29 
30 //Tower stuff
31 #include <calobase/TowerInfoContainer.h>
32 #include <calobase/TowerInfo.h>
33 #include <calobase/TowerInfoDefs.h>
34 
35 //thingy
36 #include <CLHEP/Geometry/Point3D.h>
37 
38 
39 //____________________________________________________________________________..
41  const std::string &eventFile,
42  const int eventNumber,
43  const int runid,
44  const float tow_cemc_min,
45  const float tow_hcalin_min,
46  const float tow_hcalout_min):
47  SubsysReco(name)
48  ,T(nullptr)
49  ,Outfile(name)
50  ,eventFile(eventFile)
51  ,totalCaloE(0)
52  ,iEvent(0)
53  ,eventNumber(eventNumber)
54  ,runid(runid)
55  ,tow_cemc_min(tow_cemc_min)
56  ,tow_hcalin_min(tow_hcalin_min)
57  ,tow_hcalout_min(tow_hcalout_min) {
58 
59  std::cout << "caloTreeGen::caloTreeGen(const std::string &name) Calling ctor" << std::endl;
60 }
61 
62 //____________________________________________________________________________..
64  std::cout << "caloTreeGen::~caloTreeGen() Calling dtor" << std::endl;
65 }
66 
67 //____________________________________________________________________________..
68 int caloTreeGen::Init(PHCompositeNode *topNode) {
69 
70  eventOutput.open(eventFile.c_str());
71 
72  out = new TFile(Outfile.c_str(),"RECREATE");
73 
74  T = new TTree("T","T");
75 
76  //emc
77  T -> Branch("emcTowE",&m_emcTowE);
78  T -> Branch("emcTowEta",&m_emcTowEta);
79  T -> Branch("emcTowPhi",&m_emcTowPhi);
80  T -> Branch("emcTiming",&m_emcTiming);
81  T -> Branch("emciEta",&m_emciEta);
82  T -> Branch("emciPhi",&m_emciPhi);
83 
84  T -> Branch("ohcTowE",&m_ohcTowE);
85  T -> Branch("ohcTowEta",&m_ohciTowEta);
86  T -> Branch("ohcTowPhi",&m_ohciTowPhi);
87 
88  T -> Branch("ihcTowE",&m_ihcTowE);
89  T -> Branch("ihcTowEta",&m_ihciTowEta);
90  T -> Branch("ihcTowPhi",&m_ihciTowPhi);
91 
92  T -> Branch("totalCaloE",&totalCaloE);
93 
94  //so that the histos actually get written out
96  se -> Print("NODETREE");
97 
98  eventOutput << "{\n \
99  \"EVENT\": {\n \
100  \"runid\": " << runid << ",\n \
101  \"evtid\": " << eventNumber << ",\n \
102  \"time\": 0,\n \
103  \"type\": \"Collision\",\n \
104  \"s_nn\": 200,\n \
105  \"B\": 3.0,\n \
106  \"pv\": [0,0,0]\n \
107  },\n \
108  \"META\": {\n \
109  \"HITS\": {\n \
110  \"INNERTRACKER\": {\n \
111  \"type\": \"3D\",\n \
112  \"options\": {\n \
113  \"size\": 5,\n \
114  \"color\": 16777215\n \
115  }\n \
116  },\n \
117  \"CEMC\": {\n \
118  \"type\": \"PROJECTIVE\",\n \
119  \"options\": {\n \
120  \"rmin\": 90,\n \
121  \"rmax\": 116.1,\n \
122  \"deta\": 0.025,\n \
123  \"dphi\": 0.025,\n \
124  \"color\": 16766464,\n \
125  \"transparent\": 0.6,\n \
126  \"scaleminmax\": false\n \
127  }\n \
128  },\n \
129  \"HCALIN\": {\n \
130  \"type\": \"PROJECTIVE\",\n \
131  \"options\": {\n \
132  \"rmin\": 117.27,\n \
133  \"rmax\": 165.0,\n \
134  \"deta\": 0.025,\n \
135  \"dphi\": 0.025,\n \
136  \"color\": 4290445312,\n \
137  \"transparent\": 0.6,\n \
138  \"scaleminmax\": false\n \
139  }\n \
140  },\n \
141  \"HCALOUT\": {\n \
142  \"type\": \"PROJECTIVE\",\n \
143  \"options\": {\n \
144  \"rmin\": 183.3,\n \
145  \"rmax\": 274.317,\n \
146  \"deta\": 0.025,\n \
147  \"dphi\": 0.025,\n \
148  \"color\": 24773,\n \
149  \"transparent\": 0.6,\n \
150  \"scaleminmax\": false\n \
151  }\n \
152  }\n \
153  }\n \
154  },\n \
155  \"HITS\": {\n";
156 
157  std::cout << "caloTreeGen::Init(PHCompositeNode *topNode) Initializing" << std::endl;
159 }
160 
161 //____________________________________________________________________________..
163  std::cout << "caloTreeGen::InitRun(PHCompositeNode *topNode) Initializing for Run " << runid << std::endl;
165 }
166 
167 //____________________________________________________________________________..
169 
170  // skip events until event number is reached
171  if(iEvent != eventNumber) {
172  ++iEvent;
173  return 0;
174  }
175  else std::cout << "Processing Event: " << iEvent << std::endl;
176 
177  //tower information
178  TowerInfoContainer *emcTowerContainer = findNode::getClass<TowerInfoContainer>(topNode,"TOWERINFO_CALIB_CEMC");
179  // TowerInfoContainer *emcTowerContainer = findNode::getClass<TowerInfoContainer>(topNode,"TOWERINFO_CALIB_CEMC_RETOWER");
180 
181  if(!emcTowerContainer) {
182  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERS_CEMC" << std::endl;
184  }
185 
186 
187  //Tower geometry node for location information
188  RawTowerGeomContainer *towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, "TOWERGEOM_CEMC");
189  // RawTowerGeomContainer *towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, "TOWERGEOM_HCALIN");
190 
191  if (!towergeom) {
192  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERGEOM_CEMC" << std::endl;
194  }
195 
196  float calib = 1.;
197 
198  //grab all the towers and fill their energies.
199  unsigned int tower_range = emcTowerContainer->size();
200  totalCaloE = 0;
201 
202  bool first_entry = true;
203 
204  for(unsigned int iter = 0; iter < tower_range; iter++) {
205  unsigned int towerkey = emcTowerContainer->encode_key(iter);
206  unsigned int ieta = getCaloTowerEtaBin(towerkey);
207  unsigned int iphi = getCaloTowerPhiBin(towerkey);
208  m_emciEta.push_back(ieta);
209  m_emciPhi.push_back(iphi);
210 
211  double energy = emcTowerContainer -> get_tower_at_channel(iter) -> get_energy()/calib;
212  totalCaloE += energy;
213  double time = emcTowerContainer -> get_tower_at_channel(iter) -> get_time();
214 
215  double eta = towergeom -> get_etacenter(ieta);
216  double phi = towergeom -> get_phicenter(iphi);
217  m_emcTowEta.push_back(eta);
218  m_emcTowPhi.push_back(phi);
219  m_emcTowE.push_back(energy);
220  m_emcTiming.push_back(time);
221 
222  if(energy < tow_cemc_min) continue;
223 
224  if(first_entry) {
225  eventOutput << "\"CEMC\": [{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
226  first_entry = false;
227  }
228  else eventOutput << ",{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
229  }
230  if(tower_range == 0) eventOutput << "\"CEMC\": [{}],\n";
231  else eventOutput << "],\n";
232 
233  first_entry = true;
234 
235  //tower information
236  TowerInfoContainer *ohcTowerContainer = findNode::getClass<TowerInfoContainer>(topNode,"TOWERINFO_CALIB_HCALOUT");
237  if(!ohcTowerContainer) {
238  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERINFO_CALIB_OHCAL" << std::endl;
240  }
241 
242  //Tower geometry node for location information
243  towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, "TOWERGEOM_HCALOUT");
244 
245  if (!towergeom) {
246  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERGEOM_HCALOUT" << std::endl;
248  }
249 
250  tower_range = ohcTowerContainer->size();
251 
252  for(unsigned int iter = 0; iter < tower_range; iter++) {
253  unsigned int towerkey = ohcTowerContainer->encode_key(iter);
254  unsigned int ieta = getCaloTowerEtaBin(towerkey);
255  unsigned int iphi = getCaloTowerPhiBin(towerkey);
256  m_ohciTowEta.push_back(ieta);
257  m_ohciTowPhi.push_back(iphi);
258 
259  // double eta = eta_map[ieta];
260  // double phi = phi_map[iphi];
261  double eta = towergeom -> get_etacenter(ieta);
262  double phi = towergeom -> get_phicenter(iphi);
263 
264  double energy = ohcTowerContainer -> get_tower_at_channel(iter) -> get_energy();
265  m_ohcTowE.push_back(energy);
266 
267  if(energy < tow_hcalout_min) continue;
268 
269  if(first_entry) {
270  eventOutput << "\"HCALOUT\": [{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
271  first_entry = false;
272  }
273  else eventOutput << ",{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
274  }
275 
276  if(tower_range == 0) eventOutput << "\"HCALOUT\": [{}],\n";
277  else eventOutput << "],\n";
278 
279  first_entry = true;
280 
281  TowerInfoContainer *ihcTowerContainer = findNode::getClass<TowerInfoContainer>(topNode,"TOWERINFO_CALIB_HCALIN");
282  if(!ihcTowerContainer) {
283  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERINFO_CALIB_HCALIN" << std::endl;
285  }
286 
287  //Tower geometry node for location information
288  towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, "TOWERGEOM_HCALIN");
289 
290  if (!towergeom) {
291  std::cout << PHWHERE << "caloTreeGen::process_event Could not find node TOWERGEOM_HCALIN" << std::endl;
293  }
294 
295  tower_range = ihcTowerContainer->size();
296 
297  for(unsigned int iter = 0; iter < tower_range; iter++) {
298  unsigned int towerkey = ihcTowerContainer->encode_key(iter);
299  unsigned int ieta = getCaloTowerEtaBin(towerkey);
300  unsigned int iphi = getCaloTowerPhiBin(towerkey);
301  m_ihciTowEta.push_back(ieta);
302  m_ihciTowPhi.push_back(iphi);
303 
304  // double eta = eta_map[ieta];
305  // double phi = phi_map[iphi];
306  double eta = towergeom -> get_etacenter(ieta);
307  double phi = towergeom -> get_phicenter(iphi);
308 
309  double energy = ihcTowerContainer -> get_tower_at_channel(iter) -> get_energy();
310  m_ihcTowE.push_back(energy);
311 
312  if(energy < tow_hcalin_min) continue;
313 
314  if(first_entry) {
315  eventOutput << "\"HCALIN\": [{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
316  first_entry = false;
317  }
318  else eventOutput << ",{ \"eta\": " << eta << ", \"phi\": " << phi << " , \"e\": " << energy << "}\n";
319  }
320 
321  if(tower_range == 0) eventOutput << "\"HCALIN\": [{}]\n";
322  else eventOutput << "]\n";
323 
324  T -> Fill();
325 
327 }
328 
329 //____________________________________________________________________________..
331 {
332  m_emcTowPhi.clear();
333  m_emcTowEta.clear();
334  m_emcTowE.clear();
335  m_emcTiming.clear();
336  m_emciEta.clear();
337  m_emciPhi.clear();
338 
339  m_ihcTowE.clear();
340  m_ihciTowEta.clear();
341  m_ihciTowPhi.clear();
342 
343  m_ohcTowE.clear();
344  m_ohciTowEta.clear();
345  m_ohciTowPhi.clear();
346 
348 }
349 
350 //____________________________________________________________________________..
351 int caloTreeGen::EndRun(const int runnumber)
352 {
353  std::cout << "caloTreeGen::EndRun(const int runnumber) Ending Run for Run " << runnumber << std::endl;
355 }
356 
357 //____________________________________________________________________________..
358 int caloTreeGen::End(PHCompositeNode *topNode)
359 {
360  std::cout << "caloTreeGen::End(PHCompositeNode *topNode) This is the End..." << std::endl;
361 
362  out -> cd();
363  T -> Write();
364  out -> Close();
365  delete out;
366 
367  eventOutput << "},\n \
368  \"TRACKS\": {\n \
369  \"INNERTRACKER\": []\n \
370  }}";
371  eventOutput.close();
372  //hm -> dumpHistos(Outfile.c_str(), "UPDATE");
374 }
375 
376 //____________________________________________________________________________..
378 {
379  std::cout << "caloTreeGen::Reset(PHCompositeNode *topNode) being Reset" << std::endl;
381 }
382 
383 //____________________________________________________________________________..
384 void caloTreeGen::Print(const std::string &what) const
385 {
386  std::cout << "caloTreeGen::Print(const std::string &what) const Printing info for " << what << std::endl;
387 }
388 // convert from calorimeter key to phi bin
389 //____________________________________________________________________________..
390 unsigned int caloTreeGen::getCaloTowerPhiBin(const unsigned int key)
391 {
392  unsigned int etabin = key >> 16U;
393  unsigned int phibin = key - (etabin << 16U);
394  return phibin;
395 }
396 
397 //____________________________________________________________________________..
398 // convert from calorimeter key to eta bin
399 unsigned int caloTreeGen::getCaloTowerEtaBin(const unsigned int key)
400 {
401  unsigned int etabin = key >> 16U;
402  return etabin;
403 }
404 //____________________________________________________________________________..
405 float caloTreeGen::getMaxTowerE(RawCluster *cluster, TowerInfoContainer *towerContainer)
406 {
407  RawCluster::TowerConstRange towers = cluster -> get_towers();
409 
410  float maxEnergy = 0;
411  for(toweriter = towers.first; toweriter != towers.second; toweriter++)
412  {
413  float towE = toweriter -> second;
414 
415  if( towE > maxEnergy) maxEnergy = towE;
416  }
417  return maxEnergy;
418 }
419 //____________________________________________________________________________..
420 std::vector<int> caloTreeGen::returnClusterTowEta(RawCluster *cluster, TowerInfoContainer *towerContainer)
421 {
422  RawCluster::TowerConstRange towers = cluster -> get_towers();
424 
425  std::vector<int> towerIDsEta;
426  for(toweriter = towers.first; toweriter != towers.second; toweriter++) towerIDsEta.push_back(RawTowerDefs::decode_index1(toweriter -> first));
427 
428  return towerIDsEta;
429 }
430 //____________________________________________________________________________..
431 std::vector<int> caloTreeGen::returnClusterTowPhi(RawCluster *cluster, TowerInfoContainer *towerContainer)
432 {
433  RawCluster::TowerConstRange towers = cluster -> get_towers();
435 
436  std::vector<int> towerIDsPhi;
437  for(toweriter = towers.first; toweriter != towers.second; toweriter++) towerIDsPhi.push_back(RawTowerDefs::decode_index2(toweriter -> first));
438  return towerIDsPhi;
439 }
440 //____________________________________________________________________________..
441 std::vector<float> caloTreeGen::returnClusterTowE(RawCluster *cluster, TowerInfoContainer *towerContainer)
442 {
443  RawCluster::TowerConstRange towers = cluster -> get_towers();
445 
446  std::vector<float> towerE;
447  for(toweriter = towers.first; toweriter != towers.second; toweriter++) towerE.push_back(toweriter -> second);
448 
449  return towerE;
450 }