Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HCalib.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HCalib.C
1 //HCAL Calibration
2 //Abhisek Sen
3 //
4 #include "HCalib.h"
5 #include <fun4all/SubsysReco.h>
10 #include <phool/getClass.h>
12 #include <phool/getClass.h>
13 
15 #include <phparameter/PHParameters.h>
16 #include <g4detectors/PHG4Cell.h>
19 
23 
24 #include <calobase/RawTowerContainer.h>
25 #include <calobase/RawTowerGeomContainer.h>
26 #include <calobase/RawTower.h>
27 #include <TH1F.h>
28 
29 using namespace std;
30 
32 SubsysReco("HCal_Calibration")
33 {
34  towers=0;
35  slats = 0;
36  threshold=0.000; //GeV
37  outfile=0;
38  fill_towers = 1;
39  fill_slats = 1;
40  is_proto = true;
41  return;
42 }
43 
45 {
46  outfile = new TFile("out_calib.root","RECREATE");
48 }
49 
51 {
52  //cout << "Processing event:: HCalib " << endl;
53  string detnames[] = {"HCALIN","HCALOUT"};
54  for(int seg=0; seg<2; seg++)
55  {
56  GetNodes(topNode,detnames[seg]);
57 
58  if(fill_towers and towers)
59  {
60  auto twr_range = towers->getTowers();
61  for (auto twr_iter = twr_range.first; twr_iter != twr_range.second; ++twr_iter)
62  {
63  RawTower* tower = twr_iter->second;
64  int etabin = tower->get_bineta();
65  int phibin = tower->get_binphi();
66  int key = genkey(seg, etabin, phibin);
67  auto it = tower_hists.find(key);
68  if(it==tower_hists.end())
69  {
70  string hname=detnames[seg]+"_Tower_Energy_ieta_"+to_string(etabin)+"_iphi_"+to_string(phibin);
71  TH1F *h = new TH1F(hname.c_str(),hname.c_str(),500,0,50);
72  tower_hists[key] = h;
73  h->Fill( 1e3*tower->get_energy());
74  }
75  else {
76  tower_hists[key]->Fill( 1e3*tower->get_energy());
77  }
78  }
79 
80  }
81 
82 
83  /*
84  for(unsigned int ieta=1; ieta<23; ieta++)
85  {
86  for(unsigned int iphi=1; iphi<63; iphi++)
87  {
88  RawTower *twr = towers->getTower(ieta,iphi);
89  if(!twr) continue;
90 
91 
92  float twr_e = twr->get_energy();
93 
94  //cout << "Energy " << twr->get_energy() << endl;
95  if(twr_e<threshold) continue;
96 
97  bool good_tower = true; //false;
98  if(!good_tower)
99  {
100  // check for ieta-1, ieta+1
101  RawTower *twr_p = towers->getTower(ieta-1,iphi);
102  RawTower *twr_n = towers->getTower(ieta+1,iphi);
103  if(twr_p && twr_n)
104  {
105  if(twr_p->get_energy()>threshold &&
106  twr_n->get_energy()>threshold ) good_tower=true;
107  }
108 
109  //check for iphi-1, iphi+1
110  twr_p = towers->getTower(ieta,iphi-1);
111  twr_n = towers->getTower(ieta,iphi-1);
112  if(twr_p && twr_n)
113  {
114  if(twr_p->get_energy()>threshold &&
115  twr_n->get_energy()>threshold ) good_tower=true;
116  }
117 
118  //Diagonal
119  twr_p = towers->getTower(ieta-1,iphi-1);
120  twr_n = towers->getTower(ieta+1,iphi+1);
121  if(twr_p && twr_n)
122  {
123  if(twr_p->get_energy()>threshold &&
124  twr_n->get_energy()>threshold ) good_tower=true;
125  }
126 
127  //Diagonal
128  twr_p = towers->getTower(ieta-1,iphi+1);
129  twr_n = towers->getTower(ieta+1,iphi-1);
130  if(twr_p && twr_n)
131  {
132  if(twr_p->get_energy()>threshold &&
133  twr_n->get_energy()>threshold ) good_tower=true;
134  }
135  }
136 
137  if(good_tower) Fill(twr,seg);
138 
139  }
140  }
141  }
142  */
143  if(fill_slats)
144  {
145  if(is_proto && proto_slats)
146  {
147  auto cell_range = proto_slats->getScintillatorSlats();
148  for (auto cell_iter = cell_range.first; cell_iter != cell_range.second; ++cell_iter)
149  {
150  PHG4ScintillatorSlat *cell = cell_iter->second;
151  int etabin = cell->get_column();
152  int phibin = cell->get_row();
153  int key = genkey(seg, etabin, phibin);
154  auto it = slat_hists.find(key);
155  if(it==slat_hists.end())
156  {
157  string hname=detnames[seg]+"_Slat_Energy_ieta_"+to_string(etabin)+"_iphi_"+to_string(phibin);
158  TH1F *h = new TH1F(hname.c_str(),hname.c_str(),500,0,50);
159  slat_hists[key] = h;
160  h->Fill( 1e3*cell->get_light_yield() );
161  }
162  else {
163  slat_hists[key]->Fill( 1e3*cell->get_light_yield() );
164  }
165  }
166  }
167 
168  if(slats)
169  {
170  auto cell_range = slats->getCells();
171  for (auto cell_iter = cell_range.first; cell_iter != cell_range.second; ++cell_iter)
172  {
173  PHG4Cell *cell = cell_iter->second;
176  int key = genkey(seg, etabin, phibin);
177  auto it = slat_hists.find(key);
178  if(it==slat_hists.end())
179  {
180  string hname=detnames[seg]+"_Slat_Energy_ieta_"+to_string(etabin)+"_iphi_"+to_string(phibin);
181  TH1F *h = new TH1F(hname.c_str(),hname.c_str(),500,0,50);
182  slat_hists[key] = h;
183  h->Fill( 1e3*cell->get_light_yield() );
184  }
185  else {
186  slat_hists[key]->Fill( 1e3*cell->get_light_yield() );
187  }
188  }
189  }
190  }
191 
192  }
193 
195 }
196 
198 {
199  outfile->cd();
200  for( auto it: tower_hists)
201  {
202  if(it.second->GetEntries()>0)
203  it.second->Write();
204  }
205 
206  for(auto it : slat_hists)
207  {
208  if(it.second->GetEntries()>0)
209  it.second->Write();
210  }
211 
212  outfile->Close();
214 }
215 
216 void HCalib::GetNodes(PHCompositeNode *topNode, const string &det)
217 {
218  static int ncalls = 0;
219  //string towernode = "TOWER_CALIB_" + det;
220  string towernode = "TOWER_SIM_" + det;
221  towers = findNode::getClass<RawTowerContainer>(topNode,towernode.c_str());
222  if (!towers) {
223  if(ncalls<5) cerr << PHWHERE << " ERROR: Can't find " << towernode << endl;
224  ncalls++;
225  //exit(-1);
226  }
227 
228  //Slats
229  std::string cellnodename = "G4CELL_" + det;
230  slats = findNode::getClass<PHG4CellContainer>(topNode, cellnodename);
231  if(!slats)
232  {
233  if(ncalls<5) cerr << PHWHERE << " Node missing " << cellnodename << endl;
234  }
235 
236  //Proto slats
237  if( is_proto )
238  {
239  proto_slats = findNode::getClass<PHG4ScintillatorSlatContainer>(topNode, cellnodename);
240  if(!proto_slats)
241  {
242  if(ncalls<5) cerr << PHWHERE << " Prototype Node missing " << cellnodename << endl;
243  }
244  }
245 
246 }
247 
248 int HCalib::genkey(const int detid, const int etabin, const int phibin)
249 {
250  //Assuming
251  //At most detid : 2, etabin: 24, phibin:256
252  return 30000*detid + 500*etabin + phibin;
253 }