NCFS-Pack
A generic (astro)particle physics analysis framework
 
Loading...
Searching...
No Matches
NcBlocks.cxx
Go to the documentation of this file.
1
31
33
388
389#include "NcBlocks.h"
390#include "Riostream.h"
391
392ClassImp(NcBlocks); // Class implementation to enable ROOT I/O
393
396{
402
403 fMode=0;
404}
405
414
416{
422
423 fMode=0;
424}
425
426Double_t NcBlocks::GetPrior(Int_t n,Double_t fpr)
427{
453
454 Double_t prior=0;
455
456 if (fMode<1 || fMode>3 || n<1 || fpr<0 || fpr>1)
457 {
458 cout << " *NcBlocks::GetPrior* Inconsistent input : mode=" << fMode << " n=" << n << " fpr=" << fpr << endl;
459 return 0;
460 }
461
462 Double_t rn=n;
463
464 if (fMode==3)
465 {
466 Double_t c=0;
467 Double_t s=0;
468 if (fpr>0.045 && fpr<0.055) // Fit of J.D. Scargle for fpr=0.05
469 {
470 c=1.32;
471 s=0.577;
472 }
473 else
474 {
475 c=51.29*TMath::Landau(fpr,-0.152,0.03167);
476 s=0.5807+0.2317*fpr;
477 }
478 prior=c+s*log10(rn);
479 }
480 else // Data Mode 1 and 2
481 {
482 prior=4.-log(73.53*fpr*pow(rn,-0.478));
483 }
484
485 prior=-prior;
486
487 return prior;
488}
489
490Double_t NcBlocks::GetBlockFitness(Double_t n,Double_t len)
491{
507
508 Double_t fb=0;
509
510 if ((fMode!=1 && fMode!=2) || (fMode==1 && n<1) || len<=0)
511 {
512 cout << " *NcBlocks::GetBlockFitness* Inconsistent input : mode=" << fMode << " n=" << n << " len=" << len << endl;
513 return 0;
514 }
515
516 if (n<=0) return 0;
517
518 fb=n*log(n/len);
519
520 return fb;
521}
522
523Double_t NcBlocks::GetBlocks(TH1* hin,Double_t fpr,TH1* hout,Int_t ntrig)
524{
555
556 if (!hin || !hout) return 0;
557
558 Int_t n=hin->GetNbinsX();
559
560 if (!n) return 0;
561
562 // Set the Data Mode for binned data, if it was not set already
563 // Also initialize the output histogram title for binned data
564 if (!fMode)
565 {
566 fMode=2;
567 TString title;
568 title.Form("Bayesian Block representation with FPR=%-.3g",fpr);
569 hout->SetTitle(title);
570 }
571
572 TArrayD best(n); // Array with optimal fitness values
573 TArrayI last(n); // Array with indices of optimal last change points
574 TArrayD lengths(n); // Array with the lengths of the optimal partition blocks
575 TArrayD counts(n); // Array with the event counts of the optimal partition blocks
576
577 Double_t blen=0; // Length of a certain block
578 Double_t bcount=0; // Event count in a certain block
579 Double_t prior=0; // Contribution of each block to the prior fitness for a certain partition
580 Double_t bfit=0; // Fitness of a certain block
581 Double_t pfit=0; // Fitness of a certain partition
582
583 Double_t xlow=0;
584 Double_t xup=0;
585 Int_t index=0;
586
587 // Variables for measurement of an observable (i.e. Data Mode 3)
588 Double_t yk=0; // Measured value
589 Double_t sigk=0; // Error on the measured value
590 Double_t a=0;
591 Double_t b=0;
592
593 // The parameters for the optimal partition
594 Double_t optfit=0;
595 Int_t optj=0;
596 Double_t optlen=0;
597 Double_t optcount=0;
598
599 // Parameters and counter for trigger mode
600 Int_t oldoptj=0;
601 Double_t oldoptlen=0;
602 Int_t ncp=0;
603 Double_t xtrig=0;
604 Double_t ytrig=0;
605 Double_t oldytrig=0;
606
607 // Add Data Cells one by one to the sample to be partioned
608 Int_t ncells=0;
609 Int_t first=1;
610 for (Int_t i=1; i<=n; i++)
611 {
612 ncells=i;
613 prior=GetPrior(i,fpr);
614 xup=hin->GetBinLowEdge(i)+hin->GetBinWidth(i);
615 // Loop over all possible block partitions for this Data Cell sample
616 first=1;
617 for (Int_t j=1; j<=i; j++)
618 {
619 xlow=hin->GetBinLowEdge(j);
620 blen=xup-xlow;
621 bcount=0;
622 bfit=0;
623 a=0;
624 b=0;
625 if (fMode==1 || fMode==2)
626 {
627 bcount=hin->Integral(j,i);
628 bfit=GetBlockFitness(fabs(bcount),blen);
629 }
630 if (fMode==3)
631 {
632 for (Int_t k=j; k<=i; k++)
633 {
634 yk=hin->GetBinContent(k);
635 sigk=hin->GetBinError(k);
636 sigk=sigk*sigk;
637 if (sigk)
638 {
639 a+=1./(sigk);
640 b+=yk/sigk;
641 }
642 }
643 if (a)
644 {
645 bcount=b/a; // Weighted mean of the y-values in the block
646 bfit=b*b/(2.*a);
647 }
648 }
649 pfit=prior+bfit;
650 index=j-1; // Array index reduced by 1 because of C++ array index convention
651 if (index>0) pfit+=best.At(index-1);
652
653 // Record attributes for the optimal partition
654 if (first)
655 {
656 optfit=pfit;
657 optj=j;
658 optlen=blen;
659 optcount=bcount;
660 first=0;
661 }
662 else
663 {
664 if (pfit>optfit)
665 {
666 optfit=pfit;
667 optj=j;
668 optlen=blen;
669 optcount=bcount;
670 }
671 }
672 } // End of loop over possible block partitions
673
674 // Store the attributes of the optimal partition
675 best.SetAt(optfit,i-1);
676 last.SetAt(optj,i-1);
677 lengths.SetAt(optlen,i-1);
678 counts.SetAt(optcount,i-1);
679
680 if (!ntrig || optj==1) continue;
681
682 // Check for triggering on a new change point
683 oldoptj=last.At(i-2);
684 oldoptlen=lengths.At(i-2);
685 oldytrig=0;
686 if ((fMode==1 || fMode==2) && oldoptlen) oldytrig=counts.At(i-2)/oldoptlen;
687 if (fMode==3) oldytrig=counts.At(i-2);
688 ytrig=0;
689 if ((fMode==1 || fMode==2) && optlen) ytrig=counts.At(i-1)/optlen;
690 if (fMode==3) ytrig=counts.At(i-1);
691
692 if (optj>oldoptj && ((ntrig>0 && ytrig>oldytrig) || (ntrig<0 && ytrig<oldytrig)))
693 {
694 ncp++;
695 xtrig=hin->GetBinLowEdge(optj);
696 }
697
698 // Stop when the requested number of triggers has been reached
699 if (ncp>=abs(ntrig)) break;
700
701 } // End of adding Data Cells one by one
702
703 // Obtain the change points and corresponding partition information
704 Int_t jcp=0;
705 index=ncells;
706 Double_t x=0;
707 Double_t y=0;
708 ncp=0;
709 TArrayD xarr(ncells+1);
710 TArrayD yarr(ncells+1);
711 while (index>0)
712 {
713 index--; // Reduce array index by 1 because of C++ array convention
714 jcp=last.At(index);
715 ncp++;
716 x=hin->GetBinLowEdge(jcp)+lengths.At(index);
717 y=0;
718 if (fMode==1 || fMode==2)
719 {
720 if (lengths.At(index)) y=counts.At(index)/lengths.At(index);
721 }
722 if (fMode==3) y=counts.At(index);
723
724 xarr.SetAt(x,ncp-1);
725 yarr.SetAt(y,ncp-1);
726
727 // Also mark the start of the first bin
728 if (jcp==1) xarr.SetAt(hin->GetBinLowEdge(jcp),ncp);
729
730 index=jcp-1;
731 }
732
733 // Create the corresponding variable binned histogram
734 Double_t* xbins=new Double_t[ncp+1];
735 Double_t* yvals=new Double_t[ncp+1];
736
737 for (Int_t i=0; i<=ncp; i++)
738 {
739 xbins[i]=xarr.At(ncp-i);
740 yvals[i]=yarr.At(ncp-i);
741 }
742
743 hout->SetBins(ncp,xbins);
744 for (Int_t i=1; i<=ncp; i++)
745 {
746 hout->SetBinContent(i,yvals[i]);
747 }
748
749 // Determine the first trigger point after the full sample analysis
750 if (!ntrig) xtrig=xbins[1];
751
752 hout->SetLineWidth(2);
753 hout->SetLineColor(kBlue);
754 hout->SetStats(kFALSE);
755
756 // Set the output histogram and axes titles
757 TString title,str;
758 title="Bayesian Block representation for histogram ";
759 title+=hin->GetName();
760 title+=" with FPR= %-.3g";
761 title+=";";
762 str=hin->GetXaxis()->GetTitle();
763 if (str=="") str="Recordings (e.g. time)";
764 title+=str;
765 title+=";";
766 str=hin->GetYaxis()->GetTitle();
767 if (str=="") str="Counts";
768 title+=str;
769 str=title.Format(title.Data(),fpr);
770 hout->SetTitle(str.Data());
771
772 // Indicate the requested trigger in a legend
773 if (ntrig)
774 {
775 str.Form("Requested trigger at : %-.3g",xtrig);
776
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);
781
782 TList* hlist=hout->GetListOfFunctions();
783 hlist->Add(leg);
784 }
785
786 // Reset the Data Mode for subsequent invokations
787 fMode=0;
788
789 delete [] xbins;
790 delete [] yvals;
791
792 return xtrig;
793}
794
795Double_t NcBlocks::GetBlocks(NcSample s,Int_t i,Double_t fpr,TH1* hout,Int_t ntrig)
796{
831
832 if (!hout)
833 {
834 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
835 return 0;
836 }
837
838 Int_t n=s.GetN();
839 Int_t store=s.GetStoreMode();
840 Int_t dim=s.GetDimension();
841
842 if (n<2 || !store || dim<1 || i<1 || i>dim || fpr<0 || fpr>1)
843 {
844 cout << " *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
845 cout << " Store Mode:" << store << " Entries:" << n << " Dimension:" << dim << " i:" << i << " fpr:" << fpr << endl;
846 return 0;
847 }
848
849 // Set Data Mode for unbinned event data
850 fMode=1;
851
852 // Represent each observation as 1 count in a variable binned histogram
853 Double_t* xbins=new Double_t[n];
854 Double_t val=0;
855 for (Int_t idx=1; idx<=n; idx++)
856 {
857 val=s.GetEntry(idx,i,1,i);
858 xbins[idx-1]=val;
859 }
860
861 TH1F hin("","",n-1,xbins);
862 for (Int_t j=1; j<n; j++)
863 {
864 hin.SetBinContent(j,1);
865 }
866
867 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
868
869 // Set the output histogram and axes titles
870 TString title,str;
871 title="Bayesian Block representation for NcSample ";
872 title+=s.GetName();
873 title+=" with FPR=%-.3g";
874 title+=";Recordings of variable ";
875 title+=i;
876 title+=" (";
877 title+=s.GetVariableName(i);
878 title+=")";
879 title+=";Count rate";
880 str=title.Format(title.Data(),fpr);
881 hout->SetTitle(str.Data());
882
883 delete [] xbins;
884
885 return xtrig;
886}
887
888Double_t NcBlocks::GetBlocks(NcSample s,TString name,Double_t fpr,TH1* hout,Int_t ntrig)
889{
924
925 Int_t i=s.GetIndex(name);
926
927 Double_t xtrig=GetBlocks(s,i,fpr,hout,ntrig);
928
929 return xtrig;
930}
931
932Double_t NcBlocks::GetBlocks(Int_t n,Double_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
933{
971
972 if (!hout)
973 {
974 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
975 return 0;
976 }
977
978 if (!arr || n<2 || fpr<0 || fpr>1)
979 {
980 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
981 if (!arr)
982 {
983 cout << " Array pointer is 0." << endl;
984 }
985 else
986 {
987 cout << " Entries:" << n << " fpr:" << fpr << endl;
988 }
989 return 0;
990 }
991
992 NcSample s;
993 s.SetStoreMode();
994
995 for (Int_t i=0; i<n; i++)
996 {
997 s.Enter(arr[i]);
998 }
999
1000 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1001
1002 // Set the output histogram and axes titles
1003 TString title;
1004 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1005 title+=";Recordings (e.g. time);Count rate";
1006 hout->SetTitle(title);
1007
1008 return xtrig;
1009}
1010
1011Double_t NcBlocks::GetBlocks(Int_t n,Float_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
1012{
1050
1051 if (!hout)
1052 {
1053 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1054 return 0;
1055 }
1056
1057 if (!arr || n<2 || fpr<0 || fpr>1)
1058 {
1059 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1060 if (!arr)
1061 {
1062 cout << " Array pointer is 0." << endl;
1063 }
1064 else
1065 {
1066 cout << " Entries:" << n << " fpr:" << fpr << endl;
1067 }
1068 return 0;
1069 }
1070
1071 NcSample s;
1072 s.SetStoreMode();
1073
1074 for (Int_t i=0; i<n; i++)
1075 {
1076 s.Enter(arr[i]);
1077 }
1078
1079 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1080
1081 // Set the output histogram and axes titles
1082 TString title;
1083 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1084 title+=";Recordings (e.g. time);Count rate";
1085 hout->SetTitle(title);
1086
1087 return xtrig;
1088}
1089
1090Double_t NcBlocks::GetBlocks(TGraphErrors gr,Double_t fpr,TH1* hout,Int_t ntrig)
1091{
1127
1128 if (!hout)
1129 {
1130 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1131 return 0;
1132 }
1133
1134 Int_t n=gr.GetN();
1135
1136 if (n<2 || fpr<0 || fpr>1)
1137 {
1138 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraphErrors treatment." << endl;
1139 cout << " Entries:" << n << " fpr:" << fpr << endl;
1140 return 0;
1141 }
1142
1143 // Set Data Mode for measurements of a continuous observable
1144 fMode=3;
1145
1146 // Sort the data points with increasing x-value
1147 gr.Sort();
1148
1149 // Represent each observation as a value in a variable binned histogram
1150 Double_t* xbins=new Double_t[n+1];
1151 Double_t x=0;
1152 Double_t y=0;
1153 Double_t err=0;
1154 Double_t dist=0;
1155 Double_t dmin=-1;
1156 for (Int_t i=0; i<n; i++)
1157 {
1158 gr.GetPoint(i,x,y);
1159 err=fabs(gr.GetErrorX(i));
1160 xbins[i]=x-err;
1161 if (i>0)
1162 {
1163 dist=xbins[i]-xbins[i-1];
1164 if (dmin<0 || dist<dmin) dmin=dist;
1165 }
1166 }
1167 // Add an extra bin to contain the last measurement
1168 xbins[n]=xbins[n-1]+dmin;
1169
1170 TH1F hin("","",n,xbins);
1171 for (Int_t j=1; j<=n; j++)
1172 {
1173 gr.GetPoint(j-1,x,y);
1174 err=fabs(gr.GetErrorY(j-1));
1175 hin.SetBinContent(j,y);
1176 hin.SetBinError(j,err);
1177 }
1178
1179 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
1180
1181 // Set the output histogram and axes titles
1182 TString title,str;
1183 TString xtitle="Samplings (e.g. time)";
1184 TString ytitle="Measured value";
1185 TAxis* ax=gr.GetXaxis();
1186 if (ax)
1187 {
1188 str=ax->GetTitle();
1189 if (str != "") xtitle=str;
1190 }
1191 ax=gr.GetYaxis();
1192 if (ax)
1193 {
1194 str=ax->GetTitle();
1195 if (str != "") ytitle=str;
1196 }
1197 title="Bayesian Block representation for TGraphErrors ";
1198 title+=gr.GetName();
1199 title+=" with FPR=%-.3g;";
1200 title+=xtitle;
1201 title+=";";
1202 title+=ytitle;
1203 str=title.Format(title,fpr);
1204 hout->SetTitle(str);
1205
1206 delete [] xbins;
1207
1208 return xtrig;
1209}
1210
1211Double_t NcBlocks::GetBlocks(TGraph gr,TF1 f,Double_t fpr,TH1* hout,Int_t ntrig)
1212{
1261
1262 NcSample s;
1263 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&f);
1264
1265 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1266
1267 TString str=" and input errors : ";
1268 str+=f.GetExpFormula("p");
1269 str.ReplaceAll("x","y");
1270
1271 TString title=hout->GetTitle();
1272 title+=str;
1273 hout->SetTitle(title);
1274
1275 return xtrig;
1276}
1277
1278Double_t NcBlocks::GetBlocks(TGraph gr,TString f,Double_t fpr,TH1* hout,Int_t ntrig)
1279{
1329
1330 TF1 func("func",f);
1331 NcSample s;
1332 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&func);
1333
1334 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1335
1336 TString str=" and input errors : ";
1337 str+=func.GetExpFormula("p");
1338 str.ReplaceAll("x","y");
1339
1340 TString title=hout->GetTitle();
1341 title+=str;
1342 hout->SetTitle(title);
1343
1344 return xtrig;
1345}
1346
1347Double_t NcBlocks::GetBlocks(TGraph gr,Double_t nrms,Double_t fpr,TH1* hout,Int_t ntrig)
1348{
1398
1399 // Obtain the RMS deviation of all the y-values
1400 Double_t rms=gr.GetRMS(2);
1401
1402 // Determine the error for each y-value and convert into a function format
1403 Double_t err=fabs(nrms*rms);
1404
1405 TString f;
1406 f.Form("%-.5g",err);
1407
1408 Double_t xtrig=GetBlocks(gr,f,fpr,hout,ntrig);
1409
1410 TString str;
1411 str.Form(" from nrms=%-.3g",fabs(nrms));
1412
1413 TString title=hout->GetTitle();
1414 title+=str;
1415 hout->SetTitle(title);
1416
1417 return xtrig;
1418}
1419
1420Int_t NcBlocks::GetBlocks(TH1* hin,TH1* hout,Int_t n,Int_t mode)
1421{
1450
1451 if (!hin)
1452 {
1453 cout << " *NcBlocks::GetBlocks* Error : Input histogram not specified." << endl;
1454 return 0;
1455 }
1456
1457 if (!hout)
1458 {
1459 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1460 return 0;
1461 }
1462
1463 Int_t nbins=hin->GetNbinsX();
1464
1465 if (!nbins || n<1 || n>nbins || mode<0 || mode>2)
1466 {
1467 cout << " *NcBlocks::GetBlocks* Inconsistent input nbins=" << nbins << " n=" << n << " mode=" << mode << endl;
1468 return 0;
1469 }
1470
1471 // Retrieve the various sets of "n" bins from the input histogram
1472 Int_t jbin=0;
1473 Double_t x=0;
1474 Double_t y=0;
1475 Double_t xlow=0;
1476 Double_t xup=0;
1477 Double_t binwidth=0;
1478 NcSample s;
1479 if (mode==1) s.SetStoreMode(1);
1480 Int_t nblocks=0;
1481 Double_t average=0;
1482 TArrayD xarr(nbins);
1483 TArrayD yarr(nbins);
1484 while (jbin<nbins)
1485 {
1486 for (Int_t i=0; i<n; i++)
1487 {
1488 jbin++;
1489
1490 if (jbin>nbins) break;
1491
1492 x=hin->GetBinCenter(jbin);
1493 y=hin->GetBinContent(jbin);
1494 binwidth=hin->GetBinWidth(jbin);
1495 if (i==0) xlow=hin->GetBinLowEdge(jbin);
1496 xup=x+0.5*binwidth;
1497 s.Enter(x,y);
1498 }
1499 if (mode==0) average=s.GetMean(2);
1500 if (mode==1) average=s.GetMedian(2);
1501 if (mode==2) average=s.GetRMS(2);
1502 nblocks++;
1503 xarr.SetAt(xlow,nblocks-1);
1504 yarr.SetAt(average,nblocks-1);
1505 s.Reset();
1506 } // End of while loop
1507
1508 // Create the corresponding variable binned output histogram
1509 Double_t* xbins=new Double_t[nblocks+1];
1510 Double_t* yvals=new Double_t[nblocks+1];
1511
1512 for (Int_t i=0; i<nblocks; i++)
1513 {
1514 xbins[i]=xarr.At(i);
1515 yvals[i]=yarr.At(i);
1516 }
1517 // Add an extra bin to contain the last data
1518 xbins[nblocks]=(1.+1e-6)*xup;
1519 yvals[nblocks]=yvals[nblocks-1];
1520
1521 hout->SetBins(nblocks,xbins);
1522 for (Int_t i=1; i<=nblocks; i++)
1523 {
1524 hout->SetBinContent(i,yvals[i-1]);
1525 }
1526
1527 hout->SetLineWidth(2);
1528 hout->SetLineColor(kBlue);
1529 hout->SetStats(kFALSE);
1530
1531 // Set the output histogram and axes titles
1532 TString title,str;
1533 title="Block representation for histogram ";
1534 title+=hin->GetName();
1535 title+=" grouped in %-d consecutive bins";
1536 title+=";";
1537 str=hin->GetXaxis()->GetTitle();
1538 if (str=="") str="Recordings (e.g. time)";
1539 title+=str;
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";
1545 title+=str;
1546 str=title.Format(title.Data(),n);
1547 hout->SetTitle(str.Data());
1548
1549 delete [] xbins;
1550 delete [] yvals;
1551
1552 return nblocks;
1553}
1554
1555Int_t NcBlocks::GetBlocks(NcSample s,Int_t i,TH1* hout,Int_t n,Int_t mode)
1556{
1586
1587 if (!hout)
1588 {
1589 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1590 return 0;
1591 }
1592
1593 Int_t nen=s.GetN();
1594 Int_t store=s.GetStoreMode();
1595 Int_t dim=s.GetDimension();
1596
1597 if (!store || dim<1 || i<1 || i>dim || n<1 || n>nen || mode<0 || mode>2)
1598 {
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;
1601 return 0;
1602 }
1603
1604 TGraph gr=s.GetGraph(i);
1605
1606 Int_t nblocks=GetBlocks(&gr,hout,n,mode);
1607
1608 // Set the output histogram and axes titles
1609 TString title,str;
1610 title="Block representation for NcSample ";
1611 title+=s.GetName();
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 ";
1618 title+=i;
1619 title+=" (";
1620 title+=s.GetVariableName(i);
1621 title+=")";
1622 str=title.Format(title.Data(),n);
1623 hout->SetTitle(str.Data());
1624
1625 return nblocks;
1626}
1627
1628Int_t NcBlocks::GetBlocks(NcSample s,TString name,TH1* hout,Int_t n,Int_t mode)
1629{
1659
1660 Int_t i=s.GetIndex(name);
1661
1662 Int_t nblocks=GetBlocks(s,i,hout,n,mode);
1663
1664 return nblocks;
1665}
1666
1667Int_t NcBlocks::GetBlocks(Int_t nr,Double_t* arr,TH1* hout,Int_t n,Int_t mode)
1668{
1697
1698 if (!hout)
1699 {
1700 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1701 return 0;
1702 }
1703
1704 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1705 {
1706 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1707 if (!arr)
1708 {
1709 cout << " Array pointer is 0." << endl;
1710 }
1711 else
1712 {
1713 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1714 }
1715 return 0;
1716 }
1717
1718 NcSample s;
1719 s.SetStoreMode();
1720
1721 for (Int_t i=0; i<nr; i++)
1722 {
1723 s.Enter(arr[i]);
1724 }
1725
1726 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1727
1728 // Set the output histogram and axes titles
1729 TString title;
1730 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1731 title+=";Sampling number";
1732 if (mode==0) title+=";Mean ";
1733 if (mode==1) title+=";Median ";
1734 if (mode==2) title+=";RMS ";
1735 title+="value";
1736 hout->SetTitle(title);
1737
1738 return nblocks;
1739}
1740
1741Int_t NcBlocks::GetBlocks(Int_t nr,Float_t* arr,TH1* hout,Int_t n,Int_t mode)
1742{
1771
1772 if (!hout)
1773 {
1774 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1775 return 0;
1776 }
1777
1778 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1779 {
1780 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1781 if (!arr)
1782 {
1783 cout << " Array pointer is 0." << endl;
1784 }
1785 else
1786 {
1787 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1788 }
1789 return 0;
1790 }
1791
1792 NcSample s;
1793 s.SetStoreMode();
1794
1795 for (Int_t i=0; i<nr; i++)
1796 {
1797 s.Enter(arr[i]);
1798 }
1799
1800 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1801
1802 // Set the output histogram and axes titles
1803 TString title;
1804 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1805 title+=";Sampling number";
1806 if (mode==0) title+=";Mean ";
1807 if (mode==1) title+=";Median ";
1808 if (mode==2) title+=";RMS ";
1809 title+="value";
1810 hout->SetTitle(title);
1811
1812 return nblocks;
1813}
1814
1815Int_t NcBlocks::GetBlocks(TGraph* gr,TH1* hout,Int_t n,Int_t mode)
1816{
1845
1846 if (!gr)
1847 {
1848 cout << " *NcBlocks::GetBlocks* Error : Input TGraph not specified." << endl;
1849 return 0;
1850 }
1851
1852 if (!hout)
1853 {
1854 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1855 return 0;
1856 }
1857
1858 if (n<1 || mode<0 || mode>2)
1859 {
1860 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraph treatment : n=" << n << " mode=" << mode << endl;
1861 return 0;
1862 }
1863
1864 Int_t npoints=gr->GetN();
1865
1866 if (!npoints) return 0;
1867
1868 // Sort the data points with increasing x-value
1869 gr->Sort();
1870
1871 // Represent each observation as a value in a variable binned histogram
1872 Double_t* xbins=new Double_t[npoints+1];
1873 Double_t x=0;
1874 Double_t y=0;
1875 for (Int_t i=0; i<npoints; i++)
1876 {
1877 gr->GetPoint(i,x,y);
1878 xbins[i]=x;
1879 }
1880 // Add an extra bin to contain the last measurement
1881 xbins[npoints]=(1.+1e-6)*xbins[npoints-1];
1882
1883 TH1F hin("","",npoints,xbins);
1884 for (Int_t j=1; j<=npoints; j++)
1885 {
1886 gr->GetPoint(j-1,x,y);
1887 hin.SetBinContent(j,y);
1888 }
1889
1890 Int_t nblocks=GetBlocks(&hin,hout,n,mode);
1891
1892 // Set the output histogram and axes titles
1893 TString title,str;
1894 TString xtitle="Samplings (e.g. time)";
1895 TString ytitle="Measured value";
1896 TAxis* ax=gr->GetXaxis();
1897 if (ax)
1898 {
1899 str=ax->GetTitle();
1900 if (str != "") xtitle=str;
1901 }
1902 ax=gr->GetYaxis();
1903 if (ax)
1904 {
1905 str=ax->GetTitle();
1906 if (str != "") ytitle=str;
1907 }
1908 title="Block representation for TGraph ";
1909 title+=gr->GetName();
1910 title+=" grouped in %-d consecutive samples;";
1911 title+=xtitle;
1912 if (mode==0) title+=";Mean ";
1913 if (mode==1) title+=";Median ";
1914 if (mode==2) title+=";RMS ";
1915 title+=ytitle;
1916 str=title.Format(title,n);
1917 hout->SetTitle(str);
1918
1919 delete [] xbins;
1920
1921 return nblocks;
1922}
1923
1924Int_t NcBlocks::Add(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
1925{
1959
1960 if (hout) hout->Reset();
1961
1962 if (!h1 || !h2 || !hout) return 1;
1963
1964 Int_t ndim1=h1->GetDimension();
1965 Int_t ndim2=h2->GetDimension();
1966 Int_t ndimo=hout->GetDimension();
1967
1968 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
1969 {
1970 cout << " *NcBlocks::Add* Error : Histograms should all be 1-dimensional." << endl;
1971 return 1;
1972 }
1973
1974 // Make the X-axis of "hout" identical to the X-axis of "h1"
1975 TString name=hout->GetName();
1976 h1->Copy(*hout);
1977 hout->Reset();
1978
1979 TString name1=h1->GetName();
1980 if (name1=="") name1="h1";
1981
1982 TString name2=h2->GetName();
1983 if (name2=="") name2="h2";
1984
1985 TString sc="+";
1986 if (c<0) sc="-";
1987 if (fabs(c)!=1)
1988 {
1989 sc.Form("%-+.3g",c);
1990 sc+="*";
1991 }
1992
1993 TString sd="";
1994 sd.Form("%-+.3g",d);
1995
1996 TString title="Resulting histogram of: ";
1997 title+=name1;
1998 if (c)
1999 {
2000 title+=sc;
2001 title+=name2;
2002 }
2003 if (d) title+=sd;
2004 if (scale)
2005 {
2006 title+=" (scaled w.r.t. bin size)";
2007 }
2008 else
2009 {
2010 title+=" (not scaled w.r.t. bin size)";
2011 }
2012 title+=";";
2013 TAxis* axis1=h1->GetXaxis();
2014 title+=axis1->GetTitle();
2015 title+=";";
2016 axis1=h1->GetYaxis();
2017 title+=axis1->GetTitle();
2018
2019 hout->SetName(name);
2020 hout->SetTitle(title);
2021
2022 Int_t nb1=h1->GetNbinsX();
2023 Int_t nb2=h2->GetNbinsX();
2024
2025 if (!nb1 || !nb2) return 1;
2026
2027 // Get the largest bin size of "h1"
2028 Double_t bwidth1=0;
2029 Double_t bwmax1=0;
2030 Int_t imax1=0;
2031 for (Int_t i=1; i<=nb1; i++)
2032 {
2033 bwidth1=h1->GetBinWidth(i);
2034 if (i==1 || bwidth1>bwmax1)
2035 {
2036 bwmax1=bwidth1;
2037 imax1=i;
2038 }
2039 }
2040
2041 // Get the smallest bin size of "h2"
2042 Double_t bwidth2=0;
2043 Double_t bwmin2=0;
2044 Int_t imin2=0;
2045 for (Int_t i=1; i<=nb2; i++)
2046 {
2047 bwidth2=h2->GetBinWidth(i);
2048 if (i==1 || bwidth2<bwmin2)
2049 {
2050 bwmin2=bwidth2;
2051 imin2=i;
2052 }
2053 }
2054
2055 Double_t ratio=bwmax1/bwmin2;
2056 if (ratio>1.001)
2057 {
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);
2060 return 1;
2061 }
2062
2063 // Loop over all the bins of the input histogram h1
2064 Double_t x1=0;
2065 Double_t y1=0;
2066 Int_t i2=0;
2067 Double_t y2=0;
2068 Double_t ynew=0;
2069 TAxis* axis2=h2->GetXaxis();
2070 for (Int_t i1=1; i1<=nb1; i1++)
2071 {
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);
2078
2079 // Do not consider underflow or overflow bins
2080 if (i2<1 || i2>nb2) continue;
2081
2082 if (scale) y2*=bwidth1/bwidth2;
2083 ynew=y1+c*y2+d;
2084 hout->SetBinContent(i1,ynew);
2085 }
2086
2087 return 0;
2088}
2089
2090Int_t NcBlocks::Add(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2091{
2113
2114 if (gout) gout->Set(0);
2115
2116 if (!gr || !h || !gout) return 1;
2117
2118 TString nameg=gr->GetName();
2119 if (nameg=="") nameg="gr";
2120
2121 TString nameh=h->GetName();
2122 if (nameh=="") nameh="h";
2123
2124 TString sc="+";
2125 if (c<0) sc="-";
2126 if (fabs(c)!=1)
2127 {
2128 sc.Form("%-+.3g",c);
2129 sc+="*";
2130 }
2131
2132 TString sd="";
2133 sd.Form("%-+.3g",d);
2134
2135 TString title="Resulting graph of: ";
2136 title+=nameg;
2137 if (c)
2138 {
2139 title+=sc;
2140 title+=nameh;
2141 }
2142 if (d) title+=sd;
2143 title+=";";
2144 TAxis* axis=0;
2145 axis=gr->GetXaxis();
2146 title+=axis->GetTitle();
2147 title+=";";
2148 axis=gr->GetYaxis();
2149 title+=axis->GetTitle();
2150
2151 gout->SetTitle(title);
2152
2153 Int_t ndim=h->GetDimension();
2154
2155 if (ndim!=1)
2156 {
2157 cout << " *NcBlocks::Add* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2158 return 1;
2159 }
2160
2161 Int_t np=gr->GetN();
2162 Int_t nb=h->GetNbinsX();
2163
2164 if (!np || !nb) return 1;
2165
2166 // Loop over all the points in the graph
2167 Double_t x=0;
2168 Double_t y=0;
2169 Double_t ex=0;
2170 Double_t ey=0;
2171 Int_t hbin=0;
2172 Double_t hval=0;
2173 Double_t ynew=0;
2174 axis=h->GetXaxis();
2175 for (Int_t i=0; i<np; i++)
2176 {
2177 gr->GetPoint(i,x,y);
2178 hbin=axis->FindFixBin(x);
2179 hval=h->GetBinContent(hbin);
2180
2181 // Do not consider underflow or overflow bins
2182 if (hbin<1 || hbin>nb) continue;
2183
2184 ynew=y+c*hval+d;
2185 gout->SetPoint(i,x,ynew);
2186
2187 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2188 {
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);
2194 }
2195 }
2196
2197 return 0;
2198}
2199
2200Int_t NcBlocks::Divide(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
2201{
2233
2234 if (hout) hout->Reset();
2235
2236 if (!c)
2237 {
2238 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2239 return 1;
2240 }
2241
2242 if (!h1 || !h2 || !hout) return 1;
2243
2244 Int_t ndim1=h1->GetDimension();
2245 Int_t ndim2=h2->GetDimension();
2246 Int_t ndimo=hout->GetDimension();
2247
2248 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2249 {
2250 cout << " *NcBlocks::Divide* Error : Histograms should all be 1-dimensional." << endl;
2251 return 1;
2252 }
2253
2254 // Make the X-axis of "hout" identical to the X-axis of "h1"
2255 TString name=hout->GetName();
2256 h1->Copy(*hout);
2257 hout->Reset();
2258
2259 TString name1=h1->GetName();
2260 if (name1=="") name1="h1";
2261
2262 TString name2=h2->GetName();
2263 if (name2=="") name2="h2";
2264
2265 TString sc="/";
2266 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2267
2268 TString sd;
2269 if (c>0)
2270 {
2271 sd="";
2272 if (d) sd.Form("%-.3g+",d);
2273 }
2274 else
2275 {
2276 sd="-";
2277 if (d) sd.Form("%-.3g-",d);
2278 }
2279
2280 TString title="Resulting histogram of: ";
2281 if (sd!="") title+=sd;
2282 if (c)
2283 {
2284 title+=name1;
2285 if (sc.Contains("*")) title+="/(";
2286 title+=sc;
2287 title+=name2;
2288 if (sc.Contains("*")) title+=")";
2289 }
2290 if (scale)
2291 {
2292 title+=" (scaled w.r.t. bin size)";
2293 }
2294 else
2295 {
2296 title+=" (not scaled w.r.t. bin size)";
2297 }
2298 title+=";";
2299 TAxis* axis1=h1->GetXaxis();
2300 title+=axis1->GetTitle();
2301 title+=";";
2302 axis1=h1->GetYaxis();
2303 title+=axis1->GetTitle();
2304
2305 hout->SetName(name);
2306 hout->SetTitle(title);
2307
2308 Int_t nb1=h1->GetNbinsX();
2309 Int_t nb2=h2->GetNbinsX();
2310
2311 if (!nb1 || !nb2) return 1;
2312
2313 // Get the largest bin size of "h1"
2314 Double_t bwidth1=0;
2315 Double_t bwmax1=0;
2316 Int_t imax1=0;
2317 for (Int_t i=1; i<=nb1; i++)
2318 {
2319 bwidth1=h1->GetBinWidth(i);
2320 if (i==1 || bwidth1>bwmax1)
2321 {
2322 bwmax1=bwidth1;
2323 imax1=i;
2324 }
2325 }
2326
2327 // Get the smallest bin size of "h2"
2328 Double_t bwidth2=0;
2329 Double_t bwmin2=0;
2330 Int_t imin2=0;
2331 for (Int_t i=1; i<=nb2; i++)
2332 {
2333 bwidth2=h2->GetBinWidth(i);
2334 if (i==1 || bwidth2<bwmin2)
2335 {
2336 bwmin2=bwidth2;
2337 imin2=i;
2338 }
2339 }
2340
2341 Double_t ratio=bwmax1/bwmin2;
2342 if (ratio>1.001)
2343 {
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);
2346 return 1;
2347 }
2348
2349 // Loop over all the bins of the input histogram h1
2350 Double_t x1=0;
2351 Double_t y1=0;
2352 Int_t i2=0;
2353 Double_t y2=0;
2354 Double_t val=0;
2355 Double_t ynew=0;
2356 TAxis* axis2=h2->GetXaxis();
2357 for (Int_t i1=1; i1<=nb1; i1++)
2358 {
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);
2365
2366 // Do not consider underflow or overflow bins
2367 if (i2<1 || i2>nb2) continue;
2368
2369 val=c*y2;
2370 if (!val) continue;
2371
2372 if (scale) y2*=bwidth1/bwidth2;
2373 ynew=d+y1/val;
2374 hout->SetBinContent(i1,ynew);
2375 }
2376
2377 return 0;
2378}
2379
2380Int_t NcBlocks::Divide(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2381{
2401
2402 if (gout) gout->Set(0);
2403
2404 if (!c)
2405 {
2406 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2407 return 1;
2408 }
2409
2410 if (!gr || !h || !gout) return 1;
2411
2412 TString nameg=gr->GetName();
2413 if (nameg=="") nameg="gr";
2414
2415 TString nameh=h->GetName();
2416 if (nameh=="") nameh="h";
2417
2418 TString sc="/";
2419 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2420
2421 TString sd;
2422 if (c>0)
2423 {
2424 sd="";
2425 if (d) sd.Form("%-.3g+",d);
2426 }
2427 else
2428 {
2429 sd="-";
2430 if (d) sd.Form("%-.3g-",d);
2431 }
2432
2433 TString title="Resulting graph of: ";
2434 if (sd!="") title+=sd;
2435 if (c)
2436 {
2437 title+=nameg;
2438 if (sc.Contains("*")) title+="/(";
2439 title+=sc;
2440 title+=nameh;
2441 if (sc.Contains("*")) title+=")";
2442 }
2443 title+=";";
2444 TAxis* axis=0;
2445 axis=gr->GetXaxis();
2446 title+=axis->GetTitle();
2447 title+=";";
2448 axis=gr->GetYaxis();
2449 title+=axis->GetTitle();
2450
2451 gout->SetTitle(title);
2452
2453 Int_t ndim=h->GetDimension();
2454
2455 if (ndim!=1)
2456 {
2457 cout << " *NcBlocks::Divide* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2458 return 1;
2459 }
2460
2461 Int_t np=gr->GetN();
2462 Int_t nb=h->GetNbinsX();
2463
2464 if (!np || !nb) return 1;
2465
2466 // Loop over all the points in the graph
2467 Double_t x=0;
2468 Double_t y=0;
2469 Double_t ex=0;
2470 Double_t ey=0;
2471 Int_t hbin=0;
2472 Double_t hval=0;
2473 Double_t val=0;
2474 Double_t ynew=0;
2475 axis=h->GetXaxis();
2476 Int_t j=0;
2477 for (Int_t i=0; i<np; i++)
2478 {
2479 gr->GetPoint(i,x,y);
2480 hbin=axis->FindFixBin(x);
2481 hval=h->GetBinContent(hbin);
2482
2483 // Do not consider underflow or overflow bins
2484 if (hbin<1 || hbin>nb) continue;
2485
2486 val=c*hval;
2487 if (!val) continue;
2488
2489 ynew=d+y/val;
2490 gout->SetPoint(j,x,ynew);
2491
2492 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2493 {
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);
2499 }
2500
2501 j++;
2502 }
2503
2504 return 0;
2505}
2506
2507Int_t NcBlocks::Rebin(TH1* hin,TH1* hout,Bool_t scale,Int_t nbins,Double_t xmin,Double_t xmax)
2508{
2543
2544 if (hout) hout->Reset();
2545
2546 if (!hin || !hout) return 0;
2547
2548 Int_t ndimi=hin->GetDimension();
2549 Int_t ndimo=hout->GetDimension();
2550
2551 if (ndimi!=1 || ndimo!=1)
2552 {
2553 cout << " *NcBlocks::Rebin* Error : Histograms should both be 1-dimensional." << endl;
2554 return 0;
2555 }
2556
2557 Int_t nb1=hin->GetNbinsX();
2558
2559 if (!nb1) return 0;
2560
2561 TAxis* xaxis=hin->GetXaxis();
2562 TAxis* yaxis=hin->GetYaxis();
2563
2564 if (xmax<xmin)
2565 {
2566 xmin=xaxis->GetXmin();
2567 xmax=xaxis->GetXmax();
2568 }
2569
2570 Double_t bwidth1=0;
2571 Double_t xlow1=0;
2572 Double_t xup1=0;
2573
2574 // Automatic binwidth setting
2575 if (nbins<=0)
2576 {
2577 Double_t bwmin=-1;
2578 for (Int_t i=1; i<=nb1; i++)
2579 {
2580 bwidth1=hin->GetBinWidth(i);
2581 xlow1=hin->GetBinLowEdge(i);
2582 xup1=xlow1+bwidth1;
2583
2584 if (xlow1>=xmax || xup1<=xmin) continue;
2585
2586 if (bwmin<0 || bwidth1<bwmin) bwmin=bwidth1;
2587 }
2588
2589 if (bwmin<0)
2590 {
2591 cout << " *NcBlocks::Rebin* Error : Input histogram had no data in requested interval [xmin,xmax]." << endl;
2592 return 0;
2593 }
2594
2595 Double_t rnbins=(xmax-xmin)/bwmin;
2596 nbins=int(rnbins);
2597 Double_t diff=rnbins-double(nbins);
2598 if (diff) nbins=nbins+1;
2599 }
2600
2601 hout->SetBins(nbins,xmin,xmax);
2602 Double_t bwidth=hout->GetBinWidth(1);
2603
2604 TString name=hin->GetName();
2605 if (name=="") name="hin";
2606
2607 TString snb="";
2608 snb.Form(" nbins=%-i",nbins);
2609
2610 TString smin="";
2611 smin.Form(" xmin=%-.3g",xmin);
2612
2613 TString smax="";
2614 smax.Form(" xmax=%-.3g",xmax);
2615
2616 TString title="Uniformly binned version of histogram: ";
2617 title+=name;
2618 title+=snb;
2619 title+=smin;
2620 title+=smax;
2621 if (scale)
2622 {
2623 title+=" (scaled w.r.t. bin size)";
2624 }
2625 else
2626 {
2627 title+=" (not scaled w.r.t. bin size)";
2628 }
2629 title+=";";
2630 title+=xaxis->GetTitle();
2631 title+=";";
2632 title+=yaxis->GetTitle();
2633
2634 hout->SetTitle(title);
2635
2636 // Check if the binning of "hout" is not coarser than that of "hin" within [xmin,xmax]
2637 for (Int_t i=1; i<=nb1; i++)
2638 {
2639 bwidth1=hin->GetBinWidth(i);
2640 xlow1=hin->GetBinLowEdge(i);
2641 xup1=xlow1+bwidth1;
2642
2643 if (xlow1>=xmax || xup1<=xmin) continue;
2644
2645 if (bwidth1<bwidth)
2646 {
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);
2649 return 0;
2650 }
2651 }
2652
2653 // Loop over all the bins of the output histogram hout
2654 Double_t x=0;
2655 Int_t i1=0;
2656 Double_t y1=0;
2657 for (Int_t i=1; i<=nbins; i++)
2658 {
2659 x=hout->GetBinCenter(i);
2660 i1=xaxis->FindFixBin(x);
2661
2662 // Do not consider underflow or overflow bins
2663 if (i1<1 || i1>nb1) continue;
2664
2665 y1=hin->GetBinContent(i1);
2666 bwidth1=hin->GetBinWidth(i1);
2667 if (scale) y1=y1*bwidth/bwidth1;
2668 hout->SetBinContent(i,y1);
2669 }
2670
2671 return nbins;
2672}
2673
ClassImp(NcBlocks)
(Bayesian) Block treatment of sequential data.
Definition NcBlocks.h:17
Double_t GetBlockFitness(Double_t n, Double_t len)
Definition NcBlocks.cxx:490
virtual ~NcBlocks()
Definition NcBlocks.cxx:406
Double_t GetPrior(Int_t n, Double_t fpr)
Definition NcBlocks.cxx:426
Int_t Rebin(TH1 *hin, TH1 *hout, Bool_t scale, Int_t nbins=0, Double_t xmin=0, Double_t xmax=-1)
Int_t fMode
Definition NcBlocks.h:44
Int_t Divide(TH1 *h1, TH1 *h2, TH1 *hout, Bool_t scale, Double_t c, Double_t d=0)
Int_t Add(TH1 *h1, TH1 *h2, TH1 *hout, Bool_t scale, Double_t c, Double_t d=0)
Double_t GetBlocks(TH1 *hin, Double_t fpr, TH1 *hout, Int_t ntrig=0)
Definition NcBlocks.cxx:523
Sampling and statistics tools for various multi-dimensional data samples.
Definition NcSample.h:28
Int_t GetStoreMode() const
Int_t GetN() const
Int_t GetIndex(TString name) const
Double_t GetMedian(Int_t i)
TGraphErrors GetGraphErrors(TGraph *g, Int_t ix=0, Int_t iy=0, TF1 *fx=0, TF1 *fy=0)
void Reset()
Definition NcSample.cxx:255
Double_t GetRMS(Int_t i) const
void Enter(Double_t x)
Definition NcSample.cxx:357
Double_t GetMean(Int_t i) const
TString GetVariableName(Int_t i) const
Int_t GetDimension() const
TGraph GetGraph(Int_t i, TF1 *f=0)
void SetStoreMode(Int_t mode=1, Int_t nmax=0, Int_t i=0)
Double_t GetEntry(Int_t i, Int_t j, Int_t mode=0, Int_t k=0)