35 #include "TApplication.h" 44 void MakeSpectra(
const char*& filename,
int& prog,
const char*& fname,
int& nsclr,
int& ncycle,
double* rate,
45 int* channel,
int& index,
int* trun,
double& thresh);
47 void DoAnalysis(
const char*& fname,
int& nfile,
double* rate,
int& nsclr,
int& patlen,
int& ncycle,
int* trun,
48 double& eor,
const char*& hname,
const char*& iname,
const char*& jname,
const char*& kname,
49 const char*& lname,
const char*& mname,
const char*& nname,
int& nscaler);
53 TApplication theApp(
"Analysis", &argc,
argv);
56 std::ifstream filelist(
"/home/mbowry/Documents/ROOT/Backup/newfilelist_hs.txt");
57 std::ifstream ODBlist(
"/home/mbowry/Documents/ROOT/Backup/ODBlist_hs.txt");
58 const char* fname =
"viewscaler.root";
59 const char* hname =
"random_counts.txt";
60 const char* iname =
"combined_counts.txt";
61 const char* jname =
"random_seed.txt";
62 const char* kname =
"RESULTS.txt";
63 const char* lname =
"asymmetric_error.txt";
64 const char* mname =
"random_deadtime.txt";
65 const char* nname =
"accepted_rand.txt";
67 int addr[9] = {0x0000, 0x000d, 0x0101, 0x0107, 0x020b,
68 0x020c, 0x0302, 0x030c, 0x030d};
69 double freq[4] = {2.e3, 5.e3, 1.e4, 2.e4};
78 int tdiff[4] = {0, 0, 0, 0};
86 auto* line =
new char[len];
87 auto* odb =
new char[len];
89 if(!(filelist.is_open())) {
90 std::cerr<<
"Failed to open filelist. Check path. "<<std::endl;
92 }
else if(!(ODBlist.is_open())) {
93 std::cerr<<
"Failed to open ODBlist. Check path. "<<std::endl;
104 const char* starttime =
"Start time binary";
105 const char* stoptime =
"Stop time binary";
112 int line_count = std::count(
113 std::istreambuf_iterator<char>(filelist), std::istreambuf_iterator<char>(),
'\n');
114 printf(
DMAGENTA "-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/" RESET_COLOR 116 printf(
DGREEN "Started Deadtime v1 / 04-Mar-2016 / mbowry@triumf.ca" RESET_COLOR "\n");
117 printf(
DBLUE "Calculates deadtime on a channel-by-channel basis using precision scaler data" RESET_COLOR "\n");
118 printf(
DBLUE "Sub-runs must be merged (e.g. using gadd) before running this program" RESET_COLOR "\n");
119 printf(
DBLUE "A list of fragments (run files) and their corresponding ODB files must be provided" RESET_COLOR "\n");
122 printf(
DBLUE "Deadtime results are recorded in %s" RESET_COLOR "\n", kname);
123 printf(
DMAGENTA "-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/" RESET_COLOR 128 filelist.seekg(0, std::ios::beg);
132 while(counter < line_count) {
133 filelist.getline(line, len,
'\n');
134 const char* filename = line;
136 ODBlist.getline(odb, len,
'\n');
137 std::ifstream InFile(odb);
138 while(InFile.good()) {
139 getline(InFile, odbline);
140 posa = odbline.find(starttime);
141 posb = odbline.find(stoptime);
142 if(posa != std::string::npos && odbline.size() >
static_cast<size_t>(sub)) {
143 odbline = odbline.substr(sub);
144 tstart = std::stoi(odbline,
nullptr, 10);
145 }
else if(posb != std::string::npos && odbline.size() >
static_cast<size_t>(sub - 1)) {
146 odbline = odbline.substr((sub - 1));
147 tend = std::stoi(odbline,
nullptr, 10);
150 runtime = tend - tstart;
151 nppg = floor(runtime / patlen);
152 tdiff[counter] = runtime;
155 printf(
DBLUE "Read ODB %s : run time = %i seconds / %i transitions" RESET_COLOR "\n", odb, runtime, nppg);
156 }
else if(runtime > 600) {
157 printf(
DBLUE "Read ODB %s : run time = %i minutes / %i transitions" RESET_COLOR "\n", odb, (runtime / 60),
161 for(
int index = scaleri; index < (scalerf + 1); index++) {
163 printf(
DMAGENTA "Creation of histograms suppressed .. performing analysis step only." RESET_COLOR "\n");
165 MakeSpectra(filename, counter, fname, nsclr, ncycle, q, p, index, td, thresh);
181 nscaler = nds / counter;
185 DoAnalysis(fname, line_count, q, nsclr, patlen, ncycle, td, eor, hname, iname, jname, kname, lname, mname, nname,
197 printf(
DBLUE "Addresses with both source and pulser inputs:" RESET_COLOR "\n");
198 for(
int i = 0; i < 10; ++i) {
205 void MakeSpectra(
const char*& filename,
int& prog,
const char*& fname,
int& nsclr,
int& ncycle,
double*,
int* channel,
206 int& index,
int* trun,
double&)
211 auto** grif =
new TH1D*[nsc];
216 auto* rf =
new TFile(filename,
"read");
217 TTree* maple =
dynamic_cast<TTree*
>(rf->Get(
"ScalerTree"));
219 int nofBins = *trun / ncycle;
230 new TH1D(Form(
"grif%d_0x%04x_%d", prog, *channel, index),
231 Form(
"Address 0x%04x, scaler %i vs time in cycle; time [s]; counts/%d s", *channel, index, ncycle),
239 maple->SetBranchAddress(
"TScalerData", &scaler);
240 Long64_t nentries = maple->GetEntries();
245 if(prog == 0 && index == 0) {
246 vs =
new TFile(fname,
"recreate");
248 vs =
new TFile(fname,
"update");
251 for(
int i = 0; i < nsc; i++) {
255 for(Long64_t e = 0; e < nentries; e++) {
257 if(scaler->
GetAddress() ==
static_cast<UInt_t
>(*channel)) {
260 if(prev != 0 && prev < scaler->GetScaler(index) && (xaxis - xpast) <= (
double(ncycle) + clk)) {
261 yaxis = (scaler->
GetScaler(index) - prev);
262 grif[k]->Fill(xaxis, yaxis);
273 printf(
DGREEN "Created histograms for scaler %i : file = %s" RESET_COLOR "\n", index, filename);
275 for(
int i = 0; i < nsc; i++) {
290 printf(
DBLUE "ROOT file %s opens with no problems.." RESET_COLOR "\n", fname);
295 void DoAnalysis(
const char*& fname,
int& nfile,
double* rate,
int& nsclr,
int& patlen,
int&,
int* trun,
double& eor,
296 const char*& hname,
const char*& iname,
const char*& jname,
const char*& kname,
const char*& lname,
297 const char*& mname,
const char*& nname,
int& nscaler)
300 auto* vs =
new TFile(fname,
"read");
302 ofile.open(
"diagnostic.txt");
303 FILE* random = fopen(hname,
"w");
304 FILE* combine = fopen(iname,
"w");
305 FILE* rng = fopen(jname,
"w");
306 FILE* deadtime = fopen(kname,
"w");
307 FILE* asymerror = fopen(lname,
"w");
308 FILE* randdt = fopen(mname,
"w");
309 FILE* randw = fopen(nname,
"w");
314 int ppgstat[2] = {0, 0};
317 timer = time(
nullptr);
321 TIter next(f.GetListOfKeys());
323 auto** spec =
new TH1D*[nfile * (nsc * nscaler)];
328 double rp1[8] = {-15, 15, -15, 15, 0, 20, 85, 115};
329 double rp2[8] = {-15, 15, -15, 15, 0, 20, 85, 115};
330 double rp3[8] = {-15, 15, -15, 15, 0, 20, 170, 230};
331 double rp4[8] = {-15, 15, -15, 15, 0, 20, 200, 300};
332 double* lowrtau = &rp1[0];
333 double* upprtau = &rp1[1];
339 fprintf(deadtime,
"#np=pulser,nr=source,nrp=source+pulser,tau=deadtime,erb=FWHM est. err,erf=full range " 340 "err,erp=standard err propagation");
341 fprintf(deadtime,
"\n");
342 fprintf(deadtime,
"%s\t\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"#spec",
"chan",
"np",
"nr",
"+/-",
343 "nrp",
"+",
"-",
"tau",
"+erb",
"-erb",
"+erf",
"-erf",
"+/-erp");
344 fprintf(deadtime,
"\n");
345 fprintf(deadtime,
"%s\t\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"#",
"-",
"kHz",
"kHz",
"kHz",
"kHz",
346 "kHz",
"kHz",
"us",
"us",
"us",
"us",
"us",
"us");
347 fprintf(deadtime,
"\n");
348 fprintf(randdt,
"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
"#rc",
"rr",
"rc-rr",
"rtau",
"lim1",
"lim2",
"flag",
350 fprintf(randdt,
"\n");
352 while((key = dynamic_cast<TKey*>(next())) !=
nullptr) {
353 int numpat = floor(*trun / patlen);
354 const char* sname = key->GetName();
355 spec[cnt] =
dynamic_cast<TH1D*
>(vs->Get(sname));
366 double sum = 0, sumc = 0;
392 auto** ppg =
new int*[numpat];
393 for(
int i = 0; i < numpat; ++i) {
400 auto** trans =
new double*[xbins];
401 for(
int i = 0; i < xbins; ++i) {
402 trans[i] =
new double[3];
404 auto** freq =
new double*[fbin];
405 for(
int i = 0; i < fbin; ++i) {
406 freq[i] =
new double[2];
408 auto** bnd =
new int*[numpat];
409 for(
int i = 0; i < numpat; ++i) {
413 for(
int i = 0; i < numpat; i++) {
420 for(
int i = 0; i <= xbins; i++) {
421 x = spec[cnt]->GetBinContent(i);
422 if(w > 0 && x > 0 && x > w) {
423 diff = ((x - w) / w);
425 }
else if(w > 0 && x > 0 && x < w) {
426 diff = ((w - x) / x);
428 }
else if(x > 0 && w == 0) {
431 for(
int j = (i - 1); j >= 0; j--) {
432 z = spec[cnt]->GetBinContent(j);
434 diff = ((x - z) / z);
437 }
else if(z > 0 && x < z) {
438 diff = ((z - x) / x);
444 }
else if(w > 0 && x == 0) {
447 for(
int j = (i + 1); j <= *trun; j++) {
448 z = spec[cnt]->GetBinContent(j);
455 for(
int j = *trun; j >= (*trun - (int(0.7 * patlen))); j--) {
456 y = spec[cnt]->GetBinContent(j);
458 dz = (abs(y - v) / v);
459 if(dz > 0 && dv > 0) {
468 v = spec[cnt]->GetBinContent(j);
478 }
else if(x == 0 && w == 0) {
484 w = spec[cnt]->GetBinContent(i);
485 if(abs(diff) > rmax) {
489 rmax = rmax + (0.1 * rmax);
491 for(
int i = 0; i <= xbins; i++) {
492 for(
int j = 0; j < fbin; j++) {
494 freq[j][0] = (0. + (double(j) * (rmax / double(fbin))));
497 if(abs(trans[i][1]) > (0. + (double(j) * (rmax / double(fbin)))) &&
498 abs(trans[i][1]) <= (rmax / double(fbin)) + (
double(j) * (rmax / double(fbin)))) {
499 freq[j][1] = freq[j][1] + 1;
504 for(
int j = 0; j < fbin; j++) {
505 if(freq[j][1] > max) {
507 dlim = (2 * ((rmax / double(fbin)) + (
double(j) * (rmax / double(fbin)))));
511 for(
int i = 0; i <= xbins; i++) {
512 if(abs(trans[i][1]) > dlim) {
515 printf(
DRED "Warning: Found more PPG transition(s) than expected in spectrum %s (%i/%i)" RESET_COLOR 523 bnd[nt - 1][1] = trans[i][2];
528 printf(
DGREEN "Found correct number of PPG transitions in spectrum %s (%i/%i)" RESET_COLOR "\n", sname, nt,
531 }
else if(nt < numpat) {
532 printf(
DRED "Warning: Found too few PPG transitions in spectrum %s (%i/%i)" RESET_COLOR "\n", sname, nt,
539 for(
int i = 0; i < numpat; i++) {
540 ppg[i + 1][0] = bnd[i][0];
541 ppg[i][1] = (bnd[i][0]) - 1;
546 }
else if(bnd[0][1] == 1) {
552 for(
int i = 0; i < numpat; i++) {
553 ofile<<ppg[i][0]<<
"\t"<<ppg[i][1]<<std::endl;
555 ofile<<
"/"<<std::endl;
559 for(
int i = sref; i < numpat; i += 2) {
560 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
561 x = spec[cnt]->GetBinContent(j);
568 rrand = (wrand / nrand);
570 int lim1 = rrand - (0.5 * dlim * rrand);
571 int lim2 = rrand + (0.5 * dlim * rrand);
572 int dsbin = (lim2 - lim1) / 20;
573 auto** dspec =
new int*[dsbin];
574 for(
int i = 0; i < dsbin; ++i) {
575 dspec[i] =
new int[2];
577 for(
int i = 0; i < dsbin; i++) {
578 dspec[i][0] = lim1 + (i * ((lim2 - lim1) / dsbin));
582 for(
int i = sref; i < numpat; i += 2) {
583 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
584 x = spec[cnt]->GetBinContent(j);
586 for(
int k = 0; k < dsbin; k++) {
587 if(x >= dspec[k][0] && x < dspec[k + 1][0]) {
593 sum = sum + pow((x - rrand), 2);
597 sdrand = sqrt(sum / (nrand - 1));
600 for(
int i = 0; i < dsbin; i++) {
601 fprintf(random,
"%i \t %i", dspec[i][0], dspec[i][1]);
602 fprintf(random,
"\n");
604 fprintf(random,
"/");
605 fprintf(random,
"\n");
610 for(
int i = pref; i < numpat; i += 2) {
611 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
612 x = spec[cnt]->GetBinContent(j);
621 rcomb = (wcomb / ncomb);
623 for(
int i = pref; i < numpat; i += 2) {
624 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
625 x = spec[cnt]->GetBinContent(j);
627 maxl = (0.5 * (pow((x - rcomb), 2))) / x;
637 rcmin = spec[cnt]->GetBinContent(minb);
641 for(
int i = pref; i < numpat; i += 2) {
642 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
643 x = spec[cnt]->GetBinContent(j);
645 maxl = (0.5 * (pow((x - rcmin), 2))) / x;
647 if(maxl <= (minl + 0.5) && x < rcmin && x < lbd) {
650 if(maxl <= (minl + 0.5) && x > rcmin && x > ubd) {
659 lim1 = lbd - (2.0 * abs(rcmin - lbd));
660 lim2 = ubd + (2.0 * abs(rcmin - ubd));
662 dsbin = (lim2 - lim1) / 20;
663 for(
int i = 0; i < dsbin; i++) {
664 dspec[i][0] = lim1 + (i * ((lim2 - lim1) / dsbin));
668 for(
int i = pref; i < numpat; i += 2) {
669 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
670 x = spec[cnt]->GetBinContent(j);
672 for(
int k = 0; k < dsbin; k++) {
673 if(x >= dspec[k][0] && x < dspec[k + 1][0]) {
679 sumc = sumc + pow((x - rcomb), 2);
683 sdcomb = sqrt(sumc / (ncomb - 1));
686 for(
int i = 0; i < dsbin; i++) {
687 fprintf(combine,
"%i \t %i", dspec[i][0], dspec[i][1]);
688 fprintf(combine,
"\n");
690 fprintf(combine,
"/");
691 fprintf(combine,
"\n");
698 double a1 = abs(rcmin - ubd);
699 double a2 = abs(rcmin - lbd);
700 double maxa = std::max(a1, a2);
701 double uhi = (1.5 * maxa);
703 double du = (uhi - ulo) / 1e3;
710 int nrow = int((uhi - ulo) / du);
711 double w2 = pow((pow(sdrand, 2)), 2) / ((2. * pow(sdrand, 2)));
713 auto** array =
new double*[nrow];
714 for(
int i = 0; i < nrow; ++i) {
715 array[i] =
new double[2];
726 x1 = (u * w1) / (w1 + w2);
727 x2 = (u * w2) / (w1 + w2);
729 w1 = pow(((a1 * a2) + ((a1 - a2) * x1)), 2) / ((2. * (a1 * a2)) + ((a1 - a2) * x1));
730 lnl = -0.5 * ((pow(x1, 2) / ((a1 * a2) + ((a1 - a2) * x1))) + (pow(x2, 2) / (pow(sdrand, 2))));
732 if(itr > 0 && lnl > minl) {
745 sigm = array[umin][0];
746 sigp = array[umin][0];
747 for(
int i = 1; i < nrow; i++) {
748 if(array[i][1] >= (minl - 0.5) && array[i][0] < array[umin][0] && array[i][0] < sigm) {
751 if(array[i][1] >= (minl - 0.5) && array[i][0] > array[umin][0] && array[i][0] > sigp) {
757 fprintf(asymerror,
"%s\t %s\t %s",
"#chan",
"Err[Hz]",
"Ln(L)");
758 fprintf(asymerror,
"\n");
759 for(
int i = 1; i < nrow; i++) {
760 fprintf(asymerror,
"%i \t %.4E \t %.4E", cnt % nsc, array[i][0], array[i][1]);
761 fprintf(asymerror,
"\n");
763 fprintf(asymerror,
"/");
764 fprintf(asymerror,
"\n");
768 double tau = ((1.0 / rrand) * (1.0 - sqrt((rcmin - rrand) / (*rate)))) * 1.0e6;
777 int iter = 1e4, bin = 10;
779 double var1, var2, var3;
782 int wrow = 0, wsize = int(pow(wbin, 2));
783 auto** randcheck =
new double*[bin];
784 for(
int i = 0; i < bin; ++i) {
785 randcheck[i] =
new double[2];
787 auto** randtau =
new double*[iter];
788 for(
int i = 0; i < iter; ++i) {
789 randtau[i] =
new double[2];
791 auto** wspec =
new double*[wsize];
792 for(
int i = 0; i < wsize; ++i) {
793 wspec[i] =
new double[3];
799 for(
int i = 0; i < bin; i++) {
800 randcheck[i][0] = 0 + (double(i) * (1 / double(bin)));
804 for(
int i = 0; i < wbin; i++) {
805 for(
int j = 0; j < wbin; j++) {
806 wspec[wrow][0] = ((rcmin - rrand) + sigm) + (double(i) * ((sigp - sigm) /
double(wbin)));
808 wspec[wrow][1] = (*lowrtau) + (double(j) * ((*upprtau - *lowrtau) /
double(wbin)));
815 while(i < (nrow - binsize)) {
816 if(array[i][0] >= sigm && array[i][0] <= sigp) {
817 l1 = ((rcmin - rrand) + array[i][0]);
818 l2 = ((rcmin - rrand) + array[i + binsize][0]);
820 for(
int j = 0; j < iter; j++) {
821 tempa = rand() / double(RAND_MAX);
822 var1 = ((tempa * (a1 + a2)) + (rcmin - a2));
823 tempb = rand() / double(RAND_MAX);
824 var2 = ((tempb * (2.e0 * sdrand)) + (rrand - sdrand));
825 var3 = (var1 - var2);
828 if(var3 >= l1 && var3 <= l2) {
831 rtau = ((1.0 / var2) * (1.0 - sqrt(var3 / (*rate)))) * 1.0e6;
833 for(
int k = 0; k < wsize; k++) {
834 if(var3 >= wspec[k][0] && var3 < wspec[k + wbin][0] && rtau >= wspec[k][1] &&
835 rtau < wspec[k + 1][1]) {
856 double gmax = 0, hmax = 0;
857 double erbp = 0, erbm = 0, erfp = 0, erfm = 0;
858 double frmin = 1e6, frmax = 0, srmin = 1e6, srmax = 0;
860 for(i = 0; i < wsize; i++) {
861 if(wspec[i][2] > gmax) {
867 for(i = 0; i < wsize; i++) {
868 if(wspec[i][1] > srmax && wspec[i][2] > hmax) {
871 if(wspec[i][1] < srmin && wspec[i][2] > hmax) {
876 for(i = 0; i < wsize; i++) {
877 if(wspec[i][1] > frmax && wspec[i][2] > 0) {
880 if(wspec[i][1] < frmin && wspec[i][2] > 0) {
885 erbm = abs(tau - srmin);
886 erbp = abs(tau - srmax);
887 erfm = abs(tau - frmin);
888 erfp = abs(tau - frmax);
890 double p1 = sqrt(pow(sdcomb, 2) + pow(sdrand, 2));
891 double p1r = p1 / (rcmin - rrand);
892 double p2 = (rcmin - rrand) / (*rate);
895 double p3 = (0.5 * p1r * p2);
896 double eprop = tau * (sqrt(pow((p3 / (1 - sqrt(p2))), 2) + pow((sdrand / rrand), 2)));
899 fprintf(randdt,
"%i \t %.2f \t %.2f \t %.2f \t %.2f", cnt, srmin, srmax, frmin, frmax);
901 fprintf(randdt,
"\n");
905 for(i = 0; i < wsize; i++) {
906 fprintf(randw,
"%.2f \t %.2f \t %.2f", wspec[i][0], wspec[i][1], wspec[i][2]);
907 fprintf(randw,
"\n");
909 fprintf(randw,
"//end channel 0");
910 fprintf(randw,
"\n");
923 fprintf(deadtime,
"%s\t%i\t%.1f\t%.1f\t%.2f\t%.1f\t%.2f\t%.2f\t %.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n", sname,
924 (cnt % nsc), ((*rate) / 1e3), (rrand / 1e3), (sdrand / 1e3), (rcmin / 1e3), (abs(rcmin - ubd) / 1e3),
925 (abs(rcmin - lbd) / 1e3), tau, erbp, erbm, erfp, erfm, eprop);
926 fprintf(deadtime,
"\n");
930 if(cnt % (nsc * nscaler) == 0) {
935 if(cnt % (nsc) == 0) {
936 for(i = 0; i < 2; i++) {
941 if(cnt % (nsc) == 0 && cnt % (nsc * nscaler) == 0) {
946 }
else if(cflag == 2) {
949 }
else if(cflag == 3) {
965 int good = ppgstat[0];
969 printf(
DRED "Correct # of PPG transitions obtained for %i out of %i spectra" RESET_COLOR "\n", good, cnt);
971 printf(
DBLUE "Correct # of PPG transitions obtained for %i out of %i spectra" RESET_COLOR "\n", good, cnt);
void CheckFile(const char *&fname)
std::vector< UInt_t > GetScaler() const
ULong64_t GetTimeStamp() const
void Printaddress(int *channel)
void MakeSpectra(const char *&filename, int &prog, const char *&fname, int &nsclr, int &ncycle, double *rate, int *channel, int &index, int *trun, double &thresh)
UInt_t GetAddress() const
void Clear(Option_t *opt="") override
void DoAnalysis(const char *&fname, int &nfile, double *rate, int &nsclr, int &patlen, int &ncycle, int *trun, double &eor, const char *&hname, const char *&iname, const char *&jname, const char *&kname, const char *&lname, const char *&mname, const char *&nname, int &nscaler)
int main(int argc, char *argv[])