Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Proto4TowerCalib.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Proto4TowerCalib.C
1 #include "Proto4TowerCalib.h"
2 
3 #include <calobase/RawTowerContainer.h>
5 #include <pdbcalbase/PdbParameterMap.h>
6 #include <phparameter/PHParameters.h>
7 #include <prototype4/RawTower_Prototype4.h>
8 #include <prototype4/RawTower_Temperature.h>
9 
10 #include <Event/EventTypes.h>
13 #include <fun4all/Fun4AllServer.h>
14 #include <fun4all/PHTFileServer.h>
15 #include <fun4all/SubsysReco.h>
16 #include <phool/PHCompositeNode.h>
17 #include <phool/getClass.h>
18 
19 #include <phool/PHCompositeNode.h>
20 
21 #include <g4main/PHG4Particle.h>
23 #include <g4main/PHG4VtxPoint.h>
24 
25 #include <TFile.h>
26 #include <TString.h>
27 #include <TH1F.h>
28 #include <TH2F.h>
29 #include <TLorentzVector.h>
30 #include <TNtuple.h>
31 #include <TVector3.h>
32 #include <TChain.h>
33 
34 #include <algorithm>
35 #include <cassert>
36 #include <cmath>
37 #include <exception>
38 #include <iostream>
39 #include <set>
40 #include <sstream>
41 #include <stdexcept>
42 #include <vector>
43 
44 using namespace std;
45 
47 
49  : SubsysReco("Proto4TowerCalib")
50  , _is_sim(true)
51  , _filename(filename)
52  , _ievent(0)
53 {
54  Verbosity(1);
55 
56  _tower.reset();
57 
58  _mInPut_flag = 1;
59  _mStartEvent = -1;
60  _mStopEvent = -1;
61  _mDet = "HCALIN";
62  _mCol = 0;
63  _mPedestal = 100.0;
65 }
66 
68 {
69 }
70 
73 {
75  Fun4AllHistoManager *hm = se->getHistoManager("Proto4TowerCalib_HISTOS");
76 
77  if (not hm)
78  {
79  cout
80  << "Proto4TowerCalib::get_HistoManager - Making Fun4AllHistoManager Proto4TowerCalib_HISTOS"
81  << endl;
82  hm = new Fun4AllHistoManager("Proto4TowerCalib_HISTOS");
83  se->registerHistoManager(hm);
84  }
85 
86  assert(hm);
87 
88  return hm;
89 }
90 
92 {
93  if (Verbosity())
94  cout << "Proto4TowerCalib::InitRun" << endl;
95 
96  _ievent = 0;
97 
98  PHNodeIterator iter(topNode);
99  PHCompositeNode *dstNode = static_cast<PHCompositeNode *>(iter.findFirst(
100  "PHCompositeNode", "DST"));
101  if (!dstNode)
102  {
103  std::cerr << PHWHERE << "DST Node missing, doing nothing." << std::endl;
104  throw runtime_error(
105  "Failed to find DST node in EmcRawTowerBuilder::CreateNodes");
106  }
107 
109 }
110 
112 {
113  cout << "Proto4TowerCalib::End - write to " << _filename << endl;
115 
117  assert(hm);
118  for (unsigned int i = 0; i < hm->nHistos(); i++)
119  hm->getHisto(i)->Write();
120 
121  // if (_T_EMCalTrk)
122  // _T_EMCalTrk->Write();
123 
125 }
126 
128 {
129  _ievent = 0;
130 
131  cout << "Proto4TowerCalib::get_HistoManager - Making PHTFileServer "
132  << _filename << endl;
133  PHTFileServer::get().open(_filename, "RECREATE");
134 
136  assert(hm);
137 
139  TH1F *hNormalization = new TH1F("hNormalization", "hNormalization", 1, 0.5, 1.5);
140  hNormalization->GetXaxis()->SetBinLabel(1, "ALL");
141  hNormalization->GetXaxis()->SetTitle("Cuts");
142  hNormalization->GetYaxis()->SetTitle("Event count");
143  hm->registerHisto(hNormalization);
144 
145  // SIM HCALIN/HCALOUT
146  if(_is_sim)
147  {
148  TH1F *h_hcalin_tower_sim[16];
149  TH1F *h_hcalout_tower_sim[16];
150  for(int i_tower = 0; i_tower < 16; ++i_tower)
151  {
152  string HistName_sim;
153  HistName_sim = Form("h_hcalin_tower_%d_sim",i_tower);
154  h_hcalin_tower_sim[i_tower] = new TH1F(HistName_sim.c_str(),HistName_sim.c_str(),500,-0.5,99.5);
155  hm->registerHisto(h_hcalin_tower_sim[i_tower]);
156 
157  HistName_sim = Form("h_hcalout_tower_%d_sim",i_tower);
158  h_hcalout_tower_sim[i_tower] = new TH1F(HistName_sim.c_str(),HistName_sim.c_str(),500,-0.5,99.5);
159  hm->registerHisto(h_hcalout_tower_sim[i_tower]);
160  }
161  }
162 
163  // HCALIN LG
164  TH1F *h_hcalin_lg_tower_raw[16];
165  TH1F *h_hcalin_lg_tower_calib[16];
166  for(int i_tower = 0; i_tower < 16; ++i_tower)
167  {
168  string HistName_raw = Form("h_hcalin_lg_tower_%d_raw",i_tower);
169  h_hcalin_lg_tower_raw[i_tower] = new TH1F(HistName_raw.c_str(),HistName_raw.c_str(),40,0.5,16000.5);
170  hm->registerHisto(h_hcalin_lg_tower_raw[i_tower]);
171 
172  string HistName_calib = Form("h_hcalin_lg_tower_%d_calib",i_tower);
173  h_hcalin_lg_tower_calib[i_tower] = new TH1F(HistName_calib.c_str(),HistName_calib.c_str(),100,-0.5,19.5);
174  hm->registerHisto(h_hcalin_lg_tower_calib[i_tower]);
175  }
176 
177  // HCALOUT LG
178  TH1F *h_hcalout_lg_tower_raw[16];
179  TH1F *h_hcalout_lg_tower_calib[16];
180  for(int i_tower = 0; i_tower < 16; ++i_tower)
181  {
182  string HistName_raw = Form("h_hcalout_lg_tower_%d_raw",i_tower);
183  h_hcalout_lg_tower_raw[i_tower] = new TH1F(HistName_raw.c_str(),HistName_raw.c_str(),40,0.5,16000.5);
184  hm->registerHisto(h_hcalout_lg_tower_raw[i_tower]);
185 
186  string HistName_calib = Form("h_hcalout_lg_tower_%d_calib",i_tower);
187  h_hcalout_lg_tower_calib[i_tower] = new TH1F(HistName_calib.c_str(),HistName_calib.c_str(),100,-0.5,19.5);
188  hm->registerHisto(h_hcalout_lg_tower_calib[i_tower]);
189  }
190 
191  // HCALOUT HG
192  TH1F *h_hcalout_hg_tower_raw[16];
193  TH1F *h_hcalout_hg_tower_calib[16];
194  for(int i_tower = 0; i_tower < 16; ++i_tower)
195  {
196  string HistName_raw = Form("h_hcalout_hg_tower_%d_raw",i_tower);
197  h_hcalout_hg_tower_raw[i_tower] = new TH1F(HistName_raw.c_str(),HistName_raw.c_str(),40,0.5,16000.5);
198  hm->registerHisto(h_hcalout_hg_tower_raw[i_tower]);
199 
200  string HistName_calib = Form("h_hcalout_hg_tower_%d_calib",i_tower);
201  h_hcalout_hg_tower_calib[i_tower] = new TH1F(HistName_calib.c_str(),HistName_calib.c_str(),100,-0.5,19.5);
202  hm->registerHisto(h_hcalout_hg_tower_calib[i_tower]);
203  }
204 
205  // help index files with TChain
206  TTree *T = new TTree("HCAL_Info", "HCAL_Info");
207  assert(T);
208  hm->registerHisto(T);
209 
210  T->Branch("TowerCalib", &_tower);
211 
213 }
214 
216 {
217  if (Verbosity() > 2)
218  cout << "Proto4TowerCalib::process_event() entered" << endl;
219 
220  // init eval objects
221  _tower.reset();
222 
224  assert(hm);
225 
226  // EventHeader *eventheader = findNode::getClass<EventHeader>(topNode, "EventHeader");
227 
228  /*
229  if (eventheader->get_EvtType() != DATAEVENT)
230  {
231  cout << __PRETTY_FUNCTION__
232  << " disgard this event: " << endl;
233 
234  eventheader->identify();
235 
236  return Fun4AllReturnCodes::DISCARDEVENT;
237  }
238  */
239 
240  if (_is_sim)
241  {
243  PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
244 
245  assert(truthInfoList);
246 
247 
248  const PHG4Particle *p =
249  truthInfoList->GetPrimaryParticleRange().first->second;
250  assert(p);
251 
252  const PHG4VtxPoint *v = truthInfoList->GetVtx(p->get_vtx_id());
253  assert(v);
254  }
255 
256  // normalization
257  TH1F *hNormalization = dynamic_cast<TH1F *>(hm->getHisto("hNormalization"));
258  assert(hNormalization);
259 
260  hNormalization->Fill("ALL", 1);
261 
262  // get DST nodes
263 
264  // HCALIN/HCALOUT SIM information
265  RawTowerContainer *TOWER_SIM_HCALIN = findNode::getClass<
266  RawTowerContainer>(topNode, "TOWER_SIM_HCALIN");
267  if(_is_sim)
268  {
269  assert(TOWER_SIM_HCALIN);
270  }
271 
272  RawTowerContainer *TOWER_SIM_HCALOUT = findNode::getClass<
273  RawTowerContainer>(topNode, "TOWER_SIM_HCALOUT");
274  if(_is_sim)
275  {
276  assert(TOWER_SIM_HCALOUT);
277  }
278 
279  // HCALIN LG information
280  RawTowerContainer *TOWER_RAW_LG_HCALIN = findNode::getClass<
281  RawTowerContainer>(topNode, "TOWER_RAW_LG_HCALIN");
282  assert(TOWER_RAW_LG_HCALIN);
283 
284  RawTowerContainer *TOWER_CALIB_LG_HCALIN = findNode::getClass<
285  RawTowerContainer>(topNode, "TOWER_CALIB_LG_HCALIN");
286  assert(TOWER_CALIB_LG_HCALIN);
287 
288  // HCALOUT LG information
289  RawTowerContainer *TOWER_RAW_LG_HCALOUT = findNode::getClass<
290  RawTowerContainer>(topNode, "TOWER_RAW_LG_HCALOUT");
291  assert(TOWER_RAW_LG_HCALOUT);
292 
293  RawTowerContainer *TOWER_CALIB_LG_HCALOUT = findNode::getClass<
294  RawTowerContainer>(topNode, "TOWER_CALIB_LG_HCALOUT");
295  assert(TOWER_CALIB_LG_HCALOUT);
296 
297  // HCALOUT HG information
298  RawTowerContainer *TOWER_RAW_HG_HCALOUT = findNode::getClass<
299  RawTowerContainer>(topNode, "TOWER_RAW_HG_HCALOUT");
300  assert(TOWER_RAW_HG_HCALOUT);
301 
302  RawTowerContainer *TOWER_CALIB_HG_HCALOUT = findNode::getClass<
303  RawTowerContainer>(topNode, "TOWER_CALIB_HG_HCALOUT");
304  assert(TOWER_CALIB_HG_HCALOUT);
305 
306  // simple clustering by matching to cluster of max energy
307  // pair<int, int> max_tower = find_max(TOWER_CALIB_LG_HCALOUT, 1);
308 
309  // process SIM HCALIN/HCALOUT
310  if(_is_sim)
311  {
312  double hcalin_sum_e_sim = 0;
313  double hcalout_sum_e_sim = 0;
314  {
315  auto range_hcalin_sim = TOWER_SIM_HCALIN->getTowers(); // sim
316  for (auto it = range_hcalin_sim.first; it != range_hcalin_sim.second; ++it)
317  {
318  RawTower *tower = it->second;
319  assert(tower);
320 
321  const int col = tower->get_bineta();
322  const int row = tower->get_binphi();
323  const int chanNum = getChannelNumber(row,col);
324 
325  // cout << "HCALIN SIM: ";
326  // tower->identify();
327  // cout << "getSimChannelNumber = " << chanNum << endl;
328 
329  if (col < 0 or col >= 4)
330  continue;
331  if (row < 0 or row >= 4)
332  continue;
333 
334  const double energy_sim = tower->get_energy();
335  hcalin_sum_e_sim += energy_sim;
336  _tower.hcalin_twr_sim[chanNum] = energy_sim;
337 
338  string HistName_sim = Form("h_hcalin_tower_%d_sim",chanNum);
339  TH1F *h_hcalin_sim = dynamic_cast<TH1F *>(hm->getHisto(HistName_sim.c_str()));
340  assert(h_hcalin_sim);
341  h_hcalin_sim->Fill(_tower.hcalin_twr_sim[chanNum]*1000.0); // convert to MeV
342  }
343  _tower.hcalin_e_sim = hcalin_sum_e_sim;
344 
345  auto range_hcalout_sim = TOWER_SIM_HCALOUT->getTowers(); // sim
346  for (auto it = range_hcalout_sim.first; it != range_hcalout_sim.second; ++it)
347  {
348  RawTower *tower = it->second;
349  assert(tower);
350  // cout << "HCALOUT SIM: ";
351  // tower->identify();
352 
353  const int col = tower->get_bineta();
354  const int row = tower->get_binphi();
355  const int chanNum = getChannelNumber(row,col);
356 
357  if (col < 0 or col >= 4)
358  continue;
359  if (row < 0 or row >= 4)
360  continue;
361 
362  const double energy_sim = tower->get_energy();
363  hcalout_sum_e_sim += energy_sim;
364  _tower.hcalout_twr_sim[chanNum] = energy_sim;
365 
366  string HistName_sim = Form("h_hcalout_tower_%d_sim",chanNum);
367  TH1F *h_hcalout_sim = dynamic_cast<TH1F *>(hm->getHisto(HistName_sim.c_str()));
368  assert(h_hcalout_sim);
369  h_hcalout_sim->Fill(_tower.hcalout_twr_sim[chanNum]*1000.0); // convert to MeV
370  }
371  _tower.hcalout_e_sim = hcalout_sum_e_sim;
372  }
373  _tower.hcal_total_sim = hcalin_sum_e_sim + hcalout_sum_e_sim;
374  _tower.hcal_asym_sim = (hcalin_sum_e_sim - hcalout_sum_e_sim)/(hcalin_sum_e_sim + hcalout_sum_e_sim);
375  }
376 
377  // process HCALIN LG
378  double hcalin_sum_lg_e_raw = 0;
379  double hcalin_sum_lg_e_calib = 0;
380  {
381  auto range_hcalin_lg_raw = TOWER_RAW_LG_HCALIN->getTowers(); // raw
382  for (auto it = range_hcalin_lg_raw.first; it != range_hcalin_lg_raw.second; ++it)
383  {
384  RawTower *tower = it->second;
385  assert(tower);
386  const int col = tower->get_bineta();
387  const int row = tower->get_binphi();
388  const int chanNum = getChannelNumber(row,col);
389 
390  // cout << "HCALIN RAW: ";
391  // tower->identify();
392  // cout << "getDataChannelNumber = " << chanNum << endl;
393 
394  if (col < 0 or col >= 4)
395  continue;
396  if (row < 0 or row >= 4)
397  continue;
398 
399  const double energy_raw = tower->get_energy();
400  hcalin_sum_lg_e_raw += energy_raw;
401  _tower.hcalin_lg_twr_raw[chanNum] = energy_raw;
402 
403  string HistName_raw = Form("h_hcalin_lg_tower_%d_raw",chanNum);
404  TH1F *h_hcalin_lg_raw = dynamic_cast<TH1F *>(hm->getHisto(HistName_raw.c_str()));
405  assert(h_hcalin_lg_raw);
406  h_hcalin_lg_raw->Fill(_tower.hcalin_lg_twr_raw[chanNum]);
407  }
408  _tower.hcalin_lg_e_raw = hcalin_sum_lg_e_raw;
409 
410  auto range_hcalin_lg_calib = TOWER_CALIB_LG_HCALIN->getTowers(); // calib
411  for (auto it = range_hcalin_lg_calib.first; it != range_hcalin_lg_calib.second; ++it)
412  {
413  RawTower *tower = it->second;
414  assert(tower);
415 
416  const int col = tower->get_bineta();
417  const int row = tower->get_binphi();
418  const int chanNum = getChannelNumber(row,col);
419 
420  if (col < 0 or col >= 4)
421  continue;
422  if (row < 0 or row >= 4)
423  continue;
424 
425  const double energy_calib = tower->get_energy();
426  hcalin_sum_lg_e_calib += energy_calib;
427  _tower.hcalin_lg_twr_calib[chanNum] = energy_calib;
428 
429  string HistName_calib = Form("h_hcalin_lg_tower_%d_calib",chanNum);
430  TH1F *h_hcalin_lg_calib = dynamic_cast<TH1F *>(hm->getHisto(HistName_calib.c_str()));
431  assert(h_hcalin_lg_calib);
432  h_hcalin_lg_calib->Fill(_tower.hcalin_lg_twr_calib[chanNum]);
433  }
434  _tower.hcalin_lg_e_calib = hcalin_sum_lg_e_calib;
435  }
436 
437  // process HCALOUT LG
438  double hcalout_sum_lg_e_raw = 0;
439  double hcalout_sum_lg_e_calib = 0;
440  {
441  auto range_hcalout_lg_raw = TOWER_RAW_LG_HCALOUT->getTowers(); // raw
442  for (auto it = range_hcalout_lg_raw.first; it != range_hcalout_lg_raw.second; ++it)
443  {
444  RawTower *tower = it->second;
445  assert(tower);
446 
447  const int col = tower->get_bineta();
448  const int row = tower->get_binphi();
449  const int chanNum = getChannelNumber(row,col);
450 
451  // cout << "HCALOUT LG RAW: ";
452  // tower->identify();
453  // cout << "getDataChannelNumber = " << chanNum << endl;
454 
455  if (col < 0 or col >= 4)
456  continue;
457  if (row < 0 or row >= 4)
458  continue;
459 
460  const double energy_raw = tower->get_energy();
461  hcalout_sum_lg_e_raw += energy_raw;
462  _tower.hcalout_lg_twr_raw[chanNum] = energy_raw;
463 
464  string HistName_raw = Form("h_hcalout_lg_tower_%d_raw",chanNum);
465  TH1F *h_hcalout_lg_raw = dynamic_cast<TH1F *>(hm->getHisto(HistName_raw.c_str()));
466  assert(h_hcalout_lg_raw);
467  h_hcalout_lg_raw->Fill(_tower.hcalout_lg_twr_raw[chanNum]);
468  }
469  _tower.hcalout_lg_e_raw = hcalout_sum_lg_e_raw;
470 
471  auto range_hcalout_lg_calib = TOWER_CALIB_LG_HCALOUT->getTowers(); // calib
472  for (auto it = range_hcalout_lg_calib.first; it != range_hcalout_lg_calib.second; ++it)
473  {
474  RawTower *tower = it->second;
475  assert(tower);
476 
477  const int col = tower->get_bineta();
478  const int row = tower->get_binphi();
479  const int chanNum = getChannelNumber(row,col);
480 
481  if (col < 0 or col >= 4)
482  continue;
483  if (row < 0 or row >= 4)
484  continue;
485 
486  const double energy_calib = tower->get_energy();
487  hcalout_sum_lg_e_calib += energy_calib;
488  _tower.hcalout_lg_twr_calib[chanNum] = energy_calib;
489 
490  string HistName_calib = Form("h_hcalout_lg_tower_%d_calib",chanNum);
491  TH1F *h_hcalout_lg_calib = dynamic_cast<TH1F *>(hm->getHisto(HistName_calib.c_str()));
492  assert(h_hcalout_lg_calib);
493  h_hcalout_lg_calib->Fill(_tower.hcalout_lg_twr_calib[chanNum]);
494  }
495  _tower.hcalout_lg_e_calib = hcalout_sum_lg_e_calib;
496  }
497 
498  // process HCALOUT HG
499  double hcalout_sum_hg_e_raw = 0;
500  double hcalout_sum_hg_e_calib = 0;
501  {
502  auto range_hcalout_hg_raw = TOWER_RAW_HG_HCALOUT->getTowers(); // raw
503  for (auto it = range_hcalout_hg_raw.first; it != range_hcalout_hg_raw.second; ++it)
504  {
505  RawTower *tower = it->second;
506  assert(tower);
507 
508  const int col = tower->get_bineta();
509  const int row = tower->get_binphi();
510  const int chanNum = getChannelNumber(row,col);
511 
512  // cout << "HCALOUT HG RAW: ";
513  // tower->identify();
514  // cout << "getDataChannelNumber = " << chanNum << endl;
515 
516  if (col < 0 or col >= 4)
517  continue;
518  if (row < 0 or row >= 4)
519  continue;
520 
521  const double energy_raw = tower->get_energy();
522  hcalout_sum_hg_e_raw += energy_raw;
523  _tower.hcalout_hg_twr_raw[chanNum] = energy_raw;
524 
525  string HistName_raw = Form("h_hcalout_hg_tower_%d_raw",chanNum);
526  TH1F *h_hcalout_hg_raw = dynamic_cast<TH1F *>(hm->getHisto(HistName_raw.c_str()));
527  assert(h_hcalout_hg_raw);
528  h_hcalout_hg_raw->Fill(_tower.hcalout_hg_twr_raw[chanNum]);
529 
530  }
531  _tower.hcalout_hg_e_raw = hcalout_sum_hg_e_raw;
532 
533  auto range_hcalout_hg_calib = TOWER_CALIB_HG_HCALOUT->getTowers(); // calib
534  for (auto it = range_hcalout_hg_calib.first; it != range_hcalout_hg_calib.second; ++it)
535  {
536  RawTower *tower = it->second;
537  assert(tower);
538 
539  const int col = tower->get_bineta();
540  const int row = tower->get_binphi();
541  const int chanNum = getChannelNumber(row,col);
542 
543  if (col < 0 or col >= 4)
544  continue;
545  if (row < 0 or row >= 4)
546  continue;
547 
548  const double energy_calib = tower->get_energy();
549  hcalout_sum_hg_e_calib += energy_calib;
550  _tower.hcalout_hg_twr_calib[chanNum] = energy_calib;
551 
552  string HistName_calib = Form("h_hcalout_hg_tower_%d_calib",chanNum);
553  TH1F *h_hcalout_hg_calib = dynamic_cast<TH1F *>(hm->getHisto(HistName_calib.c_str()));
554  assert(h_hcalout_hg_calib);
555  h_hcalout_hg_calib->Fill(_tower.hcalout_hg_twr_calib[chanNum]);
556  }
557  _tower.hcalout_hg_e_calib = hcalout_sum_hg_e_calib;
558  }
559 
560  _tower.hcal_total_raw = hcalin_sum_lg_e_raw + hcalout_sum_lg_e_raw;
561  _tower.hcal_total_calib = hcalin_sum_lg_e_calib + hcalout_sum_lg_e_calib;
562 
563  _tower.hcal_asym_raw = (hcalin_sum_lg_e_raw - hcalout_sum_lg_e_raw)/(hcalin_sum_lg_e_raw + hcalout_sum_lg_e_raw);
564  _tower.hcal_asym_calib = (hcalin_sum_lg_e_calib - hcalout_sum_lg_e_calib)/(hcalin_sum_lg_e_calib + hcalout_sum_lg_e_calib);
565 
566  TTree *T = dynamic_cast<TTree *>(hm->getHisto("HCAL_Info"));
567  assert(T);
568  T->Fill();
569 
571 }
572 
573 pair<int, int>
575 {
576  const int clus_edge_size = (cluster_size - 1) / 2;
577  assert(clus_edge_size >= 0);
578 
579  pair<int, int> max(-1000, -1000);
580  double max_e = 0;
581 
582  for (int col = 0; col < n_size; ++col)
583  for (int row = 0; row < n_size; ++row)
584  {
585  double energy = 0;
586 
587  for (int dcol = col - clus_edge_size; dcol <= col + clus_edge_size;
588  ++dcol)
589  for (int drow = row - clus_edge_size; drow <= row + clus_edge_size;
590  ++drow)
591  {
592  if (dcol < 0 or drow < 0)
593  continue;
594 
595  RawTower *t = towers->getTower(dcol, drow);
596  if (t)
597  energy += t->get_energy();
598  }
599 
600  if (energy > max_e)
601  {
602  max = make_pair(col, row);
603  max_e = energy;
604  }
605  }
606 
607  return max;
608 }
609 
610 int Proto4TowerCalib::getChannelNumber(int row, int column)
611 {
612  if(!_is_sim)
613  {
614  int hbdchanIHC[4][4] = {{4, 8, 12, 16},
615  {3, 7, 11, 15},
616  {2, 6, 10, 14},
617  {1, 5, 9, 13}};
618 
619  return hbdchanIHC[row][column] - 1;
620  }
621 
622  if(_is_sim)
623  {
624  int hbdchanIHC[4][4] = {{13, 9, 5, 1},
625  {14, 10, 6, 2},
626  {15, 11, 7, 3},
627  {16, 12, 8, 4}};
628 
629  return hbdchanIHC[row][column] - 1;
630  }
631 
632  return -1;
633 }
634 
636 {
637  if(_is_sim) _mList = "SIM";
638  if(!_is_sim) _mList = "RAW";
639  string inputdir = "/sphenix/user/xusun/TestBeam/TowerCalib/";
640  string InPutList = Form("/direct/phenix+u/xusun/WorkSpace/sPHENIX/analysis/Prototype4/HCal/macros/list/Proto4TowerInfo%s_%s_%d.list",_mList.c_str(),_mDet.c_str(),_mCol);
641 
642  mChainInPut = new TChain("HCAL_Info");
643 
644  if (!InPutList.empty()) // if input file is ok
645  {
646  cout << "Open input database file list: " << InPutList.c_str() << endl;
647  ifstream in(InPutList.c_str()); // input stream
648  if(in)
649  {
650  cout << "input database file list is ok" << endl;
651  char str[255]; // char array for each file name
652  Long64_t entries_save = 0;
653  while(in)
654  {
655  in.getline(str,255); // take the lines of the file list
656  if(str[0] != 0)
657  {
658  string addfile;
659  addfile = str;
660  addfile = inputdir+addfile;
661  mChainInPut->AddFile(addfile.c_str(),-1,"HCAL_Info");
662  long file_entries = mChainInPut->GetEntries();
663  cout << "File added to data chain: " << addfile.c_str() << " with " << (file_entries-entries_save) << " entries" << endl;
664  entries_save = file_entries;
665  }
666  }
667  }
668  else
669  {
670  cout << "WARNING: input database file input is problemtic" << endl;
671  _mInPut_flag = 0;
672  }
673  }
674 
675  // Set the input tree
676  if (_mInPut_flag == 1 && !mChainInPut->GetBranch( "TowerCalib" ))
677  {
678  cerr << "ERROR: Could not find branch 'HCAL_Tower' in tree!" << endl;
679  }
680 
682  if(_mInPut_flag == 1)
683  {
684  mChainInPut->SetBranchAddress("TowerCalib", &_mTower);
685 
686  long NumOfEvents = (long)mChainInPut->GetEntries();
687  cout << "total number of events: " << NumOfEvents << endl;
688  _mStartEvent = 0;
689  _mStopEvent = NumOfEvents;
690  }
691 
692  // initialize TH1Fs for energy/adc spectra
693  for(int i_tower = 0; i_tower < 16; ++i_tower)
694  {
695  std::string HistName = Form("h_m%s_%s_twr_%d",_mDet.c_str(),_mList.c_str(),i_tower);
696  if(_is_sim) h_mHCAL[i_tower] = new TH1F(HistName.c_str(),HistName.c_str(),500,0.5,100.5);
697  if(!_is_sim) h_mHCAL[i_tower] = new TH1F(HistName.c_str(),HistName.c_str(),80,0.5,16000.5);
698  // if(!_is_sim) h_mHCAL[i_tower] = new TH1F(HistName.c_str(),HistName.c_str(),500,0.5,100.5);
699  }
700 
701  return 0;
702 }
703 
705 {
706  cout << "Make()" << endl;
707  unsigned long start_event_use = _mStartEvent;
708  unsigned long stop_event_use = _mStopEvent;
709 
710  mChainInPut->SetBranchAddress("TowerCalib", &_mTower);
711  mChainInPut->GetEntry(0);
712 
713  for(unsigned long i_event = start_event_use; i_event < stop_event_use; ++i_event)
714  // for(unsigned long i_event = 20; i_event < 40; ++i_event)
715  {
716  if (!mChainInPut->GetEntry( i_event )) // take the event -> information is stored in event
717  break; // end of data chunk
718 
719  if(_is_sim) // cosmic simulation
720  {
721  for(int i_tower = 0; i_tower < 16; ++i_tower)
722  {
723  if(_mDet == "HCALIN") // HCALIN
724  {
725  hcal_twr[i_tower] = _mTower->hcalin_twr_sim[i_tower];
726  h_mHCAL[i_tower]->Fill(hcal_twr[i_tower]*1000.0); // convert to MeV
727  }
728  if(_mDet == "HCALOUT") // HCALOUT
729  {
730  hcal_twr[i_tower] = _mTower->hcalout_twr_sim[i_tower];
731  h_mHCAL[i_tower]->Fill(hcal_twr[i_tower]*1000.0); // convert to MeV
732  }
733  }
734  }
735  if(!_is_sim) // cosmic data
736  {
737  for(int i_tower = 0; i_tower < 16; ++i_tower) // 1st loop to decide pedestal
738  {
739  if(_mDet == "HCALIN") // HCALIN
740  {
741  hcal_twr[i_tower] = _mTower->hcalin_lg_twr_raw[i_tower];
742  if( hcal_twr[i_tower] > _mPedestal ) _is_sig[i_tower] = true;
743  }
744  if(_mDet == "HCALOUT") // HCALOUT
745  {
746  hcal_twr[i_tower] = _mTower->hcalout_hg_twr_raw[i_tower];
747  if(hcal_twr[i_tower] > _mPedestal) _is_sig[i_tower] = true;
748  }
749  }
750  if( is_sig(_mCol) )
751  {
752  // cout << "i_event = " << i_event << ", is_sig " << is_sig(_mCol) << endl;
753  fill_sig(_mCol);
754  }
755  reset_pedestal();
756  }
757  }
758 
759  return 1;
760 }
761 
763 {
764  cout << "Finish()" << endl;
765  mFile_OutPut = new TFile(_filename.c_str(),"RECREATE");
766  mFile_OutPut->cd();
767  for(int i_tower = 0; i_tower < 16; ++i_tower)
768  {
769  h_mHCAL[i_tower]->Write();
770  }
771  mFile_OutPut->Close();
772 
773  return 1;
774 }
775 
777 {
778  int twr_0 = getChannelNumber(0,colID);
779  int twr_1 = getChannelNumber(1,colID);
780  int twr_2 = getChannelNumber(2,colID);
781  int twr_3 = getChannelNumber(3,colID);
782 
783  if(_is_sig[twr_0] && _is_sig[twr_1] && _is_sig[twr_2]) return true;
784  if(_is_sig[twr_0] && _is_sig[twr_1] && _is_sig[twr_3]) return true;
785  if(_is_sig[twr_0] && _is_sig[twr_2] && _is_sig[twr_3]) return true;
786  if(_is_sig[twr_1] && _is_sig[twr_2] && _is_sig[twr_3]) return true;
787 
788  return false;
789 }
790 
792 {
793  for(int i_row = 0; i_row < 4; ++i_row)
794  {
795  // fill adc spectra for cosmic
796  int i_tower = getChannelNumber(i_row,colID);
797  h_mHCAL[i_tower]->Fill(hcal_twr[i_tower]);
798  }
799 
800  return 1;
801 }
802 
804 {
805  for(int i_tower = 0; i_tower < 16; ++i_tower)
806  {
807  hcal_twr[i_tower] = -1.0; // set tower energy
808  _is_sig[i_tower] = false; // set pedestal cut
809  }
810 
811  return 1;
812 }