-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCA_data.cxx
1787 lines (1682 loc) · 65.5 KB
/
MCA_data.cxx
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// started by C. Krauss in 2008
// updated to include calibration and analysis in 2012
//
// Compare spectra
// database of background lines
#include "sstream"
#include "math.h"
#include "TMath.h"
#include "MCA_data.h"
#include "TCanvas.h"
#include "TRegexp.h"
#include "TFile.h"
#include "TLegend.h"
#include "TF1.h"
#include "iostream"
#include "fstream"
#include "iomanip"
#include "TSpectrum.h"
#include "TString.h"
#include "TPolyMarker.h"
#include "TGraphErrors.h"
using namespace std;
// define reference times for source strength
#define JAN2011 1293904800 // (Jan 1 2011, 12:00 MT)
#define NOV2010 1288634400 // (Nov 1 2010, 12:00 MT)
#define APR1974 134049600 // (Apr 1 1974, 12:00 GMT)
#define DAYSINYEAR 365.2425
#define SECONDSINDAY (3600.0*24.0)
#define SECONDSINYEAR (DAYSINYEAR*SECONDSINDAY)
ClassImp(MCA_data);
ClassImp(CalMCA_data);
ClassImp(GeCalibrate);
ClassImp(SourceData);
MCA_data::MCA_data(const char *name):TNamed(name, "MCA data"){
hdata = 0;
};
MCA_data::~MCA_data(){
// if (hdata!=0)
// delete hdata;
};
int MCA_data::Read(char *filename){
SetName(filename);
char* ext = strrchr(filename,'.');
if (ext !=0){
if (!strcmp(ext,".CNF")||!strcmp(ext,".cnf"))
Read_CNF(filename);
else
if (!strcmp(ext,".IEC")||!strcmp(ext,".iec"))
Read_IEC(filename);
else {
cout << "File format unknown as input type." << endl;
return -1;
}
}
else{
cout << " filename needs to have an extension." << endl;
return -1;
}
return 0;
}
int MCA_data::Read_CNF(char *filename){
static int instance = 0;
instance++;
// char basename[300];
// char outfilename[300];
soutfilename = new char[300];
sshortname = new char[300];
strncpy(sshortname,filename,300);
char *ext = strrchr(sshortname,'.');
if (!strcmp(ext,".CNF")||!strcmp(ext,".cnf")){
ext[0] = 0;
cout<< " opening CNF file: "<<sshortname<<endl;
strcpy(soutfilename,sshortname);
strncpy(soutfilename+strlen(soutfilename),".root",10);
}
if (strrchr(sshortname,'/')){
strncpy(sshortname,strrchr(sshortname,'/')+1,strlen(sshortname)-(sshortname-strrchr(sshortname,'/')));
}
SetName(sshortname);
ifstream inf(filename,ios::binary);
unsigned short header[8];
inf.read((char*)header,16);
// header[3] = (header[3] & 0xF) >> 4 |(header[3]&0xF0)>>4| (header[3]&0xF00)<<4 | (header[3]&0xF000)>>4;
cout << " 1: " << header[0] << " 2: " << header[1] << " 3: " << header[2] << endl;
cout << " 4: " << header[3] << " 5: " << header[4] << " 6: " << header[5] << endl;
cout << " 7: " << header[6] << " 8: " << header[7] << endl;
// int channels = header[3];
int channels = 0;
if (header[5] == 38400){
cout << " 32768 channels in file !?" << endl;
if (header[6]==2)
channels = 32768;
if (header[6]==1)
channels = 32768/2;
}
if (header[5] == 37888){
cout << " 32768 channels in file !?" << endl;
channels = 32768;
}
if (header[5] == 39936){
cout << " 32768 channels in file !?" << endl;
channels = 32768;
}
if (header[5] == 5632){
cout << " 8192 channels in file !?" << endl;
channels = 32768/4;// 8k file
header[5] = 38400;
}
if (channels == 0){
cerr << " unkonwn file type! aborting." << endl;
exit(0);
}
// if (header[1] == 4 && header[2] == 1028) {
// channels = 32768/4.;
// header[5] = 38400;
// }
// if (header[1] == 0 && header[2] == 1024) {
// channels = 32768;
// }
cout << " number of channels: " << channels << " " << " data offset: " << header[5]<< endl;
// int channel = 32768;
int date_offs = 0xb07; // Current version of CNF file has date and time info start here, time is consecutive
int realt_offs = 0xb0f;
int livet_offs = 0xb17;
inf.seekg(date_offs);
unsigned long long date, livet, realt;
inf.read((char*)&date,8);
double ddate = double(date)/1e7;
double realdate = ddate - 3506691600; // date in CNF file is in seconds since 1858, Nov 16, 7:00 GMT, the offset to unix time is given here. (time in Mountain time)
// realdate -= 2* 3600; // convert to eastern time
time_t real_date_t = (time_t(realdate));
SetStartTime(real_date_t);
char *time_c = ctime(&real_date_t);
cout << " converted, extracted time: " << time_c;
char *tmp_c = strchr(time_c,'\n');
tmp_c[0]= '\0';
inf.read((char*)&realt,8);
inf.read((char*)&livet,8);
double drealt = double(~(realt)+1)/1e7; // Calculate 2's complement and convert to seconds...
SetRealTime(drealt);
double dlivet = double(~(livet)+1)/1e7;
SetLiveTime(dlivet);
SetChannels(channels);
cout << " data taking time (real): " << setprecision(10)<< drealt << "s, (live): " << dlivet << "s" << endl;
inf.seekg(header[5]);
unsigned int mydata[channels];
inf.read((char*)mydata,channels*4);
char title[400];
sprintf(title,"MCA Data, %s, %7.2lf s live time",time_c, GetLiveTime());
char hn[400];
sprintf(hn,"data_%d",instance-1);
SetData(new TH1I(hn,title, GetChannels(),0,GetChannels()+1));
int entries = 0;
for (int i = 0 ; i<channels ; i++){
GetData()->SetBinContent(1+i,mydata[i]);
entries += mydata[i];
}
GetData()->SetEntries(entries);
inf.close();
// TFile yo(outfilename,"recreate");
// h->Write();
// yo.Close();
cout << " Done, success." << endl;
return 1;
}
int MCA_data::Read_IEC(char *filename){
static int instance = 0;
instance++;
char buffer[1000];
ifstream inf(filename);
if (!inf.is_open()) {
cout << " file " << filename << " not found " << endl;
return 0;
}
// char outname[300],shortname[300];
soutfilename = new char[300];
sshortname = new char[300];
if (strrchr(filename,'.')!=0){
strncpy(sshortname,filename,strrchr(filename,'.')-filename);
sshortname[strrchr(filename,'.')-filename] = '\0';
// cout << "DEBUG: " << sshortname << " " << filename<< " " << strchr(filename,'.')-filename<< endl;
}
else
strncpy(sshortname,filename,strlen(filename));
// cout << " DEBUG: " << sshortname << endl;
sprintf(soutfilename,"%s.root",sshortname);
// TFile *yo = new TFile(soutfilename,"recreate");
// remove leading path
if (strrchr(sshortname,'/')){
strncpy(sshortname,strrchr(sshortname,'/')+1,strlen(sshortname)-(sshortname-strrchr(sshortname,'/')));
}
SetName(sshortname);
// MCA_data mydata(sshortname);
// first line contains system information,
inf.getline(buffer,999);
// second line contains live time, real time and channel number
inf.getline(buffer,999);
char tmp[50], *tmp2;
strncpy(tmp,buffer+4,14);
tmp[14] = '\0';
SetLiveTime(strtod(tmp,&tmp2));
strncpy(tmp,buffer+18,14);
tmp[14] = '\0';
SetRealTime(strtod(tmp,&tmp2));
strncpy(tmp,buffer+32,strlen(buffer)-32);
SetChannels(atoi(tmp));
// third line contains start time and date
inf.getline(buffer,999);
struct tm tdate;
tdate.tm_mday = atoi(buffer+4); // day
tdate.tm_mon = atoi(buffer+7)-1; // month
tdate.tm_year = atoi(buffer+10)+100; // year
tdate.tm_hour = atoi(buffer+13); //hour
tdate.tm_min = atoi(buffer+16); // minutes
tdate.tm_sec = atoi(buffer+19); //seconds
SetStartTime(mktime(&tdate));
time_t tmp_t = GetStartTime();
char *time_c = ctime(&tmp_t);
char *tmp_c = strchr(time_c,'\n');
tmp_c[0]= '\0';
cout << " got time: " << time_c << endl;
// fourth line contains energy calibration (unused here)
inf.getline(buffer,999);
cout << " got channels: " << GetChannels() << endl;
sprintf(buffer,"MCA Data, %s, %7.2lf s live time",time_c, GetLiveTime());
char hn[400];
sprintf(hn,"data_%d",instance-1);
SetData(new TH1I(hn,buffer, GetChannels(),0,GetChannels()+1));
bool found = false;
while (!inf.eof()){
inf.getline(buffer,999);
// cout << buffer << endl;
if (!strncmp(buffer,"A004USERDEFINED",15)) {
found = true;
break;
}
}
if (found){
// data entries start here
int data[5];
int channel = 0;
int entries = 0;
while (!inf.eof() && channel+5<GetChannels()){
inf.getline(buffer,999);
// cout << buffer << endl;
sscanf(buffer+4,"%d %d %d %d %d %d",&channel,&data[0],&data[1],&data[2],&data[3],&data[4]);
GetData()->SetBinContent(channel+1,data[0]);
GetData()->SetBinContent(channel+2,data[1]);
GetData()->SetBinContent(channel+3,data[2]);
GetData()->SetBinContent(channel+4,data[3]);
GetData()->SetBinContent(channel+5,data[4]);
entries += data[0] + data[1] + data[2] + data[3] + data[4];
if (inf.eof()) break;
}
GetData()->SetEntries(entries);
// mydata.Write();
cout << " done, success." << endl;
}
else{
cout << " data elements not found, no output." << endl;
return 0;
}
// yo->Close();
return 1;
}
TH1D* MCA_data::GetNormalizedData(){
hdata->Sumw2();
TH1D* rv = new TH1D("data_norm",hdata->GetTitle(),hdata->GetNbinsX(),hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
for (int i = 0 ; i < hdata->GetNbinsX()+2 ; i++){ // include under- and overflow bins.
rv->SetBinContent(i,hdata->GetBinContent(i)/dreal_time);
rv->SetBinError(i,hdata->GetBinError(i)/dreal_time);
}
rv->SetEntries(double(hdata->GetEntries()));
return rv;
}
void SourceData::InitCo60(TString filename, bool old){
// Data from http://nucleardata.nuclear.lu.se/nucleardata/toi/nuclide.asp?iZA=270060
//Eg (keV) Ig (%) Decay mode
//346.93 7 0.0076 5 b-
//826.06 3 0.0076 8 b-
//1173.237 4 99.9736 7 b-
//1332.501 5 99.9856 4 b-
//2158.57 10 0.00111 18 b-
//2505 2.0E-6 4 b-
vdGammaEnergy.push_back(346.93);
vdLineIntensity.push_back(0.0076);
vdGammaEnergy.push_back(826.06);
vdLineIntensity.push_back(0.0076);
vdGammaEnergy.push_back(1173.237);
vdLineIntensity.push_back(99.9736);
vdGammaEnergy.push_back(1332.501);
vdLineIntensity.push_back(99.9856);
vdGammaEnergy.push_back(2158.57);
vdLineIntensity.push_back(0.00111);
vdGammaEnergy.push_back(2505.692);
vdLineIntensity.push_back(2.0E-6);
if (old){
dSourceStrength = 0.39701; // source stength in MBq (10.73uCi)
tReferenceDate = APR1974;
}
else{
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
}
dHalfLife = 1925.28*SECONDSINDAY; // in seconds, 1925.28d (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Co60");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitAm241(TString filename, bool old){
// Data from http://nucleardata.nuclear.lu.se/nucleardata/toi/nuclide.asp?iZA=95024
// list truncated, only 60keV line implemented...
//Eg (keV) Ig (%) Decay mode
//13.81 2 a
//26.3448 2 2.40 2 a
//27.03 a
//31.4 a
//32.183 0.0174 4 a
//33.1964 3 0.126 3 a
//38.54 3 a
//42.73 5 0.0055 11 a
//43.423 10 0.073 8 a
//51.01 3 0.000026 12 a
//54.0 a
//55.56 2 0.0181 18 a
//56.8 a
//57.85 5 0.0052 15 a
//59.5412 2 35.9 4 a
//61.46 a
//64.83 2 0.000145 18 a
//67.45 5 0.00042 10 a
//69.76 3 0.0029 4 a
//75.8 2 ~0.0006 a
//78.1 a
vdGammaEnergy.push_back(59.5412);
vdLineIntensity.push_back(39.9);
if (old){
dSourceStrength = 0.43216; // source stength in MBq (11.68uCi)
tReferenceDate = APR1974;
}
else {
cerr << " new Am241 Source does not exist in our inventory." << endl;
exit(0);
}
dHalfLife = 432.6*SECONDSINYEAR; // in seconds, 432.6y (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Am241");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitCo57(TString filename){
//Eg (keV) Ig (%) Decay mode
//14.41300 15 9.16 15 e
//122.0614 4 85.60 17 e
//136.4743 5 10.68 8 e
//230.29 2 0.0004 4 e
//339.54 18 0.0139 3 e
//352.36 1 0.0132 3 e
//366.75 1 0.0013 3 e
//569.92 4 0.017 1 e
//692.03 2 0.157 9 e
//706.40 20 0.0253 5 e
vdGammaEnergy.push_back(14.413);
vdLineIntensity.push_back(9.16);
vdGammaEnergy.push_back(122.0614);
vdLineIntensity.push_back(85.6);
vdGammaEnergy.push_back(136.4743);
vdLineIntensity.push_back(10.68);
vdGammaEnergy.push_back(230.29);
vdLineIntensity.push_back(0.0004);
vdGammaEnergy.push_back(339.54);
vdLineIntensity.push_back(0.0139);
vdGammaEnergy.push_back(352.36);
vdLineIntensity.push_back(0.0132);
vdGammaEnergy.push_back(366.75);
vdLineIntensity.push_back(0.0013);
vdGammaEnergy.push_back(569.92);
vdLineIntensity.push_back(0.017);
vdGammaEnergy.push_back(692.03);
vdLineIntensity.push_back(0.157);
vdGammaEnergy.push_back(706.40);
vdLineIntensity.push_back(0.0253);
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
dHalfLife = 271.74*SECONDSINDAY; // in seconds, 271.74d (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Co57");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitBa133(TString filename, bool old){
// Data from http://nucleardata.nuclear.lu.se/nucleardata/toi/nuclide.asp?iZA=560133
// Eg (keV) Ig (%) Decay mode
// 53.161 1 2.199 22 e
// 79.6139 26 2.62 6 e
// 80.9971 14 34.06 27 e
// 160.613 8 0.645 8 e
// 223.234 12 0.450 4 e
// 276.398 2 7.164 22 e
// 302.853 1 18.33 6 e
// 356.017 2 62.05 19 e
// 383.851 3 8.94 3 e
vdGammaEnergy.push_back(53.161);
vdLineIntensity.push_back(2.199);
vdGammaEnergy.push_back(79.6139);
vdLineIntensity.push_back(2.62);
vdGammaEnergy.push_back(80.9971);
vdLineIntensity.push_back(34.06);
vdGammaEnergy.push_back(160.613);
vdLineIntensity.push_back(0.645);
vdGammaEnergy.push_back(223.234);
vdLineIntensity.push_back(0.450);
vdGammaEnergy.push_back(276.394);
vdLineIntensity.push_back(7.164);
vdGammaEnergy.push_back(302.853);
vdLineIntensity.push_back(18.33);
vdGammaEnergy.push_back(356.017);
vdLineIntensity.push_back(62.05);
vdGammaEnergy.push_back(383.851);
vdLineIntensity.push_back(8.94);
if (old){
tReferenceDate = APR1974;
dSourceStrength = 0.39627; // source stength in MBq (10.71uCi)
}
else{
tReferenceDate = JAN2011;
dSourceStrength = 0.037; // source stength in MBq
}
dHalfLife = 10.551*SECONDSINYEAR; // in seconds, 10.551y (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Ba133");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
bool SourceData::GetFile(){
if (sFileName.Contains(".root")){
TFile y(sFileName);
if (y.IsZombie()) cerr<< "File is zombie!" << endl;
sFileName.Resize(sFileName.Last('.'));
if (sFileName.Last('/')!=0)
sFileName.Replace(0,1+sFileName.Last('/'),0,0);
// cout << sFileName << endl;
MCA_data *pdata = (MCA_data*) y.Get(sFileName);
if (pdata != 0){
data = *pdata;
return true;
}
}
else{
if (sFileName.Contains(".IEC") || sFileName.Contains(".iec")){
MCA_data *pdata = new MCA_data();
char *fn = new char[300];
strncpy(fn,sFileName.Data(),300);
if (pdata->Read_IEC(fn)!=0){
data = *pdata;//small memory leak here. ignore.
return true;
}
}
if (sFileName.Contains(".CNF") || sFileName.Contains(".cnf")){
MCA_data *pdata = new MCA_data();
char *fn = new char[300];
strncpy(fn,sFileName.Data(),300);
if (pdata->Read_CNF(fn)!=0){
data = *pdata;//small memory leak here. ignore.
return true;
}
}
}
return false;
}
void SourceData::InitMn54(TString filename){
// Data from http://nucleardata.nuclear.lu.se/nucleardata/toi/nuclide.asp?iZA=250054
//Eg (keV) Ig (%) Decay mode
//834.848 3 99.976 1 e+b+
vdGammaEnergy.push_back(834.848);
vdLineIntensity.push_back(99.976);
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
dHalfLife = 312.12*SECONDSINDAY; // in seconds, 312.12d (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Mn54");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitCs137(TString filename, bool old){
//Eg (keV) Ig (%) Decay mode
//283.53 4 0.00058 8 b-
//661.657 3 85.1 2 b-
vdGammaEnergy.push_back(283.53);
vdLineIntensity.push_back(0.00058);
vdGammaEnergy.push_back(661.657);
vdLineIntensity.push_back(85.1);
if (old){
dSourceStrength = 0.41033; // source stength in MBq (11.09uCi)
tReferenceDate = APR1974;
}
else{
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
}
dHalfLife = 30.8*SECONDSINYEAR; // in seconds, 30.8y (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Cs137");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitCd109(TString filename){
//Eg (keV) Ig (%) Decay mode
//88.04 5 3.61 10 e
vdGammaEnergy.push_back(88.04);
vdLineIntensity.push_back(3.61);
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
dHalfLife = 461.4*SECONDSINDAY; // in seconds, 461.4 days (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Cd109");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitHg203(TString filename, bool old){
//Eg (keV) Ig (%) Decay mode
// 279.1952 10 81.56 % (NNDC, Dec 2012)
vdGammaEnergy.push_back(279.1952);
vdLineIntensity.push_back(81.56);
if (old){
dSourceStrength = 0.43438; // source strength in MBq (11.74uCi)
tReferenceDate = APR1974;
}
else{
cerr << " New Hg203 source does not exist in our inventory" << endl;
exit(0);
}
dHalfLife = 46.594*SECONDSINDAY; // in seconds, 46.594 days (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Hg203");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitSr90(TString filename){
cerr << " Not implemented. " << endl;
}
void SourceData::InitTl204(TString filename){
//XR l 9.99 0.81 % 3 8.1E-5 3
//XR kα2 68.894 0.469 % 15 3.23E-4 10
//XR kα1 70.818 0.789 % 24 5.59E-4 17
//XR kβ3 79.824 0.095 % 3 7.60E-5 24
//XR kβ1 80.225 0.182 % 6 1.46E-4 5
//XR kβ2 82.473 0.0659 % 21 5.44E-5 17
// from NNDC database....
vdGammaEnergy.push_back(9.99);
vdLineIntensity.push_back(0.810);
vdGammaEnergy.push_back(68.894);
vdLineIntensity.push_back(0.469);
vdGammaEnergy.push_back(70.818);
vdLineIntensity.push_back(0.789);
vdGammaEnergy.push_back(79.824);
vdLineIntensity.push_back(0.095);
vdGammaEnergy.push_back(80.225);
vdLineIntensity.push_back(0.182);
vdGammaEnergy.push_back(82.473);
vdLineIntensity.push_back(0.0659);
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = NOV2010;
dHalfLife = 3.783*365.2425*SECONDSINDAY; // in seconds, 3.783y (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Tl204");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitZn65(TString filename){
//344.95 20 0.0030 3 e+b+
//770.6 2 0.0030 3 e+b+
//1115.546 4 50.60 24 e+b+
vdGammaEnergy.push_back(344.95);
vdLineIntensity.push_back(0.0030);
vdGammaEnergy.push_back(770.6);
vdLineIntensity.push_back(0.0030);
vdGammaEnergy.push_back(1115.546);
vdLineIntensity.push_back(50.60);
dSourceStrength = 0.037; // source stength in MBq
tReferenceDate = JAN2011;
dHalfLife = 243.93*SECONDSINDAY; // in seconds, 243.93d (NNDC)
for (int i = 0 ; i<vdGammaEnergy.size(); i++)
vdChannelNumber.push_back(0);
sFileName = filename;
sIsotopeName = TString("Zn65");
if (!GetFile() ) cerr << " File not opened. Error" <<endl;
}
void SourceData::InitFe55(TString filename){
cerr << " Not implemented. " << endl;
}
void GeCalibrate::Init(vector<TString> filenames){
for (vector<TString>::iterator i=filenames.begin() ; i!=filenames.end();i++){
cout << "File: " << *i << endl;
// file must start with isotope name:
if ((*i).Contains("Co-60")){
cout << "found Co60 file" << endl;
SourceData *d = new SourceData();
if ((*i).Contains("old"))
d->InitCo60(*i, true);
else
d->InitCo60(*i);
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+6);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Hg-203")){
cout << "found Hg203 file" << endl;
SourceData *d = new SourceData();
d->InitHg203(*i, (*i).Contains("old"));
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+6);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Am-241")){
cout << "found Am241 file" << endl;
SourceData *d = new SourceData();
if ((*i).Contains("old"))
d->InitAm241(*i, true);
else
d->InitAm241(*i);
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+6);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Mn-54")){
cout << "found Mn54 file" << endl;
SourceData *d = new SourceData();
d->InitMn54(*i);
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+6);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Ba-133")){
cout << "found Ba133 file" << endl;
SourceData *d = new SourceData();
if ((*i).Contains("old"))
d->InitBa133(*i, true);
else
d->InitBa133(*i);
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+7);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Zn-65")){
cout << "found Zn65 file" << endl;
SourceData *d = new SourceData();
d->InitZn65(*i);
vpData.push_back(d);
char tmp[60];
strcpy(tmp,(*i).Data()+6);
char* t2 = strrchr(tmp,'.');
t2[0] = 0;
cal_date = TString(tmp);
}
if ((*i).Contains("Cd-109")){
cout << "found Cd109 file" << endl;
SourceData *d = new SourceData();
d->InitCd109(*i);
vpData.push_back(d);
}
if ((*i).Contains("Co-57")){
cout << "found Co-57 file" << endl;
SourceData *d = new SourceData();
d->InitCo57(*i);
vpData.push_back(d);
}
if ((*i).Contains("Cs-137")){
cout << "found Cs-137 file" << endl;
SourceData *d = new SourceData();
if ((*i).Contains("old"))
d->InitCs137(*i, true);
else
d->InitCs137(*i);
vpData.push_back(d);
}
if ((*i).Contains("Tl-204")){
cout << "found Tl-204 file" << endl;
SourceData *d = new SourceData();
d->InitTl204(*i);
vpData.push_back(d);
}
if ((*i).Contains("Fe-55")){
cout << "found Fe-55 file" << endl;
SourceData *d = new SourceData();
d->InitFe55(*i);
vpData.push_back(d);
}
}
if (vpData.size()==0){
cerr<< " Error: no source run data found. Aborting." << endl;
exit(0);
}
if (vpData.size()<filenames.size()){
cerr<< " Error: some source run data not found. Aborting. Check file name format (has to start with NN-xxx, e.g. Mn-54 or Ba-133)! " << endl;
exit(0);
}
}
CalMCA_data::CalMCA_data(MCA_data &dat) : MCA_data(dat) {
hCalData=0;
}
void CalMCA_data::Analyse(){
// ...
TCanvas *cc = new TCanvas("cc","Spectra");
cc->Divide(1,2);
cc->cd(1);
hCalData->Draw();
TSpectrum *s = new TSpectrum(160);
TH1D *b = (TH1D*) s->Background(hCalData,20,"Compton");
TH1D* d = (TH1D*)hCalData->Clone("diff");
d->Add(b,-1);
d->Rebin(16);
s->Search(d,10);
s->Search(hCalData,10);
hCalData->Draw();
b->Draw("same");
gPad->SetLogy(1);
cc->cd(2);
d->Draw();
TList *functions = hCalData->GetListOfFunctions();
TPolyMarker *pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
double *X;
double *Y;
X= pm->GetX();
Y= pm->GetY();
for (int i = 0 ; i<pm->GetN() ; i++){
cout << " lines: " << i << " " << X[i] << " " << Y[i] << endl;
}
}
CalMCA_data* GeCalibrate::ApplyCalibration(TString filename){
// Calculate livetime corrected and calibrated histogram from raw (root) datafile.
if (!valid_calibration) {
cerr << "calibration in file is not valid!" << endl;
exit(0);
}
MCA_data *pdata = 0;
if (filename.Contains(".root")){
TFile y(filename);
if (y.IsZombie()) {
cerr<< "File is zombie!" << endl;
exit(0);
}
filename.Resize(filename.Last('.'));
if (filename.Last('/')!=0)
filename.Replace(0,1+filename.Last('/'),0,0);
// cout << sFileName << endl;
pdata = (MCA_data*) y.Get(filename);
}
else{
if (filename.Contains(".IEC") || filename.Contains(".iec")){
pdata = new MCA_data(filename.Data());
char *fn = new char[200];
strncpy(fn,filename.Data(),filename.Length());
if (pdata->Read_IEC(fn)==0){
cerr << " File " << filename << " could not be read."<< endl;
exit(0);
}
}
if (filename.Contains(".CNF") || filename.Contains(".cnf")){
pdata = new MCA_data(filename.Data());
char *fn = new char[200];
strncpy(fn,filename.Data(),filename.Length());
if (pdata->Read_CNF(fn)==0){
cerr << " File " << filename << " could not be read."<< endl;
exit(0);
}
}
}
CalMCA_data *caldat = 0;
if (pdata != 0){
// got the raw data...
TH1D *hist = pdata->GetNormalizedData();
// time corrected data
caldat = new CalMCA_data(*pdata);
int bins = hist->GetNbinsX();
double bmin = GetEnergyfromChannel(hist->GetXaxis()->GetXmin());
double bmax = GetEnergyfromChannel(hist->GetXaxis()->GetXmax());
if (bmin==bmax) return 0;
caldat->SetCalData(new TH1D("cal_data",hist->GetTitle(),bins,bmin,bmax));
for (int i = 0; i < bins+2 ; i++){
caldat->GetCalData()->SetBinContent(i,hist->GetBinContent(i));
caldat->GetCalData()->SetBinError(i,hist->GetBinError(i));
}
caldat->GetCalData()->SetEntries(hist->GetEntries());
TString outfilename(filename+TString("_cal.root"));
cout << " Trying to write file to : " << outfilename << endl;
TFile yo(outfilename,"recreate");
caldat->Write();
yo.Close();
}
return caldat;
}
void GeCalibrate::Calibrate(){
int num_peaks[vpData.size()];
int alllines = 0;
TCanvas *c[vpData.size()];
for (int source = 0 ; source < vpData.size() ; source++){
char idn[20],name[300];
sprintf(idn,"can_%d",source);
sprintf(name,"Spectrum %s",vpData[source]->GetIsotopeName().Data());
c[source] = new TCanvas(idn,name);
TSpectrum ts(vpData[source]->GetNumLines(),2);
ts.SetResolution(6);
int peaks = ts.Search(vpData[source]->GetData()->GetData(),6,"",0.01);
vpData[source]->GetData()->GetData()->Draw();
cout << peaks << " peaks found for source " << vpData[source]->GetIsotopeName()<< endl;
num_peaks[source] = peaks;
alllines += num_peaks[source];
}
cout << " using " << alllines << " lines for calibration " << endl;
int min_peaks = 1000;
int i_min = -1;
for (int i = 0 ; i<vpData.size();i++){
if (num_peaks[i]<min_peaks) {min_peaks = num_peaks[i];i_min = i;}
}
SourceData *firstI = vpData[i_min];// get isotope data
cout << "Starting calibration with " << firstI->GetIsotopeName() << endl;
float max_intensity = 0;
int max_line = -1;
for (int ii = 0; ii < firstI->GetNumLines() ;ii++){//loop over all lines to find largest intensity
if (firstI->GetIntensity(ii)>max_intensity){
max_intensity = firstI->GetIntensity(ii);
max_line = ii;
}
}
MCA_data* firstD = firstI->GetData();// get measured spectrum
TH1I *firstH = firstD->GetData(); // get histogram
firstH->Draw();
TList *functions = firstH->GetListOfFunctions();
TPolyMarker *pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
double *X;
double *Y;
X= pm->GetX();
Y= pm->GetY();
float dmax_intensity = 0;
int max_marker = -1;
for (int i = 0 ; i < pm->GetN(); i++){
cout << i << " " << X[i] << " " << Y[i] << endl;
if (Y[i]>dmax_intensity) {
dmax_intensity = Y[i];
max_marker = i;
}
}
cout << " marker " << max_marker << " " << max_line << endl;
if (max_marker>=0 && max_line >= 0){// found match..
if (firstI->GetChannelNumber(max_line)>0) cout << "DOUBLE ASSIGNMENT OF LINE. Fix me."<< endl;
else
firstI->SetChannelNumber(max_line,X[max_marker]);
}
// now calculate two point estimate for calibration trough 0,0
double initialoffset = 0;
double initialslope = firstI->GetEnergy(max_line)/firstI->GetChannelNumber(max_line);
cout << "initial guess: E = 0 + " << initialslope << " * ch " << endl;
int line = 0;
double energies[alllines], intensities[alllines];
double channels[alllines], peakh[alllines];
double ch_e[alllines], rel_int[alllines];
double eff[alllines], eff_err[alllines];
// energies[line] = firstI->GetEnergy(max_line);
// channels[line] = firstI->GetChannelNumber(max_line);
// ch_e[line] = sqrt(channels[line]);
// line++;
// now process all other lines, but only when the points are close to the initial calibration line...
for (int isotope = 0; isotope<vpData.size();isotope++){
SourceData* source = vpData[isotope];
cout << " *****************************************************************"<< endl;
cout << source->GetIsotopeName() << endl;
MCA_data* dat = source ->GetData();// get measured spectrum
TH1I *hist = dat->GetData(); // get histogram
hist->Draw();
functions = hist->GetListOfFunctions();
pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
X= pm->GetX();
Y= pm->GetY();
bool used[pm->GetN()];
for (int i = 0; i < pm->GetN() ; i++){ used[i] = false;}// initialize array
for (int iline = 0 ; iline < source->GetNumLines() ; iline++){// loop over all lines
if (source->GetEnergy(iline) < 20) continue;
cout << " processing line " << iline <<" " << source->GetEnergy(iline)<<"keV Intensity: "<< source->GetIntensity(iline)<< endl;
// calculate the channel range for this line:
double ch = source->GetEnergy(iline) * 1/initialslope;
double min_ch = ch - (12 + 18*ch/1000);
double max_ch = ch + (12 + 18*ch/1000);
cout << " Search window: " << min_ch << " " << max_ch << endl;
if (iline==0){
char newtitle[300];
sprintf (newtitle,"%s(%d) %s", source->GetIsotopeName().Data(),source->GetNumLines(),hist->GetTitle());
hist->SetTitle(newtitle);
}
// if (isotope == i_min && iline == max_line) continue;//already used this line for initial guess
double intensity = 0;
bool match = false;
for (int i = 0 ; i < pm->GetN(); i++){//loop over all markers in the spectrum
if (X[i]<max_ch && X[i]>min_ch) {
intensity = Y[i];
// match
match = true;
if (used[i] == true){
cout << "DOUBLE ASSIGNMENT OF MARKER. FIX ME." << endl;
// if low intensity line got picked up previously
if (iline>1 && source->GetIntensity(iline) > source->GetIntensity(iline-1))
line--;
else
continue;
cout << " fixed!" << endl;
}
used[i] = true;
energies[line] = source->GetEnergy(iline);
intensities[line] = source->GetIntensity(iline);
double fit_min = X[i] - (12 + 18*ch/10000);
double fit_max = X[i] + (12 + 18*ch/10000);
double fwhm = FindFWHM(hist, X[i], Y[i]);
double area = fwhm/2.35482 * Y[i] * sqrt(2.0*TMath::Pi());
double delta_t = dat->GetStartTime() - source->GetReferenceDate();
double exp_rate = dat->GetLiveTime()*source->GetSourceStrength()*1e6*exp(-delta_t*log(2)/source->GetHalfLife())*source->GetIntensity(iline)/100.;
double efficiency = area/exp_rate;
if (efficiency<1) {
eff_err[line] = efficiency * 1/sqrt(area);
eff[line] = efficiency;
}
else{