50int main(
int argc,
char* argv[])
52 TApplication theApp(
"Analysis", &argc, argv);
55 std::ifstream filelist(
"/home/mbowry/Documents/ROOT/Backup/newfilelist_hs.txt");
56 std::ifstream ODBlist(
"/home/mbowry/Documents/ROOT/Backup/ODBlist_hs.txt");
57 const char* fname =
"viewscaler.root";
58 const char* hname =
"random_counts.txt";
59 const char* iname =
"combined_counts.txt";
60 const char* jname =
"random_seed.txt";
61 const char* kname =
"RESULTS.txt";
62 const char* lname =
"asymmetric_error.txt";
63 const char* mname =
"random_deadtime.txt";
64 const char* nname =
"accepted_rand.txt";
66 std::array<int, 9> addr = {0x0000, 0x000d, 0x0101, 0x0107, 0x020b,
67 0x020c, 0x0302, 0x030c, 0x030d};
68 std::array<double, 4> freq = {2.e3, 5.e3, 1.e4, 2.e4};
77 std::array<int, 4> tdiff = {0, 0, 0, 0};
78 int* td = tdiff.data();
80 double* q = freq.data();
85 auto* line =
new char[len];
86 auto* odb =
new char[len];
88 if(!(filelist.is_open())) {
89 std::cerr <<
"Failed to open filelist. Check path. " << std::endl;
91 }
else if(!(ODBlist.is_open())) {
92 std::cerr <<
"Failed to open ODBlist. Check path. " << std::endl;
103 const char* starttime =
"Start time binary";
104 const char* stoptime =
"Stop time binary";
111 int line_count = std::count(
112 std::istreambuf_iterator<char>(filelist), std::istreambuf_iterator<char>(),
'\n');
113 printf(
DMAGENTA "-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/" RESET_COLOR
115 printf(
DGREEN "Started Deadtime v1 / 04-Mar-2016 / mbowry@triumf.ca" RESET_COLOR "\n");
116 printf(
DBLUE "Calculates deadtime on a channel-by-channel basis using precision scaler data" RESET_COLOR "\n");
117 printf(
DBLUE "Sub-runs must be merged (e.g. using gadd) before running this program" RESET_COLOR "\n");
118 printf(
DBLUE "A list of fragments (run files) and their corresponding ODB files must be provided" RESET_COLOR "\n");
121 printf(
DBLUE "Deadtime results are recorded in %s" RESET_COLOR "\n", kname);
122 printf(
DMAGENTA "-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/" RESET_COLOR
127 filelist.seekg(0, std::ios::beg);
131 while(counter < line_count) {
132 filelist.getline(line, len,
'\n');
133 const char* filename = line;
135 ODBlist.getline(odb, len,
'\n');
136 std::ifstream InFile(odb);
137 while(InFile.good()) {
138 getline(InFile, odbline);
139 posa = odbline.find(starttime);
140 posb = odbline.find(stoptime);
141 if(posa != std::string::npos && odbline.size() >
static_cast<size_t>(sub)) {
142 odbline = odbline.substr(sub);
143 tstart = std::stoi(odbline,
nullptr, 10);
144 }
else if(posb != std::string::npos && odbline.size() >
static_cast<size_t>(sub - 1)) {
145 odbline = odbline.substr((sub - 1));
146 tend = std::stoi(odbline,
nullptr, 10);
149 runtime = tend - tstart;
150 nppg = floor(runtime / patlen);
151 tdiff[counter] = runtime;
154 printf(
DBLUE "Read ODB %s : run time = %i seconds / %i transitions" RESET_COLOR "\n", odb, runtime, nppg);
155 }
else if(runtime > 600) {
156 printf(
DBLUE "Read ODB %s : run time = %i minutes / %i transitions" RESET_COLOR "\n", odb, (runtime / 60),
160 for(
int index = scaleri; index < (scalerf + 1); index++) {
162 printf(
DMAGENTA "Creation of histograms suppressed .. performing analysis step only." RESET_COLOR "\n");
164 MakeSpectra(filename, counter, fname, nsclr, ncycle, q, p, index, td, thresh);
180 nscaler = nds / counter;
184 DoAnalysis(fname, line_count, q, nsclr, patlen, ncycle, td, eor, hname, iname, jname, kname, lname, mname, nname,
293void DoAnalysis(
const char*& fname,
int& nfile,
double* rate,
int& nsclr,
int& patlen,
int&,
int* trun,
double& eor,
294 const char*& hname,
const char*& iname,
const char*& jname,
const char*& kname,
const char*& lname,
295 const char*& mname,
const char*& nname,
int& nscaler)
298 auto* vs =
new TFile(fname,
"read");
300 ofile.open(
"diagnostic.txt");
301 FILE* random = fopen(hname,
"w");
302 FILE* combine = fopen(iname,
"w");
303 FILE* rng = fopen(jname,
"w");
304 FILE* deadtime = fopen(kname,
"w");
305 FILE* asymerror = fopen(lname,
"w");
306 FILE* randdt = fopen(mname,
"w");
307 FILE* randw = fopen(nname,
"w");
312 std::array<int, 2> ppgstat = {0, 0};
314 time_t timer = time(
nullptr);
318 TIter next(f.GetListOfKeys());
320 auto** spec =
new TH1D*[nfile * (nsc * nscaler)];
325 std::array<double, 8> rp1 = {-15, 15, -15, 15, 0, 20, 85, 115};
326 std::array<double, 8> rp2 = {-15, 15, -15, 15, 0, 20, 85, 115};
327 std::array<double, 8> rp3 = {-15, 15, -15, 15, 0, 20, 170, 230};
328 std::array<double, 8> rp4 = {-15, 15, -15, 15, 0, 20, 200, 300};
329 double* lowrtau = rp1.data();
330 double* upprtau = &rp1[1];
336 fprintf(deadtime,
"#np=pulser,nr=source,nrp=source+pulser,tau=deadtime,erb=FWHM est. err,erf=full range "
337 "err,erp=standard err propagation");
338 fprintf(deadtime,
"\n");
339 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",
"+/-",
340 "nrp",
"+",
"-",
"tau",
"+erb",
"-erb",
"+erf",
"-erf",
"+/-erp");
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",
"#",
"-",
"kHz",
"kHz",
"kHz",
"kHz",
343 "kHz",
"kHz",
"us",
"us",
"us",
"us",
"us",
"us");
344 fprintf(deadtime,
"\n");
345 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",
347 fprintf(randdt,
"\n");
349 while((key =
dynamic_cast<TKey*
>(next())) !=
nullptr) {
350 int numpat = floor(*trun / patlen);
351 const char* sname = key->GetName();
352 spec[cnt] =
dynamic_cast<TH1D*
>(vs->Get(sname));
390 auto** ppg =
new int*[numpat];
391 for(
int i = 0; i < numpat; ++i) {
398 auto** trans =
new double*[xbins];
399 for(
int i = 0; i < xbins; ++i) {
400 trans[i] =
new double[3];
402 auto** freq =
new double*[fbin];
403 for(
int i = 0; i < fbin; ++i) {
404 freq[i] =
new double[2];
406 auto** bnd =
new int*[numpat];
407 for(
int i = 0; i < numpat; ++i) {
411 for(
int i = 0; i < numpat; i++) {
418 for(
int i = 0; i <= xbins; i++) {
419 x = spec[cnt]->GetBinContent(i);
420 if(w > 0 && x > 0 && x > w) {
421 diff = ((x - w) / w);
423 }
else if(w > 0 && x > 0 && x < w) {
424 diff = ((w - x) / x);
426 }
else if(x > 0 && w == 0) {
429 for(
int j = (i - 1); j >= 0; j--) {
430 z = spec[cnt]->GetBinContent(j);
432 diff = ((x - z) / z);
437 diff = ((z - x) / x);
443 }
else if(w > 0 && x == 0) {
446 for(
int j = (i + 1); j <= *trun; j++) {
447 z = spec[cnt]->GetBinContent(j);
454 for(
int j = *trun; j >= (*trun -
static_cast<int>(0.7 * patlen)); j--) {
455 y = spec[cnt]->GetBinContent(j);
457 dz = (abs(y - v) / v);
458 if(dz > 0 && dv > 0) {
467 v = spec[cnt]->GetBinContent(j);
477 }
else if(x == 0 && w == 0) {
483 w = spec[cnt]->GetBinContent(i);
484 if(abs(diff) > rmax) {
488 rmax = rmax + (0.1 * rmax);
490 for(
int i = 0; i <= xbins; i++) {
491 for(
int j = 0; j < fbin; j++) {
493 freq[j][0] = (0. + j * (rmax / fbin));
496 if(abs(trans[i][1]) > (0. + j * (rmax / fbin)) &&
497 abs(trans[i][1]) <= (rmax / fbin) + (j * (rmax / fbin))) {
498 freq[j][1] = freq[j][1] + 1;
503 for(
int j = 0; j < fbin; j++) {
504 if(freq[j][1] > max) {
506 dlim = (2 * ((rmax / fbin) + (j * (rmax / fbin))));
510 for(
int i = 0; i <= xbins; i++) {
511 if(abs(trans[i][1]) > dlim) {
514 printf(
DRED "Warning: Found more PPG transition(s) than expected in spectrum %s (%i/%i)" RESET_COLOR
522 bnd[nt - 1][1] =
static_cast<int>(trans[i][2]);
526 printf(
DGREEN "Found correct number of PPG transitions in spectrum %s (%i/%i)" RESET_COLOR "\n", sname, nt,
529 }
else if(nt < numpat) {
530 printf(
DRED "Warning: Found too few PPG transitions in spectrum %s (%i/%i)" RESET_COLOR "\n", sname, nt,
537 for(
int i = 0; i < numpat; i++) {
538 ppg[i + 1][0] = bnd[i][0];
539 ppg[i][1] = (bnd[i][0]) - 1;
544 }
else if(bnd[0][1] == 1) {
550 for(
int i = 0; i < numpat; i++) {
551 ofile << ppg[i][0] <<
"\t" << ppg[i][1] << std::endl;
553 ofile <<
"/" << std::endl;
557 for(
int i = sref; i < numpat; i += 2) {
558 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
559 x = spec[cnt]->GetBinContent(j);
566 rrand = (wrand / nrand);
568 int lim1 =
static_cast<int>(rrand - (0.5 * dlim * rrand));
569 int lim2 =
static_cast<int>(rrand + (0.5 * dlim * rrand));
570 int dsbin = (lim2 - lim1) / 20;
571 auto** dspec =
new int*[dsbin];
572 for(
int i = 0; i < dsbin; ++i) {
573 dspec[i] =
new int[2];
575 for(
int i = 0; i < dsbin; i++) {
576 dspec[i][0] = lim1 + (i * ((lim2 - lim1) / dsbin));
580 for(
int i = sref; i < numpat; i += 2) {
581 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
582 x = spec[cnt]->GetBinContent(j);
584 for(
int k = 0; k < dsbin; k++) {
585 if(x >= dspec[k][0] && x < dspec[k + 1][0]) {
591 sum = sum + pow((x - rrand), 2);
595 sdrand = sqrt(sum / (nrand - 1));
598 for(
int i = 0; i < dsbin; i++) {
599 fprintf(random,
"%i \t %i", dspec[i][0], dspec[i][1]);
600 fprintf(random,
"\n");
602 fprintf(random,
"/");
603 fprintf(random,
"\n");
608 for(
int i = pref; i < numpat; i += 2) {
609 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
610 x = spec[cnt]->GetBinContent(j);
619 rcomb = (wcomb / ncomb);
621 for(
int i = pref; i < numpat; i += 2) {
622 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
623 x = spec[cnt]->GetBinContent(j);
625 maxl = (0.5 * (pow((x - rcomb), 2))) / x;
635 rcmin = spec[cnt]->GetBinContent(minb);
639 for(
int i = pref; i < numpat; i += 2) {
640 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
641 x = spec[cnt]->GetBinContent(j);
643 maxl = (0.5 * (pow((x - rcmin), 2))) / x;
645 if(maxl <= (minl + 0.5) && x < rcmin && x < lbd) {
648 if(maxl <= (minl + 0.5) && x > rcmin && x > ubd) {
657 lim1 =
static_cast<int>(lbd - (2.0 * abs(rcmin - lbd)));
658 lim2 =
static_cast<int>(ubd + (2.0 * abs(rcmin - ubd)));
660 dsbin = (lim2 - lim1) / 20;
661 for(
int i = 0; i < dsbin; i++) {
662 dspec[i][0] = lim1 + (i * ((lim2 - lim1) / dsbin));
666 for(
int i = pref; i < numpat; i += 2) {
667 for(
int j = (ppg[i][0] + chop); j <= (ppg[i][1] - chop); j++) {
668 x = spec[cnt]->GetBinContent(j);
670 for(
int k = 0; k < dsbin; k++) {
671 if(x >= dspec[k][0] && x < dspec[k + 1][0]) {
677 sumc = sumc + pow((x - rcomb), 2);
681 sdcomb = sqrt(sumc / (ncomb - 1));
684 for(
int i = 0; i < dsbin; i++) {
685 fprintf(combine,
"%i \t %i", dspec[i][0], dspec[i][1]);
686 fprintf(combine,
"\n");
688 fprintf(combine,
"/");
689 fprintf(combine,
"\n");
696 double a1 = abs(rcmin - ubd);
697 double a2 = abs(rcmin - lbd);
698 double maxa = std::max(a1, a2);
699 double uhi = (1.5 * maxa);
701 double du = (uhi - ulo) / 1e3;
708 int nrow =
static_cast<int>((uhi - ulo) / du);
709 double w2 = pow((pow(sdrand, 2)), 2) / ((2. * pow(sdrand, 2)));
711 auto** array =
new double*[nrow];
712 for(
int i = 0; i < nrow; ++i) {
713 array[i] =
new double[2];
724 x1 = (u * w1) / (w1 + w2);
725 x2 = (u * w2) / (w1 + w2);
727 w1 = pow(((a1 * a2) + ((a1 - a2) * x1)), 2) / ((2. * (a1 * a2)) + ((a1 - a2) * x1));
728 lnl = -0.5 * ((pow(x1, 2) / ((a1 * a2) + ((a1 - a2) * x1))) + (pow(x2, 2) / (pow(sdrand, 2))));
730 if(itr > 0 && lnl > minl) {
743 sigm = array[umin][0];
744 sigp = array[umin][0];
745 for(
int i = 1; i < nrow; i++) {
746 if(array[i][1] >= (minl - 0.5) && array[i][0] < array[umin][0] && array[i][0] < sigm) {
749 if(array[i][1] >= (minl - 0.5) && array[i][0] > array[umin][0] && array[i][0] > sigp) {
755 fprintf(asymerror,
"%s\t %s\t %s",
"#chan",
"Err[Hz]",
"Ln(L)");
756 fprintf(asymerror,
"\n");
757 for(
int i = 1; i < nrow; i++) {
758 fprintf(asymerror,
"%i \t %.4E \t %.4E", cnt % nsc, array[i][0], array[i][1]);
759 fprintf(asymerror,
"\n");
761 fprintf(asymerror,
"/");
762 fprintf(asymerror,
"\n");
766 double tau = ((1.0 / rrand) * (1.0 - sqrt((rcmin - rrand) / (*rate)))) * 1.0e6;
786 int wsize =
static_cast<int>(pow(wbin, 2));
787 auto** randcheck =
new double*[bin];
788 for(
int i = 0; i < bin; ++i) {
789 randcheck[i] =
new double[2];
791 auto** randtau =
new double*[iter];
792 for(
int i = 0; i < iter; ++i) {
793 randtau[i] =
new double[2];
795 auto** wspec =
new double*[wsize];
796 for(
int i = 0; i < wsize; ++i) {
797 wspec[i] =
new double[3];
802 for(
int i = 0; i < bin; i++) {
803 randcheck[i][0] = 0 + (
static_cast<double>(i) * (1 /
static_cast<double>(bin)));
807 for(
int i = 0; i < wbin; i++) {
808 for(
int j = 0; j < wbin; j++) {
809 wspec[wrow][0] = ((rcmin - rrand) + sigm) + (
static_cast<double>(i) * ((sigp - sigm) /
static_cast<double>(wbin)));
811 wspec[wrow][1] = (*lowrtau) + (
static_cast<double>(j) * ((*upprtau - *lowrtau) /
static_cast<double>(wbin)));
818 while(i < (nrow - binsize)) {
819 if(array[i][0] >= sigm && array[i][0] <= sigp) {
820 l1 = ((rcmin - rrand) + array[i][0]);
821 l2 = ((rcmin - rrand) + array[i + binsize][0]);
823 for(
int j = 0; j < iter; j++) {
824 tempa = rand() /
static_cast<double>(RAND_MAX);
825 var1 = ((tempa * (a1 + a2)) + (rcmin - a2));
826 tempb = rand() /
static_cast<double>(RAND_MAX);
827 var2 = ((tempb * (2.e0 * sdrand)) + (rrand - sdrand));
828 var3 = (var1 - var2);
831 if(var3 >= l1 && var3 <= l2) {
833 rtau = ((1.0 / var2) * (1.0 - sqrt(var3 / (*rate)))) * 1.0e6;
835 for(
int k = 0; k < wsize; k++) {
836 if(var3 >= wspec[k][0] && var3 < wspec[k + wbin][0] && rtau >= wspec[k][1] &&
837 rtau < wspec[k + 1][1]) {
868 for(i = 0; i < wsize; i++) {
869 if(wspec[i][2] > gmax) {
875 for(i = 0; i < wsize; i++) {
876 if(wspec[i][1] > srmax && wspec[i][2] > hmax) {
879 if(wspec[i][1] < srmin && wspec[i][2] > hmax) {
884 for(i = 0; i < wsize; i++) {
885 if(wspec[i][1] > frmax && wspec[i][2] > 0) {
888 if(wspec[i][1] < frmin && wspec[i][2] > 0) {
893 erbm = abs(tau - srmin);
894 erbp = abs(tau - srmax);
895 erfm = abs(tau - frmin);
896 erfp = abs(tau - frmax);
898 double p1 = sqrt(pow(sdcomb, 2) + pow(sdrand, 2));
899 double p1r = p1 / (rcmin - rrand);
900 double p2 = (rcmin - rrand) / (*rate);
903 double p3 = (0.5 * p1r * p2);
904 double eprop = tau * (sqrt(pow((p3 / (1 - sqrt(p2))), 2) + pow((sdrand / rrand), 2)));
907 fprintf(randdt,
"%i \t %.2f \t %.2f \t %.2f \t %.2f", cnt, srmin, srmax, frmin, frmax);
909 fprintf(randdt,
"\n");
913 for(i = 0; i < wsize; i++) {
914 fprintf(randw,
"%.2f \t %.2f \t %.2f", wspec[i][0], wspec[i][1], wspec[i][2]);
915 fprintf(randw,
"\n");
917 fprintf(randw,
"//end channel 0");
918 fprintf(randw,
"\n");
931 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,
932 (cnt % nsc), ((*rate) / 1e3), (rrand / 1e3), (sdrand / 1e3), (rcmin / 1e3), (abs(rcmin - ubd) / 1e3),
933 (abs(rcmin - lbd) / 1e3), tau, erbp, erbm, erfp, erfm, eprop);
934 fprintf(deadtime,
"\n");
938 if(cnt % (nsc * nscaler) == 0) {
943 if(cnt % (nsc) == 0) {
944 for(i = 0; i < 2; i++) {
949 if(cnt % (nsc) == 0 && cnt % (nsc * nscaler) == 0) {
954 }
else if(cflag == 2) {
957 }
else if(cflag == 3) {
973 int good = ppgstat[0];
977 printf(
DRED "Correct # of PPG transitions obtained for %i out of %i spectra" RESET_COLOR "\n", good, cnt);
979 printf(
DBLUE "Correct # of PPG transitions obtained for %i out of %i spectra" RESET_COLOR "\n", good, cnt);