1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include <iostream> |
10 | #include <iomanip> |
11 | using namespace std; |
12 | |
13 | #include "DTAGMDigiHit.h" |
14 | #include "DTAGMTDCDigiHit.h" |
15 | #include "DTAGMGeometry.h" |
16 | #include "DTAGMHit_factory.h" |
17 | #include <DAQ/Df250PulseIntegral.h> |
18 | #include <DAQ/Df250PulsePedestal.h> |
19 | #include <DAQ/Df250Config.h> |
20 | |
21 | using namespace jana; |
22 | |
23 | const int DTAGMHit_factory::k_fiber_good; |
24 | const int DTAGMHit_factory::k_fiber_bad; |
25 | const int DTAGMHit_factory::k_fiber_noisy; |
26 | |
27 | |
28 | |
29 | |
30 | jerror_t DTAGMHit_factory::init(void) |
31 | { |
32 | DELTA_T_ADC_TDC_MAX = 10.0; |
33 | gPARMS->SetDefaultParameter("TAGMHit:DELTA_T_ADC_TDC_MAX", DELTA_T_ADC_TDC_MAX, |
34 | "Maximum difference in ns between a (calibrated) fADC time and" |
35 | " F1TDC time for them to be matched in a single hit"); |
36 | |
37 | fadc_a_scale = 0; |
38 | fadc_t_scale = 0; |
39 | t_base = 0; |
40 | t_tdc_base=0; |
41 | |
42 | |
43 | for (int row = 0; row <= TAGM_MAX_ROW5; ++row) { |
44 | for (int col = 0; col <= TAGM_MAX_COLUMN102; ++col) { |
45 | fadc_gains[row][col] = 0; |
46 | fadc_pedestals[row][col] = 0; |
47 | fadc_time_offsets[row][col] = 0; |
48 | tdc_time_offsets[row][col] = 0; |
49 | fiber_quality[row][col] = 0; |
50 | } |
51 | } |
52 | |
53 | return NOERROR; |
54 | } |
55 | |
56 | |
57 | |
58 | |
59 | jerror_t DTAGMHit_factory::brun(jana::JEventLoop *eventLoop, int32_t runnumber) |
60 | { |
61 | |
62 | static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
63 | static set<int> runs_announced; |
64 | pthread_mutex_lock(&print_mutex); |
65 | bool print_messages = false; |
66 | if(runs_announced.find(runnumber) == runs_announced.end()){ |
67 | print_messages = true; |
68 | runs_announced.insert(runnumber); |
69 | } |
70 | pthread_mutex_unlock(&print_mutex); |
71 | |
72 | |
73 | fadc_a_scale = 1.1; |
74 | fadc_t_scale = 0.0625; |
75 | t_base = 0.; |
76 | |
77 | pthread_mutex_unlock(&print_mutex); |
78 | if(print_messages) jout << "In DTAGMHit_factory, loading constants..." << std::endl; |
79 | |
80 | |
81 | map<string,double> base_time_offset; |
82 | if (eventLoop->GetCalib("/PHOTON_BEAM/microscope/base_time_offset",base_time_offset)) |
83 | jout << "Error loading /PHOTON_BEAM/microscope/base_time_offset !" << endl; |
84 | if (base_time_offset.find("TAGM_BASE_TIME_OFFSET") != base_time_offset.end()) |
85 | t_base = base_time_offset["TAGM_BASE_TIME_OFFSET"]; |
86 | else |
87 | jerr << "Unable to get TAGM_BASE_TIME_OFFSET from /PHOTON_BEAM/microscope/base_time_offset !" << endl; |
88 | if (base_time_offset.find("TAGM_TDC_BASE_TIME_OFFSET") != base_time_offset.end()) |
89 | t_tdc_base = base_time_offset["TAGM_TDC_BASE_TIME_OFFSET"]; |
90 | else |
91 | jerr << "Unable to get TAGM_TDC_BASE_TIME_OFFSET from /PHOTON_BEAM/microscope/base_time_offset !" << endl; |
92 | |
93 | if (load_ccdb_constants("fadc_gains", "gain", fadc_gains) && |
94 | load_ccdb_constants("fadc_pedestals", "pedestal", fadc_pedestals) && |
95 | load_ccdb_constants("fadc_time_offsets", "offset", fadc_time_offsets) && |
96 | load_ccdb_constants("tdc_time_offsets", "offset", tdc_time_offsets) && |
97 | load_ccdb_constants("fiber_quality", "code", fiber_quality) ) |
98 | { |
99 | return NOERROR; |
100 | } |
101 | return UNRECOVERABLE_ERROR; |
102 | } |
103 | |
104 | |
105 | |
106 | |
107 | jerror_t DTAGMHit_factory::evnt(JEventLoop *loop, uint64_t eventnumber) |
108 | { |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | vector<const DTAGMGeometry*> tagmGeomVect; |
120 | eventLoop->Get( tagmGeomVect ); |
121 | if (tagmGeomVect.size() < 1) |
122 | return OBJECT_NOT_AVAILABLE; |
123 | const DTAGMGeometry& tagmGeom = *(tagmGeomVect[0]); |
124 | |
125 | const DTTabUtilities* locTTabUtilities = NULL__null; |
126 | loop->GetSingle(locTTabUtilities); |
127 | |
128 | |
129 | vector<const DTAGMDigiHit*> digihits; |
130 | loop->Get(digihits); |
131 | for (unsigned int i=0; i < digihits.size(); i++) { |
132 | const DTAGMDigiHit *digihit = digihits[i]; |
133 | |
134 | |
135 | |
136 | const Df250PulsePedestal* PPobj = NULL__null; |
137 | digihit->GetSingle(PPobj); |
138 | if (PPobj != NULL__null){ |
139 | if (PPobj->pedestal == 0 || PPobj->pulse_peak == 0) continue; |
140 | } |
141 | else continue; |
142 | |
143 | |
144 | |
145 | double pedestal = fadc_pedestals[digihit->row][digihit->column]; |
| Value stored to 'pedestal' during its initialization is never read |
146 | |
147 | const Df250PulseIntegral* PIobj = NULL__null; |
148 | digihit->GetSingle(PIobj); |
149 | if (PIobj != NULL__null) { |
150 | |
151 | |
152 | |
153 | double single_sample_ped = (double)PIobj->pedestal; |
154 | double nsamples_integral = (double)PIobj->nsamples_integral; |
155 | double nsamples_pedestal = (double)PIobj->nsamples_pedestal; |
156 | pedestal = single_sample_ped * nsamples_integral/nsamples_pedestal; |
157 | } |
158 | else continue; |
159 | |
160 | |
161 | int quality = fiber_quality[digihit->row][digihit->column]; |
162 | if (quality == k_fiber_bad || quality == k_fiber_noisy) |
163 | continue; |
164 | |
165 | |
166 | |
167 | |
168 | DTAGMHit *hit = new DTAGMHit; |
169 | int row = digihit->row; |
170 | int column = digihit->column; |
171 | hit->row = row; |
172 | hit->column = column; |
173 | double Elow = tagmGeom.getElow(column); |
174 | double Ehigh = tagmGeom.getEhigh(column); |
175 | hit->E = (Elow + Ehigh)/2; |
176 | hit->t = 0; |
177 | |
178 | |
179 | double P = PPobj->pulse_peak - PPobj->pedestal; |
180 | double A = digihit->pulse_integral; |
181 | double T = digihit->pulse_time; |
182 | A -= pedestal; |
183 | hit->integral = A; |
184 | hit->pulse_peak = P; |
185 | hit->npix_fadc = A * fadc_a_scale * fadc_gains[row][column]; |
186 | hit->time_fadc = T * fadc_t_scale - fadc_time_offsets[row][column] + t_base; |
187 | hit->t=hit->time_fadc; |
188 | hit->has_TDC=false; |
189 | hit->has_fADC=true; |
190 | |
191 | hit->AddAssociatedObject(digihit); |
192 | _data.push_back(hit); |
193 | } |
194 | |
195 | |
196 | |
197 | |
198 | vector<const DTAGMTDCDigiHit*> tdcdigihits; |
199 | loop->Get(tdcdigihits); |
200 | for (unsigned int i=0; i < tdcdigihits.size(); i++) { |
201 | const DTAGMTDCDigiHit *digihit = tdcdigihits[i]; |
202 | |
203 | |
204 | int row = digihit->row; |
205 | int column = digihit->column; |
206 | double T = locTTabUtilities->Convert_DigiTimeToNs_F1TDC(digihit) - tdc_time_offsets[row][column] + t_tdc_base; |
207 | |
208 | |
209 | |
210 | DTAGMHit *hit = 0; |
211 | for (unsigned int j=0; j < _data.size(); ++j) { |
212 | if (_data[j]->row == row && _data[j]->column == column && |
213 | fabs(T - _data[j]->time_fadc) < DELTA_T_ADC_TDC_MAX) |
214 | { |
215 | hit = _data[j]; |
216 | } |
217 | } |
218 | if (hit == 0) { |
219 | hit = new DTAGMHit; |
220 | hit->row = row; |
221 | hit->column = column; |
222 | double Elow = tagmGeom.getElow(column); |
223 | double Ehigh = tagmGeom.getEhigh(column); |
224 | hit->E = (Elow + Ehigh)/2; |
225 | hit->time_fadc = 0; |
226 | hit->npix_fadc = 0; |
227 | hit->integral = 0; |
228 | hit->pulse_peak = 0; |
229 | hit->has_fADC=false; |
230 | _data.push_back(hit); |
231 | } |
232 | hit->time_tdc=T; |
233 | hit->has_TDC=true; |
234 | |
235 | hit->t = T; |
236 | |
237 | |
238 | |
239 | hit->AddAssociatedObject(digihit); |
240 | } |
241 | |
242 | return NOERROR; |
243 | } |
244 | |
245 | |
246 | |
247 | |
248 | jerror_t DTAGMHit_factory::erun(void) |
249 | { |
250 | return NOERROR; |
251 | } |
252 | |
253 | |
254 | |
255 | |
256 | jerror_t DTAGMHit_factory::fini(void) |
257 | { |
258 | return NOERROR; |
259 | } |
260 | |
261 | |
262 | |
263 | |
264 | bool DTAGMHit_factory::load_ccdb_constants( |
265 | std::string table_name, |
266 | std::string column_name, |
267 | double result[TAGM_MAX_ROW5+1][TAGM_MAX_COLUMN102+1]) |
268 | { |
269 | std::vector< std::map<std::string, double> > table; |
270 | std::string ccdb_key = "/PHOTON_BEAM/microscope/" + table_name; |
271 | if (eventLoop->GetCalib(ccdb_key, table)) |
272 | { |
273 | jout << "Error loading " << ccdb_key << " from ccdb!" << std::endl; |
274 | return false; |
275 | } |
276 | for (unsigned int i=0; i < table.size(); ++i) { |
277 | int row = (table[i])["row"]; |
278 | int col = (table[i])["column"]; |
279 | result[row][col] = (table[i])[column_name]; |
280 | } |
281 | return true; |
282 | } |