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{
558
559 if (!hin || !hout) return 0;
560
561 Int_t n=hin->GetNbinsX();
562
563 if (!n) return 0;
564
565 if (fabs(fpr)>1)
566 {
567 printf(" *NcBlocks::GetBlocks* Inconsistent parameter fpr=%-g for histogram treatment. \n",fpr);
568 return 0;
569 }
570
571 // Set the Data Mode for binned data, if it was not set already
572 // Also initialize the output histogram title for binned data
573 if (!fMode)
574 {
575 fMode=2;
576 TString title;
577 title.Form("Bayesian Block representation with FPR=%-.3g",fpr);
578 hout->SetTitle(title);
579 }
580
581 TArrayD best(n); // Array with optimal fitness values
582 TArrayI last(n); // Array with indices of optimal last change points
583 TArrayD lengths(n); // Array with the lengths of the optimal partition blocks
584 TArrayD counts(n); // Array with the event counts of the optimal partition blocks
585
586 Double_t blen=0; // Length of a certain block
587 Double_t bcount=0; // Event count in a certain block
588 Double_t prior=0; // Contribution of each block to the prior fitness for a certain partition
589 Double_t bfit=0; // Fitness of a certain block
590 Double_t pfit=0; // Fitness of a certain partition
591
592 Double_t xlow=0;
593 Double_t xup=0;
594 Int_t index=0;
595
596 // Variables for measurement of an observable (i.e. Data Mode 3)
597 Double_t yk=0; // Measured value
598 Double_t sigk=0; // Error on the measured value
599 Double_t a=0;
600 Double_t b=0;
601
602 // The parameters for the optimal partition
603 Double_t optfit=0;
604 Int_t optj=0;
605 Double_t optlen=0;
606 Double_t optcount=0;
607
608 // Parameters and counter for trigger mode
609 Int_t oldoptj=0;
610 Double_t oldoptlen=0;
611 Int_t ncp=0;
612 Double_t xtrig=0;
613 Double_t ytrig=0;
614 Double_t oldytrig=0;
615
616 // Add Data Cells one by one to the sample to be partioned
617 Int_t ncells=0;
618 Int_t first=1;
619 Int_t jstart=1;
620 for (Int_t i=1; i<=n; i++)
621 {
622 ncells=i;
623 prior=GetPrior(i,fabs(fpr));
624 xup=hin->GetBinLowEdge(i)+hin->GetBinWidth(i);
625 // Loop over all possible block partitions for this Data Cell sample
626 // For fpr<0 it will ignore the already constructed blocks in view of cpu speed
627 jstart=1;
628 if (fpr<0 && optj) jstart=optj;
629 first=1;
630 for (Int_t j=jstart; j<=i; j++)
631 {
632 xlow=hin->GetBinLowEdge(j);
633 blen=xup-xlow;
634 bcount=0;
635 bfit=0;
636 a=0;
637 b=0;
638 if (fMode==1 || fMode==2)
639 {
640 bcount=hin->Integral(j,i);
641 bfit=GetBlockFitness(fabs(bcount),blen);
642 }
643 if (fMode==3)
644 {
645 for (Int_t k=j; k<=i; k++)
646 {
647 yk=hin->GetBinContent(k);
648 sigk=hin->GetBinError(k);
649 sigk=sigk*sigk;
650 if (sigk)
651 {
652 a+=1./(sigk);
653 b+=yk/sigk;
654 }
655 }
656 if (a)
657 {
658 bcount=b/a; // Weighted mean of the y-values in the block
659 bfit=b*b/(2.*a);
660 }
661 }
662 pfit=prior+bfit;
663 index=j-1; // Array index reduced by 1 because of C++ array index convention
664 if (index>0) pfit+=best.At(index-1);
665
666 // Record attributes for the optimal partition
667 if (first)
668 {
669 optfit=pfit;
670 optj=j;
671 optlen=blen;
672 optcount=bcount;
673 first=0;
674 }
675 else
676 {
677 if (pfit>optfit)
678 {
679 optfit=pfit;
680 optj=j;
681 optlen=blen;
682 optcount=bcount;
683 }
684 }
685 } // End of loop over possible block partitions
686
687 // Store the attributes of the optimal partition
688 best.SetAt(optfit,i-1);
689 last.SetAt(optj,i-1);
690 lengths.SetAt(optlen,i-1);
691 counts.SetAt(optcount,i-1);
692
693 if (!ntrig || optj==1) continue;
694
695 // Check for triggering on a new change point
696 oldoptj=last.At(i-2);
697 oldoptlen=lengths.At(i-2);
698 oldytrig=0;
699 if ((fMode==1 || fMode==2) && oldoptlen) oldytrig=counts.At(i-2)/oldoptlen;
700 if (fMode==3) oldytrig=counts.At(i-2);
701 ytrig=0;
702 if ((fMode==1 || fMode==2) && optlen) ytrig=counts.At(i-1)/optlen;
703 if (fMode==3) ytrig=counts.At(i-1);
704
705 if (optj>oldoptj && ((ntrig>0 && ytrig>oldytrig) || (ntrig<0 && ytrig<oldytrig)))
706 {
707 ncp++;
708 xtrig=hin->GetBinLowEdge(optj);
709 }
710
711 // Stop when the requested number of triggers has been reached
712 if (ncp>=abs(ntrig)) break;
713
714 } // End of adding Data Cells one by one
715
716 // Obtain the change points and corresponding partition information
717 Int_t jcp=0;
718 index=ncells;
719 Double_t x=0;
720 Double_t y=0;
721 ncp=0;
722 TArrayD xarr(ncells+1);
723 TArrayD yarr(ncells+1);
724 while (index>0)
725 {
726 index--; // Reduce array index by 1 because of C++ array convention
727 jcp=last.At(index);
728 ncp++;
729 x=hin->GetBinLowEdge(jcp)+lengths.At(index);
730 y=0;
731 if (fMode==1 || fMode==2)
732 {
733 if (lengths.At(index)) y=counts.At(index)/lengths.At(index);
734 }
735 if (fMode==3) y=counts.At(index);
736
737 xarr.SetAt(x,ncp-1);
738 yarr.SetAt(y,ncp-1);
739
740 // Also mark the start of the first bin
741 if (jcp==1) xarr.SetAt(hin->GetBinLowEdge(jcp),ncp);
742
743 index=jcp-1;
744 }
745
746 // Create the corresponding variable binned histogram
747 Double_t* xbins=new Double_t[ncp+1];
748 Double_t* yvals=new Double_t[ncp+1];
749
750 for (Int_t i=0; i<=ncp; i++)
751 {
752 xbins[i]=xarr.At(ncp-i);
753 yvals[i]=yarr.At(ncp-i);
754 }
755
756 hout->SetBins(ncp,xbins);
757 for (Int_t i=1; i<=ncp; i++)
758 {
759 hout->SetBinContent(i,yvals[i]);
760 }
761
762 // Determine the first trigger point after the full sample analysis
763 if (!ntrig) xtrig=xbins[1];
764
765 hout->SetLineWidth(2);
766 hout->SetLineColor(kBlue);
767 hout->SetStats(kFALSE);
768
769 // Set the output histogram and axes titles
770 TString title,str;
771 title="Bayesian Block representation for histogram ";
772 title+=hin->GetName();
773 title+=" with FPR= %-.3g";
774 title+=";";
775 str=hin->GetXaxis()->GetTitle();
776 if (str=="") str="Recordings (e.g. time)";
777 title+=str;
778 title+=";";
779 str=hin->GetYaxis()->GetTitle();
780 if (str=="") str="Counts";
781 title+=str;
782 str=title.Format(title.Data(),fpr);
783 hout->SetTitle(str.Data());
784
785 // Indicate the requested trigger in a legend
786 if (ntrig)
787 {
788 str.Form("Requested trigger at : %-.3g",xtrig);
789
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);
794
795 TList* hlist=hout->GetListOfFunctions();
796 hlist->Add(leg);
797 }
798
799 // Reset the Data Mode for subsequent invokations
800 fMode=0;
801
802 delete [] xbins;
803 delete [] yvals;
804
805 return xtrig;
806}
807
808Double_t NcBlocks::GetBlocks(NcSample s,Int_t i,Double_t fpr,TH1* hout,Int_t ntrig)
809{
845
846 if (!hout)
847 {
848 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
849 return 0;
850 }
851
852 Int_t n=s.GetN();
853 Int_t store=s.GetStoreMode();
854 Int_t dim=s.GetDimension();
855
856 if (n<2 || !store || dim<1 || i<1 || i>dim || fabs(fpr)>1)
857 {
858 cout << " *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
859 cout << " Store Mode:" << store << " Entries:" << n << " Dimension:" << dim << " i:" << i << " fpr:" << fpr << endl;
860 return 0;
861 }
862
863 // Set Data Mode for unbinned event data
864 fMode=1;
865
866 // Represent each observation as 1 count in a variable binned histogram
867 TArrayD xarr(n+1);
868 Double_t val=0;
869 Int_t idstore=0;
870 for (Int_t idx=1; idx<=n; idx++)
871 {
872 val=s.GetEntry(idx,i,1,i);
873
874 // Check for a double occurance of an observation
875 if (idstore>0)
876 {
877 if (val-xarr[idstore-1]<=0) continue;
878 }
879
880 xarr[idstore]=val;
881 idstore++;
882 }
883
884 // Obtain the correct amount of bin lower edges
885 if (idstore<n)
886 {
887 n=idstore;
888 xarr.Set(n);
889 }
890
891 Double_t* xbins=xarr.GetArray();
892 TH1F hin("","",n-1,xbins);
893 for (Int_t j=1; j<n; j++)
894 {
895 hin.SetBinContent(j,1);
896 }
897
898 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
899
900 // Set the output histogram and axes titles
901 TString title,str;
902 title="Bayesian Block representation for NcSample ";
903 title+=s.GetName();
904 title+=" with FPR=%-.3g";
905 title+=";Recordings of variable ";
906 title+=i;
907 title+=" (";
908 title+=s.GetVariableName(i);
909 title+=")";
910 title+=";Count rate";
911 str=title.Format(title.Data(),fpr);
912 hout->SetTitle(str.Data());
913
914 return xtrig;
915}
916
917Double_t NcBlocks::GetBlocks(NcSample s,TString name,Double_t fpr,TH1* hout,Int_t ntrig)
918{
954
955 Int_t i=s.GetIndex(name);
956
957 Double_t xtrig=GetBlocks(s,i,fpr,hout,ntrig);
958
959 return xtrig;
960}
961
962Double_t NcBlocks::GetBlocks(Int_t n,Double_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
963{
1002
1003 if (!hout)
1004 {
1005 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for array treatment." << endl;
1006 return 0;
1007 }
1008
1009 if (!arr || n<2 || fabs(fpr)>1)
1010 {
1011 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1012 if (!arr)
1013 {
1014 cout << " Array pointer is 0." << endl;
1015 }
1016 else
1017 {
1018 cout << " Entries:" << n << " fpr:" << fpr << endl;
1019 }
1020 return 0;
1021 }
1022
1023 NcSample s;
1024 s.SetStoreMode();
1025
1026 for (Int_t i=0; i<n; i++)
1027 {
1028 s.Enter(arr[i]);
1029 }
1030
1031 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1032
1033 // Set the output histogram and axes titles
1034 TString title;
1035 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1036 title+=";Recordings (e.g. time);Count rate";
1037 hout->SetTitle(title);
1038
1039 return xtrig;
1040}
1041
1042Double_t NcBlocks::GetBlocks(Int_t n,Float_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
1043{
1082
1083 if (!hout)
1084 {
1085 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for array treatment." << endl;
1086 return 0;
1087 }
1088
1089 if (!arr || n<2 || fabs(fpr)>1)
1090 {
1091 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1092 if (!arr)
1093 {
1094 cout << " Array pointer is 0." << endl;
1095 }
1096 else
1097 {
1098 cout << " Entries:" << n << " fpr:" << fpr << endl;
1099 }
1100 return 0;
1101 }
1102
1103 NcSample s;
1104 s.SetStoreMode();
1105
1106 for (Int_t i=0; i<n; i++)
1107 {
1108 s.Enter(arr[i]);
1109 }
1110
1111 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1112
1113 // Set the output histogram and axes titles
1114 TString title;
1115 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1116 title+=";Recordings (e.g. time);Count rate";
1117 hout->SetTitle(title);
1118
1119 return xtrig;
1120}
1121
1122Double_t NcBlocks::GetBlocks(TGraphErrors gr,Double_t fpr,TH1* hout,Int_t ntrig)
1123{
1160
1161 if (!hout)
1162 {
1163 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for TGraphErrors treatment." << endl;
1164 return 0;
1165 }
1166
1167 Int_t n=gr.GetN();
1168
1169 if (n<2 || fabs(fpr)>1)
1170 {
1171 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraphErrors treatment." << endl;
1172 cout << " Entries:" << n << " fpr:" << fpr << endl;
1173 return 0;
1174 }
1175
1176 // Set Data Mode for measurements of a continuous observable
1177 fMode=3;
1178
1179 // Sort the data points with increasing x-value
1180 gr.Sort();
1181
1182 // Represent each observation as a value in a variable binned histogram
1183 Double_t* xbins=new Double_t[n+1];
1184 Double_t x=0;
1185 Double_t y=0;
1186 Double_t err=0;
1187 Double_t dist=0;
1188 Double_t dmin=-1;
1189 for (Int_t i=0; i<n; i++)
1190 {
1191 gr.GetPoint(i,x,y);
1192 err=fabs(gr.GetErrorX(i));
1193 xbins[i]=x-err;
1194 if (i>0)
1195 {
1196 dist=xbins[i]-xbins[i-1];
1197 if (dmin<0 || dist<dmin) dmin=dist;
1198 }
1199 }
1200 // Add an extra bin to contain the last measurement
1201 xbins[n]=xbins[n-1]+dmin;
1202
1203 TH1F hin("","",n,xbins);
1204 for (Int_t j=1; j<=n; j++)
1205 {
1206 gr.GetPoint(j-1,x,y);
1207 err=fabs(gr.GetErrorY(j-1));
1208 hin.SetBinContent(j,y);
1209 hin.SetBinError(j,err);
1210 }
1211
1212 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
1213
1214 // Set the output histogram and axes titles
1215 TString title,str;
1216 TString xtitle="Samplings (e.g. time)";
1217 TString ytitle="Measured value";
1218 TAxis* ax=gr.GetXaxis();
1219 if (ax)
1220 {
1221 str=ax->GetTitle();
1222 if (str != "") xtitle=str;
1223 }
1224 ax=gr.GetYaxis();
1225 if (ax)
1226 {
1227 str=ax->GetTitle();
1228 if (str != "") ytitle=str;
1229 }
1230 title="Bayesian Block representation for TGraphErrors ";
1231 title+=gr.GetName();
1232 title+=" with FPR=%-.3g;";
1233 title+=xtitle;
1234 title+=";";
1235 title+=ytitle;
1236 str=title.Format(title,fpr);
1237 hout->SetTitle(str);
1238
1239 delete [] xbins;
1240
1241 return xtrig;
1242}
1243
1244Double_t NcBlocks::GetBlocks(TGraph gr,TF1 f,Double_t fpr,TH1* hout,Int_t ntrig)
1245{
1295
1296 NcSample s;
1297 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&f);
1298
1299 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1300
1301 TString str=" and input errors : ";
1302 str+=f.GetExpFormula("p");
1303 str.ReplaceAll("x","y");
1304
1305 TString title=hout->GetTitle();
1306 title+=str;
1307 hout->SetTitle(title);
1308
1309 return xtrig;
1310}
1311
1312Double_t NcBlocks::GetBlocks(TGraph gr,TString f,Double_t fpr,TH1* hout,Int_t ntrig)
1313{
1364
1365 TF1 func("func",f);
1366 NcSample s;
1367 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&func);
1368
1369 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1370
1371 TString str=" and input errors : ";
1372 str+=func.GetExpFormula("p");
1373 str.ReplaceAll("x","y");
1374
1375 TString title=hout->GetTitle();
1376 title+=str;
1377 hout->SetTitle(title);
1378
1379 return xtrig;
1380}
1381
1382Double_t NcBlocks::GetBlocks(TGraph gr,Double_t nrms,Double_t fpr,TH1* hout,Int_t ntrig)
1383{
1434
1435 // Obtain the RMS deviation of all the y-values
1436 Double_t rms=gr.GetRMS(2);
1437
1438 // Determine the error for each y-value and convert into a function format
1439 Double_t err=fabs(nrms*rms);
1440
1441 TString f;
1442 f.Form("%-.5g",err);
1443
1444 Double_t xtrig=GetBlocks(gr,f,fpr,hout,ntrig);
1445
1446 TString str;
1447 str.Form(" from nrms=%-.3g",fabs(nrms));
1448
1449 TString title=hout->GetTitle();
1450 title+=str;
1451 hout->SetTitle(title);
1452
1453 return xtrig;
1454}
1455
1456Int_t NcBlocks::GetBlocks(TH1* hin,TH1* hout,Int_t n,Int_t mode)
1457{
1486
1487 if (!hin)
1488 {
1489 cout << " *NcBlocks::GetBlocks* Error : Input histogram not specified." << endl;
1490 return 0;
1491 }
1492
1493 if (!hout)
1494 {
1495 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1496 return 0;
1497 }
1498
1499 Int_t nbins=hin->GetNbinsX();
1500
1501 if (!nbins || n<1 || n>nbins || mode<0 || mode>2)
1502 {
1503 cout << " *NcBlocks::GetBlocks* Inconsistent input nbins=" << nbins << " n=" << n << " mode=" << mode << endl;
1504 return 0;
1505 }
1506
1507 // Retrieve the various sets of "n" bins from the input histogram
1508 Int_t jbin=0;
1509 Double_t x=0;
1510 Double_t y=0;
1511 Double_t xlow=0;
1512 Double_t xup=0;
1513 Double_t binwidth=0;
1514 NcSample s;
1515 if (mode==1) s.SetStoreMode(1);
1516 Int_t nblocks=0;
1517 Double_t average=0;
1518 TArrayD xarr(nbins);
1519 TArrayD yarr(nbins);
1520 while (jbin<nbins)
1521 {
1522 for (Int_t i=0; i<n; i++)
1523 {
1524 jbin++;
1525
1526 if (jbin>nbins) break;
1527
1528 x=hin->GetBinCenter(jbin);
1529 y=hin->GetBinContent(jbin);
1530 binwidth=hin->GetBinWidth(jbin);
1531 if (i==0) xlow=hin->GetBinLowEdge(jbin);
1532 xup=x+0.5*binwidth;
1533 s.Enter(x,y);
1534 }
1535 if (mode==0) average=s.GetMean(2);
1536 if (mode==1) average=s.GetMedian(2);
1537 if (mode==2) average=s.GetRMS(2);
1538 nblocks++;
1539 xarr.SetAt(xlow,nblocks-1);
1540 yarr.SetAt(average,nblocks-1);
1541 s.Reset();
1542 } // End of while loop
1543
1544 // Create the corresponding variable binned output histogram
1545 Double_t* xbins=new Double_t[nblocks+1];
1546 Double_t* yvals=new Double_t[nblocks+1];
1547
1548 for (Int_t i=0; i<nblocks; i++)
1549 {
1550 xbins[i]=xarr.At(i);
1551 yvals[i]=yarr.At(i);
1552 }
1553 // Add an extra bin to contain the last data
1554 xbins[nblocks]=(1.+1e-6)*xup;
1555 yvals[nblocks]=yvals[nblocks-1];
1556
1557 hout->SetBins(nblocks,xbins);
1558 for (Int_t i=1; i<=nblocks; i++)
1559 {
1560 hout->SetBinContent(i,yvals[i-1]);
1561 }
1562
1563 hout->SetLineWidth(2);
1564 hout->SetLineColor(kBlue);
1565 hout->SetStats(kFALSE);
1566
1567 // Set the output histogram and axes titles
1568 TString title,str;
1569 title="Block representation for histogram ";
1570 title+=hin->GetName();
1571 title+=" grouped in %-d consecutive bins";
1572 title+=";";
1573 str=hin->GetXaxis()->GetTitle();
1574 if (str=="") str="Recordings (e.g. time)";
1575 title+=str;
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";
1581 title+=str;
1582 str=title.Format(title.Data(),n);
1583 hout->SetTitle(str.Data());
1584
1585 delete [] xbins;
1586 delete [] yvals;
1587
1588 return nblocks;
1589}
1590
1591Int_t NcBlocks::GetBlocks(NcSample s,Int_t i,TH1* hout,Int_t n,Int_t mode)
1592{
1622
1623 if (!hout)
1624 {
1625 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1626 return 0;
1627 }
1628
1629 Int_t nen=s.GetN();
1630 Int_t store=s.GetStoreMode();
1631 Int_t dim=s.GetDimension();
1632
1633 if (!store || dim<1 || i<1 || i>dim || n<1 || n>nen || mode<0 || mode>2)
1634 {
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;
1637 return 0;
1638 }
1639
1640 TGraph gr=s.GetGraph(i);
1641
1642 Int_t nblocks=GetBlocks(&gr,hout,n,mode);
1643
1644 // Set the output histogram and axes titles
1645 TString title,str;
1646 title="Block representation for NcSample ";
1647 title+=s.GetName();
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 ";
1654 title+=i;
1655 title+=" (";
1656 title+=s.GetVariableName(i);
1657 title+=")";
1658 str=title.Format(title.Data(),n);
1659 hout->SetTitle(str.Data());
1660
1661 return nblocks;
1662}
1663
1664Int_t NcBlocks::GetBlocks(NcSample s,TString name,TH1* hout,Int_t n,Int_t mode)
1665{
1695
1696 Int_t i=s.GetIndex(name);
1697
1698 Int_t nblocks=GetBlocks(s,i,hout,n,mode);
1699
1700 return nblocks;
1701}
1702
1703Int_t NcBlocks::GetBlocks(Int_t nr,Double_t* arr,TH1* hout,Int_t n,Int_t mode)
1704{
1733
1734 if (!hout)
1735 {
1736 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1737 return 0;
1738 }
1739
1740 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1741 {
1742 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1743 if (!arr)
1744 {
1745 cout << " Array pointer is 0." << endl;
1746 }
1747 else
1748 {
1749 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1750 }
1751 return 0;
1752 }
1753
1754 NcSample s;
1755 s.SetStoreMode();
1756
1757 for (Int_t i=0; i<nr; i++)
1758 {
1759 s.Enter(arr[i]);
1760 }
1761
1762 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1763
1764 // Set the output histogram and axes titles
1765 TString title;
1766 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1767 title+=";Sampling number";
1768 if (mode==0) title+=";Mean ";
1769 if (mode==1) title+=";Median ";
1770 if (mode==2) title+=";RMS ";
1771 title+="value";
1772 hout->SetTitle(title);
1773
1774 return nblocks;
1775}
1776
1777Int_t NcBlocks::GetBlocks(Int_t nr,Float_t* arr,TH1* hout,Int_t n,Int_t mode)
1778{
1807
1808 if (!hout)
1809 {
1810 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1811 return 0;
1812 }
1813
1814 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1815 {
1816 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1817 if (!arr)
1818 {
1819 cout << " Array pointer is 0." << endl;
1820 }
1821 else
1822 {
1823 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1824 }
1825 return 0;
1826 }
1827
1828 NcSample s;
1829 s.SetStoreMode();
1830
1831 for (Int_t i=0; i<nr; i++)
1832 {
1833 s.Enter(arr[i]);
1834 }
1835
1836 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1837
1838 // Set the output histogram and axes titles
1839 TString title;
1840 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1841 title+=";Sampling number";
1842 if (mode==0) title+=";Mean ";
1843 if (mode==1) title+=";Median ";
1844 if (mode==2) title+=";RMS ";
1845 title+="value";
1846 hout->SetTitle(title);
1847
1848 return nblocks;
1849}
1850
1851Int_t NcBlocks::GetBlocks(TGraph* gr,TH1* hout,Int_t n,Int_t mode)
1852{
1881
1882 if (!gr)
1883 {
1884 cout << " *NcBlocks::GetBlocks* Error : Input TGraph not specified." << endl;
1885 return 0;
1886 }
1887
1888 if (!hout)
1889 {
1890 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1891 return 0;
1892 }
1893
1894 if (n<1 || mode<0 || mode>2)
1895 {
1896 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraph treatment : n=" << n << " mode=" << mode << endl;
1897 return 0;
1898 }
1899
1900 Int_t npoints=gr->GetN();
1901
1902 if (!npoints) return 0;
1903
1904 // Sort the data points with increasing x-value
1905 gr->Sort();
1906
1907 // Represent each observation as a value in a variable binned histogram
1908 Double_t* xbins=new Double_t[npoints+1];
1909 Double_t x=0;
1910 Double_t y=0;
1911 for (Int_t i=0; i<npoints; i++)
1912 {
1913 gr->GetPoint(i,x,y);
1914 xbins[i]=x;
1915 }
1916 // Add an extra bin to contain the last measurement
1917 xbins[npoints]=(1.+1e-6)*xbins[npoints-1];
1918
1919 TH1F hin("","",npoints,xbins);
1920 for (Int_t j=1; j<=npoints; j++)
1921 {
1922 gr->GetPoint(j-1,x,y);
1923 hin.SetBinContent(j,y);
1924 }
1925
1926 Int_t nblocks=GetBlocks(&hin,hout,n,mode);
1927
1928 // Set the output histogram and axes titles
1929 TString title,str;
1930 TString xtitle="Samplings (e.g. time)";
1931 TString ytitle="Measured value";
1932 TAxis* ax=gr->GetXaxis();
1933 if (ax)
1934 {
1935 str=ax->GetTitle();
1936 if (str != "") xtitle=str;
1937 }
1938 ax=gr->GetYaxis();
1939 if (ax)
1940 {
1941 str=ax->GetTitle();
1942 if (str != "") ytitle=str;
1943 }
1944 title="Block representation for TGraph ";
1945 title+=gr->GetName();
1946 title+=" grouped in %-d consecutive samples;";
1947 title+=xtitle;
1948 if (mode==0) title+=";Mean ";
1949 if (mode==1) title+=";Median ";
1950 if (mode==2) title+=";RMS ";
1951 title+=ytitle;
1952 str=title.Format(title,n);
1953 hout->SetTitle(str);
1954
1955 delete [] xbins;
1956
1957 return nblocks;
1958}
1959
1960Int_t NcBlocks::Add(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
1961{
1995
1996 if (hout) hout->Reset();
1997
1998 if (!h1 || !h2 || !hout) return 1;
1999
2000 Int_t ndim1=h1->GetDimension();
2001 Int_t ndim2=h2->GetDimension();
2002 Int_t ndimo=hout->GetDimension();
2003
2004 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2005 {
2006 cout << " *NcBlocks::Add* Error : Histograms should all be 1-dimensional." << endl;
2007 return 1;
2008 }
2009
2010 // Make the X-axis of "hout" identical to the X-axis of "h1"
2011 TString name=hout->GetName();
2012 h1->Copy(*hout);
2013 hout->Reset();
2014
2015 TString name1=h1->GetName();
2016 if (name1=="") name1="h1";
2017
2018 TString name2=h2->GetName();
2019 if (name2=="") name2="h2";
2020
2021 TString sc="+";
2022 if (c<0) sc="-";
2023 if (fabs(c)!=1)
2024 {
2025 sc.Form("%-+.3g",c);
2026 sc+="*";
2027 }
2028
2029 TString sd="";
2030 sd.Form("%-+.3g",d);
2031
2032 TString title="Resulting histogram of: ";
2033 title+=name1;
2034 if (c)
2035 {
2036 title+=sc;
2037 title+=name2;
2038 }
2039 if (d) title+=sd;
2040 if (scale)
2041 {
2042 title+=" (scaled w.r.t. bin size)";
2043 }
2044 else
2045 {
2046 title+=" (not scaled w.r.t. bin size)";
2047 }
2048 title+=";";
2049 TAxis* axis1=h1->GetXaxis();
2050 title+=axis1->GetTitle();
2051 title+=";";
2052 axis1=h1->GetYaxis();
2053 title+=axis1->GetTitle();
2054
2055 hout->SetName(name);
2056 hout->SetTitle(title);
2057
2058 Int_t nb1=h1->GetNbinsX();
2059 Int_t nb2=h2->GetNbinsX();
2060
2061 if (!nb1 || !nb2) return 1;
2062
2063 // Get the largest bin size of "h1"
2064 Double_t bwidth1=0;
2065 Double_t bwmax1=0;
2066 Int_t imax1=0;
2067 for (Int_t i=1; i<=nb1; i++)
2068 {
2069 bwidth1=h1->GetBinWidth(i);
2070 if (i==1 || bwidth1>bwmax1)
2071 {
2072 bwmax1=bwidth1;
2073 imax1=i;
2074 }
2075 }
2076
2077 // Get the smallest bin size of "h2"
2078 Double_t bwidth2=0;
2079 Double_t bwmin2=0;
2080 Int_t imin2=0;
2081 for (Int_t i=1; i<=nb2; i++)
2082 {
2083 bwidth2=h2->GetBinWidth(i);
2084 if (i==1 || bwidth2<bwmin2)
2085 {
2086 bwmin2=bwidth2;
2087 imin2=i;
2088 }
2089 }
2090
2091 Double_t ratio=bwmax1/bwmin2;
2092 if (ratio>1.001)
2093 {
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);
2096 return 1;
2097 }
2098
2099 // Loop over all the bins of the input histogram h1
2100 Double_t x1=0;
2101 Double_t y1=0;
2102 Int_t i2=0;
2103 Double_t y2=0;
2104 Double_t ynew=0;
2105 TAxis* axis2=h2->GetXaxis();
2106 for (Int_t i1=1; i1<=nb1; i1++)
2107 {
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);
2114
2115 // Do not consider underflow or overflow bins
2116 if (i2<1 || i2>nb2) continue;
2117
2118 if (scale) y2*=bwidth1/bwidth2;
2119 ynew=y1+c*y2+d;
2120 hout->SetBinContent(i1,ynew);
2121 }
2122
2123 return 0;
2124}
2125
2126Int_t NcBlocks::Add(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2127{
2149
2150 if (gout) gout->Set(0);
2151
2152 if (!gr || !h || !gout) return 1;
2153
2154 TString nameg=gr->GetName();
2155 if (nameg=="") nameg="gr";
2156
2157 TString nameh=h->GetName();
2158 if (nameh=="") nameh="h";
2159
2160 TString sc="+";
2161 if (c<0) sc="-";
2162 if (fabs(c)!=1)
2163 {
2164 sc.Form("%-+.3g",c);
2165 sc+="*";
2166 }
2167
2168 TString sd="";
2169 sd.Form("%-+.3g",d);
2170
2171 TString title="Resulting graph of: ";
2172 title+=nameg;
2173 if (c)
2174 {
2175 title+=sc;
2176 title+=nameh;
2177 }
2178 if (d) title+=sd;
2179 title+=";";
2180 TAxis* axis=0;
2181 axis=gr->GetXaxis();
2182 title+=axis->GetTitle();
2183 title+=";";
2184 axis=gr->GetYaxis();
2185 title+=axis->GetTitle();
2186
2187 gout->SetTitle(title);
2188
2189 Int_t ndim=h->GetDimension();
2190
2191 if (ndim!=1)
2192 {
2193 cout << " *NcBlocks::Add* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2194 return 1;
2195 }
2196
2197 Int_t np=gr->GetN();
2198 Int_t nb=h->GetNbinsX();
2199
2200 if (!np || !nb) return 1;
2201
2202 // Loop over all the points in the graph
2203 Double_t x=0;
2204 Double_t y=0;
2205 Double_t ex=0;
2206 Double_t ey=0;
2207 Int_t hbin=0;
2208 Double_t hval=0;
2209 Double_t ynew=0;
2210 axis=h->GetXaxis();
2211 for (Int_t i=0; i<np; i++)
2212 {
2213 gr->GetPoint(i,x,y);
2214 hbin=axis->FindFixBin(x);
2215 hval=h->GetBinContent(hbin);
2216
2217 // Do not consider underflow or overflow bins
2218 if (hbin<1 || hbin>nb) continue;
2219
2220 ynew=y+c*hval+d;
2221 gout->SetPoint(i,x,ynew);
2222
2223 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2224 {
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);
2230 }
2231 }
2232
2233 return 0;
2234}
2235
2236Int_t NcBlocks::Divide(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
2237{
2269
2270 if (hout) hout->Reset();
2271
2272 if (!c)
2273 {
2274 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2275 return 1;
2276 }
2277
2278 if (!h1 || !h2 || !hout) return 1;
2279
2280 Int_t ndim1=h1->GetDimension();
2281 Int_t ndim2=h2->GetDimension();
2282 Int_t ndimo=hout->GetDimension();
2283
2284 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2285 {
2286 cout << " *NcBlocks::Divide* Error : Histograms should all be 1-dimensional." << endl;
2287 return 1;
2288 }
2289
2290 // Make the X-axis of "hout" identical to the X-axis of "h1"
2291 TString name=hout->GetName();
2292 h1->Copy(*hout);
2293 hout->Reset();
2294
2295 TString name1=h1->GetName();
2296 if (name1=="") name1="h1";
2297
2298 TString name2=h2->GetName();
2299 if (name2=="") name2="h2";
2300
2301 TString sc="/";
2302 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2303
2304 TString sd;
2305 if (c>0)
2306 {
2307 sd="";
2308 if (d) sd.Form("%-.3g+",d);
2309 }
2310 else
2311 {
2312 sd="-";
2313 if (d) sd.Form("%-.3g-",d);
2314 }
2315
2316 TString title="Resulting histogram of: ";
2317 if (sd!="") title+=sd;
2318 if (c)
2319 {
2320 title+=name1;
2321 if (sc.Contains("*")) title+="/(";
2322 title+=sc;
2323 title+=name2;
2324 if (sc.Contains("*")) title+=")";
2325 }
2326 if (scale)
2327 {
2328 title+=" (scaled w.r.t. bin size)";
2329 }
2330 else
2331 {
2332 title+=" (not scaled w.r.t. bin size)";
2333 }
2334 title+=";";
2335 TAxis* axis1=h1->GetXaxis();
2336 title+=axis1->GetTitle();
2337 title+=";";
2338 axis1=h1->GetYaxis();
2339 title+=axis1->GetTitle();
2340
2341 hout->SetName(name);
2342 hout->SetTitle(title);
2343
2344 Int_t nb1=h1->GetNbinsX();
2345 Int_t nb2=h2->GetNbinsX();
2346
2347 if (!nb1 || !nb2) return 1;
2348
2349 // Get the largest bin size of "h1"
2350 Double_t bwidth1=0;
2351 Double_t bwmax1=0;
2352 Int_t imax1=0;
2353 for (Int_t i=1; i<=nb1; i++)
2354 {
2355 bwidth1=h1->GetBinWidth(i);
2356 if (i==1 || bwidth1>bwmax1)
2357 {
2358 bwmax1=bwidth1;
2359 imax1=i;
2360 }
2361 }
2362
2363 // Get the smallest bin size of "h2"
2364 Double_t bwidth2=0;
2365 Double_t bwmin2=0;
2366 Int_t imin2=0;
2367 for (Int_t i=1; i<=nb2; i++)
2368 {
2369 bwidth2=h2->GetBinWidth(i);
2370 if (i==1 || bwidth2<bwmin2)
2371 {
2372 bwmin2=bwidth2;
2373 imin2=i;
2374 }
2375 }
2376
2377 Double_t ratio=bwmax1/bwmin2;
2378 if (ratio>1.001)
2379 {
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);
2382 return 1;
2383 }
2384
2385 // Loop over all the bins of the input histogram h1
2386 Double_t x1=0;
2387 Double_t y1=0;
2388 Int_t i2=0;
2389 Double_t y2=0;
2390 Double_t val=0;
2391 Double_t ynew=0;
2392 TAxis* axis2=h2->GetXaxis();
2393 for (Int_t i1=1; i1<=nb1; i1++)
2394 {
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);
2401
2402 // Do not consider underflow or overflow bins
2403 if (i2<1 || i2>nb2) continue;
2404
2405 val=c*y2;
2406 if (!val) continue;
2407
2408 if (scale) y2*=bwidth1/bwidth2;
2409 ynew=d+y1/val;
2410 hout->SetBinContent(i1,ynew);
2411 }
2412
2413 return 0;
2414}
2415
2416Int_t NcBlocks::Divide(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2417{
2437
2438 if (gout) gout->Set(0);
2439
2440 if (!c)
2441 {
2442 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2443 return 1;
2444 }
2445
2446 if (!gr || !h || !gout) return 1;
2447
2448 TString nameg=gr->GetName();
2449 if (nameg=="") nameg="gr";
2450
2451 TString nameh=h->GetName();
2452 if (nameh=="") nameh="h";
2453
2454 TString sc="/";
2455 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2456
2457 TString sd;
2458 if (c>0)
2459 {
2460 sd="";
2461 if (d) sd.Form("%-.3g+",d);
2462 }
2463 else
2464 {
2465 sd="-";
2466 if (d) sd.Form("%-.3g-",d);
2467 }
2468
2469 TString title="Resulting graph of: ";
2470 if (sd!="") title+=sd;
2471 if (c)
2472 {
2473 title+=nameg;
2474 if (sc.Contains("*")) title+="/(";
2475 title+=sc;
2476 title+=nameh;
2477 if (sc.Contains("*")) title+=")";
2478 }
2479 title+=";";
2480 TAxis* axis=0;
2481 axis=gr->GetXaxis();
2482 title+=axis->GetTitle();
2483 title+=";";
2484 axis=gr->GetYaxis();
2485 title+=axis->GetTitle();
2486
2487 gout->SetTitle(title);
2488
2489 Int_t ndim=h->GetDimension();
2490
2491 if (ndim!=1)
2492 {
2493 cout << " *NcBlocks::Divide* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2494 return 1;
2495 }
2496
2497 Int_t np=gr->GetN();
2498 Int_t nb=h->GetNbinsX();
2499
2500 if (!np || !nb) return 1;
2501
2502 // Loop over all the points in the graph
2503 Double_t x=0;
2504 Double_t y=0;
2505 Double_t ex=0;
2506 Double_t ey=0;
2507 Int_t hbin=0;
2508 Double_t hval=0;
2509 Double_t val=0;
2510 Double_t ynew=0;
2511 axis=h->GetXaxis();
2512 Int_t j=0;
2513 for (Int_t i=0; i<np; i++)
2514 {
2515 gr->GetPoint(i,x,y);
2516 hbin=axis->FindFixBin(x);
2517 hval=h->GetBinContent(hbin);
2518
2519 // Do not consider underflow or overflow bins
2520 if (hbin<1 || hbin>nb) continue;
2521
2522 val=c*hval;
2523 if (!val) continue;
2524
2525 ynew=d+y/val;
2526 gout->SetPoint(j,x,ynew);
2527
2528 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2529 {
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);
2535 }
2536
2537 j++;
2538 }
2539
2540 return 0;
2541}
2542
2543Int_t NcBlocks::Rebin(TH1* hin,TH1* hout,Bool_t scale,Int_t nbins,Double_t xmin,Double_t xmax)
2544{
2579
2580 if (hout) hout->Reset();
2581
2582 if (!hin || !hout) return 0;
2583
2584 Int_t ndimi=hin->GetDimension();
2585 Int_t ndimo=hout->GetDimension();
2586
2587 if (ndimi!=1 || ndimo!=1)
2588 {
2589 cout << " *NcBlocks::Rebin* Error : Histograms should both be 1-dimensional." << endl;
2590 return 0;
2591 }
2592
2593 Int_t nb1=hin->GetNbinsX();
2594
2595 if (!nb1) return 0;
2596
2597 TAxis* xaxis=hin->GetXaxis();
2598 TAxis* yaxis=hin->GetYaxis();
2599
2600 if (xmax<xmin)
2601 {
2602 xmin=xaxis->GetXmin();
2603 xmax=xaxis->GetXmax();
2604 }
2605
2606 Double_t bwidth1=0;
2607 Double_t xlow1=0;
2608 Double_t xup1=0;
2609
2610 // Automatic binwidth setting
2611 if (nbins<=0)
2612 {
2613 Double_t bwmin=-1;
2614 for (Int_t i=1; i<=nb1; i++)
2615 {
2616 bwidth1=hin->GetBinWidth(i);
2617 xlow1=hin->GetBinLowEdge(i);
2618 xup1=xlow1+bwidth1;
2619
2620 if (xlow1>=xmax || xup1<=xmin) continue;
2621
2622 if (bwmin<0 || bwidth1<bwmin) bwmin=bwidth1;
2623 }
2624
2625 if (bwmin<0)
2626 {
2627 cout << " *NcBlocks::Rebin* Error : Input histogram had no data in requested interval [xmin,xmax]." << endl;
2628 return 0;
2629 }
2630
2631 Double_t rnbins=(xmax-xmin)/bwmin;
2632 nbins=int(rnbins);
2633 Double_t diff=rnbins-double(nbins);
2634 if (diff) nbins=nbins+1;
2635 }
2636
2637 hout->SetBins(nbins,xmin,xmax);
2638 Double_t bwidth=hout->GetBinWidth(1);
2639
2640 TString name=hin->GetName();
2641 if (name=="") name="hin";
2642
2643 TString snb="";
2644 snb.Form(" nbins=%-i",nbins);
2645
2646 TString smin="";
2647 smin.Form(" xmin=%-.3g",xmin);
2648
2649 TString smax="";
2650 smax.Form(" xmax=%-.3g",xmax);
2651
2652 TString title="Uniformly binned version of histogram: ";
2653 title+=name;
2654 title+=snb;
2655 title+=smin;
2656 title+=smax;
2657 if (scale)
2658 {
2659 title+=" (scaled w.r.t. bin size)";
2660 }
2661 else
2662 {
2663 title+=" (not scaled w.r.t. bin size)";
2664 }
2665 title+=";";
2666 title+=xaxis->GetTitle();
2667 title+=";";
2668 title+=yaxis->GetTitle();
2669
2670 hout->SetTitle(title);
2671
2672 // Check if the binning of "hout" is not coarser than that of "hin" within [xmin,xmax]
2673 for (Int_t i=1; i<=nb1; i++)
2674 {
2675 bwidth1=hin->GetBinWidth(i);
2676 xlow1=hin->GetBinLowEdge(i);
2677 xup1=xlow1+bwidth1;
2678
2679 if (xlow1>=xmax || xup1<=xmin) continue;
2680
2681 if (bwidth1<bwidth)
2682 {
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);
2685 return 0;
2686 }
2687 }
2688
2689 // Loop over all the bins of the output histogram hout
2690 Double_t x=0;
2691 Int_t i1=0;
2692 Double_t y1=0;
2693 for (Int_t i=1; i<=nbins; i++)
2694 {
2695 x=hout->GetBinCenter(i);
2696 i1=xaxis->FindFixBin(x);
2697
2698 // Do not consider underflow or overflow bins
2699 if (i1<1 || i1>nb1) continue;
2700
2701 y1=hin->GetBinContent(i1);
2702 bwidth1=hin->GetBinWidth(i1);
2703 if (scale) y1=y1*bwidth/bwidth1;
2704 hout->SetBinContent(i,y1);
2705 }
2706
2707 return nbins;
2708}
2709
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)