FairRoot/PandaRoot
PndDiscTaskDigitization.cxx
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Author: Oliver Merle (Oliver.Merle@exp2.physik.uni-giessen.de)
3 // Changes: Mustafa Schmidt (Mustafa.A.Schmidt@physik.uni-giessen.de)
4 // Date: 30.11.2015
5 // Description: Digitization of Monte Carlo hits
6 //-------------------------------------------------------------------------
7 
8 // project
10 #include "PndDiscDigitizedHit.h"
11 #include "PndDiscSensorMCPoint.h"
12 #include "PndDiscParticleMCPoint.h"
13 
14 // FAIR/PROOT
15 #include "FairRootManager.h"
16 #include "FairRun.h"
17 #include "FairEventHeader.h"
18 #include "PndDiscWriteoutBuffer.h"
19 
20 // ROOT
21 #include "TClonesArray.h"
22 #include "TString.h"
23 #include "TGeoManager.h"
24 #include "TRandom.h"
25 #include "TMath.h"
26 
27 // cpp
28 #include <cstdlib>
29 
30 
31 
32 
33 //Definition of the sellmeier equation to calculate the refractive index from the photon wavelength
35 {
36  double lambda_um_sq = lambda_um*lambda_um;
37  return sqrt( coeff[0]/(1.0-(coeff[3]/lambda_um_sq)) + coeff[1]/(1.0-(coeff[4]/lambda_um_sq)) + coeff[2]/(1.0-(coeff[5]/lambda_um_sq)) + 1.0);
38 }
39 
40 
41 
42 
43 PndDiscTaskDigitization::PndDiscTaskDigitization() : PndPersistencyTask("PndDiscTaskDigitization"), mc_point_branch_id(0), tclarr_mc_points(NULL), writeout_buffer(NULL), fMcEventHeader(NULL), is_time_based(kTRUE), is_persistent(kTRUE)
44 #ifndef USESENSORGRID
45  ,pde_interpolator(0, ROOT::Math::Interpolation::kLINEAR)
46 #endif
47 {
48  particle_types.insert(1);
49 
50  //Defining the TClonesArrays
51  tclarr_particle_tracks_out = new TClonesArray("PndDiscParticleMCPoint"); //TClonesArray for MCTruth tracks
52  array = new TClonesArray("PndDiscDigitizedHit"); //TClonesArray for digitized hits
53 
54  //Defining the branch- and foldernames
55  branch_name_mc_point = "DiscSensorMCPoint";
56  branch_name_digits = "DiscDigit";
57  folder_name_digits = "DiscDIRC";
58 }
59 
60 
61 
62 
64 {
65  if(writeout_buffer != NULL) delete writeout_buffer;
66 }
67 
68 
69 
70 
72 {
73  return kSUCCESS;
74 }
75 
76 
77 
78 
80 {
81  // Get IO manager instance:
82  FairRootManager* io_manager = FairRootManager::Instance();
83  if(!io_manager)
84  {
85  LOG(FATAL) << "FairRootManager instance is NULL !!!";
86  return kFATAL;
87  }
88 
89  // Get sensor hits from input tree:
90  tclarr_mc_points = (TClonesArray*) io_manager->GetObject(branch_name_mc_point);
91  if(!tclarr_mc_points)
92  {
93  LOG(ERROR) << "Branch "<< branch_name_mc_point.Data() <<" is not accessible through FairRootManager.";
94  return kERROR;
95  }
96  mc_point_branch_id = io_manager->GetBranchId(branch_name_mc_point);
97 
98  // Create the buffer for time based simulation:
99  //writeout_buffer = new DiscDIRC_WriteoutBuffer(branch_name_digits, folder_name_digits, is_persistent);
100  //writeout_buffer->SetVerbose(1);
101  //writeout_buffer = (DiscDIRC_WriteoutBuffer*)io_manager->RegisterWriteoutBuffer(branch_name_digits, writeout_buffer);
102 
103  // Set buffering mode:
104  //writeout_buffer->ActivateBuffering(is_time_based);
105 
106  // MCTruth output if particle types are set
107  if(particle_types.size() > 0)
108  {
109  // get particles from input tree
110  tclarr_particle_tracks_in = (TClonesArray*) io_manager->GetObject("DiscParticleMCPoint");
112  {
113  LOG(ERROR) << "GetObject(\"DiscParticleMCPoint\") returned NULL";
114  return kERROR;
115  }
116 
117  // create output branch for particle tracks
118  //tclarr_particle_tracks_out = new TClonesArray("DiscParticleMCPoint");
119  FairRootManager::Instance()->Register("DiscMCTruthTracks","DiscDIRC", tclarr_particle_tracks_out, GetPersistency());
120  FairRootManager::Instance()->Register("DiscDigit","DiscDIRC", array, GetPersistency());
121  }
122 
123 
124  // Initialize PDE values of Sensor:
125  {
126  double pde_wl_nm[74] =
127  {
128  269.949916528,
129  275.959933222,
130  280.634390651,
131  285.30884808,
132  289.983305509,
133  295.993322204,
134  302.003338898,
135  308.013355593,
136  313.355592654,
137  320.701168614,
138  326.711185309,
139  333.388981636,
140  338.731218698,
141  346.74457429,
142  351.41903172,
143  358.764607679,
144  366.110183639,
145  375.459098497,
146  382.804674457,
147  389.482470785,
148  398.16360601,
149  407.512520868,
150  414.190317195,
151  420.20033389,
152  424.207011686,
153  430.217028381,
154  434.89148581,
155  440.233722871,
156  442.904841402,
157  446.911519199,
158  452.25375626,
159  456.260434057,
160  459.59933222,
161  462.938230384,
162  468.948247078,
163  474.958263773,
164  478.964941569,
165  483.639398998,
166  488.98163606,
167  492.320534224,
168  498.998330551,
169  504.340567613,
170  508.347245409,
171  513.021702838,
172  517.696160267,
173  522.370617696,
174  525.041736227,
175  530.383973289,
176  533.722871452,
177  539.065108514,
178  545.075125209,
179  550.41736227,
180  556.427378965,
181  563.772954925,
182  569.782971619,
183  577.128547579,
184  585.809682805,
185  593.823038397,
186  601.168614357,
187  608.514190317,
188  610.517529215,
189  615.859766277,
190  621.202003339,
191  630.550918197,
192  637.228714524,
193  644.574290484,
194  652.587646077,
195  659.933222037,
196  670.61769616,
197  676.627712855,
198  682.637729549,
199  689.983305509,
200  696.661101836,
201  702.003338898
202  };
203 
204  double pde_efficiency[74] =
205  {
206  0.152452,
207  0.159915,
208  0.168443,
209  0.173774,
210  0.179104,
211  0.182836,
212  0.186034,
213  0.189232,
214  0.191898,
215  0.195096,
216  0.200959,
217  0.206823,
218  0.208955,
219  0.210021,
220  0.211087,
221  0.214819,
222  0.220682,
223  0.224947,
224  0.227612,
225  0.227079,
226  0.223348,
227  0.219616,
228  0.215352,
229  0.211620,
230  0.208955,
231  0.204691,
232  0.198827,
233  0.194563,
234  0.190832,
235  0.186567,
236  0.180704,
237  0.176439,
238  0.171109,
239  0.166311,
240  0.157249,
241  0.147655,
242  0.141258,
243  0.136461,
244  0.132196,
245  0.128998,
246  0.124733,
247  0.122601,
248  0.119403,
249  0.114606,
250  0.108742,
251  0.100746,
252  0.095416,
253  0.085821,
254  0.078891,
255  0.068230,
256  0.061301,
257  0.054904,
258  0.050107,
259  0.045842,
260  0.040512,
261  0.037313,
262  0.032516,
263  0.028252,
264  0.025586,
265  0.022388,
266  0.021855,
267  0.020256,
268  0.018124,
269  0.015991,
270  0.013859,
271  0.013326,
272  0.011727,
273  0.010661,
274  0.009062,
275  0.007463,
276  0.006397,
277  0.005330,
278  0.004264,
279  0.003731
280  };
281 
282 #ifdef USESENSORGRID
284  photo_detector->SetPDE(74, pde_wl_nm, pde_efficiency);
285 #else
286  //pde_interpolator.SetData(74, pde_wl_nm, pde_efficiency);
287 #endif
288  }
289 
290  return kSUCCESS;
291 }
292 
293 
295 {
296  PndDiscSensorMCPoint * mc_point = NULL;
297  Int_t i = 0, n_mc_points = tclarr_mc_points->GetEntriesFast();
298  FairEventHeader* event_header = (FairEventHeader*)FairRootManager::Instance()->GetObject("EventHeader.");
299  Int_t input_file_id = event_header->GetInputFileId();
300 
301  // --------------------------------------------------------
302  // some fixed design parameters:
303  // --------------------------------------------------------
304  //Double_t rms_time_ns = 0.021; //[R.K. 01/2017] unused variable?
305  Double_t binning_ns = 0.050;
306  Double_t radiator_thickness = 2.0;
307  Double_t rms_roughness_nm = 1.5;
308  //Double_t dead_time = 20.0; //[R.K. 01/2017] unused variable?
309  // need refractive index for reflection probability:
310  Double_t sellmeier_coeff [6] = {0.473115591, 0.631038719, 0.906404498, 0.012995717, 0.0041280992, 98.7685322};
311  // --------------------------------------------------------
312 
313  array->Clear();
314 
315  for(i=0; i<n_mc_points; i++)
316  {
317  mc_point = (PndDiscSensorMCPoint*)tclarr_mc_points->At(i); //Get Entries from Sensor
318  Double_t photon_momentum = sqrt( (mc_point->GetPx()*mc_point->GetPx()) + (mc_point->GetPy()*mc_point->GetPy()) + (mc_point->GetPz()*mc_point->GetPz()));
319  Double_t wavelength_nm = (1.239841939/photon_momentum)*1E-06;
320  Double_t rindex = n_phase_sellmeier(sellmeier_coeff, wavelength_nm*1E-3);
321  Double_t sin_iref_angle = sin(mc_point->GetTotalReflectionAngle());
322  Double_t reflection_probability = 1. - pow(4.*TMath::Pi()*rms_roughness_nm * sin_iref_angle * rindex/wavelength_nm, 2);
323 
324  // Apply reflection probability
325  Int_t n_reflections = (Int_t)(mc_point->GetLength()*sin_iref_angle/radiator_thickness);
326  if( gRandom->Uniform(1.0) > pow( reflection_probability, n_reflections) ) continue;
327 
328  // --------------------------------------------------------
329  // Detection - digitize the hit ...
330  // --------------------------------------------------------
331 
332  double absolute_time = mc_point->GetTime() + FairRootManager::Instance()->GetEventTime();
333  double tdc_time_ns;
334  SensorGrid::PixelInfo pixel_info;
335 
336  int pixel_id = photo_detector->Detect(mc_point->GetX(), mc_point->GetY(), absolute_time, wavelength_nm, pixel_info, tdc_time_ns);
337  if( pixel_id < 0) continue;
338 
339  tdc_time_ns = (floor(tdc_time_ns/binning_ns) + 0.5) * binning_ns;
340 
341  Double_t hit_x, hit_z;
342  photo_detector->GetGrid()->PixelToPosition(pixel_info, hit_x, hit_z);
343 
344  int classifier = 0;
345  if(is_run_mixed && input_file_id == 0) classifier = 2;
346 
347  Int_t copy_number = mc_point->GetDetectorID();
348  div_t res = div(copy_number, 27);
349  Int_t detector_id = res.quot;
350  Int_t readout_id = res.rem;
351 
352  if(gGeoManager == NULL)
353  {
354  LOG(FATAL) << "gGeoManager is NULL - cannot retrieve geo information !!!";
355  }
356 
357  PndDiscDigitizedHit * digit = new PndDiscDigitizedHit(FairLink(input_file_id, event_header->GetMCEntryNumber(), mc_point_branch_id, i),
358  detector_id, readout_id, readout_id*3 + 3-pixel_info.column_on_grid, pixel_info.pixel_number,
359  pixel_info.row_on_grid, hit_z, tdc_time_ns, absolute_time, classifier);
360 
361  // buffer is responsible to delete digit:
362  //writeout_buffer->FillNewData(digit, absolute_time, absolute_time+dead_time);
363 
364  digit = new((*array)[array->GetEntriesFast()]) PndDiscDigitizedHit(*digit);
365  digit->Print();
366  }
367 
368  if( (!is_run_mixed || input_file_id > 0) && particle_types.size() > 0)
369  {
370  PndDiscParticleMCPoint * particle_mc_point;
371  n_mc_points = tclarr_particle_tracks_in->GetEntries();
372  for(i=0; i<n_mc_points; i++)
373  {
374  particle_mc_point = (PndDiscParticleMCPoint*) tclarr_particle_tracks_in->At(i);
375  //if(particle_types.count(abs(particle_mc_point->pdgCode)) == 0) continue;
376  if(particle_mc_point->pos_in_inside!=0) continue; //check if created inside
377  if(!particle_mc_point->is_primary) continue; //check if particle is primary
378  particle_mc_point = new((*tclarr_particle_tracks_out)[tclarr_particle_tracks_out->GetEntriesFast()]) PndDiscParticleMCPoint(*particle_mc_point);
379  Double_t particle_tof = particle_mc_point->GetTime();
380  particle_mc_point->SetTime(particle_tof + FairRootManager::Instance()->GetEventTime());
381  }
382  }
383 }
384 
385 
387 {
389  FinishEvents();
390 }
391 
392 
394 {
395 }
396 
397 
399 {
400  particle_types.insert(abs(pdg_code));
401 }
402 
403 
405 
virtual void Print(std::ostream &out=std::cout)
Double_t n_phase_sellmeier(Double_t *coeff, Double_t lambda_um)
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
TVector3 photon_momentum
virtual void Exec(Option_t *opt)
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
DiscDIRC_Photodetector * photo_detector
friend F32vec4 sin(const F32vec4 &a)
Definition: P4_F32vec4.h:111
FairWriteoutBuffer * writeout_buffer
particle types to filter in output
TString branch_name_mc_point
Branch name where mc points were stored.
TString folder_name_digits
Folder name for output in root file.
void SetPDE(int n_entries, const double *wavelength_nm, const double *pde)
TGeoManager * gGeoManager
#define USESENSORGRID
Int_t mc_point_branch_id
Cache branch id of the mc point branch for linking with FairLink.
Double_t
TClonesArray * tclarr_particle_tracks_out
virtual bool PixelToPosition(PixelInfo &pixel_info, double &x, double &y) const =0
TString branch_name_digits
Branch name where digitized hits shall be stored.
const Double_t & GetTotalReflectionAngle()
FairMCPoint forces the implementation.
ClassImp(PndAnaContFact)
int Detect(double const &hit_pos_x, double const &hit_pos_y, double const &hit_time_ns, double const &wavelength_nm, PixelInfo &pixel_info, double &smeared_time_ns) const
Handle photon detection:
Double_t Pi
TClonesArray * tclarr_particle_tracks_in
to cache the pointer to input TClonesArray returned by IO manager.