GEMC  2.3
Geant4 Monte-Carlo Framework
ctof_hitprocess.cc
Go to the documentation of this file.
1 // G4 headers
2 #include "G4Poisson.hh"
3 #include "Randomize.hh"
4 
5 #include <CCDB/Calibration.h>
6 #include <CCDB/Model/Assignment.h>
7 #include <CCDB/CalibrationGenerator.h>
8 using namespace ccdb;
9 
10 // gemc headers
11 #include "ctof_hitprocess.h"
12 
13 // CLHEP units
14 #include "CLHEP/Units/PhysicalConstants.h"
15 using namespace CLHEP;
16 
17 static ctofConstants initializeCTOFConstants(int runno)
18 {
19  ctofConstants ctc;
20 
21  cout<<"Entering initializeCTOF"<<endl;
22 
23  // do not initialize at the beginning, only after the end of the first event,
24  // with the proper run number coming from options or run table
25  if(runno == -1) return ctc;
26 
27  ctc.runNo = runno;
28  ctc.date = "2015-11-29";
29  if(getenv ("CCDB_CONNECTION") != NULL)
30  ctc.connection = (string) getenv("CCDB_CONNECTION");
31  else
32  ctc.connection = "mysql://clas12reader@clasdb.jlab.org/clas12";
33 
34  ctc.variation = "default";
35 
36  ctc.npaddles = 48;
37  ctc.thick = 3.0;
38 
39  ctc.dEdxMIP = 1.956; // muons in polyvinyltoluene
40  ctc.dEMIP = ctc.thick*ctc.dEdxMIP;
41  ctc.pmtPEYld = 500;
42  ctc.tdcLSB = 41.6667; // counts per ns (24 ps LSB)
43 
44  cout<<"CTOF:Setting time resolution"<<endl;
45 
46  for(int c=1; c<ctc.npaddles+1;c++)
47  {
48  ctc.tres.push_back(1e-3*65.); //ps to ns
49  }
50 
51  int isec,ilay,istr;
52 
53  vector<vector<double> > data;
54 
55  auto_ptr<Calibration> calib(CalibrationGenerator::CreateCalibration(ctc.connection));
56  cout<<"Connecting to "<<ctc.connection<<"/calibration/ctof"<<endl;
57 
58  cout<<"CTOF:Getting attenuation"<<endl;
59  sprintf(ctc.database,"/calibration/ctof/attenuation:%d",ctc.runNo);
60  data.clear(); calib->GetCalib(data,ctc.database);
61  for(unsigned row = 0; row < data.size(); row++)
62  {
63  isec = data[row][0]; ilay = data[row][1]; istr = data[row][2];
64  ctc.attlen[isec-1][ilay-1][0].push_back(data[row][3]);
65  ctc.attlen[isec-1][ilay-1][1].push_back(data[row][4]);
66  }
67 
68  cout<<"CTOF:Getting effective_velocity"<<endl;
69  sprintf(ctc.database,"/calibration/ctof/effective_velocity:%d",ctc.runNo);
70  data.clear(); calib->GetCalib(data,ctc.database);
71  for(unsigned row = 0; row < data.size(); row++)
72  {
73  isec = data[row][0]; ilay = data[row][1]; istr = data[row][2];
74  ctc.veff[isec-1][ilay-1][0].push_back(data[row][3]);
75  ctc.veff[isec-1][ilay-1][1].push_back(data[row][4]);
76  }
77 
78  cout<<"CTOF:Getting status"<<endl;
79  sprintf(ctc.database,"/calibration/ctof/status:%d",ctc.runNo);
80  data.clear() ; calib->GetCalib(data,ctc.database);
81  for(unsigned row = 0; row < data.size(); row++)
82  {
83  isec = data[row][0]; ilay = data[row][1]; istr = data[row][2];
84  ctc.status[isec-1][ilay-1][0].push_back(data[row][3]);
85  ctc.status[isec-1][ilay-1][1].push_back(data[row][4]);
86  }
87 
88  cout<<"CTOF:Getting gain_balance"<<endl;
89  sprintf(ctc.database,"/calibration/ctof/gain_balance:%d",ctc.runNo);
90  data.clear() ; calib->GetCalib(data,ctc.database);
91  for(unsigned row = 0; row < data.size(); row++)
92  {
93  isec = data[row][0]; ilay = data[row][1]; istr = data[row][2];
94  ctc.countsForMIP[isec-1][ilay-1][0].push_back(data[row][3]);
95  ctc.countsForMIP[isec-1][ilay-1][1].push_back(data[row][4]);
96  }
97 
98  /* For future use in HitProcess
99  cout<<"Getting time_walk"<<endl;
100  sprintf(ctc.database,"/calibration/ctof/time_walk:%d",ctc.runNo);
101  data.clear() ; calib->GetCalib(data,ctc.database);
102  for(unsigned row = 0; row < data.size(); row++)
103  {
104  isec = data[row][0]; ilay = data[row][1]; istr = data[row][2];
105  ctc.twlk[isec-1][ilay-1][0].push_back(data[row][3]);
106  ctc.twlk[isec-1][ilay-1][1].push_back(data[row][4]);
107  ctc.twlk[isec-1][ilay-1][2].push_back(data[row][5]);
108  ctc.twlk[isec-1][ilay-1][3].push_back(data[row][6]);
109  ctc.twlk[isec-1][ilay-1][4].push_back(data[row][7]);
110  ctc.twlk[isec-1][ilay-1][5].push_back(data[row][8]);
111  }
112  */
113 
114  return ctc;
115 }
116 
118 {
119  if(ctc.runNo != runno)
120  {
121  cout << " > Initializing " << HCname << " digitization for run number " << runno << endl;
122  ctc = initializeCTOFConstants(runno);
123  ctc.runNo = runno;
124  }
125 }
126 
127 
128 map<string, double> ctof_HitProcess :: integrateDgt(MHit* aHit, int hitn)
129 {
130  map<string, double> dgtz;
131  vector<identifier> identity = aHit->GetId();
132 
133  int sector = 1;
134  int panel = 1;
135  int paddle = identity[0].id;
136 
137  // Get the paddle length: in ctof paddles are boxes, the length is the y dimension
138  double length = aHit->GetDetector().dimensions[2];
139 
140  trueInfos tInfos(aHit);
141 
142  // Distances from upstream, downstream
143  double dUp = length + tInfos.ly;
144  double dDn = length - tInfos.ly;
145 
146  // attenuation length
147  double attlenUp = ctc.attlen[sector-1][panel-1][0][paddle-1];
148  double attlenDn = ctc.attlen[sector-1][panel-1][1][paddle-1];
149 
150  // attenuation factor
151  double attUp = exp(-dUp/cm/attlenUp);
152  double attDn = exp(-dDn/cm/attlenDn);
153 
154  // Gain factors to simulate CTOF PMT gain matching algorithm.
155  // Each U,D PMT pair has HV adjusted so geometeric mean sqrt(U*D)
156  // is independent of counter length, which compensates for
157  // the factor exp(-L/2/attlen) where L=full length of bar.
158  double gainUp = sqrt(attUp*attDn);
159  double gainDn = gainUp;
160 
161  // Attenuated light at PMT
162  double eneUp = tInfos.eTot*attUp;
163  double eneDn = tInfos.eTot*attDn;
164 
165  double adcu = 0.;
166  double adcd = 0.;
167  double tdcu = 0.;
168  double tdcd = 0.;
169  double adcuu = 0.;
170  double adcdu = 0.;
171  double tdcuu = 0.;
172  double tdcdu = 0.;
173 
174  // Fluctuate the light measured by the PMT with
175  // Poisson distribution for emitted photoelectrons
176  // Treat Up and Dn separately, in case nphe=0
177 
178  if (eneUp>0)
179  adcuu = eneUp*ctc.countsForMIP[sector-1][panel-1][0][paddle-1]/ctc.dEMIP/gainUp;
180 
181  if (eneDn>0)
182  adcdu = eneDn*ctc.countsForMIP[sector-1][panel-1][1][paddle-1]/ctc.dEMIP/gainDn;
183 
184 
185  double npheUp = G4Poisson(eneUp*ctc.pmtPEYld);
186  eneUp = npheUp/ctc.pmtPEYld;
187 
188  if (eneUp>0) {
189  adcu = eneUp*ctc.countsForMIP[sector-1][panel-1][0][paddle-1]/ctc.dEMIP/gainUp;
190  //double A = ctc.twlk[sector-1][panel-1][0][paddle-1];
191  //double B = ctc.twlk[sector-1][panel-1][1][paddle-1];
192  //double C = ctc.twlk[sector-1][panel-1][2][paddle-1];
193  //double timeWalkUp = A/(B+C*sqrt(adcu));
194  double timeWalkUp = 0.;
195  double tUpU = tInfos.time + dUp/ctc.veff[sector-1][panel-1][0][paddle-1]/cm + timeWalkUp;
196  double tUp = G4RandGauss::shoot(tUpU, sqrt(2)*ctc.tres[paddle-1]);
197  tdcuu = tUpU*ctc.tdcLSB;
198  tdcu = tUp*ctc.tdcLSB;
199  }
200 
201  double npheDn = G4Poisson(eneDn*ctc.pmtPEYld);
202  eneDn = npheDn/ctc.pmtPEYld;
203 
204  if (eneDn>0) {
205  adcd = eneDn*ctc.countsForMIP[sector-1][panel-1][1][paddle-1]/ctc.dEMIP/gainDn;
206  //double A = ctc.twlk[sector-1][panel-1][3][paddle-1];
207  //double B = ctc.twlk[sector-1][panel-1][4][paddle-1];
208  //double C = ctc.twlk[sector-1][panel-1][5][paddle-1];
209  //double timeWalkDn = A/(B+C*sqrt(adcd));
210  double timeWalkDn = 0.;
211  double tDnU = tInfos.time + dDn/ctc.veff[sector-1][panel-1][1][paddle-1]/cm + timeWalkDn;
212  double tDn = G4RandGauss::shoot(tDnU, sqrt(2)*ctc.tres[paddle-1]);
213  tdcdu = tDnU*ctc.tdcLSB;
214  tdcd = tDn*ctc.tdcLSB;
215  }
216 
217  dgtz["hitn"] = hitn;
218  dgtz["paddle"] = paddle;
219  dgtz["ADCU"] = (int) adcu;
220  dgtz["ADCD"] = (int) adcd;
221  dgtz["TDCU"] = (int) tdcu;
222  dgtz["TDCD"] = (int) tdcd;
223  dgtz["ADCUu"] = (int) adcuu;
224  dgtz["ADCDu"] = (int) adcdu;
225  dgtz["TDCUu"] = (int) tdcuu;
226  dgtz["TDCDu"] = (int) tdcdu;
227 
228  return dgtz;
229 }
230 
231 vector<identifier> ctof_HitProcess :: processID(vector<identifier> id, G4Step* aStep, detector Detector)
232 {
233  id[id.size()-1].id_sharing = 1;
234  return id;
235 }
236 
237 
238 map< string, vector <int> > ctof_HitProcess :: multiDgt(MHit* aHit, int hitn)
239 {
240  map< string, vector <int> > MH;
241 
242  return MH;
243 }
244 
245 // this static function will be loaded first thing by the executable
246 ctofConstants ctof_HitProcess::ctc = initializeCTOFConstants(-1);
vector< double > attlen[1][1][2]
void initWithRunNumber(int runno)
vector< identifier > processID(vector< identifier >, G4Step *, detector)
double ly
Definition: HitProcess.h:42
vector< double > tres
map< string, vector< int > > multiDgt(MHit *, int)
vector< identifier > GetId()
Definition: Hit.h:103
double time
Definition: HitProcess.h:43
vector< int > status[1][1][2]
char database[80]
double eTot
Definition: HitProcess.h:40
vector< double > veff[1][1][2]
Definition: Hit.h:22
static ctofConstants ctc
vector< double > countsForMIP[1][1][2]
vector< double > dimensions
vector of dimensions. Size, units depends on solid type
Definition: detector.h:77
detector GetDetector()
Definition: Hit.h:108
map< string, double > integrateDgt(MHit *, int)