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{
561
562 if (hout) hout->Reset();
563
564 if (!hin || !hout) return 0;
565
566 Int_t n=hin->GetNbinsX();
567
568 if (!n) return 0;
569
570 if (fabs(fpr)>1)
571 {
572 printf(" *NcBlocks::GetBlocks* Inconsistent parameter fpr=%-g for histogram treatment. \n",fpr);
573 return 0;
574 }
575
576 // Set the Data Mode for binned data, if it was not set already
577 // Also initialize the output histogram title for binned data
578 if (!fMode)
579 {
580 fMode=2;
581 TString title;
582 title.Form("Bayesian Block representation with FPR=%-.3g",fpr);
583 hout->SetTitle(title);
584 }
585
586 TArrayD best(n); // Array with optimal fitness values
587 TArrayI last(n); // Array with indices of optimal last change points
588 TArrayD lengths(n); // Array with the lengths of the optimal partition blocks
589 TArrayD counts(n); // Array with the event counts of the optimal partition blocks
590
591 Double_t blen=0; // Length of a certain block
592 Double_t bcount=0; // Event count in a certain block
593 Double_t prior=0; // Contribution of each block to the prior fitness for a certain partition
594 Double_t bfit=0; // Fitness of a certain block
595 Double_t pfit=0; // Fitness of a certain partition
596
597 Double_t xlow=0;
598 Double_t xup=0;
599 Int_t index=0;
600
601 // Variables for measurement of an observable (i.e. Data Mode 3)
602 Double_t yk=0; // Measured value
603 Double_t sigk=0; // Error on the measured value
604 Double_t a=0;
605 Double_t b=0;
606
607 // The parameters for the optimal partition
608 Double_t optfit=0;
609 Int_t optj=0;
610 Double_t optlen=0;
611 Double_t optcount=0;
612
613 // Parameters and counter for trigger mode
614 Int_t oldoptj=0;
615 Double_t oldoptlen=0;
616 Int_t ncp=0;
617 Double_t xtrig=0;
618 Double_t ytrig=0;
619 Double_t oldytrig=0;
620
621 // Add Data Cells one by one to the sample to be partioned
622 Int_t ncells=0;
623 Int_t first=1;
624 Int_t jstart=1;
625 for (Int_t i=1; i<=n; i++)
626 {
627 ncells=i;
628 prior=GetPrior(i,fabs(fpr));
629 xup=hin->GetBinLowEdge(i)+hin->GetBinWidth(i);
630 // Loop over all possible block partitions for this Data Cell sample
631 // For fpr<0 it will ignore the already constructed blocks in view of cpu speed
632 jstart=1;
633 if (fpr<0 && optj) jstart=optj;
634 first=1;
635 for (Int_t j=jstart; j<=i; j++)
636 {
637 xlow=hin->GetBinLowEdge(j);
638 blen=xup-xlow;
639 bcount=0;
640 bfit=0;
641 a=0;
642 b=0;
643 if (fMode==1 || fMode==2)
644 {
645 bcount=hin->Integral(j,i);
646 bfit=GetBlockFitness(fabs(bcount),blen);
647 }
648 if (fMode==3)
649 {
650 for (Int_t k=j; k<=i; k++)
651 {
652 yk=hin->GetBinContent(k);
653 sigk=hin->GetBinError(k);
654 sigk=sigk*sigk;
655 if (sigk)
656 {
657 a+=1./(sigk);
658 b+=yk/sigk;
659 }
660 }
661 if (a)
662 {
663 bcount=b/a; // Weighted mean of the y-values in the block
664 bfit=b*b/(2.*a);
665 }
666 }
667 pfit=prior+bfit;
668 index=j-1; // Array index reduced by 1 because of C++ array index convention
669 if (index>0) pfit+=best.At(index-1);
670
671 // Record attributes for the optimal partition
672 if (first)
673 {
674 optfit=pfit;
675 optj=j;
676 optlen=blen;
677 optcount=bcount;
678 first=0;
679 }
680 else
681 {
682 if (pfit>optfit)
683 {
684 optfit=pfit;
685 optj=j;
686 optlen=blen;
687 optcount=bcount;
688 }
689 }
690 } // End of loop over possible block partitions
691
692 // Store the attributes of the optimal partition
693 best.SetAt(optfit,i-1);
694 last.SetAt(optj,i-1);
695 lengths.SetAt(optlen,i-1);
696 counts.SetAt(optcount,i-1);
697
698 if (!ntrig || optj==1) continue;
699
700 // Check for triggering on a new change point
701 oldoptj=last.At(i-2);
702 oldoptlen=lengths.At(i-2);
703 oldytrig=0;
704 if ((fMode==1 || fMode==2) && oldoptlen) oldytrig=counts.At(i-2)/oldoptlen;
705 if (fMode==3) oldytrig=counts.At(i-2);
706 ytrig=0;
707 if ((fMode==1 || fMode==2) && optlen) ytrig=counts.At(i-1)/optlen;
708 if (fMode==3) ytrig=counts.At(i-1);
709
710 if (optj>oldoptj && ((ntrig>0 && ytrig>oldytrig) || (ntrig<0 && ytrig<oldytrig)))
711 {
712 ncp++;
713 xtrig=hin->GetBinLowEdge(optj);
714 }
715
716 // Stop when the requested number of triggers has been reached
717 if (ncp>=abs(ntrig)) break;
718
719 } // End of adding Data Cells one by one
720
721 // Obtain the change points and corresponding partition information
722 Int_t jcp=0;
723 index=ncells;
724 Double_t x=0;
725 Double_t y=0;
726 Int_t iup=0;
727 Int_t nfilled=0;
728 ncp=0;
729 TArrayD xarr(ncells+1);
730 TArrayD yarr(ncells+1);
731 while (index>0)
732 {
733 index--; // Reduce array index by 1 because of C++ array convention
734 jcp=last[index];
735 ncp++;
736 x=hin->GetBinLowEdge(jcp)+lengths[index];
737 iup=hin->FindFixBin(x);
738 y=0;
739
740 if (fMode==1 && lengths[index]) y=counts[index]/lengths[index];
741
742 if (fMode==2)
743 {
744 nfilled=0;
745 for (Int_t i=jcp; i<=iup; i++)
746 {
747 if (hin->GetBinContent(i)) nfilled++;
748 }
749 if (nfilled) y=counts[index]/double(nfilled); // Take the average of the filled bins in this block as Y-value
750 }
751
752 if (fMode==3) y=counts[index];
753
754 xarr.SetAt(x,ncp-1);
755 yarr.SetAt(y,ncp-1);
756
757 // Also mark the start of the first bin
758 if (jcp==1) xarr.SetAt(hin->GetBinLowEdge(jcp),ncp);
759
760 index=jcp-1;
761 }
762
763 // Create the corresponding variable binned histogram
764 Double_t* xbins=new Double_t[ncp+1];
765 Double_t* yvals=new Double_t[ncp+1];
766
767 for (Int_t i=0; i<=ncp; i++)
768 {
769 xbins[i]=xarr.At(ncp-i);
770 yvals[i]=yarr.At(ncp-i);
771 }
772
773 hout->SetBins(ncp,xbins);
774 for (Int_t i=1; i<=ncp; i++)
775 {
776 hout->SetBinContent(i,yvals[i]);
777 }
778
779 // Set the number of entries for the output histogram
780 hout->SetEntries(hin->GetEntries());
781
782 // Determine the first trigger point after the full sample analysis
783 if (!ntrig) xtrig=xbins[1];
784
785 hout->SetLineWidth(2);
786 hout->SetLineColor(kBlue);
787 hout->SetStats(kFALSE);
788
789 // Set the output histogram and axes titles
790 TString title,str;
791 title="Bayesian Block representation for histogram ";
792 title+=hin->GetName();
793 title+=" with FPR= %-.3g";
794 title+=";";
795 str=hin->GetXaxis()->GetTitle();
796 if (str=="") str="Recordings (e.g. time)";
797 title+=str;
798 title+=";";
799 str=hin->GetYaxis()->GetTitle();
800 if (str=="") str="Counts";
801 title+=str;
802 str=title.Format(title.Data(),fpr);
803 hout->SetTitle(str.Data());
804
805 // Indicate the requested trigger in a legend
806 if (ntrig)
807 {
808 str.Form("Requested trigger at : %-.3g",xtrig);
809
810 TLegend* leg=new TLegend(0.5,0.85,0.7,0.9,str);
811 leg->SetFillColor(0);
812 leg->SetTextColor(kBlue);
813 leg->SetTextAlign(22);
814
815 TList* hlist=hout->GetListOfFunctions();
816 hlist->Add(leg);
817 }
818
819 // Reset the Data Mode for subsequent invokations
820 fMode=0;
821
822 delete [] xbins;
823 delete [] yvals;
824
825 return xtrig;
826}
827
828Double_t NcBlocks::GetBlocks(NcSample s,Int_t i,Double_t fpr,TH1* hout,Int_t ntrig)
829{
869
870 if (!hout)
871 {
872 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
873 return 0;
874 }
875
876 Int_t n=s.GetN();
877 Int_t store=s.GetStoreMode();
878 Int_t dim=s.GetDimension();
879
880 if (n<2 || !store || dim<1 || i<1 || i>dim || fabs(fpr)>1)
881 {
882 cout << " *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
883 cout << " Store Mode:" << store << " Entries:" << n << " Dimension:" << dim << " i:" << i << " fpr:" << fpr << endl;
884 return 0;
885 }
886
887 // Set Data Mode for unbinned event data
888 fMode=1;
889
890 // Represent each observation as 1 count in a variable binned histogram
891 TArrayD xarr(n);
892 Double_t val=0;
893 Int_t idstore=0;
894 for (Int_t idx=1; idx<=n; idx++)
895 {
896 val=s.GetEntry(idx,i,1,i); // Get an entry after ordering the values in increasing order
897
898 // Check for a double occurance of an observation
899 if (idstore>0)
900 {
901 if (val-xarr[idstore-1]<=0) continue;
902 }
903
904 xarr[idstore]=val;
905 idstore++;
906 }
907
908
909 // Obtain the correct amount of bin lower edges
910 if (idstore<n)
911 {
912 n=idstore;
913 xarr.Set(n);
914 }
915
916 Double_t* xbins=xarr.GetArray();
917 TH1F hin("","",n-1,xbins);
918 for (Int_t j=1; j<n; j++)
919 {
920 hin.SetBinContent(j,1);
921 }
922
923 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
924
925 // Set the number of entries for "hout"
926 hout->SetEntries(double(n));
927
928 // Set the output histogram and axes titles
929 TString title,str;
930 title="Bayesian Block representation for NcSample ";
931 title+=s.GetName();
932 title+=" with FPR=%-.3g";
933 title+=";Recordings of variable ";
934 title+=i;
935 title+=" (";
936 title+=s.GetVariableName(i);
937 title+=")";
938 title+=";Count rate";
939 str=title.Format(title.Data(),fpr);
940 hout->SetTitle(str.Data());
941
942 return xtrig;
943}
944
945Double_t NcBlocks::GetBlocks(NcSample s,TString name,Double_t fpr,TH1* hout,Int_t ntrig)
946{
986
987 Int_t i=s.GetIndex(name);
988
989 Double_t xtrig=GetBlocks(s,i,fpr,hout,ntrig);
990
991 return xtrig;
992}
993
994Double_t NcBlocks::GetBlocks(Int_t n,Double_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
995{
1037
1038 if (!hout)
1039 {
1040 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for array treatment." << endl;
1041 return 0;
1042 }
1043
1044 if (!arr || n<2 || fabs(fpr)>1)
1045 {
1046 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1047 if (!arr)
1048 {
1049 cout << " Array pointer is 0." << endl;
1050 }
1051 else
1052 {
1053 cout << " Entries:" << n << " fpr:" << fpr << endl;
1054 }
1055 return 0;
1056 }
1057
1058 NcSample s;
1059 s.SetStoreMode();
1060
1061 for (Int_t i=0; i<n; i++)
1062 {
1063 s.Enter(arr[i]);
1064 }
1065
1066 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1067
1068 // Set the output histogram and axes titles
1069 TString title;
1070 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1071 title+=";Recordings (e.g. time);Count rate";
1072 hout->SetTitle(title);
1073
1074 return xtrig;
1075}
1076
1077Double_t NcBlocks::GetBlocks(Int_t n,Float_t* arr,Double_t fpr,TH1* hout,Int_t ntrig)
1078{
1120
1121 if (!hout)
1122 {
1123 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for array treatment." << endl;
1124 return 0;
1125 }
1126
1127 if (!arr || n<2 || fabs(fpr)>1)
1128 {
1129 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1130 if (!arr)
1131 {
1132 cout << " Array pointer is 0." << endl;
1133 }
1134 else
1135 {
1136 cout << " Entries:" << n << " fpr:" << fpr << endl;
1137 }
1138 return 0;
1139 }
1140
1141 NcSample s;
1142 s.SetStoreMode();
1143
1144 for (Int_t i=0; i<n; i++)
1145 {
1146 s.Enter(arr[i]);
1147 }
1148
1149 Double_t xtrig=GetBlocks(s,1,fpr,hout,ntrig);
1150
1151 // Set the output histogram and axes titles
1152 TString title;
1153 title.Form("Bayesian Block representation for unbinned array data with FPR=%-.3g",fpr);
1154 title+=";Recordings (e.g. time);Count rate";
1155 hout->SetTitle(title);
1156
1157 return xtrig;
1158}
1159
1160Double_t NcBlocks::GetBlocks(TGraphErrors gr,Double_t fpr,TH1* hout,Int_t ntrig)
1161{
1201
1202 if (!hout)
1203 {
1204 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified for TGraphErrors treatment." << endl;
1205 return 0;
1206 }
1207
1208 Int_t n=gr.GetN();
1209
1210 if (n<2 || fabs(fpr)>1)
1211 {
1212 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraphErrors treatment." << endl;
1213 cout << " Entries:" << n << " fpr:" << fpr << endl;
1214 return 0;
1215 }
1216
1217 // Set Data Mode for measurements of a continuous observable
1218 fMode=3;
1219
1220 // Sort the data points with increasing x-value
1221 gr.Sort();
1222
1223 // Represent each observation as a value in a variable binned histogram
1224 Double_t* xbins=new Double_t[n+1];
1225 Double_t x=0;
1226 Double_t y=0;
1227 Double_t err=0;
1228 Double_t dist=0;
1229 Double_t dmin=-1;
1230 for (Int_t i=0; i<n; i++)
1231 {
1232 gr.GetPoint(i,x,y);
1233 err=fabs(gr.GetErrorX(i));
1234 xbins[i]=x-err;
1235 if (i>0)
1236 {
1237 dist=xbins[i]-xbins[i-1];
1238 if (dmin<0 || dist<dmin) dmin=dist;
1239 }
1240 }
1241 // Add an extra bin to contain the last measurement
1242 xbins[n]=xbins[n-1]+dmin;
1243
1244 TH1F hin("","",n,xbins);
1245 for (Int_t j=1; j<=n; j++)
1246 {
1247 gr.GetPoint(j-1,x,y);
1248 err=fabs(gr.GetErrorY(j-1));
1249 hin.SetBinContent(j,y);
1250 hin.SetBinError(j,err);
1251 }
1252
1253 Double_t xtrig=GetBlocks(&hin,fpr,hout,ntrig);
1254
1255 // Set the number of entries for "hout"
1256 hout->SetEntries(double(n));
1257
1258 // Set the output histogram and axes titles
1259 TString title,str;
1260 TString xtitle="Samplings (e.g. time)";
1261 TString ytitle="Measured value";
1262 TAxis* ax=gr.GetXaxis();
1263 if (ax)
1264 {
1265 str=ax->GetTitle();
1266 if (str != "") xtitle=str;
1267 }
1268 ax=gr.GetYaxis();
1269 if (ax)
1270 {
1271 str=ax->GetTitle();
1272 if (str != "") ytitle=str;
1273 }
1274 title="Bayesian Block representation for TGraphErrors ";
1275 title+=gr.GetName();
1276 title+=" with FPR=%-.3g;";
1277 title+=xtitle;
1278 title+=";";
1279 title+=ytitle;
1280 str=title.Format(title,fpr);
1281 hout->SetTitle(str);
1282
1283 delete [] xbins;
1284
1285 return xtrig;
1286}
1287
1288Double_t NcBlocks::GetBlocks(TGraph gr,TF1 f,Double_t fpr,TH1* hout,Int_t ntrig)
1289{
1342
1343 NcSample s;
1344 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&f);
1345
1346 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1347
1348 TString str=" and input errors : ";
1349 str+=f.GetExpFormula("p");
1350 str.ReplaceAll("x","y");
1351
1352 TString title=hout->GetTitle();
1353 title+=str;
1354 hout->SetTitle(title);
1355
1356 return xtrig;
1357}
1358
1359Double_t NcBlocks::GetBlocks(TGraph gr,TString f,Double_t fpr,TH1* hout,Int_t ntrig)
1360{
1414
1415 TF1 func("func",f);
1416 NcSample s;
1417 TGraphErrors gre=s.GetGraphErrors(&gr,0,0,0,&func);
1418
1419 Double_t xtrig=GetBlocks(gre,fpr,hout,ntrig);
1420
1421 TString str=" and input errors : ";
1422 str+=func.GetExpFormula("p");
1423 str.ReplaceAll("x","y");
1424
1425 TString title=hout->GetTitle();
1426 title+=str;
1427 hout->SetTitle(title);
1428
1429 return xtrig;
1430}
1431
1432Double_t NcBlocks::GetBlocks(TGraph gr,Double_t nrms,Double_t fpr,TH1* hout,Int_t ntrig)
1433{
1487
1488 // Obtain the RMS deviation of all the y-values
1489 Double_t rms=gr.GetRMS(2);
1490
1491 // Determine the error for each y-value and convert into a function format
1492 Double_t err=fabs(nrms*rms);
1493
1494 TString f;
1495 f.Form("%-.5g",err);
1496
1497 Double_t xtrig=GetBlocks(gr,f,fpr,hout,ntrig);
1498
1499 TString str;
1500 str.Form(" from nrms=%-.3g",fabs(nrms));
1501
1502 TString title=hout->GetTitle();
1503 title+=str;
1504 hout->SetTitle(title);
1505
1506 return xtrig;
1507}
1508
1509Int_t NcBlocks::GetBlocks(TH1* hin,TH1* hout,Int_t n,Int_t mode)
1510{
1542
1543 if (!hin)
1544 {
1545 cout << " *NcBlocks::GetBlocks* Error : Input histogram not specified." << endl;
1546 return 0;
1547 }
1548
1549 if (!hout)
1550 {
1551 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1552 return 0;
1553 }
1554
1555 Int_t nbins=hin->GetNbinsX();
1556
1557 if (!nbins || n<1 || n>nbins || mode<0 || mode>2)
1558 {
1559 cout << " *NcBlocks::GetBlocks* Inconsistent input nbins=" << nbins << " n=" << n << " mode=" << mode << endl;
1560 return 0;
1561 }
1562
1563 // Retrieve the various sets of "n" bins from the input histogram
1564 Int_t jbin=0;
1565 Double_t x=0;
1566 Double_t y=0;
1567 Double_t xlow=0;
1568 Double_t xup=0;
1569 Double_t binwidth=0;
1570 NcSample s;
1571 if (mode==1) s.SetStoreMode(1);
1572 Int_t nblocks=0;
1573 Double_t average=0;
1574 TArrayD xarr(nbins);
1575 TArrayD yarr(nbins);
1576 while (jbin<nbins)
1577 {
1578 for (Int_t i=0; i<n; i++)
1579 {
1580 jbin++;
1581
1582 if (jbin>nbins) break;
1583
1584 x=hin->GetBinCenter(jbin);
1585 y=hin->GetBinContent(jbin);
1586 binwidth=hin->GetBinWidth(jbin);
1587 if (i==0) xlow=hin->GetBinLowEdge(jbin);
1588 xup=x+0.5*binwidth;
1589 s.Enter(x,y);
1590 }
1591 if (mode==0) average=s.GetMean(2);
1592 if (mode==1) average=s.GetMedian(2);
1593 if (mode==2) average=s.GetRMS(2);
1594 nblocks++;
1595 xarr.SetAt(xlow,nblocks-1);
1596 yarr.SetAt(average,nblocks-1);
1597 s.Reset();
1598 } // End of while loop
1599
1600 // Create the corresponding variable binned output histogram
1601 Double_t* xbins=new Double_t[nblocks+1];
1602 Double_t* yvals=new Double_t[nblocks+1];
1603
1604 for (Int_t i=0; i<nblocks; i++)
1605 {
1606 xbins[i]=xarr.At(i);
1607 yvals[i]=yarr.At(i);
1608 }
1609 // Add an extra bin to contain the last data
1610 xbins[nblocks]=(1.+1e-6)*xup;
1611 yvals[nblocks]=yvals[nblocks-1];
1612
1613 hout->SetBins(nblocks,xbins);
1614 for (Int_t i=1; i<=nblocks; i++)
1615 {
1616 hout->SetBinContent(i,yvals[i-1]);
1617 }
1618
1619 // Set the number of entries for "hout"
1620 hout->SetEntries(hin->GetEntries());
1621
1622 hout->SetLineWidth(2);
1623 hout->SetLineColor(kBlue);
1624 hout->SetStats(kFALSE);
1625
1626 // Set the output histogram and axes titles
1627 TString title,str;
1628 title="Block representation for histogram ";
1629 title+=hin->GetName();
1630 title+=" grouped in %-d consecutive bins";
1631 title+=";";
1632 str=hin->GetXaxis()->GetTitle();
1633 if (str=="") str="Recordings (e.g. time)";
1634 title+=str;
1635 if (mode==0) title+=";Mean ";
1636 if (mode==1) title+=";Median ";
1637 if (mode==2) title+=";RMS ";
1638 str=hin->GetYaxis()->GetTitle();
1639 if (str=="") str="Counts";
1640 title+=str;
1641 str=title.Format(title.Data(),n);
1642 hout->SetTitle(str.Data());
1643
1644 delete [] xbins;
1645 delete [] yvals;
1646
1647 return nblocks;
1648}
1649
1650Int_t NcBlocks::GetBlocks(NcSample s,Int_t i,TH1* hout,Int_t n,Int_t mode)
1651{
1685
1686 if (!hout)
1687 {
1688 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1689 return 0;
1690 }
1691
1692 Int_t nen=s.GetN();
1693 Int_t store=s.GetStoreMode();
1694 Int_t dim=s.GetDimension();
1695
1696 if (!store || dim<1 || i<1 || i>dim || n<1 || n>nen || mode<0 || mode>2)
1697 {
1698 cout << " *NcBlocks::GetBlocks* Inconsistent input for NcSample treatment." << endl;
1699 cout << " Store Mode:" << store << " Entries:" << nen << " Dimension:" << dim << " i:" << i << " n:" << n << " mode:" << mode << endl;
1700 return 0;
1701 }
1702
1703 TGraph gr=s.GetGraph(i);
1704
1705 Int_t nblocks=GetBlocks(&gr,hout,n,mode);
1706
1707 // Set the output histogram and axes titles
1708 TString title,str;
1709 title="Block representation for NcSample ";
1710 title+=s.GetName();
1711 title+=" grouped in %-d consecutive samples";
1712 title+=";Sampling number";
1713 if (mode==0) title+=";Mean ";
1714 if (mode==1) title+=";Median ";
1715 if (mode==2) title+=";RMS ";
1716 title+="of variable ";
1717 title+=i;
1718 title+=" (";
1719 title+=s.GetVariableName(i);
1720 title+=")";
1721 str=title.Format(title.Data(),n);
1722 hout->SetTitle(str.Data());
1723
1724 return nblocks;
1725}
1726
1727Int_t NcBlocks::GetBlocks(NcSample s,TString name,TH1* hout,Int_t n,Int_t mode)
1728{
1762
1763 Int_t i=s.GetIndex(name);
1764
1765 Int_t nblocks=GetBlocks(s,i,hout,n,mode);
1766
1767 return nblocks;
1768}
1769
1770Int_t NcBlocks::GetBlocks(Int_t nr,Double_t* arr,TH1* hout,Int_t n,Int_t mode)
1771{
1803
1804 if (!hout)
1805 {
1806 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1807 return 0;
1808 }
1809
1810 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1811 {
1812 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1813 if (!arr)
1814 {
1815 cout << " Array pointer is 0." << endl;
1816 }
1817 else
1818 {
1819 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1820 }
1821 return 0;
1822 }
1823
1824 NcSample s;
1825 s.SetStoreMode();
1826
1827 for (Int_t i=0; i<nr; i++)
1828 {
1829 s.Enter(arr[i]);
1830 }
1831
1832 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1833
1834 // Set the output histogram and axes titles
1835 TString title;
1836 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1837 title+=";Sampling number";
1838 if (mode==0) title+=";Mean ";
1839 if (mode==1) title+=";Median ";
1840 if (mode==2) title+=";RMS ";
1841 title+="value";
1842 hout->SetTitle(title);
1843
1844 return nblocks;
1845}
1846
1847Int_t NcBlocks::GetBlocks(Int_t nr,Float_t* arr,TH1* hout,Int_t n,Int_t mode)
1848{
1880
1881 if (!hout)
1882 {
1883 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1884 return 0;
1885 }
1886
1887 if (!arr || n<1 || n>nr || mode<0 || mode>2)
1888 {
1889 cout << " *NcBlocks::GetBlocks* Inconsistent input for array treatment." << endl;
1890 if (!arr)
1891 {
1892 cout << " Array pointer is 0." << endl;
1893 }
1894 else
1895 {
1896 cout << " Entries:" << nr << " n:" << n << " mode:" << mode << endl;
1897 }
1898 return 0;
1899 }
1900
1901 NcSample s;
1902 s.SetStoreMode();
1903
1904 for (Int_t i=0; i<nr; i++)
1905 {
1906 s.Enter(arr[i]);
1907 }
1908
1909 Int_t nblocks=GetBlocks(s,1,hout,n,mode);
1910
1911 // Set the output histogram and axes titles
1912 TString title;
1913 title.Form("Block representation for array data grouped in %-d consecutive recordings",n);
1914 title+=";Sampling number";
1915 if (mode==0) title+=";Mean ";
1916 if (mode==1) title+=";Median ";
1917 if (mode==2) title+=";RMS ";
1918 title+="value";
1919 hout->SetTitle(title);
1920
1921 return nblocks;
1922}
1923
1924Int_t NcBlocks::GetBlocks(TGraph* gr,TH1* hout,Int_t n,Int_t mode)
1925{
1957
1958 if (!gr)
1959 {
1960 cout << " *NcBlocks::GetBlocks* Error : Input TGraph not specified." << endl;
1961 return 0;
1962 }
1963
1964 if (!hout)
1965 {
1966 cout << " *NcBlocks::GetBlocks* Error : Output histogram not specified." << endl;
1967 return 0;
1968 }
1969
1970 if (n<1 || mode<0 || mode>2)
1971 {
1972 cout << " *NcBlocks::GetBlocks* Inconsistent input for TGraph treatment : n=" << n << " mode=" << mode << endl;
1973 return 0;
1974 }
1975
1976 Int_t npoints=gr->GetN();
1977
1978 if (!npoints) return 0;
1979
1980 // Sort the data points with increasing x-value
1981 gr->Sort();
1982
1983 // Represent each observation as a value in a variable binned histogram
1984 Double_t* xbins=new Double_t[npoints+1];
1985 Double_t x=0;
1986 Double_t y=0;
1987 for (Int_t i=0; i<npoints; i++)
1988 {
1989 gr->GetPoint(i,x,y);
1990 xbins[i]=x;
1991 }
1992 // Add an extra bin to contain the last measurement
1993 xbins[npoints]=(1.+1e-6)*xbins[npoints-1];
1994
1995 TH1F hin("","",npoints,xbins);
1996 for (Int_t j=1; j<=npoints; j++)
1997 {
1998 gr->GetPoint(j-1,x,y);
1999 hin.SetBinContent(j,y);
2000 }
2001
2002 Int_t nblocks=GetBlocks(&hin,hout,n,mode);
2003
2004 // Set the number of entries for "hout"
2005 hout->SetEntries(double(npoints));
2006
2007 // Set the output histogram and axes titles
2008 TString title,str;
2009 TString xtitle="Samplings (e.g. time)";
2010 TString ytitle="Measured value";
2011 TAxis* ax=gr->GetXaxis();
2012 if (ax)
2013 {
2014 str=ax->GetTitle();
2015 if (str != "") xtitle=str;
2016 }
2017 ax=gr->GetYaxis();
2018 if (ax)
2019 {
2020 str=ax->GetTitle();
2021 if (str != "") ytitle=str;
2022 }
2023 title="Block representation for TGraph ";
2024 title+=gr->GetName();
2025 title+=" grouped in %-d consecutive samples;";
2026 title+=xtitle;
2027 if (mode==0) title+=";Mean ";
2028 if (mode==1) title+=";Median ";
2029 if (mode==2) title+=";RMS ";
2030 title+=ytitle;
2031 str=title.Format(title,n);
2032 hout->SetTitle(str);
2033
2034 delete [] xbins;
2035
2036 return nblocks;
2037}
2038
2039Int_t NcBlocks::Add(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d,Int_t mode)
2040{
2081
2082 if (hout) hout->Reset();
2083
2084 if (!h1 || !h2 || !hout) return 1;
2085
2086 Int_t ndim1=h1->GetDimension();
2087 Int_t ndim2=h2->GetDimension();
2088 Int_t ndimo=hout->GetDimension();
2089
2090 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2091 {
2092 cout << " *NcBlocks::Add* Error : Histograms should all be 1-dimensional." << endl;
2093 return 1;
2094 }
2095
2096 // Make the X-axis of "hout" identical to the X-axis of "h1"
2097 TString name=hout->GetName();
2098 h1->Copy(*hout);
2099 hout->Reset();
2100
2101 TString name1=h1->GetName();
2102 if (name1=="") name1="h1";
2103
2104 TString name2=h2->GetName();
2105 if (name2=="") name2="h2";
2106
2107 TString sc="+";
2108 if (c<0) sc="-";
2109 if (fabs(c)!=1)
2110 {
2111 sc.Form("%-+.3g",c);
2112 sc+="*";
2113 }
2114
2115 TString sd="";
2116 sd.Form("%-+.3g",d);
2117
2118 TString title="Resulting histogram of: ";
2119 title+=name1;
2120 if (c)
2121 {
2122 title+=sc;
2123 title+=name2;
2124 }
2125 if (d) title+=sd;
2126 if (scale)
2127 {
2128 title+=" (scaled w.r.t. bin size)";
2129 }
2130 else
2131 {
2132 title+=" (not scaled w.r.t. bin size)";
2133 }
2134 title+=";";
2135 TAxis* axis1=h1->GetXaxis();
2136 title+=axis1->GetTitle();
2137 title+=";";
2138 axis1=h1->GetYaxis();
2139 title+=axis1->GetTitle();
2140
2141 hout->SetName(name);
2142 hout->SetTitle(title);
2143
2144 Int_t nb1=h1->GetNbinsX();
2145 Int_t nb2=h2->GetNbinsX();
2146
2147 if (!nb1 || !nb2)
2148 {
2149 printf(" *NcBlocks::Add* Error : nbins of h1 is %-i and nbins of h2 is %-i. \n",nb1,nb2);
2150 return 1;
2151 }
2152
2153 // Check whether the range of the axes of h1 and h2 overlap
2154 Double_t x1min=h1->GetXaxis()->GetXmin();
2155 Double_t x1max=h1->GetXaxis()->GetXmax();
2156 Double_t x2min=h2->GetXaxis()->GetXmin();
2157 Double_t x2max=h2->GetXaxis()->GetXmax();
2158
2159 if (x1min>x2max || x1max<x2min)
2160 {
2161 printf(" *NcBlocks::Add* Error : The ranges of the input histograms do not overlap. \n");
2162 return 1;
2163 }
2164
2166 // Scan over the bins of h1 and h2 to create the data for hout //
2168
2169 Int_t i1=1;
2170 Double_t bwidth1=h1->GetBinWidth(i1);
2171 Double_t x1low=h1->GetBinLowEdge(i1);
2172 Double_t x1up=x1low+bwidth1;
2173 Int_t i2=0;
2174 Int_t i2up=0;
2175 Double_t bwidth2=0;
2176 Double_t x2low=0;
2177 Double_t x2up=0;
2178 Double_t y1=0;
2179 Double_t frac1=1;
2180 Double_t y2=0;
2181 Double_t frac2=1;
2182 Double_t xlow=0;
2183 Double_t xup=0;
2184 Double_t y=0;
2185 NcSample s;
2186 s.SetStoreMode();
2187 Double_t epsilon=1e-10; // To circumvent machine accuracy issues
2188 Int_t iret=0;
2189
2190 while (i1<=nb1)
2191 {
2192 y1=h1->GetBinContent(i1);
2193
2194 i2=h2->FindFixBin(x1low+fabs(epsilon*x1low));
2195 i2up=h2->FindFixBin(x1up+fabs(epsilon*x1up));
2196
2197 if ((i2<1 && i2up<1) || (i2>nb2 && i2up>nb2) || x1up<=x1low || (mode<0 && c<0 && !y1) || (mode>0 && c>0 && !y1)) // No overlap
2198 {
2199 // Regard the h2 bin content as 0 for this h1 bin
2200 y=d+y1;
2201 if (x1up>x1low) s.Enter(x1low,y);
2202 // Go for the next h1 bin
2203 i1++;
2204 x1low=h1->GetBinLowEdge(i1);
2205 bwidth1=h1->GetBinWidth(i1);
2206 x1up=x1low+bwidth1;
2207 continue;
2208 }
2209
2210 // Overlapping bins of h1 and h2 -> Determine the bin boundaries of hout
2211 if (i2<1) i2=1;
2212 if (i2>nb2) i2=nb2;
2213 x2low=h2->GetBinLowEdge(i2);
2214 bwidth2=h2->GetBinWidth(i2);
2215 x2up=x2low+bwidth2;
2216 y2=h2->GetBinContent(i2);
2217 xlow=x1low;
2218 xup=x1up;
2219
2220 // Regard the h2 bin as 0 for the part that it doesn't cover the h1 bin
2221 if (x2low>xlow)
2222 {
2223 y=d+y1;
2224 if (x1up>x1low) s.Enter(xlow,y);
2225 xlow=x2low;
2226 }
2227 if (x2up<xup) xup=x2up;
2228
2229 // Protection against possible infinite loop due to machine accuracy
2230 if (xup<=xlow)
2231 {
2232 iret=1;
2233 break;
2234 }
2235
2236 frac1=(xup-xlow)/bwidth1;
2237 frac2=(xup-xlow)/bwidth2;
2238 if (scale)
2239 {
2240 y1*=frac1;
2241 y2*=frac2;
2242 }
2243 y=d+y1+c*y2;
2244 if (frac1>epsilon && frac2>epsilon) s.Enter(xlow,y); // Protection against machine accuracy
2245 x1low=xup;
2246 i1=h1->FindFixBin(x1low+fabs(epsilon*x1low));
2247 bwidth1=h1->GetBinWidth(i1);
2248 x1up=bwidth1+h1->GetBinLowEdge(i1);
2249 }
2250
2252 // Create the hout histogram //
2254
2255 Int_t nbins=s.GetN();
2256 Double_t* xbins=new Double_t[nbins+1];
2257
2258 for (Int_t i=0; i<nbins; i++)
2259 {
2260 xbins[i]=s.GetEntry(i+1,"X");
2261 }
2262
2263 // Set also the last boundary
2264 xup=x1max;
2265 xbins[nbins]=xup;
2266
2267 hout->SetBins(nbins,xbins);
2268
2269 for (Int_t jbin=1; jbin<=nbins; jbin++)
2270 {
2271 y=s.GetEntry(jbin,"Y");
2272 hout->SetBinContent(jbin,y);
2273 }
2274
2275 // Set the number of entries for "hout"
2276 hout->SetEntries(h1->GetEntries());
2277
2278 delete [] xbins;
2279
2280 return iret;
2281}
2282
2283Int_t NcBlocks::Add(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2284{
2306
2307 if (gout) gout->Set(0);
2308
2309 if (!gr || !h || !gout) return 1;
2310
2311 TString nameg=gr->GetName();
2312 if (nameg=="") nameg="gr";
2313
2314 TString nameh=h->GetName();
2315 if (nameh=="") nameh="h";
2316
2317 TString sc="+";
2318 if (c<0) sc="-";
2319 if (fabs(c)!=1)
2320 {
2321 sc.Form("%-+.3g",c);
2322 sc+="*";
2323 }
2324
2325 TString sd="";
2326 sd.Form("%-+.3g",d);
2327
2328 TString title="Resulting graph of: ";
2329 title+=nameg;
2330 if (c)
2331 {
2332 title+=sc;
2333 title+=nameh;
2334 }
2335 if (d) title+=sd;
2336 title+=";";
2337 TAxis* axis=0;
2338 axis=gr->GetXaxis();
2339 title+=axis->GetTitle();
2340 title+=";";
2341 axis=gr->GetYaxis();
2342 title+=axis->GetTitle();
2343
2344 gout->SetTitle(title);
2345
2346 Int_t ndim=h->GetDimension();
2347
2348 if (ndim!=1)
2349 {
2350 cout << " *NcBlocks::Add* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2351 return 1;
2352 }
2353
2354 Int_t np=gr->GetN();
2355 Int_t nb=h->GetNbinsX();
2356
2357 if (!np || !nb) return 1;
2358
2359 // Loop over all the points in the graph
2360 Double_t x=0;
2361 Double_t y=0;
2362 Double_t ex=0;
2363 Double_t ey=0;
2364 Int_t hbin=0;
2365 Double_t hval=0;
2366 Double_t ynew=0;
2367 axis=h->GetXaxis();
2368 for (Int_t i=0; i<np; i++)
2369 {
2370 gr->GetPoint(i,x,y);
2371 hbin=axis->FindFixBin(x);
2372 hval=h->GetBinContent(hbin);
2373
2374 // Do not consider underflow or overflow bins
2375 if (hbin<1 || hbin>nb) continue;
2376
2377 ynew=y+c*hval+d;
2378 gout->SetPoint(i,x,ynew);
2379
2380 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2381 {
2382 TGraphErrors* gre=(TGraphErrors*)gr;
2383 TGraphErrors* goute=(TGraphErrors*)gout;
2384 ex=gre->GetErrorX(i);
2385 ey=gre->GetErrorY(i);
2386 goute->SetPointError(i,ex,ey);
2387 }
2388 }
2389
2390 return 0;
2391}
2392
2393Int_t NcBlocks::Divide(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
2394{
2427
2428 if (hout) hout->Reset();
2429
2430 if (!c)
2431 {
2432 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2433 return 1;
2434 }
2435
2436 if (!h1 || !h2 || !hout) return 1;
2437
2438 Int_t ndim1=h1->GetDimension();
2439 Int_t ndim2=h2->GetDimension();
2440 Int_t ndimo=hout->GetDimension();
2441
2442 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2443 {
2444 cout << " *NcBlocks::Divide* Error : Histograms should all be 1-dimensional." << endl;
2445 return 1;
2446 }
2447
2448 // Make the X-axis of "hout" identical to the X-axis of "h1"
2449 TString name=hout->GetName();
2450 h1->Copy(*hout);
2451 hout->Reset();
2452
2453 TString name1=h1->GetName();
2454 if (name1=="") name1="h1";
2455
2456 TString name2=h2->GetName();
2457 if (name2=="") name2="h2";
2458
2459 TString sc="/";
2460 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2461
2462 TString sd;
2463 if (c>0)
2464 {
2465 sd="";
2466 if (d) sd.Form("%-.3g+",d);
2467 }
2468 else
2469 {
2470 sd="-";
2471 if (d) sd.Form("%-.3g-",d);
2472 }
2473
2474 TString title="Resulting histogram of: ";
2475 if (sd!="") title+=sd;
2476 if (c)
2477 {
2478 title+=name1;
2479 if (sc.Contains("*")) title+="/(";
2480 title+=sc;
2481 title+=name2;
2482 if (sc.Contains("*")) title+=")";
2483 }
2484 if (scale)
2485 {
2486 title+=" (scaled w.r.t. bin size)";
2487 }
2488 else
2489 {
2490 title+=" (not scaled w.r.t. bin size)";
2491 }
2492 title+=";";
2493 TAxis* axis1=h1->GetXaxis();
2494 title+=axis1->GetTitle();
2495 title+=";";
2496 axis1=h1->GetYaxis();
2497 title+=axis1->GetTitle();
2498
2499 hout->SetName(name);
2500 hout->SetTitle(title);
2501
2502 Int_t nb1=h1->GetNbinsX();
2503 Int_t nb2=h2->GetNbinsX();
2504
2505 if (!nb1 || !nb2)
2506 {
2507 printf(" *NcBlocks::Divide* Error : nbins of h1 is %-i and nbins of h2 is %-i. \n",nb1,nb2);
2508 return 1;
2509 }
2510
2511 // Check whether the range of the axes of h1 and h2 overlap
2512 Double_t x1min=h1->GetXaxis()->GetXmin();
2513 Double_t x1max=h1->GetXaxis()->GetXmax();
2514 Double_t x2min=h2->GetXaxis()->GetXmin();
2515 Double_t x2max=h2->GetXaxis()->GetXmax();
2516
2517 if (x1min>x2max || x1max<x2min)
2518 {
2519 printf(" *NcBlocks::Divide* Error : The ranges of the input histograms do not overlap. \n");
2520 return 1;
2521 }
2522
2524 // Scan over the bins of h1 and h2 to create the data for hout //
2526
2527 Int_t i1=1;
2528 Double_t bwidth1=h1->GetBinWidth(i1);
2529 Double_t x1low=h1->GetBinLowEdge(i1);
2530 Double_t x1up=x1low+bwidth1;
2531 Int_t i2=0;
2532 Int_t i2up=0;
2533 Double_t bwidth2=0;
2534 Double_t x2low=0;
2535 Double_t x2up=0;
2536 Double_t y1=0;
2537 Double_t frac1=1;
2538 Double_t y2=0;
2539 Double_t frac2=1;
2540 Double_t xlow=0;
2541 Double_t xup=0;
2542 Double_t val=0;
2543 Double_t y=0;
2544 NcSample s;
2545 s.SetStoreMode();
2546 Double_t epsilon=1e-10; // To circumvent machine accuracy issues
2547 Int_t iret=0;
2548
2549 while (i1<=nb1 && i2<=nb2)
2550 {
2551 i2=h2->FindFixBin(x1low+fabs(epsilon*x1low));
2552 i2up=h2->FindFixBin(x1up+fabs(epsilon*x1up));
2553
2554 if ((i2<1 && i2up<1) || (i2>nb2 && i2up>nb2) || x1up<=x1low) // No overlap
2555 {
2556 i1++;
2557 x1low=h1->GetBinLowEdge(i1);
2558 bwidth1=h1->GetBinWidth(i1);
2559 x1up=x1low+bwidth1;
2560 continue;
2561 }
2562
2563 y1=h1->GetBinContent(i1);
2564
2565 // If the current bin of h1 is empty, also make it empty in hout and move to the next bin of h1
2566 if (!y1)
2567 {
2568 s.Enter(x1low,0);
2569 i1++;
2570 x1low=h1->GetBinLowEdge(i1);
2571 bwidth1=h1->GetBinWidth(i1);
2572 x1up=x1low+bwidth1;
2573 continue;
2574 }
2575
2576 // Overlapping bins of h1 and h2 -> Determine the bin boundaries of hout
2577 if (i2<1) i2=1;
2578 if (i2>nb2) i2=nb2;
2579 x2low=h2->GetBinLowEdge(i2);
2580 bwidth2=h2->GetBinWidth(i2);
2581 x2up=x2low+bwidth2;
2582 y2=h2->GetBinContent(i2);
2583 xlow=x1low;
2584 xup=x1up;
2585 if (x2low>xlow) xlow=x2low;
2586 if (x2up<xup) xup=x2up;
2587
2588 // Protection against possible infinite loop due to machine accuracy
2589 if (xup<=xlow)
2590 {
2591 iret=1;
2592 break;
2593 }
2594
2595 frac1=(xup-xlow)/bwidth1;
2596 frac2=(xup-xlow)/bwidth2;
2597 if (scale)
2598 {
2599 y1*=frac1;
2600 y2*=frac2;
2601 }
2602 y=0;
2603 val=c*y2;
2604 if (val) y=d+y1/val;
2605 if (frac1>epsilon && frac2>epsilon) s.Enter(xlow,y); // Protection against machine accuracy
2606 x1low=xup;
2607 i1=h1->FindFixBin(x1low+fabs(epsilon*x1low));
2608 bwidth1=h1->GetBinWidth(i1);
2609 x1up=bwidth1+h1->GetBinLowEdge(i1);
2610 }
2611
2613 // Create the hout histogram //
2615
2616 Int_t nbins=s.GetN();
2617 Double_t* xbins=new Double_t[nbins+1];
2618
2619 for (Int_t i=0; i<nbins; i++)
2620 {
2621 xbins[i]=s.GetEntry(i+1,"X");
2622 }
2623
2624 // Set also the last boundary
2625 xup=x1max;
2626 if (x2max<x1max) xup=x2max;
2627 xbins[nbins]=xup;
2628
2629 hout->SetBins(nbins,xbins);
2630
2631 for (Int_t jbin=1; jbin<=nbins; jbin++)
2632 {
2633 y=s.GetEntry(jbin,"Y");
2634 hout->SetBinContent(jbin,y);
2635 }
2636
2637 // Set the number of entries for "hout"
2638 hout->SetEntries(h1->GetEntries());
2639
2640 delete [] xbins;
2641
2642 return iret;
2643}
2644
2645Int_t NcBlocks::Divide(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
2646{
2666
2667 if (gout) gout->Set(0);
2668
2669 if (!c)
2670 {
2671 printf(" *NcBlocks::Divide* Error : Invalid value c=0. \n");
2672 return 1;
2673 }
2674
2675 if (!gr || !h || !gout) return 1;
2676
2677 TString nameg=gr->GetName();
2678 if (nameg=="") nameg="gr";
2679
2680 TString nameh=h->GetName();
2681 if (nameh=="") nameh="h";
2682
2683 TString sc="/";
2684 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2685
2686 TString sd;
2687 if (c>0)
2688 {
2689 sd="";
2690 if (d) sd.Form("%-.3g+",d);
2691 }
2692 else
2693 {
2694 sd="-";
2695 if (d) sd.Form("%-.3g-",d);
2696 }
2697
2698 TString title="Resulting graph of: ";
2699 if (sd!="") title+=sd;
2700 if (c)
2701 {
2702 title+=nameg;
2703 if (sc.Contains("*")) title+="/(";
2704 title+=sc;
2705 title+=nameh;
2706 if (sc.Contains("*")) title+=")";
2707 }
2708 title+=";";
2709 TAxis* axis=0;
2710 axis=gr->GetXaxis();
2711 title+=axis->GetTitle();
2712 title+=";";
2713 axis=gr->GetYaxis();
2714 title+=axis->GetTitle();
2715
2716 gout->SetTitle(title);
2717
2718 Int_t ndim=h->GetDimension();
2719
2720 if (ndim!=1)
2721 {
2722 cout << " *NcBlocks::Divide* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
2723 return 1;
2724 }
2725
2726 Int_t np=gr->GetN();
2727 Int_t nb=h->GetNbinsX();
2728
2729 if (!np || !nb) return 1;
2730
2731 // Loop over all the points in the graph
2732 Double_t x=0;
2733 Double_t y=0;
2734 Double_t ex=0;
2735 Double_t ey=0;
2736 Int_t hbin=0;
2737 Double_t hval=0;
2738 Double_t val=0;
2739 Double_t ynew=0;
2740 axis=h->GetXaxis();
2741 Int_t j=0;
2742 for (Int_t i=0; i<np; i++)
2743 {
2744 gr->GetPoint(i,x,y);
2745 hbin=axis->FindFixBin(x);
2746 hval=h->GetBinContent(hbin);
2747
2748 // Do not consider underflow or overflow bins
2749 if (hbin<1 || hbin>nb) continue;
2750
2751 val=c*hval;
2752 if (!val) continue;
2753
2754 ynew=d+y/val;
2755 gout->SetPoint(j,x,ynew);
2756
2757 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
2758 {
2759 TGraphErrors* gre=(TGraphErrors*)gr;
2760 TGraphErrors* goute=(TGraphErrors*)gout;
2761 ex=gre->GetErrorX(i);
2762 ey=gre->GetErrorY(i);
2763 goute->SetPointError(j,ex,ey);
2764 }
2765
2766 j++;
2767 }
2768
2769 return 0;
2770}
2771
2772Int_t NcBlocks::Multiply(TH1* h1,TH1* h2,TH1* hout,Bool_t scale,Double_t c,Double_t d)
2773{
2806
2807 if (hout) hout->Reset();
2808
2809 if (!h1 || !h2 || !hout) return 1;
2810
2811 Int_t ndim1=h1->GetDimension();
2812 Int_t ndim2=h2->GetDimension();
2813 Int_t ndimo=hout->GetDimension();
2814
2815 if (ndim1!=1 || ndim2!=1 || ndimo!=1)
2816 {
2817 cout << " *NcBlocks::Multiply* Error : Histograms should all be 1-dimensional." << endl;
2818 return 1;
2819 }
2820
2821 // Make the X-axis of "hout" identical to the X-axis of "h1"
2822 TString name=hout->GetName();
2823 h1->Copy(*hout);
2824 hout->Reset();
2825
2826 TString name1=h1->GetName();
2827 if (name1=="") name1="h1";
2828
2829 TString name2=h2->GetName();
2830 if (name2=="") name2="h2";
2831
2832 TString sc="";
2833 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
2834
2835 TString sd;
2836 if (c>=0)
2837 {
2838 sd="";
2839 if (d) sd.Form("%-.3g+",d);
2840 }
2841 else
2842 {
2843 sd="-";
2844 if (d) sd.Form("%-.3g-",d);
2845 }
2846
2847 TString title="Resulting histogram of: ";
2848 if (sd!="") title+=sd;
2849 title+=sc;
2850 title+=name1;
2851 title+="*";
2852 title+=name2;
2853 if (scale)
2854 {
2855 title+=" (scaled w.r.t. bin size)";
2856 }
2857 else
2858 {
2859 title+=" (not scaled w.r.t. bin size)";
2860 }
2861 title+=";";
2862 TAxis* axis1=h1->GetXaxis();
2863 title+=axis1->GetTitle();
2864 title+=";";
2865 axis1=h1->GetYaxis();
2866 title+=axis1->GetTitle();
2867
2868 hout->SetName(name);
2869 hout->SetTitle(title);
2870
2871 Int_t nb1=h1->GetNbinsX();
2872 Int_t nb2=h2->GetNbinsX();
2873
2874 if (!nb1 || !nb2)
2875 {
2876 printf(" *NcBlocks::Multiply* Error : nbins of h1 is %-i and nbins of h2 is %-i. \n",nb1,nb2);
2877 return 1;
2878 }
2879
2880 // Check whether the range of the axes of h1 and h2 overlap
2881 Double_t x1min=h1->GetXaxis()->GetXmin();
2882 Double_t x1max=h1->GetXaxis()->GetXmax();
2883 Double_t x2min=h2->GetXaxis()->GetXmin();
2884 Double_t x2max=h2->GetXaxis()->GetXmax();
2885
2886 if (x1min>x2max || x1max<x2min)
2887 {
2888 printf(" *NcBlocks::Multiply* Error : The ranges of the input histograms do not overlap. \n");
2889 return 1;
2890 }
2891
2893 // Scan over the bins of h1 and h2 to create the data for hout //
2895
2896 Int_t i1=1;
2897 Double_t bwidth1=h1->GetBinWidth(i1);
2898 Double_t x1low=h1->GetBinLowEdge(i1);
2899 Double_t x1up=x1low+bwidth1;
2900 Int_t i2=0;
2901 Int_t i2up=0;
2902 Double_t bwidth2=0;
2903 Double_t x2low=0;
2904 Double_t x2up=0;
2905 Double_t y1=0;
2906 Double_t frac1=1;
2907 Double_t y2=0;
2908 Double_t frac2=1;
2909 Double_t xlow=0;
2910 Double_t xup=0;
2911 Double_t val=0;
2912 Double_t y=0;
2913 NcSample s;
2914 s.SetStoreMode();
2915 Double_t epsilon=1e-10; // To circumvent machine accuracy issues
2916 Int_t iret=0;
2917
2918 while (i1<=nb1 && i2<=nb2)
2919 {
2920 i2=h2->FindFixBin(x1low+fabs(epsilon*x1low));
2921 i2up=h2->FindFixBin(x1up+fabs(epsilon*x1up));
2922
2923 if ((i2<1 && i2up<1) || (i2>nb2 && i2up>nb2) || x1up<=x1low) // No overlap
2924 {
2925 i1++;
2926 x1low=h1->GetBinLowEdge(i1);
2927 bwidth1=h1->GetBinWidth(i1);
2928 x1up=x1low+bwidth1;
2929 continue;
2930 }
2931
2932 y1=h1->GetBinContent(i1);
2933
2934 // If the current bin of h1 is empty, there is only a contribution from the offset "d"
2935 if (!y1)
2936 {
2937 s.Enter(x1low,d);
2938 i1++;
2939 x1low=h1->GetBinLowEdge(i1);
2940 bwidth1=h1->GetBinWidth(i1);
2941 x1up=x1low+bwidth1;
2942 continue;
2943 }
2944
2945 // Overlapping bins of h1 and h2 -> Determine the bin boundaries of hout
2946 if (i2<1) i2=1;
2947 if (i2>nb2) i2=nb2;
2948 x2low=h2->GetBinLowEdge(i2);
2949 bwidth2=h2->GetBinWidth(i2);
2950 x2up=x2low+bwidth2;
2951 y2=h2->GetBinContent(i2);
2952 xlow=x1low;
2953 xup=x1up;
2954 if (x2low>xlow) xlow=x2low;
2955 if (x2up<xup) xup=x2up;
2956
2957 // Protection against possible infinite loop due to machine accuracy
2958 if (xup<=xlow)
2959 {
2960 iret=1;
2961 break;
2962 }
2963
2964 frac1=(xup-xlow)/bwidth1;
2965 frac2=(xup-xlow)/bwidth2;
2966 if (scale)
2967 {
2968 y1*=frac1;
2969 y2*=frac2;
2970 }
2971 y=0;
2972 val=c*y2;
2973 y=d+y1*val;
2974 if (frac1>epsilon && frac2>epsilon) s.Enter(xlow,y); // Protection against machine accuracy
2975 x1low=xup;
2976 i1=h1->FindFixBin(x1low+fabs(epsilon*x1low));
2977 bwidth1=h1->GetBinWidth(i1);
2978 x1up=bwidth1+h1->GetBinLowEdge(i1);
2979 }
2980
2982 // Create the hout histogram //
2984
2985 Int_t nbins=s.GetN();
2986 Double_t* xbins=new Double_t[nbins+1];
2987
2988 for (Int_t i=0; i<nbins; i++)
2989 {
2990 xbins[i]=s.GetEntry(i+1,"X");
2991 }
2992
2993 // Set also the last boundary
2994 xup=x1max;
2995 if (x2max<x1max) xup=x2max;
2996 xbins[nbins]=xup;
2997
2998 hout->SetBins(nbins,xbins);
2999
3000 for (Int_t jbin=1; jbin<=nbins; jbin++)
3001 {
3002 y=s.GetEntry(jbin,"Y");
3003 hout->SetBinContent(jbin,y);
3004 }
3005
3006 // Set the number of entries for "hout"
3007 hout->SetEntries(h1->GetEntries());
3008
3009 delete [] xbins;
3010
3011 return iret;
3012}
3013
3014Int_t NcBlocks::Multiply(TGraph* gr,TH1* h,TGraph* gout,Double_t c,Double_t d)
3015{
3035
3036 if (gout) gout->Set(0);
3037
3038 if (!gr || !h || !gout) return 1;
3039
3040 TString nameg=gr->GetName();
3041 if (nameg=="") nameg="gr";
3042
3043 TString nameh=h->GetName();
3044 if (nameh=="") nameh="h";
3045
3046 TString sc="";
3047 if (fabs(c)!=1) sc.Form("%-.3g*",fabs(c));
3048
3049 TString sd;
3050 if (c>=0)
3051 {
3052 sd="";
3053 if (d) sd.Form("%-.3g+",d);
3054 }
3055 else
3056 {
3057 sd="-";
3058 if (d) sd.Form("%-.3g-",d);
3059 }
3060
3061 TString title="Resulting graph of: ";
3062 if (sd!="") title+=sd;
3063 title+=sc;
3064 title+=nameg;
3065 title+="*";
3066 title+=nameh;
3067 title+=";";
3068 TAxis* axis=0;
3069 axis=gr->GetXaxis();
3070 title+=axis->GetTitle();
3071 title+=";";
3072 axis=gr->GetYaxis();
3073 title+=axis->GetTitle();
3074
3075 gout->SetTitle(title);
3076
3077 Int_t ndim=h->GetDimension();
3078
3079 if (ndim!=1)
3080 {
3081 cout << " *NcBlocks::Multiply* Error : Histogram " << nameh << " should be 1-dimensional." << endl;
3082 return 1;
3083 }
3084
3085 Int_t np=gr->GetN();
3086 Int_t nb=h->GetNbinsX();
3087
3088 if (!np || !nb) return 1;
3089
3090 // Loop over all the points in the graph
3091 Double_t x=0;
3092 Double_t y=0;
3093 Double_t ex=0;
3094 Double_t ey=0;
3095 Int_t hbin=0;
3096 Double_t hval=0;
3097 Double_t val=0;
3098 Double_t ynew=0;
3099 axis=h->GetXaxis();
3100 Int_t j=0;
3101 for (Int_t i=0; i<np; i++)
3102 {
3103 gr->GetPoint(i,x,y);
3104 hbin=axis->FindFixBin(x);
3105 hval=h->GetBinContent(hbin);
3106
3107 // Do not consider underflow or overflow bins
3108 if (hbin<1 || hbin>nb) continue;
3109
3110 val=c*hval;
3111 if (!val) continue;
3112
3113 ynew=d+y*val;
3114 gout->SetPoint(j,x,ynew);
3115
3116 if (gr->InheritsFrom("TGraphErrors") && gout->InheritsFrom("TGraphErrors"))
3117 {
3118 TGraphErrors* gre=(TGraphErrors*)gr;
3119 TGraphErrors* goute=(TGraphErrors*)gout;
3120 ex=gre->GetErrorX(i);
3121 ey=gre->GetErrorY(i);
3122 goute->SetPointError(j,ex,ey);
3123 }
3124
3125 j++;
3126 }
3127
3128 return 0;
3129}
3130
3131Int_t NcBlocks::Rebin(TH1* hin,TH1* hout,Bool_t scale,Int_t nbins,Double_t xmin,Double_t xmax)
3132{
3167
3168 if (hout) hout->Reset();
3169
3170 if (!hin || !hout) return 0;
3171
3172 Int_t ndimi=hin->GetDimension();
3173 Int_t ndimo=hout->GetDimension();
3174
3175 if (ndimi!=1 || ndimo!=1)
3176 {
3177 cout << " *NcBlocks::Rebin* Error : Histograms should both be 1-dimensional." << endl;
3178 return 0;
3179 }
3180
3181 Int_t nb1=hin->GetNbinsX();
3182
3183 if (!nb1) return 0;
3184
3185 TAxis* xaxis=hin->GetXaxis();
3186 TAxis* yaxis=hin->GetYaxis();
3187
3188 if (xmax<xmin)
3189 {
3190 xmin=xaxis->GetXmin();
3191 xmax=xaxis->GetXmax();
3192 }
3193
3194 Double_t bwidth1=0;
3195 Double_t xlow1=0;
3196 Double_t xup1=0;
3197
3198 // Automatic binwidth setting
3199 if (nbins<=0)
3200 {
3201 Double_t bwmin=-1;
3202 for (Int_t i=1; i<=nb1; i++)
3203 {
3204 bwidth1=hin->GetBinWidth(i);
3205 xlow1=hin->GetBinLowEdge(i);
3206 xup1=xlow1+bwidth1;
3207
3208 if (xlow1>=xmax || xup1<=xmin) continue;
3209
3210 if (bwmin<0 || bwidth1<bwmin) bwmin=bwidth1;
3211 }
3212
3213 if (bwmin<0)
3214 {
3215 cout << " *NcBlocks::Rebin* Error : Input histogram had no data in requested interval [xmin,xmax]." << endl;
3216 return 0;
3217 }
3218
3219 Double_t rnbins=(xmax-xmin)/bwmin;
3220 nbins=int(rnbins);
3221 Double_t diff=rnbins-double(nbins);
3222 if (diff) nbins=nbins+1;
3223 }
3224
3225 hout->SetBins(nbins,xmin,xmax);
3226 Double_t bwidth=hout->GetBinWidth(1);
3227
3228 TString name=hin->GetName();
3229 if (name=="") name="hin";
3230
3231 TString snb="";
3232 snb.Form(" nbins=%-i",nbins);
3233
3234 TString smin="";
3235 smin.Form(" xmin=%-.3g",xmin);
3236
3237 TString smax="";
3238 smax.Form(" xmax=%-.3g",xmax);
3239
3240 TString title="Uniformly binned version of histogram: ";
3241 title+=name;
3242 title+=snb;
3243 title+=smin;
3244 title+=smax;
3245 if (scale)
3246 {
3247 title+=" (scaled w.r.t. bin size)";
3248 }
3249 else
3250 {
3251 title+=" (not scaled w.r.t. bin size)";
3252 }
3253 title+=";";
3254 title+=xaxis->GetTitle();
3255 title+=";";
3256 title+=yaxis->GetTitle();
3257
3258 hout->SetTitle(title);
3259
3260 // Check if the binning of "hout" is not coarser than that of "hin" within [xmin,xmax]
3261 for (Int_t i=1; i<=nb1; i++)
3262 {
3263 bwidth1=hin->GetBinWidth(i);
3264 xlow1=hin->GetBinLowEdge(i);
3265 xup1=xlow1+bwidth1;
3266
3267 if (xlow1>=xmax || xup1<=xmin) continue;
3268
3269 if (bwidth1<bwidth)
3270 {
3271 printf(" *NcBlocks::Rebin* Error : Input histogram had finer binning than output histogram. \n");
3272 printf(" Input: binwidth=%-g for bin=%-i Output: uniform binwidth=%-g \n",bwidth1,i,bwidth);
3273 return 0;
3274 }
3275 }
3276
3277 // Loop over all the bins of the output histogram hout
3278 Double_t x=0;
3279 Int_t i1=0;
3280 Double_t y1=0;
3281 for (Int_t i=1; i<=nbins; i++)
3282 {
3283 x=hout->GetBinCenter(i);
3284 i1=xaxis->FindFixBin(x);
3285
3286 // Do not consider underflow or overflow bins
3287 if (i1<1 || i1>nb1) continue;
3288
3289 y1=hin->GetBinContent(i1);
3290 bwidth1=hin->GetBinWidth(i1);
3291 if (scale) y1=y1*bwidth/bwidth1;
3292 hout->SetBinContent(i,y1);
3293 }
3294
3295 return nbins;
3296}
3297
ClassImp(NcBlocks)
(Bayesian) Block treatment of sequential data.
Definition NcBlocks.h:17
Int_t Multiply(TH1 *h1, TH1 *h2, TH1 *hout, Bool_t scale, Double_t c, Double_t d=0)
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 Add(TH1 *h1, TH1 *h2, TH1 *hout, Bool_t scale, Double_t c, Double_t d=0, Int_t mode=-1)
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:46
Int_t Divide(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:360
Double_t GetMean(Int_t i) const
void SetStoreMode(Int_t mode=100, Int_t nmax=0, Int_t i=0)
TString GetVariableName(Int_t i) const
Int_t GetDimension() const
TGraph GetGraph(Int_t i, TF1 *f=0)
Double_t GetEntry(Int_t i, Int_t j, Int_t mode=0, Int_t k=0)