559 if (!hin || !hout)
return 0;
561 Int_t n=hin->GetNbinsX();
567 printf(
" *NcBlocks::GetBlocks* Inconsistent parameter fpr=%-g for histogram treatment. \n",fpr);
577 title.Form(
"Bayesian Block representation with FPR=%-.3g",fpr);
578 hout->SetTitle(title);
610 Double_t oldoptlen=0;
620 for (Int_t i=1; i<=n; i++)
624 xup=hin->GetBinLowEdge(i)+hin->GetBinWidth(i);
628 if (fpr<0 && optj) jstart=optj;
630 for (Int_t j=jstart; j<=i; j++)
632 xlow=hin->GetBinLowEdge(j);
640 bcount=hin->Integral(j,i);
645 for (Int_t k=j; k<=i; k++)
647 yk=hin->GetBinContent(k);
648 sigk=hin->GetBinError(k);
664 if (index>0) pfit+=best.At(index-1);
688 best.SetAt(optfit,i-1);
689 last.SetAt(optj,i-1);
690 lengths.SetAt(optlen,i-1);
691 counts.SetAt(optcount,i-1);
693 if (!ntrig || optj==1)
continue;
696 oldoptj=last.At(i-2);
697 oldoptlen=lengths.At(i-2);
699 if ((
fMode==1 ||
fMode==2) && oldoptlen) oldytrig=counts.At(i-2)/oldoptlen;
700 if (
fMode==3) oldytrig=counts.At(i-2);
702 if ((
fMode==1 ||
fMode==2) && optlen) ytrig=counts.At(i-1)/optlen;
703 if (
fMode==3) ytrig=counts.At(i-1);
705 if (optj>oldoptj && ((ntrig>0 && ytrig>oldytrig) || (ntrig<0 && ytrig<oldytrig)))
708 xtrig=hin->GetBinLowEdge(optj);
712 if (ncp>=abs(ntrig))
break;
722 TArrayD xarr(ncells+1);
723 TArrayD yarr(ncells+1);
729 x=hin->GetBinLowEdge(jcp)+lengths.At(index);
733 if (lengths.At(index)) y=counts.At(index)/lengths.At(index);
735 if (
fMode==3) y=counts.At(index);
741 if (jcp==1) xarr.SetAt(hin->GetBinLowEdge(jcp),ncp);
747 Double_t* xbins=
new Double_t[ncp+1];
748 Double_t* yvals=
new Double_t[ncp+1];
750 for (Int_t i=0; i<=ncp; i++)
752 xbins[i]=xarr.At(ncp-i);
753 yvals[i]=yarr.At(ncp-i);
756 hout->SetBins(ncp,xbins);
757 for (Int_t i=1; i<=ncp; i++)
759 hout->SetBinContent(i,yvals[i]);
763 if (!ntrig) xtrig=xbins[1];
765 hout->SetLineWidth(2);
766 hout->SetLineColor(kBlue);
767 hout->SetStats(kFALSE);
771 title=
"Bayesian Block representation for histogram ";
772 title+=hin->GetName();
773 title+=
" with FPR= %-.3g";
775 str=hin->GetXaxis()->GetTitle();
776 if (str==
"") str=
"Recordings (e.g. time)";
779 str=hin->GetYaxis()->GetTitle();
780 if (str==
"") str=
"Counts";
782 str=title.Format(title.Data(),fpr);
783 hout->SetTitle(str.Data());
788 str.Form(
"Requested trigger at : %-.3g",xtrig);
790 TLegend* leg=
new TLegend(0.5,0.85,0.7,0.9,str);
791 leg->SetFillColor(0);
792 leg->SetTextColor(kBlue);
793 leg->SetTextAlign(22);
795 TList* hlist=hout->GetListOfFunctions();
848 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
856 if (n<2 || !store || dim<1 || i<1 || i>dim || fabs(fpr)>1)
858 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
859 cout <<
" Store Mode:" << store <<
" Entries:" << n <<
" Dimension:" << dim <<
" i:" << i <<
" fpr:" << fpr << endl;
870 for (Int_t idx=1; idx<=n; idx++)
877 if (val-xarr[idstore-1]<=0)
continue;
891 Double_t* xbins=xarr.GetArray();
892 TH1F hin(
"",
"",n-1,xbins);
893 for (Int_t j=1; j<n; j++)
895 hin.SetBinContent(j,1);
898 Double_t xtrig=
GetBlocks(&hin,fpr,hout,ntrig);
902 title=
"Bayesian Block representation for NcSample ";
904 title+=
" with FPR=%-.3g";
905 title+=
";Recordings of variable ";
910 title+=
";Count rate";
911 str=title.Format(title.Data(),fpr);
912 hout->SetTitle(str.Data());
1163 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified for TGraphErrors treatment." << endl;
1169 if (n<2 || fabs(fpr)>1)
1171 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for TGraphErrors treatment." << endl;
1172 cout <<
" Entries:" << n <<
" fpr:" << fpr << endl;
1183 Double_t* xbins=
new Double_t[n+1];
1189 for (Int_t i=0; i<n; i++)
1192 err=fabs(gr.GetErrorX(i));
1196 dist=xbins[i]-xbins[i-1];
1197 if (dmin<0 || dist<dmin) dmin=dist;
1201 xbins[n]=xbins[n-1]+dmin;
1203 TH1F hin(
"",
"",n,xbins);
1204 for (Int_t j=1; j<=n; j++)
1206 gr.GetPoint(j-1,x,y);
1207 err=fabs(gr.GetErrorY(j-1));
1208 hin.SetBinContent(j,y);
1209 hin.SetBinError(j,err);
1212 Double_t xtrig=
GetBlocks(&hin,fpr,hout,ntrig);
1216 TString xtitle=
"Samplings (e.g. time)";
1217 TString ytitle=
"Measured value";
1218 TAxis* ax=gr.GetXaxis();
1222 if (str !=
"") xtitle=str;
1228 if (str !=
"") ytitle=str;
1230 title=
"Bayesian Block representation for TGraphErrors ";
1231 title+=gr.GetName();
1232 title+=
" with FPR=%-.3g;";
1236 str=title.Format(title,fpr);
1237 hout->SetTitle(str);
1436 Double_t rms=gr.GetRMS(2);
1439 Double_t err=fabs(nrms*rms);
1442 f.Form(
"%-.5g",err);
1444 Double_t xtrig=
GetBlocks(gr,f,fpr,hout,ntrig);
1447 str.Form(
" from nrms=%-.3g",fabs(nrms));
1449 TString title=hout->GetTitle();
1451 hout->SetTitle(title);
1489 cout <<
" *NcBlocks::GetBlocks* Error : Input histogram not specified." << endl;
1495 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1499 Int_t nbins=hin->GetNbinsX();
1501 if (!nbins || n<1 || n>nbins || mode<0 || mode>2)
1503 cout <<
" *NcBlocks::GetBlocks* Inconsistent input nbins=" << nbins <<
" n=" << n <<
" mode=" << mode << endl;
1513 Double_t binwidth=0;
1518 TArrayD xarr(nbins);
1519 TArrayD yarr(nbins);
1522 for (Int_t i=0; i<n; i++)
1526 if (jbin>nbins)
break;
1528 x=hin->GetBinCenter(jbin);
1529 y=hin->GetBinContent(jbin);
1530 binwidth=hin->GetBinWidth(jbin);
1531 if (i==0) xlow=hin->GetBinLowEdge(jbin);
1535 if (mode==0) average=s.
GetMean(2);
1537 if (mode==2) average=s.
GetRMS(2);
1539 xarr.SetAt(xlow,nblocks-1);
1540 yarr.SetAt(average,nblocks-1);
1545 Double_t* xbins=
new Double_t[nblocks+1];
1546 Double_t* yvals=
new Double_t[nblocks+1];
1548 for (Int_t i=0; i<nblocks; i++)
1550 xbins[i]=xarr.At(i);
1551 yvals[i]=yarr.At(i);
1554 xbins[nblocks]=(1.+1e-6)*xup;
1555 yvals[nblocks]=yvals[nblocks-1];
1557 hout->SetBins(nblocks,xbins);
1558 for (Int_t i=1; i<=nblocks; i++)
1560 hout->SetBinContent(i,yvals[i-1]);
1563 hout->SetLineWidth(2);
1564 hout->SetLineColor(kBlue);
1565 hout->SetStats(kFALSE);
1569 title=
"Block representation for histogram ";
1570 title+=hin->GetName();
1571 title+=
" grouped in %-d consecutive bins";
1573 str=hin->GetXaxis()->GetTitle();
1574 if (str==
"") str=
"Recordings (e.g. time)";
1576 if (mode==0) title+=
";Mean ";
1577 if (mode==1) title+=
";Median ";
1578 if (mode==2) title+=
";RMS ";
1579 str=hin->GetYaxis()->GetTitle();
1580 if (str==
"") str=
"Counts";
1582 str=title.Format(title.Data(),n);
1583 hout->SetTitle(str.Data());
1625 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1633 if (!store || dim<1 || i<1 || i>dim || n<1 || n>nen || mode<0 || mode>2)
1635 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
1636 cout <<
" Store Mode:" << store <<
" Entries:" << nen <<
" Dimension:" << dim <<
" i:" << i <<
" n:" << n <<
" mode:" << mode << endl;
1642 Int_t nblocks=
GetBlocks(&gr,hout,n,mode);
1646 title=
"Block representation for NcSample ";
1648 title+=
" grouped in %-d consecutive samples";
1649 title+=
";Sampling number";
1650 if (mode==0) title+=
";Mean ";
1651 if (mode==1) title+=
";Median ";
1652 if (mode==2) title+=
";RMS ";
1653 title+=
"of variable ";
1658 str=title.Format(title.Data(),n);
1659 hout->SetTitle(str.Data());
1884 cout <<
" *NcBlocks::GetBlocks* Error : Input TGraph not specified." << endl;
1890 cout <<
" *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1894 if (n<1 || mode<0 || mode>2)
1896 cout <<
" *NcBlocks::GetBlocks* Inconsistent input for TGraph treatment : n=" << n <<
" mode=" << mode << endl;
1900 Int_t npoints=gr->GetN();
1902 if (!npoints)
return 0;
1908 Double_t* xbins=
new Double_t[npoints+1];
1911 for (Int_t i=0; i<npoints; i++)
1913 gr->GetPoint(i,x,y);
1917 xbins[npoints]=(1.+1e-6)*xbins[npoints-1];
1919 TH1F hin(
"",
"",npoints,xbins);
1920 for (Int_t j=1; j<=npoints; j++)
1922 gr->GetPoint(j-1,x,y);
1923 hin.SetBinContent(j,y);
1926 Int_t nblocks=
GetBlocks(&hin,hout,n,mode);
1930 TString xtitle=
"Samplings (e.g. time)";
1931 TString ytitle=
"Measured value";
1932 TAxis* ax=gr->GetXaxis();
1936 if (str !=
"") xtitle=str;
1942 if (str !=
"") ytitle=str;
1944 title=
"Block representation for TGraph ";
1945 title+=gr->GetName();
1946 title+=
" grouped in %-d consecutive samples;";
1948 if (mode==0) title+=
";Mean ";
1949 if (mode==1) title+=
";Median ";
1950 if (mode==2) title+=
";RMS ";
1952 str=title.Format(title,n);
1953 hout->SetTitle(str);
1960Int_t
NcBlocks::Add(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
1996 if (hout) hout->Reset();
1998 if (!h1 || !h2 || !hout)
return 1;
2000 Int_t ndim1=h1->GetDimension();
2001 Int_t ndim2=h2->GetDimension();
2002 Int_t ndimo=hout->GetDimension();
2004 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2006 cout <<
" *NcBlocks::Add* Error : Histograms should all be 1-dimensional." << endl;
2011 TString name=hout->GetName();
2015 TString name1=h1->GetName();
2016 if (name1==
"") name1=
"h1";
2018 TString name2=h2->GetName();
2019 if (name2==
"") name2=
"h2";
2025 sc.Form(
"%-+.3g",c);
2030 sd.Form(
"%-+.3g",d);
2032 TString title=
"Resulting histogram of: ";
2042 title+=
" (scaled w.r.t. bin size)";
2046 title+=
" (not scaled w.r.t. bin size)";
2049 TAxis* axis1=h1->GetXaxis();
2050 title+=axis1->GetTitle();
2052 axis1=h1->GetYaxis();
2053 title+=axis1->GetTitle();
2055 hout->SetName(name);
2056 hout->SetTitle(title);
2058 Int_t nb1=h1->GetNbinsX();
2059 Int_t nb2=h2->GetNbinsX();
2061 if (!nb1 || !nb2)
return 1;
2067 for (Int_t i=1; i<=nb1; i++)
2069 bwidth1=h1->GetBinWidth(i);
2070 if (i==1 || bwidth1>bwmax1)
2081 for (Int_t i=1; i<=nb2; i++)
2083 bwidth2=h2->GetBinWidth(i);
2084 if (i==1 || bwidth2<bwmin2)
2091 Double_t ratio=bwmax1/bwmin2;
2094 printf(
" *NcBlocks::Add* Error : Larger bin size encountered in histogram %-s than in %-s \n",name1.Data(),name2.Data());
2095 printf(
" %-s: binsize=%-g for bin=%-i %-s: binsize=%-g for bin=%-i \n",name1.Data(),bwmax1,imax1,name2.Data(),bwmin2,imin2);
2105 TAxis* axis2=h2->GetXaxis();
2106 for (Int_t i1=1; i1<=nb1; i1++)
2108 x1=h1->GetBinCenter(i1);
2109 y1=h1->GetBinContent(i1);
2110 bwidth1=h1->GetBinWidth(i1);
2111 i2=axis2->FindFixBin(x1);
2112 y2=h2->GetBinContent(i2);
2113 bwidth2=h2->GetBinWidth(i2);
2116 if (i2<1 || i2>nb2)
continue;
2118 if (scale) y2*=bwidth1/bwidth2;
2120 hout->SetBinContent(i1,ynew);
2150 if (gout) gout->Set(0);
2152 if (!gr || !h || !gout)
return 1;
2154 TString nameg=gr->GetName();
2155 if (nameg==
"") nameg=
"gr";
2157 TString nameh=h->GetName();
2158 if (nameh==
"") nameh=
"h";
2164 sc.Form(
"%-+.3g",c);
2169 sd.Form(
"%-+.3g",d);
2171 TString title=
"Resulting graph of: ";
2181 axis=gr->GetXaxis();
2182 title+=axis->GetTitle();
2184 axis=gr->GetYaxis();
2185 title+=axis->GetTitle();
2187 gout->SetTitle(title);
2189 Int_t ndim=h->GetDimension();
2193 cout <<
" *NcBlocks::Add* Error : Histogram " << nameh <<
" should be 1-dimensional." << endl;
2197 Int_t np=gr->GetN();
2198 Int_t nb=h->GetNbinsX();
2200 if (!np || !nb)
return 1;
2211 for (Int_t i=0; i<np; i++)
2213 gr->GetPoint(i,x,y);
2214 hbin=axis->FindFixBin(x);
2215 hval=h->GetBinContent(hbin);
2218 if (hbin<1 || hbin>nb)
continue;
2221 gout->SetPoint(i,x,ynew);
2223 if (gr->InheritsFrom(
"TGraphErrors") && gout->InheritsFrom(
"TGraphErrors"))
2225 TGraphErrors* gre=(TGraphErrors*)gr;
2226 TGraphErrors* goute=(TGraphErrors*)gout;
2227 ex=gre->GetErrorX(i);
2228 ey=gre->GetErrorY(i);
2229 goute->SetPointError(i,ex,ey);
2270 if (hout) hout->Reset();
2274 printf(
" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2278 if (!h1 || !h2 || !hout)
return 1;
2280 Int_t ndim1=h1->GetDimension();
2281 Int_t ndim2=h2->GetDimension();
2282 Int_t ndimo=hout->GetDimension();
2284 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2286 cout <<
" *NcBlocks::Divide* Error : Histograms should all be 1-dimensional." << endl;
2291 TString name=hout->GetName();
2295 TString name1=h1->GetName();
2296 if (name1==
"") name1=
"h1";
2298 TString name2=h2->GetName();
2299 if (name2==
"") name2=
"h2";
2302 if (fabs(c)!=1) sc.Form(
"%-.3g*",fabs(c));
2308 if (d) sd.Form(
"%-.3g+",d);
2313 if (d) sd.Form(
"%-.3g-",d);
2316 TString title=
"Resulting histogram of: ";
2317 if (sd!=
"") title+=sd;
2321 if (sc.Contains(
"*")) title+=
"/(";
2324 if (sc.Contains(
"*")) title+=
")";
2328 title+=
" (scaled w.r.t. bin size)";
2332 title+=
" (not scaled w.r.t. bin size)";
2335 TAxis* axis1=h1->GetXaxis();
2336 title+=axis1->GetTitle();
2338 axis1=h1->GetYaxis();
2339 title+=axis1->GetTitle();
2341 hout->SetName(name);
2342 hout->SetTitle(title);
2344 Int_t nb1=h1->GetNbinsX();
2345 Int_t nb2=h2->GetNbinsX();
2347 if (!nb1 || !nb2)
return 1;
2353 for (Int_t i=1; i<=nb1; i++)
2355 bwidth1=h1->GetBinWidth(i);
2356 if (i==1 || bwidth1>bwmax1)
2367 for (Int_t i=1; i<=nb2; i++)
2369 bwidth2=h2->GetBinWidth(i);
2370 if (i==1 || bwidth2<bwmin2)
2377 Double_t ratio=bwmax1/bwmin2;
2380 printf(
" *NcBlocks::Divide* Error : Larger bin size encountered in histogram %-s than in %-s \n",name1.Data(),name2.Data());
2381 printf(
" %-s: binsize=%-g for bin=%-i %-s: binsize=%-g for bin=%-i \n",name1.Data(),bwmax1,imax1,name2.Data(),bwmin2,imin2);
2392 TAxis* axis2=h2->GetXaxis();
2393 for (Int_t i1=1; i1<=nb1; i1++)
2395 x1=h1->GetBinCenter(i1);
2396 y1=h1->GetBinContent(i1);
2397 bwidth1=h1->GetBinWidth(i1);
2398 i2=axis2->FindFixBin(x1);
2399 y2=h2->GetBinContent(i2);
2400 bwidth2=h2->GetBinWidth(i2);
2403 if (i2<1 || i2>nb2)
continue;
2408 if (scale) y2*=bwidth1/bwidth2;
2410 hout->SetBinContent(i1,ynew);
2438 if (gout) gout->Set(0);
2442 printf(
" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2446 if (!gr || !h || !gout)
return 1;
2448 TString nameg=gr->GetName();
2449 if (nameg==
"") nameg=
"gr";
2451 TString nameh=h->GetName();
2452 if (nameh==
"") nameh=
"h";
2455 if (fabs(c)!=1) sc.Form(
"%-.3g*",fabs(c));
2461 if (d) sd.Form(
"%-.3g+",d);
2466 if (d) sd.Form(
"%-.3g-",d);
2469 TString title=
"Resulting graph of: ";
2470 if (sd!=
"") title+=sd;
2474 if (sc.Contains(
"*")) title+=
"/(";
2477 if (sc.Contains(
"*")) title+=
")";
2481 axis=gr->GetXaxis();
2482 title+=axis->GetTitle();
2484 axis=gr->GetYaxis();
2485 title+=axis->GetTitle();
2487 gout->SetTitle(title);
2489 Int_t ndim=h->GetDimension();
2493 cout <<
" *NcBlocks::Divide* Error : Histogram " << nameh <<
" should be 1-dimensional." << endl;
2497 Int_t np=gr->GetN();
2498 Int_t nb=h->GetNbinsX();
2500 if (!np || !nb)
return 1;
2513 for (Int_t i=0; i<np; i++)
2515 gr->GetPoint(i,x,y);
2516 hbin=axis->FindFixBin(x);
2517 hval=h->GetBinContent(hbin);
2520 if (hbin<1 || hbin>nb)
continue;
2526 gout->SetPoint(j,x,ynew);
2528 if (gr->InheritsFrom(
"TGraphErrors") && gout->InheritsFrom(
"TGraphErrors"))
2530 TGraphErrors* gre=(TGraphErrors*)gr;
2531 TGraphErrors* goute=(TGraphErrors*)gout;
2532 ex=gre->GetErrorX(i);
2533 ey=gre->GetErrorY(i);
2534 goute->SetPointError(j,ex,ey);
2543Int_t
NcBlocks::Rebin(TH1* hin,TH1* hout,Bool_t scale,Int_t nbins,Double_t xmin,Double_t xmax)
2580 if (hout) hout->Reset();
2582 if (!hin || !hout)
return 0;
2584 Int_t ndimi=hin->GetDimension();
2585 Int_t ndimo=hout->GetDimension();
2587 if (ndimi!=1 || ndimo!=1)
2589 cout <<
" *NcBlocks::Rebin* Error : Histograms should both be 1-dimensional." << endl;
2593 Int_t nb1=hin->GetNbinsX();
2597 TAxis* xaxis=hin->GetXaxis();
2598 TAxis* yaxis=hin->GetYaxis();
2602 xmin=xaxis->GetXmin();
2603 xmax=xaxis->GetXmax();
2614 for (Int_t i=1; i<=nb1; i++)
2616 bwidth1=hin->GetBinWidth(i);
2617 xlow1=hin->GetBinLowEdge(i);
2620 if (xlow1>=xmax || xup1<=xmin)
continue;
2622 if (bwmin<0 || bwidth1<bwmin) bwmin=bwidth1;
2627 cout <<
" *NcBlocks::Rebin* Error : Input histogram had no data in requested interval [xmin,xmax]." << endl;
2631 Double_t rnbins=(xmax-xmin)/bwmin;
2633 Double_t diff=rnbins-double(nbins);
2634 if (diff) nbins=nbins+1;
2637 hout->SetBins(nbins,xmin,xmax);
2638 Double_t bwidth=hout->GetBinWidth(1);
2640 TString name=hin->GetName();
2641 if (name==
"") name=
"hin";
2644 snb.Form(
" nbins=%-i",nbins);
2647 smin.Form(
" xmin=%-.3g",xmin);
2650 smax.Form(
" xmax=%-.3g",xmax);
2652 TString title=
"Uniformly binned version of histogram: ";
2659 title+=
" (scaled w.r.t. bin size)";
2663 title+=
" (not scaled w.r.t. bin size)";
2666 title+=xaxis->GetTitle();
2668 title+=yaxis->GetTitle();
2670 hout->SetTitle(title);
2673 for (Int_t i=1; i<=nb1; i++)
2675 bwidth1=hin->GetBinWidth(i);
2676 xlow1=hin->GetBinLowEdge(i);
2679 if (xlow1>=xmax || xup1<=xmin)
continue;
2683 printf(
" *NcBlocks::Rebin* Error : Input histogram had finer binning than output histogram. \n");
2684 printf(
" Input: binwidth=%-g for bin=%-i Output: uniform binwidth=%-g \n",bwidth1,i,bwidth);
2693 for (Int_t i=1; i<=nbins; i++)
2695 x=hout->GetBinCenter(i);
2696 i1=xaxis->FindFixBin(x);
2699 if (i1<1 || i1>nb1)
continue;
2701 y1=hin->GetBinContent(i1);
2702 bwidth1=hin->GetBinWidth(i1);
2703 if (scale) y1=y1*bwidth/bwidth1;
2704 hout->SetBinContent(i,y1);