-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTXPConfig.cpp
591 lines (503 loc) · 16.1 KB
/
TXPConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "./TXPConfig.h"
TXPConfig::TXPConfig(std::string XPConfig)
{
loadCal(XPConfig);
fhasGeometry = false;
}
// Default constructor, let C++ automatically handle this
TXPConfig::TXPConfig() {}
TXPConfig::TXPConfig(std::string XPConfig, std::string XPGeometry)
{
loadCal( XPConfig );
loadGeometry( XPGeometry );
}
int TXPConfig::loadCal(std::string XPConfig)
{
std::ifstream XPfile(XPConfig);
if( !XPfile.is_open() )
{
printf("Failed to open XPConfig at %s\n", XPConfig.c_str() );
return EXIT_FAILURE;
}
std::string Line;
std::istringstream LineStream;
// Variables for holding all the columns in XPConfig
int ChanN;
int detType;
int isTr;
double_t cal0;
double_t cal1;
int rangemin;
int rangemax;
while( getline( XPfile, Line ) )
{
// Check to see if the line is a comment
if( Line.find('#') != std::string::npos )
{
continue; // Skip this line
}
LineStream.str(Line); // set stream to Line
//#chan dettype isTr cal0 cal1 rangemin rangemax
LineStream >> ChanN >> detType >> isTr;
LineStream >> cal0 >> cal1;
LineStream >> rangemin >> rangemax;
// cal0*Q+cal1 = E
// for this class they need to be rearanged
fCal0Vec.push_back(cal1);
fCal1Vec.push_back(cal0);
fisTrVec.push_back(isTr);
fDetTypeVec.push_back(detType);
LineStream.clear();
}
fhasEngCalibration = true;
printf("Loaded in %d channels\n", (int) fCal0Vec.size() );
return EXIT_SUCCESS;
}
// Export current experimental config in the same way as
// XPConfig would be.
void TXPConfig::exportCal(std::string XPConfig)
{
if( !fhasEngCalibration )
{
printf("TXPConfig: No calibration!");
return;
}
std::ofstream XPOut;
XPOut.open( XPConfig );
if( !XPOut )
{
printf("exportCal: Could not open %s\n", XPConfig.c_str() );
return;
}
// Standard header
XPOut << "#chan dettype isTr cal0 cal1 rangemin rangemax" << std::endl;
// For the XPConfig, cal0*x + cal1 rather than the standard polynomial
// notation.
for( int i = 0; i < fCal0Vec.size(); i++ )
{
XPOut << i << " ";
XPOut << fDetTypeVec[i] << " ";
XPOut << fisTrVec[i] << " "; // isTr
XPOut << fCal1Vec[i] << " ";
XPOut << fCal0Vec[i] << " ";
XPOut << "0 32768" << std::endl;
}
XPOut.close();
return;
}
int TXPConfig::loadGeometry(std::string XPGeometry)
{
std::ifstream XPfile(XPGeometry);
if( !XPfile.is_open() )
{
printf("Failed to open XPConfig at %s\n", XPGeometry.c_str() );
return EXIT_FAILURE;
}
std::string Line;
std::istringstream LineStream;
// Variables for holding all the columns in XPConfig
int index;
int clov;
int cryst;
while( getline( XPfile, Line ) )
{
// Check to see if the line is a comment
if( Line.find('#') != std::string::npos )
{
continue; // Skip this line
}
LineStream.str(Line); // set stream to Line
//#index clov cryst
LineStream >> index >> clov >> cryst;
// Store detector geometry into vectors
fIndex2Clover.push_back(clov);
fIndex2Cryst.push_back(cryst);
LineStream.clear();
}
// Create geometry vectors now. Angles are measured from each x,y,z axis.
// Vectors point to positions on the unit sphere.
// Corona Ring (Detectors 0 -> 7)
fDetPositions[0] = TVector3( 0.0, 0.0, 1 );
fDetPositions[1] = TVector3( 0.0,
TMath::Sin(TMath::DegToRad() * 45.0 ),
TMath::Cos(TMath::DegToRad() * 45.0 ) );
fDetPositions[2] = TVector3( 0.0, 1, 0.0 );
fDetPositions[3] = TVector3( 0.0,
TMath::Sin(TMath::DegToRad() * 135.0),
TMath::Cos(TMath::DegToRad() * 135.0) );
fDetPositions[4] = TVector3( 0.0, 0.0, -1 );
fDetPositions[5] = TVector3( 0.0,
TMath::Sin(TMath::DegToRad() * 225.0),
TMath::Cos(TMath::DegToRad() * 225.0) );
fDetPositions[6] = TVector3( 0.0, -1, 0.0 );
fDetPositions[7] = TVector3( 0.0,
TMath::Sin(TMath::DegToRad() * 315.0),
TMath::Cos(TMath::DegToRad() * 315.0) );
// Downstream Lampshade (Detectors 8 -> 11)
fDetPositions[8] = TVector3(
TMath::Cos(TMath::DegToRad() * 45.0 ),
0.0,
TMath::Cos(TMath::DegToRad() * 45.0) );
fDetPositions[9] = TVector3(
TMath::Cos(TMath::DegToRad() * 45.0 ),
TMath::Cos(TMath::DegToRad() * 45.0),
0.0 );
fDetPositions[10] = TVector3(
TMath::Cos(TMath::DegToRad() * 45.0 ),
0.0,
TMath::Cos(TMath::DegToRad() * 135.0) );
fDetPositions[11] = TVector3(
TMath::Cos(TMath::DegToRad() * 45.0 ),
TMath::Cos(TMath::DegToRad() * 135.0),
0.0 );
// Upstream Lampshade (Detectors 12 -> 15 )
fDetPositions[12] = TVector3(
TMath::Cos(TMath::DegToRad() * 135.0),
0.0,
TMath::Cos(TMath::DegToRad() * 45.0) );
fDetPositions[13] = TVector3(
TMath::Cos(TMath::DegToRad() * 135.0 ),
TMath::Cos(TMath::DegToRad() * 315.0),
0.0 );
fDetPositions[14] = TVector3(
TMath::Cos(TMath::DegToRad() * 135.0),
0.0,
TMath::Cos(TMath::DegToRad() * 135.0) );
fDetPositions[15] = TVector3(
TMath::Cos(TMath::DegToRad() * 135.0 ),
TMath::Cos(TMath::DegToRad() * 225.0),
0.0 );
fhasGeometry = true;
printf("Loaded in %d geometry positions\n", (int) fIndex2Clover.size() );
return EXIT_SUCCESS;
}
// Export current experimental config in the same way as
// XPConfig would be.
void TXPConfig::exportGeometry(std::string XPGeometry)
{
if( !fhasGeometry )
{
printf("TXPConfig: No geometry calibration!");
return;
}
std::ofstream XPOut;
XPOut.open( XPGeometry );
if( !XPOut )
{
printf("exportGeometry: Could not open %s\n", XPGeometry.c_str() );
return;
}
// Standard header
XPOut << "# index clov cryst" << std::endl;
// For the XPConfig, cal0*x + cal1 rather than the standard polynomial
// notation.
for( int i = 0; i < fCal0Vec.size(); i++ )
{
XPOut << i << " ";
XPOut << fIndex2Clover[i] << " ";
XPOut << fIndex2Cryst[i] << " ";
XPOut << std::endl;
}
XPOut.close();
return;
}
int TXPConfig::getDetNum( int index )
{
if( fhasGeometry == false )
{
printf("getDetNum: No Geometry information loaded\n");
return 0.0;
}
return fIndex2Clover[index];
}
int TXPConfig::getCrystNum( int index )
{
if( fhasGeometry == false )
{
printf("getCrystNum: No Geometry information loaded\n");
return 0.0;
}
return fIndex2Cryst[index];
}
double_t TXPConfig::GetAngleIndex( int index1, int index2 ) {
if( fhasGeometry == false )
{
printf("GetAngle: No Geometry information loaded\n");
return 0.0;
}
int det1 = getDetNum(index1);
int det2 = getDetNum(index2);
return fDetPositions[det1].Angle(fDetPositions[det2]);
}
double_t TXPConfig::GetAngleDetec( int nDet1, int nDet2 ) {
if( fhasGeometry == false )
{
printf("GetAngle: No Geometry information loaded\n");
return 0.0;
}
return fDetPositions[nDet1].Angle(fDetPositions[nDet2]);
}
// Convert charge Q to an energy value
double_t TXPConfig::GetEnergy(int32_t &Q, short &index)
{
//double_t E = (double_t)Q + (double_t)rand()/( (double_t)RAND_MAX + 1.0);
double_t E = (double_t)Q + gRandom->Uniform();
// Check for Gain matching
// TODO: Add residual corrections
if( fhasGainMatch == 0 ) // Bin -> Energy
{
return fCal0Vec[index] + fCal1Vec[index]*E;
}
else
{
return (fCal0Vec[index]+fCal1Vec[index]*fGainMatchOffsetVec[index])
+ (fCal1Vec[index]*fGainMatchSlopeVec[index])*E;
}
}
double_t TXPConfig::GetCal( int nCoeff, int index )
{
switch( nCoeff ) {
case 0 : return fCal0Vec[index];
case 1 : return fCal1Vec[index];
}
printf("TXPConfig_ERROR: no coefficent for %d\n", nCoeff);
return 0.0;
}
void TXPConfig::SetCal( int nCoeff, int index, double_t Coeff )
{
switch( nCoeff ) {
case 0 : fCal0Vec[index] = Coeff;
return;
case 1 : fCal1Vec[index] = Coeff;
return;
}
printf("TXPConfig_ERROR: no coefficent for %d\n", nCoeff);
return;
}
bool TXPConfig::isVito(int index)
{
if( fDetTypeVec.size() == 0 )
{
printf("TXPConfig ERROR: No vito/detc information for channel %d\n", index);
return EXIT_FAILURE;
}
// check trigger
if( fisTrVec[index] == 0 )
return true;
// check detector type
if( fDetTypeVec[index] == 1 )
return false; // HPGe
if( fDetTypeVec[index] == -1 )
return true; // BGO Shield
return false; // Nothing bad, continue
}
void TXPConfig::setVitoDet( int index, bool state )
{
fvitoVec[index] = state;
}
int TXPConfig::NChan()
{
return fCal0Vec.size();
}
/* == Gain Matching Functions == */
int TXPConfig::loadGainMatch(std::string GainMatchFileLoc )
{
std::ifstream GainMatchFile(GainMatchFileLoc);
if( !GainMatchFile.is_open() )
{
printf("Failed to open gain match file at %s\n", GainMatchFileLoc.c_str() );
return EXIT_FAILURE;
}
// If gain matching already exists, destroy it
if( fhasGainMatch == true )
deleteGainMatch();
std::string Line;
std::istringstream LineStream;
// Variables for holding all the columns in XPConfig
int index;
double_t slope;
double_t offset;
while( getline( GainMatchFile, Line ) )
{
// Check to see if the line is a comment
if( Line.find('#') != std::string::npos )
{
continue; // Skip this line
}
LineStream.str(Line); // set stream to Line
//#index, offset, slope
LineStream >> index >> offset >> slope;
fGainMatchOffsetVec.push_back(offset);
fGainMatchSlopeVec.push_back(slope);
LineStream.clear();
}
fhasGainMatch = true;
printf("Loaded in %d channels\n", (int) fGainMatchOffsetVec.size() );
return EXIT_SUCCESS;
}
void TXPConfig::exportGainMatch(std::string GainMatchFileLoc)
{
if( !fhasGainMatch )
{
printf("TXPConfig: No gain match information!\n");
return;
}
std::ofstream GMOut;
GMOut.open( GainMatchFileLoc );
if( !GMOut )
{
printf("exportGainMatch: Could not open %s\n", GainMatchFileLoc.c_str() );
return;
}
// Standard header
GMOut << "#index offset slope" << std::endl;
// For the XPConfig, cal0*x + cal1 rather than the standard polynomial
// notation.
for( int i = 0; i < fGainMatchOffsetVec.size(); i++ )
{
GMOut << i << " ";
GMOut << fGainMatchOffsetVec[i] << " ";
GMOut << fGainMatchSlopeVec[i]; // isTr
GMOut << std::endl;
}
GMOut.close();
return;
}
void TXPConfig::deleteGainMatch()
{
if( fhasGainMatch == true )
{
fGainMatchOffsetVec.clear();
fGainMatchOffsetVec.clear();
}
fhasGainMatch = false;
}
// Create default values of size of the basic calibration file
void TXPConfig::CreateEmptyGainMatch()
{
if( fhasGainMatch == true )
deleteGainMatch();
for( unsigned int i = 0; i < fCal0Vec.size(); i++ )
{
fGainMatchOffsetVec.push_back(0.0);
fGainMatchOffsetVec.push_back(1.0);
}
fhasGainMatch = true;
}
void TXPConfig::SetGainMatchForIndex( int index, double_t offset, double_t slope )
{
if( fhasGainMatch == false )
CreateEmptyGainMatch();
fGainMatchOffsetVec[index] = offset;
fGainMatchSlopeVec[index] = slope;
}
void TXPConfig::deleteGainMatchForIndex( int index )
{
if( fhasGainMatch == false )
{
CreateEmptyGainMatch();
return;
}
fGainMatchOffsetVec[index] = 0.0;
fGainMatchSlopeVec[index] = 1.0;
}
/* == Processing functions == */
EvntPacket::Singles* TXPConfig::Leaf2Singles( TTreeReaderArray<int32_t> &Q,
TTreeReaderArray<int16_t> &adc, TTreeReaderArray<int16_t> &timeStamp,
int multiplicity)
{
EvntPacket::Singles* OutPacket = new EvntPacket::Singles();
for( int i =0; i < multiplicity; i++ )
{
if( Q[i] > 32760 ) // Skip overflow
continue;
if( Q[i] < 2 ) // skip underflow
continue;
if( isVito( adc[i] ) ) // skip vito
continue;
OutPacket->Energy.push_back( GetEnergy( Q[i], adc[i] ) );
OutPacket->index.push_back( adc[i] );
if( fhasGeometry == true )
{
OutPacket->detectorNum.push_back( getDetNum( adc[i] ) );
OutPacket->crystNum.push_back( getCrystNum( adc[i] ) );
}
OutPacket->timeStamp.push_back( timeStamp[i] );
}
OutPacket->multiplicity = OutPacket->Energy.size();
return OutPacket;
}
EvntPacket::Addback* TXPConfig::Leaf2Addback( TTreeReaderArray<int32_t> &Q,
TTreeReaderArray<int16_t> &adc, TTreeReaderArray<int16_t> &timeStamp,
int multiplicity)
{
// Events in an event packet are grouped together if they are from the
// same detector and the time difference between them is less than dT
int16_t dT = 80; // 10ns, coincidence dT
EvntPacket::Addback* OutPacket = new EvntPacket::Addback();
OutPacket->multiplicity = 0;
// BGO information
std::vector<int16_t> adcBGOVec;
std::vector<int16_t> timeStampBGOVec;
bool inPacket = false; // Set true if partial event is already in packet
for( int i = 0; i < multiplicity; i++ )
{
inPacket = false;
if( Q[i] > 32760 ) // Skip overflow
continue;
if( Q[i] < 2 ) // Skip underflow
continue;
// BGO Vitoing:
// Collect all the BGO events here. After the Addback packet is
// computed, the packet will need to be checked against the BGO
// list.
//
// BGOs which match the clover number will set isCompton to true
int ggBGOdT = 300; // ns
if( isBGO( adc[i] ) )
{
adcBGOVec.push_back( adc[i] );
timeStampBGOVec.push_back( timeStamp[i] );
}
if( isVito( adc[i] ) ) // skip vito
continue;
for( int j = 0; j < OutPacket->multiplicity; j++ )
{
// Loop over unprocess events and compare with the already
// processed list for events with the same clover number
// and if they are incoincidence, group them together
if( getDetNum( adc[i] ) == OutPacket->detectorNum[j] )
if( abs(timeStamp[i] - OutPacket->timeStamp[j] ) < dT )
{
OutPacket->Energy[j] += GetEnergy( Q[i], adc[i] );
OutPacket->groupedHitsNum[j] += 1;
inPacket = true;
}
}
// Add the event as a new starting point if it isn't already
// inside an event packet.
if( inPacket == false )
{
if( fIndex2Cryst[adc[i]] == -1 ) // Skip for unknown detectors
continue;
OutPacket->Energy.push_back( GetEnergy( Q[i], adc[i] ) );
OutPacket->detectorNum.push_back( getDetNum( adc[i] ) );
OutPacket->timeStamp.push_back( timeStamp[i] );
OutPacket->multiplicity++;
OutPacket->groupedHitsNum.push_back( 1 ); // first hit
OutPacket->isCompton.push_back(false);
}
// Events which match BGO detector and are in coincidence
// are likely compton events scattered out of the clover.
for( int i = 0; i < OutPacket->multiplicity; i++ )
for( int j = 0; j < adcBGOVec.size(); j++ )
{
if( OutPacket->detectorNum[i] ==
getDetNum( adcBGOVec[j] ) )
if( abs( OutPacket->timeStamp[i] -
timeStampBGOVec[j] ) < ggBGOdT )
OutPacket->isCompton[i] = true;
}
}
return OutPacket;
}