556 if (!hin || !hout)
return 0;
558 Int_t n=hin->GetNbinsX();
568 title.Form(
"Bayesian Block representation with FPR=%-.3g",fpr);
569 hout->SetTitle(title);
601 Double_t oldoptlen=0;
610 for (Int_t i=1; i<=n; i++)
614 xup=hin->GetBinLowEdge(i)+hin->GetBinWidth(i);
617 for (Int_t j=1; j<=i; j++)
619 xlow=hin->GetBinLowEdge(j);
627 bcount=hin->Integral(j,i);
632 for (Int_t k=j; k<=i; k++)
634 yk=hin->GetBinContent(k);
635 sigk=hin->GetBinError(k);
651 if (index>0) pfit+=best.At(index-1);
675 best.SetAt(optfit,i-1);
676 last.SetAt(optj,i-1);
677 lengths.SetAt(optlen,i-1);
678 counts.SetAt(optcount,i-1);
680 if (!ntrig || optj==1)
continue;
683 oldoptj=last.At(i-2);
684 oldoptlen=lengths.At(i-2);
686 if ((
fMode==1 ||
fMode==2) && oldoptlen) oldytrig=counts.At(i-2)/oldoptlen;
687 if (
fMode==3) oldytrig=counts.At(i-2);
689 if ((
fMode==1 ||
fMode==2) && optlen) ytrig=counts.At(i-1)/optlen;
690 if (
fMode==3) ytrig=counts.At(i-1);
692 if (optj>oldoptj && ((ntrig>0 && ytrig>oldytrig) || (ntrig<0 && ytrig<oldytrig)))
695 xtrig=hin->GetBinLowEdge(optj);
699 if (ncp>=abs(ntrig))
break;
709 TArrayD xarr(ncells+1);
710 TArrayD yarr(ncells+1);
716 x=hin->GetBinLowEdge(jcp)+lengths.At(index);
720 if (lengths.At(index)) y=counts.At(index)/lengths.At(index);
722 if (
fMode==3) y=counts.At(index);
728 if (jcp==1) xarr.SetAt(hin->GetBinLowEdge(jcp),ncp);
734 Double_t* xbins=
new Double_t[ncp+1];
735 Double_t* yvals=
new Double_t[ncp+1];
737 for (Int_t i=0; i<=ncp; i++)
739 xbins[i]=xarr.At(ncp-i);
740 yvals[i]=yarr.At(ncp-i);
743 hout->SetBins(ncp,xbins);
744 for (Int_t i=1; i<=ncp; i++)
746 hout->SetBinContent(i,yvals[i]);
750 if (!ntrig) xtrig=xbins[1];
752 hout->SetLineWidth(2);
753 hout->SetLineColor(kBlue);
754 hout->SetStats(kFALSE);
758 title=
"Bayesian Block representation for histogram ";
759 title+=hin->GetName();
760 title+=
" with FPR= %-.3g";
762 str=hin->GetXaxis()->GetTitle();
763 if (str==
"") str=
"Recordings (e.g. time)";
766 str=hin->GetYaxis()->GetTitle();
767 if (str==
"") str=
"Counts";
769 str=title.Format(title.Data(),fpr);
770 hout->SetTitle(str.Data());
775 str.Form(
"Requested trigger at : %-.3g",xtrig);
777 TLegend* leg=
new TLegend(0.5,0.85,0.7,0.9,str);
778 leg->SetFillColor(0);
779 leg->SetTextColor(kBlue);
780 leg->SetTextAlign(22);
782 TList* hlist=hout->GetListOfFunctions();
834 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
842 if (n<2 || !store || dim<1 || i<1 || i>dim || fpr<0 || fpr>1)
844 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
845 cout <<
" Store Mode:" << store <<
" Entries:" << n <<
" Dimension:" << dim <<
" i:" << i <<
" fpr:" << fpr << endl;
853 Double_t* xbins=
new Double_t[n];
855 for (Int_t idx=1; idx<=n; idx++)
861 TH1F hin(
"",
"",n-1,xbins);
862 for (Int_t j=1; j<n; j++)
864 hin.SetBinContent(j,1);
867 Double_t xtrig=
GetBlocks(&hin,fpr,hout,ntrig);
871 title=
"Bayesian Block representation for NcSample ";
873 title+=
" with FPR=%-.3g";
874 title+=
";Recordings of variable ";
879 title+=
";Count rate";
880 str=title.Format(title.Data(),fpr);
881 hout->SetTitle(str.Data());
1130 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1136 if (n<2 || fpr<0 || fpr>1)
1138 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for TGraphErrors treatment." << endl;
1139 cout <<
" Entries:" << n <<
" fpr:" << fpr << endl;
1150 Double_t* xbins=
new Double_t[n+1];
1156 for (Int_t i=0; i<n; i++)
1159 err=fabs(gr.GetErrorX(i));
1163 dist=xbins[i]-xbins[i-1];
1164 if (dmin<0 || dist<dmin) dmin=dist;
1168 xbins[n]=xbins[n-1]+dmin;
1170 TH1F hin(
"",
"",n,xbins);
1171 for (Int_t j=1; j<=n; j++)
1173 gr.GetPoint(j-1,x,y);
1174 err=fabs(gr.GetErrorY(j-1));
1175 hin.SetBinContent(j,y);
1176 hin.SetBinError(j,err);
1179 Double_t xtrig=
GetBlocks(&hin,fpr,hout,ntrig);
1183 TString xtitle=
"Samplings (e.g. time)";
1184 TString ytitle=
"Measured value";
1185 TAxis* ax=gr.GetXaxis();
1189 if (str !=
"") xtitle=str;
1195 if (str !=
"") ytitle=str;
1197 title=
"Bayesian Block representation for TGraphErrors ";
1198 title+=gr.GetName();
1199 title+=
" with FPR=%-.3g;";
1203 str=title.Format(title,fpr);
1204 hout->SetTitle(str);
1400 Double_t rms=gr.GetRMS(2);
1403 Double_t err=fabs(nrms*rms);
1406 f.Form(
"%-.5g",err);
1408 Double_t xtrig=
GetBlocks(gr,f,fpr,hout,ntrig);
1411 str.Form(
" from nrms=%-.3g",fabs(nrms));
1413 TString title=hout->GetTitle();
1415 hout->SetTitle(title);
1453 cout <<
" *NcBlocks::GetBlocks* Error : Input histogram not specified." << endl;
1459 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1463 Int_t nbins=hin->GetNbinsX();
1465 if (!nbins || n<1 || n>nbins || mode<0 || mode>2)
1467 cout <<
" *NcBlocks::GetBlocks* Inconsistent input nbins=" << nbins <<
" n=" << n <<
" mode=" << mode << endl;
1477 Double_t binwidth=0;
1482 TArrayD xarr(nbins);
1483 TArrayD yarr(nbins);
1486 for (Int_t i=0; i<n; i++)
1490 if (jbin>nbins)
break;
1492 x=hin->GetBinCenter(jbin);
1493 y=hin->GetBinContent(jbin);
1494 binwidth=hin->GetBinWidth(jbin);
1495 if (i==0) xlow=hin->GetBinLowEdge(jbin);
1499 if (mode==0) average=s.
GetMean(2);
1501 if (mode==2) average=s.
GetRMS(2);
1503 xarr.SetAt(xlow,nblocks-1);
1504 yarr.SetAt(average,nblocks-1);
1509 Double_t* xbins=
new Double_t[nblocks+1];
1510 Double_t* yvals=
new Double_t[nblocks+1];
1512 for (Int_t i=0; i<nblocks; i++)
1514 xbins[i]=xarr.At(i);
1515 yvals[i]=yarr.At(i);
1518 xbins[nblocks]=(1.+1e-6)*xup;
1519 yvals[nblocks]=yvals[nblocks-1];
1521 hout->SetBins(nblocks,xbins);
1522 for (Int_t i=1; i<=nblocks; i++)
1524 hout->SetBinContent(i,yvals[i-1]);
1527 hout->SetLineWidth(2);
1528 hout->SetLineColor(kBlue);
1529 hout->SetStats(kFALSE);
1533 title=
"Block representation for histogram ";
1534 title+=hin->GetName();
1535 title+=
" grouped in %-d consecutive bins";
1537 str=hin->GetXaxis()->GetTitle();
1538 if (str==
"") str=
"Recordings (e.g. time)";
1540 if (mode==0) title+=
";Mean ";
1541 if (mode==1) title+=
";Median ";
1542 if (mode==2) title+=
";RMS ";
1543 str=hin->GetYaxis()->GetTitle();
1544 if (str==
"") str=
"Counts";
1546 str=title.Format(title.Data(),n);
1547 hout->SetTitle(str.Data());
1589 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1597 if (!store || dim<1 || i<1 || i>dim || n<1 || n>nen || mode<0 || mode>2)
1599 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
1600 cout <<
" Store Mode:" << store <<
" Entries:" << nen <<
" Dimension:" << dim <<
" i:" << i <<
" n:" << n <<
" mode:" << mode << endl;
1606 Int_t nblocks=
GetBlocks(&gr,hout,n,mode);
1610 title=
"Block representation for NcSample ";
1612 title+=
" grouped in %-d consecutive samples";
1613 title+=
";Sampling number";
1614 if (mode==0) title+=
";Mean ";
1615 if (mode==1) title+=
";Median ";
1616 if (mode==2) title+=
";RMS ";
1617 title+=
"of variable ";
1622 str=title.Format(title.Data(),n);
1623 hout->SetTitle(str.Data());
1848 cout <<
" *NcBlocks::GetBlocks* Error : Input TGraph not specified." << endl;
1854 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1858 if (n<1 || mode<0 || mode>2)
1860 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for TGraph treatment : n=" << n <<
" mode=" << mode << endl;
1864 Int_t npoints=gr->GetN();
1866 if (!npoints)
return 0;
1872 Double_t* xbins=
new Double_t[npoints+1];
1875 for (Int_t i=0; i<npoints; i++)
1877 gr->GetPoint(i,x,y);
1881 xbins[npoints]=(1.+1e-6)*xbins[npoints-1];
1883 TH1F hin(
"",
"",npoints,xbins);
1884 for (Int_t j=1; j<=npoints; j++)
1886 gr->GetPoint(j-1,x,y);
1887 hin.SetBinContent(j,y);
1890 Int_t nblocks=
GetBlocks(&hin,hout,n,mode);
1894 TString xtitle=
"Samplings (e.g. time)";
1895 TString ytitle=
"Measured value";
1896 TAxis* ax=gr->GetXaxis();
1900 if (str !=
"") xtitle=str;
1906 if (str !=
"") ytitle=str;
1908 title=
"Block representation for TGraph ";
1909 title+=gr->GetName();
1910 title+=
" grouped in %-d consecutive samples;";
1912 if (mode==0) title+=
";Mean ";
1913 if (mode==1) title+=
";Median ";
1914 if (mode==2) title+=
";RMS ";
1916 str=title.Format(title,n);
1917 hout->SetTitle(str);
1924Int_t
NcBlocks::Add(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
1960 if (hout) hout->Reset();
1962 if (!h1 || !h2 || !hout)
return 1;
1964 Int_t ndim1=h1->GetDimension();
1965 Int_t ndim2=h2->GetDimension();
1966 Int_t ndimo=hout->GetDimension();
1968 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
1970 cout <<
" *NcBlocks::Add* Error : Histograms should all be 1-dimensional." << endl;
1975 TString name=hout->GetName();
1979 TString name1=h1->GetName();
1980 if (name1==
"") name1=
"h1";
1982 TString name2=h2->GetName();
1983 if (name2==
"") name2=
"h2";
1989 sc.Form(
"%-+.3g",c);
1994 sd.Form(
"%-+.3g",d);
1996 TString title=
"Resulting histogram of: ";
2006 title+=
" (scaled w.r.t. bin size)";
2010 title+=
" (not scaled w.r.t. bin size)";
2013 TAxis* axis1=h1->GetXaxis();
2014 title+=axis1->GetTitle();
2016 axis1=h1->GetYaxis();
2017 title+=axis1->GetTitle();
2019 hout->SetName(name);
2020 hout->SetTitle(title);
2022 Int_t nb1=h1->GetNbinsX();
2023 Int_t nb2=h2->GetNbinsX();
2025 if (!nb1 || !nb2)
return 1;
2031 for (Int_t i=1; i<=nb1; i++)
2033 bwidth1=h1->GetBinWidth(i);
2034 if (i==1 || bwidth1>bwmax1)
2045 for (Int_t i=1; i<=nb2; i++)
2047 bwidth2=h2->GetBinWidth(i);
2048 if (i==1 || bwidth2<bwmin2)
2055 Double_t ratio=bwmax1/bwmin2;
2058 printf(
" *NcBlocks::Add* Error : Larger bin size encountered in histogram %-s than in %-s \n",name1.Data(),name2.Data());
2059 printf(
" %-s: binsize=%-g for bin=%-i %-s: binsize=%-g for bin=%-i \n",name1.Data(),bwmax1,imax1,name2.Data(),bwmin2,imin2);
2069 TAxis* axis2=h2->GetXaxis();
2070 for (Int_t i1=1; i1<=nb1; i1++)
2072 x1=h1->GetBinCenter(i1);
2073 y1=h1->GetBinContent(i1);
2074 bwidth1=h1->GetBinWidth(i1);
2075 i2=axis2->FindFixBin(x1);
2076 y2=h2->GetBinContent(i2);
2077 bwidth2=h2->GetBinWidth(i2);
2080 if (i2<1 || i2>nb2)
continue;
2082 if (scale) y2*=bwidth1/bwidth2;
2084 hout->SetBinContent(i1,ynew);
2114 if (gout) gout->Set(0);
2116 if (!gr || !h || !gout)
return 1;
2118 TString nameg=gr->GetName();
2119 if (nameg==
"") nameg=
"gr";
2121 TString nameh=h->GetName();
2122 if (nameh==
"") nameh=
"h";
2128 sc.Form(
"%-+.3g",c);
2133 sd.Form(
"%-+.3g",d);
2135 TString title=
"Resulting graph of: ";
2145 axis=gr->GetXaxis();
2146 title+=axis->GetTitle();
2148 axis=gr->GetYaxis();
2149 title+=axis->GetTitle();
2151 gout->SetTitle(title);
2153 Int_t ndim=h->GetDimension();
2157 cout <<
" *NcBlocks::Add* Error : Histogram " << nameh <<
" should be 1-dimensional." << endl;
2161 Int_t np=gr->GetN();
2162 Int_t nb=h->GetNbinsX();
2164 if (!np || !nb)
return 1;
2175 for (Int_t i=0; i<np; i++)
2177 gr->GetPoint(i,x,y);
2178 hbin=axis->FindFixBin(x);
2179 hval=h->GetBinContent(hbin);
2182 if (hbin<1 || hbin>nb)
continue;
2185 gout->SetPoint(i,x,ynew);
2187 if (gr->InheritsFrom(
"TGraphErrors") && gout->InheritsFrom(
"TGraphErrors"))
2189 TGraphErrors* gre=(TGraphErrors*)gr;
2190 TGraphErrors* goute=(TGraphErrors*)gout;
2191 ex=gre->GetErrorX(i);
2192 ey=gre->GetErrorY(i);
2193 goute->SetPointError(i,ex,ey);
2234 if (hout) hout->Reset();
2238 printf(
" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2242 if (!h1 || !h2 || !hout)
return 1;
2244 Int_t ndim1=h1->GetDimension();
2245 Int_t ndim2=h2->GetDimension();
2246 Int_t ndimo=hout->GetDimension();
2248 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2250 cout <<
" *NcBlocks::Divide* Error : Histograms should all be 1-dimensional." << endl;
2255 TString name=hout->GetName();
2259 TString name1=h1->GetName();
2260 if (name1==
"") name1=
"h1";
2262 TString name2=h2->GetName();
2263 if (name2==
"") name2=
"h2";
2266 if (fabs(c)!=1) sc.Form(
"%-.3g*",fabs(c));
2272 if (d) sd.Form(
"%-.3g+",d);
2277 if (d) sd.Form(
"%-.3g-",d);
2280 TString title=
"Resulting histogram of: ";
2281 if (sd!=
"") title+=sd;
2285 if (sc.Contains(
"*")) title+=
"/(";
2288 if (sc.Contains(
"*")) title+=
")";
2292 title+=
" (scaled w.r.t. bin size)";
2296 title+=
" (not scaled w.r.t. bin size)";
2299 TAxis* axis1=h1->GetXaxis();
2300 title+=axis1->GetTitle();
2302 axis1=h1->GetYaxis();
2303 title+=axis1->GetTitle();
2305 hout->SetName(name);
2306 hout->SetTitle(title);
2308 Int_t nb1=h1->GetNbinsX();
2309 Int_t nb2=h2->GetNbinsX();
2311 if (!nb1 || !nb2)
return 1;
2317 for (Int_t i=1; i<=nb1; i++)
2319 bwidth1=h1->GetBinWidth(i);
2320 if (i==1 || bwidth1>bwmax1)
2331 for (Int_t i=1; i<=nb2; i++)
2333 bwidth2=h2->GetBinWidth(i);
2334 if (i==1 || bwidth2<bwmin2)
2341 Double_t ratio=bwmax1/bwmin2;
2344 printf(
" *NcBlocks::Divide* Error : Larger bin size encountered in histogram %-s than in %-s \n",name1.Data(),name2.Data());
2345 printf(
" %-s: binsize=%-g for bin=%-i %-s: binsize=%-g for bin=%-i \n",name1.Data(),bwmax1,imax1,name2.Data(),bwmin2,imin2);
2356 TAxis* axis2=h2->GetXaxis();
2357 for (Int_t i1=1; i1<=nb1; i1++)
2359 x1=h1->GetBinCenter(i1);
2360 y1=h1->GetBinContent(i1);
2361 bwidth1=h1->GetBinWidth(i1);
2362 i2=axis2->FindFixBin(x1);
2363 y2=h2->GetBinContent(i2);
2364 bwidth2=h2->GetBinWidth(i2);
2367 if (i2<1 || i2>nb2)
continue;
2372 if (scale) y2*=bwidth1/bwidth2;
2374 hout->SetBinContent(i1,ynew);
2402 if (gout) gout->Set(0);
2406 printf(
" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2410 if (!gr || !h || !gout)
return 1;
2412 TString nameg=gr->GetName();
2413 if (nameg==
"") nameg=
"gr";
2415 TString nameh=h->GetName();
2416 if (nameh==
"") nameh=
"h";
2419 if (fabs(c)!=1) sc.Form(
"%-.3g*",fabs(c));
2425 if (d) sd.Form(
"%-.3g+",d);
2430 if (d) sd.Form(
"%-.3g-",d);
2433 TString title=
"Resulting graph of: ";
2434 if (sd!=
"") title+=sd;
2438 if (sc.Contains(
"*")) title+=
"/(";
2441 if (sc.Contains(
"*")) title+=
")";
2445 axis=gr->GetXaxis();
2446 title+=axis->GetTitle();
2448 axis=gr->GetYaxis();
2449 title+=axis->GetTitle();
2451 gout->SetTitle(title);
2453 Int_t ndim=h->GetDimension();
2457 cout <<
" *NcBlocks::Divide* Error : Histogram " << nameh <<
" should be 1-dimensional." << endl;
2461 Int_t np=gr->GetN();
2462 Int_t nb=h->GetNbinsX();
2464 if (!np || !nb)
return 1;
2477 for (Int_t i=0; i<np; i++)
2479 gr->GetPoint(i,x,y);
2480 hbin=axis->FindFixBin(x);
2481 hval=h->GetBinContent(hbin);
2484 if (hbin<1 || hbin>nb)
continue;
2490 gout->SetPoint(j,x,ynew);
2492 if (gr->InheritsFrom(
"TGraphErrors") && gout->InheritsFrom(
"TGraphErrors"))
2494 TGraphErrors* gre=(TGraphErrors*)gr;
2495 TGraphErrors* goute=(TGraphErrors*)gout;
2496 ex=gre->GetErrorX(i);
2497 ey=gre->GetErrorY(i);
2498 goute->SetPointError(j,ex,ey);
2507Int_t
NcBlocks::Rebin(TH1* hin,TH1* hout,Bool_t scale,Int_t nbins,Double_t xmin,Double_t xmax)
2544 if (hout) hout->Reset();
2546 if (!hin || !hout)
return 0;
2548 Int_t ndimi=hin->GetDimension();
2549 Int_t ndimo=hout->GetDimension();
2551 if (ndimi!=1 || ndimo!=1)
2553 cout <<
" *NcBlocks::Rebin* Error : Histograms should both be 1-dimensional." << endl;
2557 Int_t nb1=hin->GetNbinsX();
2561 TAxis* xaxis=hin->GetXaxis();
2562 TAxis* yaxis=hin->GetYaxis();
2566 xmin=xaxis->GetXmin();
2567 xmax=xaxis->GetXmax();
2578 for (Int_t i=1; i<=nb1; i++)
2580 bwidth1=hin->GetBinWidth(i);
2581 xlow1=hin->GetBinLowEdge(i);
2584 if (xlow1>=xmax || xup1<=xmin)
continue;
2586 if (bwmin<0 || bwidth1<bwmin) bwmin=bwidth1;
2591 cout <<
" *NcBlocks::Rebin* Error : Input histogram had no data in requested interval [xmin,xmax]." << endl;
2595 Double_t rnbins=(xmax-xmin)/bwmin;
2597 Double_t diff=rnbins-double(nbins);
2598 if (diff) nbins=nbins+1;
2601 hout->SetBins(nbins,xmin,xmax);
2602 Double_t bwidth=hout->GetBinWidth(1);
2604 TString name=hin->GetName();
2605 if (name==
"") name=
"hin";
2608 snb.Form(
" nbins=%-i",nbins);
2611 smin.Form(
" xmin=%-.3g",xmin);
2614 smax.Form(
" xmax=%-.3g",xmax);
2616 TString title=
"Uniformly binned version of histogram: ";
2623 title+=
" (scaled w.r.t. bin size)";
2627 title+=
" (not scaled w.r.t. bin size)";
2630 title+=xaxis->GetTitle();
2632 title+=yaxis->GetTitle();
2634 hout->SetTitle(title);
2637 for (Int_t i=1; i<=nb1; i++)
2639 bwidth1=hin->GetBinWidth(i);
2640 xlow1=hin->GetBinLowEdge(i);
2643 if (xlow1>=xmax || xup1<=xmin)
continue;
2647 printf(
" *NcBlocks::Rebin* Error : Input histogram had finer binning than output histogram. \n");
2648 printf(
" Input: binwidth=%-g for bin=%-i Output: uniform binwidth=%-g \n",bwidth1,i,bwidth);
2657 for (Int_t i=1; i<=nbins; i++)
2659 x=hout->GetBinCenter(i);
2660 i1=xaxis->FindFixBin(x);
2663 if (i1<1 || i1>nb1)
continue;
2665 y1=hin->GetBinContent(i1);
2666 bwidth1=hin->GetBinWidth(i1);
2667 if (scale) y1=y1*bwidth/bwidth1;
2668 hout->SetBinContent(i,y1);