Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CaloWaveFormSim.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CaloWaveFormSim.cc
1 #include "CaloWaveFormSim.h"
2 
3 // G4Hits includes
4 #include <g4main/PHG4Hit.h>
7 #include <g4main/PHG4Particle.h>
8 #include <g4main/PHG4HitDefs.h> // for hit_idbits
9 
10 // G4Cells includes
11 #include <g4detectors/PHG4Cell.h>
13 #include <g4detectors/PHG4CellDefs.h> // for genkey, keytype
14 
15 // Tower includes
16 #include <calobase/RawTower.h>
17 #include <calobase/RawTowerContainer.h>
18 #include <calobase/RawTowerGeom.h>
19 #include <calobase/RawTowerGeomContainer.h>
20 #include <calobase/RawTowerDefs.h>
21 // Cluster includes
22 #include <calobase/RawCluster.h>
23 #include <calobase/RawClusterContainer.h>
24 
27 
28 
30 #include "g4detectors/PHG4CylinderGeom_Spacalv1.h" // for PHG4CylinderGeom_Spaca...
34 
35 
36 #include <phool/getClass.h>
37 #include <TProfile.h>
38 #include <TFile.h>
39 #include <TNtuple.h>
40 #include <TMath.h>
41 #include <cassert>
42 #include <sstream>
43 #include <string>
44 #include <TF1.h>
45 #include <phool/onnxlib.h>
46 
47 using namespace std;
48 TProfile* h_template = nullptr;
49 TProfile* h_template_ihcal = nullptr;
50 TProfile* h_template_ohcal = nullptr;
51 
52 
53 #define ROWDIM 320
54 #define COLUMNDIM 27
55 
56 
57 
58 double CaloWaveFormSim::template_function(double *x, double *par)
59 {
60  Double_t v1 = par[0]*h_template->Interpolate(x[0]-par[1])+par[2];
61  return v1;
62 }
63 
64 double CaloWaveFormSim::template_function_ihcal(double *x, double *par)
65 {
66  Double_t v1 = par[0]*h_template_ihcal->Interpolate(x[0]-par[1])+par[2];
67  return v1;
68 }
69 
70 double CaloWaveFormSim::template_function_ohcal(double *x, double *par)
71 {
72  Double_t v1 = par[0]*h_template_ohcal->Interpolate(x[0]-par[1])+par[2];
73  return v1;
74 }
75 
76 
77 
79  : SubsysReco(name)
80  , detector("CEMC")
81  , outfilename(filename)
82  , hm(nullptr)
83  , outfile(nullptr)
84  , g4hitntuple(nullptr)
85 
86 {
87 }
88 
90 {
91  delete hm;
92  delete g4hitntuple;
93 }
94 
96 {
97  rnd = new TRandom3(0);
98  //----------------------------------------------------------------------------------------------------
99  //Read in the noise file, this currently points to a tim local area file,
100  //but a copy of this file is in the git repository.
101  //----------------------------------------------------------------------------------------------------
102  noise_midrad = new TTree("noise_midrad", "tree");
103  // noise->ReadFile("/gpfs/mnt/gpfs02/sphenix/user/trinn/fitting_algorithm_playing/slowneutronsignals/CaloAna/noise.csv", "a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:a16:a17:a18:a19:a20:a21:a22:a23:a24:a25:a26:a27:a28:a29:a30:a31");
104  noise_midrad->ReadFile("/gpfs/mnt/gpfs02/sphenix/user/trinn/sPHENIX_emcal_cosmics_sector0/noise_waveforms/medium_raddmgnoise.csv", "a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:a16:a17:a18:a19:a20:a21:a22:a23:a24:a25:a26:a27:a28:a29:a30:a31");
105 
106  noise_lowrad = new TTree("noise_lowrad", "tree");
107  noise_lowrad->ReadFile("/gpfs/mnt/gpfs02/sphenix/user/trinn/sPHENIX_emcal_cosmics_sector0/noise_waveforms/low_raddmgnoise.csv", "a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:a16:a17:a18:a19:a20:a21:a22:a23:a24:a25:a26:a27:a28:a29:a30:a31");
108 
109 
110  noise_norad = new TTree("noise_norad", "tree");
111  noise_norad->ReadFile("/gpfs/mnt/gpfs02/sphenix/user/trinn/sPHENIX_emcal_cosmics_sector0/noise_waveforms/no_raddmgnoise.csv", "a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:a16:a17:a18:a19:a20:a21:a22:a23:a24:a25:a26:a27:a28:a29:a30:a31");
112 
113  for (int i = 0; i < 31;i++)
114  {
115  noise_midrad->SetBranchAddress(Form("a%d",i+1),&noise_val_midrad[i]);
116  noise_lowrad->SetBranchAddress(Form("a%d",i+1),&noise_val_lowrad[i]);
117  noise_norad->SetBranchAddress(Form("a%d",i+1),&noise_val_norad[i]);
118  }
119 
120  hm = new Fun4AllHistoManager(Name());
121  outfile = new TFile(outfilename.c_str(), "RECREATE");
122  g4hitntuple = new TTree("tree", "tree");
123 
124  g4hitntuple->Branch("primpt",&m_primpt);
125  g4hitntuple->Branch("primeta",&m_primeta);
126  g4hitntuple->Branch("primphi",&m_primphi);
127  g4hitntuple->Branch("tedep_emcal",&m_tedep,"tedep_emcal[24576]/F");
128  g4hitntuple->Branch("tedep_ihcal",&m_tedep_ihcal,"tedep_ihcal[1536]/F");
129  g4hitntuple->Branch("tedep_ohcal",&m_tedep_ohcal,"tedep_ohcal[1536]/F");
130  g4hitntuple->Branch("extractedadc_emcal",& m_extractedadc,"extractedadc_emcal[24576]/F");
131  g4hitntuple->Branch("extractedtime_emcal",& m_extractedtime,"extractedtime_emcal[24576]/F");
132  g4hitntuple->Branch("extractedadc_ihcal",& m_extractedadc_ihcal,"extractedadc_ihcal[1536]/F");
133  g4hitntuple->Branch("extractedtime_ihcal",& m_extractedtime_ihcal,"extractedtime_ihcal[1536]/F");
134  g4hitntuple->Branch("extractedadc_ohcal",& m_extractedadc_ohcal,"extractedadc_ohcal[1536]/F");
135  g4hitntuple->Branch("extractedtime_ohcal",& m_extractedtime_ohcal,"extractedtime_ohcal[1536]/F");
136  g4hitntuple->Branch("toweradc_emcal",& m_toweradc,"toweradc_emcal[24576]/F");
137  g4hitntuple->Branch("toweradc_ihcal",& m_toweradc_ihcal,"toweradc_ihcal[1536]/F");
138  g4hitntuple->Branch("toweradc_ohcal",& m_toweradc_ohcal,"toweradc_ohcal[1536]/F");
139  // g4hitntuple->Branch("waveform_ohcal",& m_waveform_ohcal,"waveform_ohcal[1536][16]/I");
140 
141  // g4hitntuple->Branch("npeaks_ihcal",& m_npeaks_ihcal,"npeaks_ihcall[1536]/I");
142  // g4hitntuple->Branch("npeaks_ohcal",& m_npeaks_ohcal,"npeaks_ohcal[1536]/I");
143 
144  //----------------------------------------------------------------------------------------------------
145  //Read in the template file, this currently points to a tim local area file,
146  //but a copy of this file is in the git repository.
147  //----------------------------------------------------------------------------------------------------
148  // std::string template_input_file = "/gpfs/mnt/gpfs02/sphenix/user/trinn/fitting_algorithm_playing/prdfcode/prototype/offline/packages/Prototype4/templates.root";
149  std::string cemc_template_input_file = "/gpfs/mnt/gpfs02/sphenix/user/trinn/fitting_algorithm_playing/waveform_simulation/calibrations/WaveformProcessing/templates/testbeam_cemc_template.root";
150  std::string ihcal_template_input_file = "/gpfs/mnt/gpfs02/sphenix/user/trinn/fitting_algorithm_playing/waveform_simulation/calibrations/WaveformProcessing/templates/testbeam_ihcal_template.root";
151  std::string ohcal_template_input_file = "/gpfs/mnt/gpfs02/sphenix/user/trinn/fitting_algorithm_playing/waveform_simulation/calibrations/WaveformProcessing/templates/testbeam_ohcal_template.root";
152 
153  TFile* fin1 = TFile::Open(cemc_template_input_file.c_str());
154  assert(fin1);
155  assert(fin1->IsOpen());
156  h_template = static_cast<TProfile*>(fin1->Get("waveform_template"));
157 
158  TFile* fin2 = TFile::Open(ihcal_template_input_file.c_str());
159  assert(fin2);
160  assert(fin2->IsOpen());
161 
162  h_template_ihcal = static_cast<TProfile*>(fin2->Get("waveform_template"));
163 
164  TFile* fin3 = TFile::Open(ohcal_template_input_file.c_str());
165  assert(fin3);
166  assert(fin3->IsOpen());
167 
168  h_template_ohcal = static_cast<TProfile*>(fin3->Get("waveform_template"));
169 
170 
173  WaveformProcessing->set_template_file("testbeam_cemc_template.root");
175 
176 
177 
180  WaveformProcessing_ihcal->set_template_file("testbeam_ihcal_template.root");
182 
183 
186  WaveformProcessing_ohcal->set_template_file("testbeam_ohcal_template.root");
188 
189 
190  light_collection_model.load_data_file(string(getenv("CALIBRATIONROOT")) + string("/CEMC/LightCollection/Prototype3Module.xml"),
191  "data_grid_light_guide_efficiency", "data_grid_fiber_trans");
192 
193 
194  std::cout << " hey do you happen? " << std::endl;
195 
196  return 0;
197 }
198 
200 {
201  process_g4hits(topNode);
202 
204 }
205 
207 {
208  bool truth = true;
209 
210 
211  PHG4CylinderGeomContainer *layergeo = findNode::getClass<PHG4CylinderGeomContainer>(topNode, "CYLINDERGEOM_CEMC");
212  if (!layergeo)
213  {
214  std::cout << "PHG4FullProjSpacalCellReco::process_event - Fatal Error - Could not locate sim geometry node "
215  << std::endl;
216  exit(1);
217  }
218 
219  const PHG4CylinderGeom *layergeom_raw = layergeo->GetFirstLayerGeom();
220  assert(layergeom_raw);
221 
222  const PHG4CylinderGeom_Spacalv3 *layergeom =
223  dynamic_cast<const PHG4CylinderGeom_Spacalv3 *>(layergeom_raw);
224  assert(layergeom);
225 
226  PHG4CylinderCellGeomContainer *seggeo = findNode::getClass<PHG4CylinderCellGeomContainer>(topNode, "CYLINDERCELLGEOM_CEMC");
227  PHG4CylinderCellGeom *geo_raw = seggeo->GetFirstLayerCellGeom();
228  PHG4CylinderCellGeom_Spacalv1 *geo = dynamic_cast<PHG4CylinderCellGeom_Spacalv1 *>(geo_raw);
229 
230  float waveform[24576][16];
231  float waveform_ihcal[1536][16];
232  float waveform_ohcal[1536][16];
233  float tedep[24576];
234  float tedep_ihcal[1536];
235  float tedep_ohcal[1536];
236  for (int i = 0; i < 24576;i++)
237  {
238  for (int j = 0; j < 16;j++)
239  {
240  m_waveform[i][j] = 0.0;
241  waveform[i][j] = 0.0;
242  }
243  m_extractedadc[i] = 0;
244  m_extractedtime[i] = 0;
245  m_toweradc[i] = 0;
246  m_ndep[i] = 0.0;
247  m_tedep[i] = 0.0;
248  tedep[i] = 0.0;
249  }
250 
251 
252  for (int i = 0; i < 1536;i++)
253  {
254  tedep_ihcal[i] = 0.0;
255  m_extractedadc_ihcal[i] = 0;
257  tedep_ohcal[i] = 0.0;
258  m_extractedadc_ohcal[i] = 0;
259  m_extractedtime_ohcal[i] = 0;
260  m_toweradc_ihcal[i] = 0.0;
261  m_toweradc_ohcal[i] = 0.0;
262  for (int j = 0; j < 16;j++)
263  {
264  m_waveform_ihcal[i][j] = 0;
265  waveform_ihcal[i][j] = 0;
266  m_waveform_ohcal[i][j] = 0;
267  waveform_ohcal[i][j] = 0;
268  }
269  }
270 
271  //---------------------------------------------------------
272  //Load in the template function as a TF1
273  //for use in waveform generation
274  //---------------------------------------------------------
275 
276  TF1* f_fit = new TF1("f_fit",template_function,0,31,3);
277  f_fit->SetParameters(1,0,0);
278 
279  TF1* f_fit_ihcal = new TF1("f_fit_ihcal",template_function_ihcal,0,31,3);
280  f_fit_ihcal->SetParameters(1,0,0);
281 
282  TF1* f_fit_ohcal = new TF1("f_fit_ohcal",template_function_ohcal,0,31,3);
283  f_fit_ohcal->SetParameters(1,0,0);
284 
285  //-----------------------------------------------------
286  //Set the timeing in of the prompt
287  //signal peak to be 4 time samples into
288  //the waveform
289  //------------------------------------------------------
290  float _shiftval = 4-f_fit->GetMaximumX();
291  f_fit->SetParameters(1,_shiftval,0);
292 
293  float _shiftval_ihcal = 4-f_fit_ihcal->GetMaximumX();
294  f_fit_ihcal->SetParameters(1,_shiftval_ihcal,0);
295 
296  float _shiftval_ohcal = 4-f_fit_ohcal->GetMaximumX();
297  f_fit_ohcal->SetParameters(1,_shiftval_ohcal,0);
298 
299 
300  ostringstream nodename;
301  nodename.str("");
302  nodename << "G4HIT_" << detector;
303  PHG4HitContainer* hits = findNode::getClass<PHG4HitContainer>(topNode, nodename.str().c_str());
304  PHG4HitContainer* hits_ihcal = findNode::getClass<PHG4HitContainer>(topNode, "G4HIT_HCALIN");
305  PHG4HitContainer* hits_ohcal = findNode::getClass<PHG4HitContainer>(topNode, "G4HIT_HCALOUT");
306 
307  //-----------------------------------------------------------------------------------------
308  //Loop over truth primary particles and record their kinematics
309  //-----------------------------------------------------------------------------------------
310  PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
311  if (truth)
312  {
314  for (PHG4TruthInfoContainer::ConstIterator truth_iter = truth_range.first; truth_iter != truth_range.second; truth_iter++)
315  {
316  PHG4Particle*part = truth_iter->second;
317  m_primid.push_back(part->get_pid());
318  float pt =TMath::Sqrt(TMath::Power(part->get_py(),2) +TMath::Power(part->get_px(),2));
319  float theta = TMath::ATan(pt/part->get_pz());
320  float phi = TMath::ATan2(part->get_py(),part->get_px());
321  float eta = -1*TMath::Log(TMath::Tan(fabs(theta/2)));
322  if (part->get_pz() < 0 )
323  {
324  eta = -1 * eta;
325  }
326  m_primpt.push_back(part->get_e());
327  m_primeta.push_back(eta);
328  m_primphi.push_back(phi);
329  m_primtrkid.push_back(part->get_track_id());
330  }
331  }
332 
333  if (hits)
334  {
335  //-----------------------------------------------------------------------
336  //Loop over G4Hits to build a waveform simulation
337  //-----------------------------------------------------------------------
338  PHG4HitContainer::ConstRange hit_range = hits->getHits();
339  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
340  {
341  //-----------------------------------------------------------------
342  //Extract position information for each G4hit
343  //information for each G4hit in the calorimeter
344  //-----------------------------------------------------------------
345  int scint_id = hit_iter->second->get_scint_id();
347  std::pair<int, int> tower_z_phi_ID = layergeom->get_tower_z_phi_ID(decoder.tower_ID, decoder.sector_ID);
348  const int &tower_ID_z = tower_z_phi_ID.first;
349  const int &tower_ID_phi = tower_z_phi_ID.second;
350 
351  PHG4CylinderGeom_Spacalv3::tower_map_t::const_iterator it_tower =
352  layergeom->get_sector_tower_map().find(decoder.tower_ID);
353  assert(it_tower != layergeom->get_sector_tower_map().end());
354 
355  const int etabin_cell = geo->get_etabin_block(tower_ID_z); // block eta bin
356  const int sub_tower_ID_x = it_tower->second.get_sub_tower_ID_x(decoder.fiber_ID);
357  const int sub_tower_ID_y = it_tower->second.get_sub_tower_ID_y(decoder.fiber_ID);
358  unsigned short etabinshort = etabin_cell * layergeom->get_n_subtower_eta() + sub_tower_ID_y;
359  unsigned short phibin_cell = tower_ID_phi * layergeom->get_n_subtower_phi() + sub_tower_ID_x;
360 
361  //----------------------------------------------------------------------------------------------------
362  //Extract light yield from g4hit and correct for light collection efficiency
363  //----------------------------------------------------------------------------------------------------
364  double light_yield = hit_iter->second->get_light_yield();
365  {
366  const double z = 0.5 * (hit_iter->second->get_local_z(0) + hit_iter->second->get_local_z(1));
367  assert(not std::isnan(z));
368  light_yield *= light_collection_model.get_fiber_transmission(z);
369  }
370  {
371  const double x = it_tower->second.get_position_fraction_x_in_sub_tower(decoder.fiber_ID);
372  const double y = it_tower->second.get_position_fraction_y_in_sub_tower(decoder.fiber_ID);
373  light_yield *= light_collection_model.get_light_guide_efficiency(x, y);
374  }
375  //-------------------------------------------------------------------------
376  //Map the G4hits to the corresponding CEMC tower
377  //-------------------------------------------------------------------------
378  int etabin = etabinshort;
379  int phibin = phibin_cell;
380  int towernumber = etabin + 96*phibin;
381  //---------------------------------------------------------------------
382  //Convert the G4hit into a waveform contribution
383  //---------------------------------------------------------------------
384  // float t0 = 0.5*(hit_iter->second->get_t(0)+hit_iter->second->get_t(1)) / 16.66667; //Average of g4hit time downscaled by 16.667 ns/time sample
385  float t0 = (hit_iter->second->get_t(0)) / 16.66667; //Place waveform at the starting time of the G4hit, avoids issues caused by excessively long lived g4hits
386  float tmax = 16.667*16;
387  float tmin = -20;
388  f_fit->SetParameters(light_yield*26000,_shiftval+t0,0); //Set the waveform template to match the expected signal from such a hit
389  if (hit_iter->second->get_t(1) >= tmin && hit_iter->second->get_t(0) <= tmax) {
390  tedep[towernumber] += light_yield*26000; // add g4hit adc deposition to the total deposition
391  }
392  //-------------------------------------------------------------------------------------------------------------
393  //For each tower add the new waveform contribution to the total waveform
394  //-------------------------------------------------------------------------------------------------------------
395  if (hit_iter->second->get_edep()*26000 > 1 && hit_iter->second->get_t(1) >= tmin && hit_iter->second->get_t(0) <= tmax)
396  {
397  for (int j = 0; j < 16;j++) // 16 is the number of time samples
398  {
399  waveform[towernumber][j] +=f_fit->Eval(j);
400  }
401  m_ndep[towernumber] +=1;
402  }
403  m_phibin.push_back(phibin);
404  m_etabin.push_back(etabin);
405  }
406  }
407 
408 
409 
410  //--------------
411  // do IHCAL
412  //--------------
413 
414  if (hits_ihcal)
415  {
416  //-----------------------------------------------------------------------
417  //Loop over G4Hits to build a waveform simulation
418  //-----------------------------------------------------------------------
419  PHG4HitContainer::ConstRange hit_range = hits_ihcal->getHits();
420  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
421  {
422  //-----------------------------------------------------------------
423  //Extract position information for each G4hit
424  //information for each G4hit in the calorimeter
425  //-----------------------------------------------------------------
426  short icolumn = hit_iter->second->get_scint_id();
427  int introw = (hit_iter->second->get_hit_id() >> PHG4HitDefs::hit_idbits);
428 
429  if (introw >= ROWDIM || introw < 0)
430  {
431  std::cout << __PRETTY_FUNCTION__ << " row " << introw
432  << " exceed array size: " << ROWDIM
433  << " adjust ROWDIM and recompile" << std::endl;
434  exit(1);
435  }
436  int towerphi = introw/4;
437  int towereta = icolumn;
438 
439  //----------------------------------------------------------------------------------------------------
440  //Extract light yield from g4hit and correct for light collection efficiency
441  //----------------------------------------------------------------------------------------------------
442  double light_yield = hit_iter->second->get_light_yield(); //raw_light_yield has no MEPHI maps applied, light_yield aoppplies the maps change at some point
443  //-------------------------------------------------------------------------
444  //Map the G4hits to the corresponding CEMC tower
445  //-------------------------------------------------------------------------
446 
447  int etabin = towereta;
448  int phibin =towerphi;
449  int towernumber = etabin + 24*phibin;
450 
451  //---------------------------------------------------------------------
452  //Convert the G4hit into a waveform contribution
453  //---------------------------------------------------------------------
454  // float t0 = 0.5*(hit_iter->second->get_t(0)+hit_iter->second->get_t(1)) / 16.66667; //Average of g4hit time downscaled by 16.667 ns/time sample
455  float t0 = (hit_iter->second->get_t(0)) / 16.66667; //Place waveform at the starting time of the G4hit, avoids issues caused by excessively long lived g4hits
456  float tmax = 16.667*16;
457  float tmin = -20;
458  f_fit_ihcal->SetParameters(light_yield*2600,_shiftval_ihcal+t0,0); //Set the waveform template to match the expected signal from such a hit
459  if (hit_iter->second->get_t(1) >= tmin && hit_iter->second->get_t(0) <= tmax) {
460  tedep_ihcal[towernumber] += light_yield*2600; // add g4hit adc deposition to the total deposition
461  }
462  //-------------------------------------------------------------------------------------------------------------
463  //For each tower add the new waveform contribution to the total waveform
464  //-------------------------------------------------------------------------------------------------------------
465  if (hit_iter->second->get_edep()*2600 > 1 &&hit_iter->second->get_t(1) >= tmin && hit_iter->second->get_t(0) <= tmax )
466  {
467  for (int j = 0; j < 16;j++) // 16 is the number of time samples
468  {
469  waveform_ihcal[towernumber][j] +=f_fit_ihcal->Eval(j);
470  }
471  }
472  }
473  }
474 
475 
476 
477  //--------------
478  // do OHCAL
479  //--------------
480 
481  if (hits_ohcal)
482  {
483  //-----------------------------------------------------------------------
484  //Loop over G4Hits to build a waveform simulation
485  //-----------------------------------------------------------------------
486  PHG4HitContainer::ConstRange hit_range = hits_ohcal->getHits();
487  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
488  {
489  //-----------------------------------------------------------------
490  //Extract position information for each G4hit
491  //information for each G4hit in the calorimeter
492  //-----------------------------------------------------------------
493  short icolumn = hit_iter->second->get_scint_id();
494  int introw = (hit_iter->second->get_hit_id() >> PHG4HitDefs::hit_idbits);
495 
496 
497  if (introw >= ROWDIM || introw < 0)
498  {
499  std::cout << __PRETTY_FUNCTION__ << " row " << introw
500  << " exceed array size: " << ROWDIM
501  << " adjust ROWDIM and recompile" << std::endl;
502  exit(1);
503  }
504 
505 
506  int towerphi = introw/5;
507  int towereta = icolumn;
508 
509  //----------------------------------------------------------------------------------------------------
510  //Extract light yield from g4hit and correct for light collection efficiency
511  //----------------------------------------------------------------------------------------------------
512 
513  double light_yield = hit_iter->second->get_light_yield(); //raw_light_yield has no MEPHI maps applied, light_yield aoppplies the maps change at some point
514  //-------------------------------------------------------------------------
515  //Map the G4hits to the corresponding CEMC tower
516  //-------------------------------------------------------------------------
517 
518  int etabin = towereta;
519  int phibin =towerphi;
520  int towernumber = etabin + 24*phibin;
521 
522  //---------------------------------------------------------------------
523  //Convert the G4hit into a waveform contribution
524  //---------------------------------------------------------------------
525  // float t0 = 0.5*(hit_iter->second->get_t(0)+hit_iter->second->get_t(1)) / 16.66667; //Average of g4hit time downscaled by 16.667 ns/time sample
526  float t0 = (hit_iter->second->get_t(0)) / 16.66667; //Place waveform at the starting time of the G4hit, avoids issues caused by excessively long lived g4hits
527  float tmax =16.667*16 ;
528  float tmin = -20;
529  f_fit_ohcal->SetParameters(light_yield*5000,_shiftval_ohcal+t0,0); //Set the waveform template to match the expected signal from such a hit
530  if (hit_iter->second->get_t(0) < tmax && hit_iter->second->get_t(1) > tmin) {
531  {
532  tedep_ohcal[towernumber] += light_yield*5000; // add g4hit adc deposition to the total deposition
533  }
534  }
535  //-------------------------------------------------------------------------------------------------------------
536  //For each tower add the new waveform contribution to the total waveform
537  //-------------------------------------------------------------------------------------------------------------
538 
539  if (hit_iter->second->get_edep()*5000 > 1 && hit_iter->second->get_t(1) >= tmin && hit_iter->second->get_t(0) <= tmax)
540  {
541  for (int j = 0; j < 16;j++) // 16 is the number of time samples
542  {
543  waveform_ohcal[towernumber][j] +=f_fit_ohcal->Eval(j);
544  }
545  }
546  }
547  }
548 
549 
550  //----------------------------------------------------------------------------------------
551  //For each tower loop over add a noise waveform
552  //from cosmic data, gain is too high but is corrected for
553  //----------------------------------------------------------------------------------------
554 
555  //-----------------------------
556  // do noise for EMCal:
557  //-----------------------------
558  for (int i = 0; i < 24576;i++)
559  {
560  int noise_waveform = (int)rnd->Uniform(0,1990);
561  noise_midrad->GetEntry(noise_waveform);
562  m_tedep[i] = tedep[i];
563  for (int k = 0; k < 16;k++)
564  {
565  m_waveform[i][k] = waveform[i][k]+(noise_val_midrad[k]-1500)/16.0+1500;
566  // m_waveform[i][k] = waveform[i][k]+1500;
567  }
568  }
569  //---------------------------
570  // do noise for ihcal:
571  //---------------------------
572  for (int i = 0; i < 1536;i++)
573  {
574  int noise_waveform = (int)rnd->Uniform(0,1990);
575  noise_lowrad->GetEntry(noise_waveform);
576  m_tedep_ihcal[i] = tedep_ihcal[i];
577  for (int k = 0; k < 16;k++)
578  {
579  m_waveform_ihcal[i][k] = waveform_ihcal[i][k]+(noise_val_lowrad[k]-1500)/16.0+1500;
580  // m_waveform_ihcal[i][k] = waveform_ihcal[i][k]+1500;
581  }
582  }
583  //---------------------------
584  // do noise for ohcal:
585  //---------------------------
586  for (int i = 0; i < 1536;i++)
587  {
588  int noise_waveform = (int)rnd->Uniform(0,1990);
589  noise_norad->GetEntry(noise_waveform);
590 
591  m_tedep_ohcal[i] = tedep_ohcal[i];
592  for (int k = 0; k < 16;k++)
593  {
594  m_waveform_ohcal[i][k] = waveform_ohcal[i][k]+(noise_val_norad[k]-1500)/16.0+1500;
595  // m_waveform_ohcal[i][k] = waveform_ohcal[i][k]+1500;
596  }
597  }
598  std::vector<std::vector<float>> fitresults;
599  std::vector<std::vector<float>> fitresults_ihcal;
600  std::vector<std::vector<float>> fitresults_ohcal;
601  //------------------------------------------
602  //Process Waveforms: EMCal
603  //------------------------------------------
604  {
605  std::vector<std::vector<float>> waveforms;
606  for (int i = 0; i < 24576;i++)
607  {
608  std::vector<float>tmp;
609  for (int j = 0; j < 16;j++)
610  {
611  tmp.push_back(m_waveform[i][j]);
612  }
613  waveforms.push_back(tmp);
614  tmp.clear();
615  }
616  fitresults = WaveformProcessing->process_waveform(waveforms);
617  for (int i = 0; i < 24576;i++)
618  {
619  m_extractedadc[i] = fitresults.at(i).at(0);
620  m_extractedtime[i] = fitresults.at(i).at(1) - _shiftval;
621  }
622  waveforms.clear();
623  }
624 
625 
626 
627  //------------------------------------------
628  //Process Waveforms: IHCal
629  //------------------------------------------
630  {
631  std::vector<std::vector<float>> waveforms;
632  for (int i = 0; i < 1536;i++)
633  {
634  std::vector<float>tmp;
635  for (int j = 0; j < 16;j++)
636  {
637  tmp.push_back(m_waveform_ihcal[i][j]);
638  }
639  waveforms.push_back(tmp);
640 
641  // int size2 = tmp.size();
642  // int n_peak = 0;
643  // for (int j = 2; j < size2-1;j++)
644  // {
645  // if (tmp.at(j) > 1.01*tmp.at(j-2) && tmp.at(j) > 1.01 * tmp.at(j+1))
646  // {
647  // n_peak++;
648  // }
649  // }
650  // m_npeaks_ihcal[i] = n_peak;
651 
652 
653 
654  tmp.clear();
655  }
656  fitresults_ihcal = WaveformProcessing_ihcal->process_waveform(waveforms);
657  for (int i = 0; i < 1536;i++)
658  {
659  m_extractedadc_ihcal[i] = fitresults_ihcal.at(i).at(0);
660  m_extractedtime_ihcal[i] = fitresults_ihcal.at(i).at(1) - _shiftval_ihcal;
661  }
662  waveforms.clear();
663  }
664 
665 
666 
667  //------------------------------------------
668  //Process Waveforms: OHCal
669  //------------------------------------------
670  {
671  std::vector<std::vector<float>> waveforms;
672  for (int i = 0; i < 1536;i++)
673  {
674  std::vector<float>tmp;
675  for (int j = 0; j < 16;j++)
676  {
677  tmp.push_back(m_waveform_ohcal[i][j]);
678  }
679  waveforms.push_back(tmp);
680 
681 
682  // int size2 = tmp.size();
683  // int n_peak = 0;
684  // for (int j = 2; j < size2-1;j++)
685  // {
686  // if (tmp.at(j) - tmp.at(j-2) > 15 && tmp.at(j) - tmp.at(j+1) > 15)
687  // {
688  // n_peak++;
689  // }
690  // }
691 
692  // m_npeaks_ohcal[i] = n_peak;
693  tmp.clear();
694  }
695 
696 
697 
698 
699  fitresults_ohcal = WaveformProcessing_ohcal->process_waveform(waveforms);
700  for (int i = 0; i < 1536;i++)
701  {
702  m_extractedadc_ohcal[i] = fitresults_ohcal.at(i).at(0);
703  m_extractedtime_ohcal[i] = fitresults_ohcal.at(i).at(1) - _shiftval_ohcal;
704  }
705  waveforms.clear();
706  }
707 
708 
709 
710  std::cout << 4 << std::endl;
711 
712 
713  {
714  RawTowerContainer *towers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_RAW_CEMC");
715 
716  RawTowerContainer::ConstRange tower_range = towers->getTowers();
717  for (RawTowerContainer::ConstIterator tower_iter = tower_range.first; tower_iter != tower_range.second; tower_iter++)
718  {
719  int phibin = tower_iter->second->get_binphi();
720  int etabin = tower_iter->second->get_bineta();
721  float energy = tower_iter->second->get_energy();
722  int towernumber = etabin + 96*phibin;
723  m_toweradc[towernumber] = energy;
724  }
725  }
726 
727  {
728  RawTowerContainer *towers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_RAW_HCALIN");
729 
730  RawTowerContainer::ConstRange tower_range = towers->getTowers();
731  for (RawTowerContainer::ConstIterator tower_iter = tower_range.first; tower_iter != tower_range.second; tower_iter++)
732  {
733  int phibin = tower_iter->second->get_binphi();
734  int etabin = tower_iter->second->get_bineta();
735  float energy = tower_iter->second->get_energy();
736  int towernumber = etabin + 24*phibin;
737  m_toweradc_ihcal[towernumber] = energy;
738  }
739  }
740 
741  {
742  RawTowerContainer *towers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_RAW_HCALOUT");
743  RawTowerContainer::ConstRange tower_range = towers->getTowers();
744  for (RawTowerContainer::ConstIterator tower_iter = tower_range.first; tower_iter != tower_range.second; tower_iter++)
745  {
746  int phibin = tower_iter->second->get_binphi();
747  int etabin = tower_iter->second->get_bineta();
748  float energy = tower_iter->second->get_energy();
749  int towernumber = etabin + 24*phibin;
750  m_toweradc_ohcal[towernumber] = energy;
751  }
752  }
753 
754  g4hitntuple->Fill();
755 
756 
757  //------------------------------
758  //Clear vector content
759  //------------------------------
760 
761  fitresults.clear();
762  fitresults_ihcal.clear();
763  fitresults_ohcal.clear();
764  m_primid.clear();
765  m_primtrkid.clear();
766  m_primpt.clear();
767  m_primeta.clear();
768  m_primphi.clear();
769  m_etabin.clear();
770  m_phibin.clear();
771 
772 
773 
774 
776 }
777 
778 
779 
781 {
782  outfile->cd();
783  g4hitntuple->Write();
784  outfile->Write();
785  outfile->Close();
786  delete outfile;
787  hm->dumpHistos(outfilename, "UPDATE");
788  return 0;
789 }