1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | #include <stdint.h> |
9 | #include <vector> |
10 | |
11 | #include <TMath.h> |
12 | |
13 | |
14 | #include "JEventProcessor_CDC_drift.h" |
15 | #include <JANA/JApplication.h> |
16 | |
17 | |
18 | using namespace std; |
19 | using namespace jana; |
20 | |
21 | |
22 | #include "CDC/DCDCHit.h" |
23 | #include "CDC/DCDCDigiHit.h" |
24 | #include "DAQ/Df125PulsePedestal.h" |
25 | #include "DAQ/Df125PulseTime.h" |
26 | |
27 | #include <TDirectory.h> |
28 | #include <TH2.h> |
29 | #include <TH1.h> |
30 | #include <TF1.h> |
31 | #include <TTree.h> |
32 | #include <TBranch.h> |
33 | |
34 | |
35 | |
36 | static TH1I *cdc_num_events = NULL__null; |
37 | |
38 | static TH1I *cdc_tfit = NULL__null; |
39 | static TH1I *cdc_afit = NULL__null; |
40 | |
41 | static TTree *tfit = NULL__null; |
42 | static TTree *afit = NULL__null; |
43 | |
44 | static bool DISABLE_FITTING = true; |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | extern "C"{ |
51 | void InitPlugin(JApplication *app){ |
52 | InitJANAPlugin(app); |
53 | app->AddProcessor(new JEventProcessor_CDC_drift()); |
54 | } |
55 | } |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | JEventProcessor_CDC_drift::JEventProcessor_CDC_drift() { |
62 | } |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | JEventProcessor_CDC_drift::~JEventProcessor_CDC_drift() { |
69 | } |
70 | |
71 | |
72 | |
73 | |
74 | jerror_t JEventProcessor_CDC_drift::init(void) { |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | const Int_t RTMAX = 12000; |
84 | |
85 | const Char_t rtunits[8] = "0.125ns"; |
86 | |
87 | const float TUNITS = 0.125; |
88 | |
89 | const Int_t AMAX = 4096; |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | japp->RootWriteLock(); |
115 | |
116 | |
117 | |
118 | gDirectory(TDirectory::CurrentDirectory())->mkdir("CDC_fits")->cd(); |
119 | |
120 | |
121 | |
122 | |
123 | cdc_num_events = new TH1I("cdc_num_events","CDC number of events",1, 0.5, 1.5); |
124 | |
125 | cdc_tfit = new TH1I("cdc_tfit",Form("CDC raw time (units of %s); raw time (%s)",rtunits,rtunits),(Int_t)RTMAX*TUNITS,0,RTMAX); |
126 | |
127 | cdc_afit = new TH1I("cdc_afit","CDC amplitude (ADC units); ADC units",AMAX,0,AMAX); |
128 | |
129 | |
130 | tfit = new TTree("timefit","drift time fit params"); |
131 | afit = new TTree("ampfit","max amplitude Landau fit params"); |
132 | |
133 | |
134 | |
135 | |
136 | Long64_t tentries; |
137 | tfit->Branch("entries",&tentries,"entries/L"); |
138 | |
139 | Double_t t0; |
140 | tfit->Branch("t0",&t0,"t0/D"); |
141 | |
142 | Double_t tmax; |
143 | tfit->Branch("tmax",&tmax,"tmax/D"); |
144 | |
145 | Double_t tmax_slope; |
146 | tfit->Branch("tmax_slope",&tmax_slope,"tmax_slope/D"); |
147 | |
148 | Double_t tdiff; |
149 | tfit->Branch("tdiff_ns",&tdiff,"tdiff/D"); |
150 | |
151 | Long64_t aentries; |
152 | afit->Branch("entries",&aentries,"entries/L"); |
153 | |
154 | Double_t normconst; |
155 | afit->Branch("normconst",&normconst,"normconst/D"); |
156 | |
157 | Double_t mpv; |
158 | afit->Branch("MPV",&mpv,"mpv/D"); |
159 | |
160 | Double_t sigma; |
161 | afit->Branch("sigma",&sigma,"sigma/D"); |
162 | |
163 | |
164 | japp->RootUnLock(); |
165 | |
166 | return NOERROR; |
167 | } |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | jerror_t JEventProcessor_CDC_drift::brun(JEventLoop *eventLoop, int runnumber) { |
174 | |
175 | return NOERROR; |
176 | } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | jerror_t JEventProcessor_CDC_drift::evnt(JEventLoop *eventLoop, int eventnumber) { |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | const uint32_t MIN_EVENTS = 10000; |
195 | const uint32_t UPDATE_INTERVAL = 10000; |
196 | |
197 | const Bool_t RESET = kFALSE; |
198 | const Bool_t VERBOSE = kFALSE; |
199 | |
200 | const uint16_t MAXAMP_FIT_MIN = 100; |
201 | const uint16_t MAXAMP_FIT_MAX = 3800; |
202 | |
203 | const float TUNITS = 0.125; |
204 | |
205 | |
206 | |
207 | uint16_t ring,straw; |
208 | uint16_t n; |
209 | uint16_t j; |
210 | |
211 | Long64_t nentries; |
212 | |
213 | int64_t previous; |
214 | |
215 | uint32_t pulse_number=-1; |
216 | |
217 | uint32_t maxamp; |
218 | |
219 | |
220 | int straw_offset[29] = {0,0,42,84,138,192,258,324,404,484,577,670,776,882,1005,1128,1263,1398,1544,1690,1848,2006,2176,2346,2528,2710,2907,3104,3313}; |
221 | |
222 | const uint16_t nstraws = 77; |
223 | |
224 | const uint16_t strawlist[] = {176, 237, 496, 497, 775, 776, 777, 782, 879, 881, 882, 895, 900, 1021, 1026, 1047, 1052, 1056, 1057, 1130, 1241, 1252, 1266, 1318, 1340, 1376, 1567, 1568, 1679, 1682, 1701, 1849, 1853, 1864, 1918, 1998, 2088, 2242, 2244, 2248, 2255, 2256, 2430, 2445, 2556, 2585, 2748, 2767, 2770, 2772, 2774, 2782, 2788, 2789, 2793, 2796, 2943, 2951, 2952, 2962, 2963, 2965, 2969, 2973, 2985, 3159, 3160, 3176, 3177, 3184, 3214, 3361, 3363, 3365, 3369, 3428, 3429}; |
225 | |
226 | |
227 | Bool_t fillhisto; |
228 | Bool_t fithisto; |
229 | |
230 | |
231 | |
232 | |
233 | vector<const DCDCDigiHit*> digihits; |
234 | eventLoop->Get(digihits); |
235 | |
236 | |
237 | |
238 | |
239 | japp->RootWriteLock(); |
240 | |
241 | fithisto = kFALSE; |
242 | |
243 | previous = (uint32_t)(cdc_tfit->GetEntries()/UPDATE_INTERVAL); |
244 | |
245 | |
246 | for (uint32_t i=0; i<digihits.size(); i++) { |
247 | |
248 | const DCDCDigiHit *digihit = digihits[i]; |
249 | const Df125PulseTime *pt = NULL__null; |
250 | |
251 | |
252 | ring = digihit->ring; |
253 | straw = digihit->straw; |
254 | n = straw_offset[ring] + straw; |
255 | |
256 | fillhisto = kFALSE; |
257 | |
258 | digihit->GetSingle(pt); |
259 | if (pt) pulse_number = pt->pulse_number; |
260 | |
261 | if ((digihit->pulse_time) && (pulse_number==0) && (!digihit->QF)) { |
262 | |
263 | j=0; |
264 | |
265 | while ((!fillhisto) && (j<nstraws)) { |
266 | |
267 | if (n == strawlist[j]) fillhisto = kTRUE; |
268 | j++; |
269 | |
270 | } |
271 | |
272 | |
273 | if (fillhisto) { |
274 | |
275 | cdc_tfit->Fill(digihit->pulse_time); |
276 | nentries = cdc_tfit->GetEntries(); |
277 | |
278 | if ( !DISABLE_FITTING |
279 | && (nentries > MIN_EVENTS) |
280 | && (uint32_t(nentries/UPDATE_INTERVAL) > previous)) |
281 | fithisto = kTRUE; |
282 | |
283 | |
284 | |
285 | const Df125PulsePedestal *pp = NULL__null; |
286 | |
287 | maxamp = 0; |
288 | |
289 | |
290 | digihit->GetSingle(pp); |
291 | |
292 | if (pp) pulse_number = pp->pulse_number; |
293 | if ((pp) && (pulse_number==0)) maxamp = pp->pulse_peak - pp->pedestal; |
294 | |
295 | if (maxamp) cdc_afit->Fill(maxamp); |
296 | |
297 | } |
298 | |
299 | } |
300 | |
301 | } |
302 | |
303 | |
304 | |
305 | if (fithisto) { |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | Double_t fitparams[9]; |
312 | Float_t startpar[9]; |
313 | int32_t fitstatus; |
314 | int32_t imax; |
315 | Double_t tdiff=0; |
316 | |
317 | |
318 | |
319 | TF1 *f = new TF1("f","[0] * (1 + [1]*exp(([3]-x)/[2]) + [7]*exp(([3]-x)/[8]) ) / ( (1+exp(([3]-x)/[5])) * (1+exp((x-[4])/[6])) )"); |
320 | |
321 | f->SetLineWidth(1); |
322 | f->SetLineColor(6); |
323 | |
324 | |
325 | |
326 | startpar[1] = 15; |
327 | startpar[7] = 3; |
328 | |
329 | f->SetParLimits(1,0,startpar[1]*2); |
330 | f->SetParLimits(7,0,startpar[7]*2); |
331 | |
332 | startpar[5] = 5*0.8/TUNITS; |
333 | startpar[6] = 25*0.8/TUNITS; |
334 | |
335 | f->SetParLimits(5,0,startpar[5]*2.0); |
336 | f->SetParLimits(6,0,startpar[6]*2.0); |
337 | |
338 | startpar[2] = 20*0.8/TUNITS; |
339 | startpar[8] = 200*0.8/TUNITS; |
340 | |
341 | f->SetParLimits(2,0,startpar[2]*3); |
342 | f->SetParLimits(8,startpar[2]*3,startpar[8]*3); |
343 | |
344 | for (j=1;j<3;j++) f->SetParameter(j,startpar[j]); |
345 | for (j=5;j<9;j++) f->SetParameter(j,startpar[j]); |
346 | |
347 | |
348 | |
349 | previous = (uint32_t)(nentries/UPDATE_INTERVAL); |
| Value stored to 'previous' is never read |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | startpar[0] = 0.0005*nentries; |
356 | |
357 | f->SetParLimits(0,0,startpar[0]*100); |
358 | f->SetParameter(0,startpar[0]); |
359 | |
360 | imax = cdc_tfit->GetMaximumBin(); |
361 | imax = imax * cdc_tfit->GetBinWidth(imax); |
362 | |
363 | startpar[3] = imax; |
364 | startpar[4] = imax+500/TUNITS; |
365 | |
366 | f->SetParLimits(3,startpar[3]-(50/TUNITS),startpar[3]); |
367 | f->SetParLimits(4,startpar[3]+(150/TUNITS),startpar[3]+(1500/TUNITS)); |
368 | |
369 | f->SetParameter(3,startpar[3]); |
370 | f->SetParameter(4,startpar[4]); |
371 | |
372 | if (!VERBOSE) fitstatus = cdc_tfit->Fit("f","Q"); |
373 | if (VERBOSE) fitstatus = cdc_tfit->Fit("f"); |
374 | |
375 | |
376 | if (fitstatus == 0) { |
377 | |
378 | f->GetParameters(fitparams); |
379 | |
380 | tdiff = (fitparams[4] - fitparams[3])*TUNITS; |
381 | |
382 | |
383 | |
384 | } else { |
385 | |
386 | tdiff = 0; |
387 | |
388 | } |
389 | |
390 | |
391 | if (VERBOSE) printf("fitstatus:%1i nentries:%5lld [0] %2.0f [1] %2.0f [2] %3.0f [3] %4.0f [4] %4.0f [5] %4.1f [6] %3.0f [7] %3.1f [8] %4.0f [tmax] %3.0f\n",fitstatus,nentries,fitparams[0],fitparams[1],fitparams[2],fitparams[3],fitparams[4],fitparams[5],fitparams[6],fitparams[7],fitparams[8],tdiff); |
392 | |
393 | |
394 | tfit->SetBranchAddress("entries",&nentries); |
395 | tfit->SetBranchAddress("t0",&fitparams[3]); |
396 | tfit->SetBranchAddress("tmax",&fitparams[4]); |
397 | tfit->SetBranchAddress("tmax_slope",&fitparams[6]); |
398 | tfit->SetBranchAddress("tdiff_ns",&tdiff); |
399 | |
400 | tfit->Fill(); |
401 | |
402 | |
403 | |
404 | Double_t ampfitparams[3]; |
405 | |
406 | TF1 *lan = new TF1("lan","landau",MAXAMP_FIT_MIN,MAXAMP_FIT_MAX); |
407 | lan->SetLineColor(6); |
408 | lan->SetLineWidth(1); |
409 | |
410 | if (!VERBOSE) fitstatus = cdc_afit->Fit(lan,"QRLL"); |
411 | if (VERBOSE) fitstatus = cdc_afit->Fit(lan,"RLL"); |
412 | |
413 | if (!fitstatus) lan->GetParameters(ampfitparams); |
414 | |
415 | nentries = cdc_afit->GetEntries(); |
416 | |
417 | if (VERBOSE) printf("maxamp fitstatus:%1i nentries:%5lld [0] %2.0f [1] %2.0f [2] %3.0f \n",fitstatus,nentries,ampfitparams[0],ampfitparams[1],ampfitparams[2]); |
418 | |
419 | afit->SetBranchAddress("entries",&nentries); |
420 | afit->SetBranchAddress("normconst",&fitparams[0]); |
421 | afit->SetBranchAddress("MPV",&fitparams[1]); |
422 | afit->SetBranchAddress("sigma",&fitparams[2]); |
423 | |
424 | afit->Fill(); |
425 | |
426 | |
427 | |
428 | |
429 | if (RESET) cdc_tfit->Reset(); |
430 | if (RESET) cdc_afit->Reset(); |
431 | |
432 | |
433 | } |
434 | |
435 | |
436 | |
437 | japp->RootUnLock(); |
438 | |
439 | |
440 | |
441 | return NOERROR; |
442 | } |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | jerror_t JEventProcessor_CDC_drift::erun(void) { |
449 | |
450 | |
451 | |
452 | return NOERROR; |
453 | } |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | jerror_t JEventProcessor_CDC_drift::fini(void) { |
460 | |
461 | |
462 | |
463 | return NOERROR; |
464 | } |
465 | |
466 | |
467 | |
468 | |