Changeset 1826 for trunk/MagicSoft/Mars/mhist
- Timestamp:
- 03/18/03 17:20:56 (22 years ago)
- Location:
- trunk/MagicSoft/Mars/mhist
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mhist/MHMatrix.cc
r1809 r1826 206 206 207 207 for (int x=0; x<m.GetNcols(); x++) 208 for (int y=0; y<m.GetNrows(); y++) 209 fM(y, x) = m(y, x); 208 { 209 TVector vold(fM.GetNcols()); 210 TMatrixColumn(fM, x) = vold = TMatrixColumn(m, x); 211 } 210 212 } 211 213 … … 242 244 243 245 for (int x=0; x<fM.GetNcols(); x++) 244 for (int y=0; y<fM.GetNrows(); y++) 245 fM(y, x) = m(y, x); 246 { 247 TVector vold(fM.GetNcols()); 248 TMatrixColumn(fM, x) = vold = TMatrixColumn(m, x); 249 } 246 250 247 251 return kTRUE; … … 366 370 avg /= rows; 367 371 368 for (int y=0; y<rows; y++) 369 m(y, x) -= avg; 372 TMatrixColumn(m, x) += -avg; 370 373 } 371 374 … … 584 587 { 585 588 TVector vold(n); 586 vold = TMatrixRow(fM, idx[i]); 587 588 TMatrixRow rownew(m, i); 589 rownew = vold; 589 TMatrixRow(m, i) = vold = TMatrixRow(fM, idx[i]); 590 590 } 591 591 … … 666 666 { 667 667 TVector vold(fM.GetNcols()); 668 vold = TMatrixRow(fM, oldrow); 669 670 TMatrixRow rownew(fM, newrow); 671 rownew = vold; 672 newrow++; 668 TMatrixRow(fM, newrow++) = vold = TMatrixRow(fM, oldrow); 673 669 } 674 670 675 671 oldrow++; 676 672 } 677 678 /*679 680 TMatrix m(n, fM.GetNcols());681 for (int i=0; i<n; i++)682 {683 TVector vold(n);684 vold = TMatrixRow(fM, idx[i]);685 686 TMatrixRow rownew(m, i);687 rownew = vold;688 }689 */690 673 } 691 674 … … 701 684 // thsh histogram containing the target distribution of the variable 702 685 // nmaxevts maximum number of events in the reference matrix 703 // 704 Bool_t MHMatrix::DefRefMatrix(const Int_t refcolumn, const TH1F *thsh, 705 const Int_t nmaxevts) 706 { 707 if (!fM.IsValid()) 708 { 709 *fLog << "MHMatrix::DefRefMatrix; matrix not initialized" << endl; 710 return kFALSE; 711 } 712 713 const TH1F *fHthsh; // target distribution 714 TH1F *fHthd; // normalization factors 715 TH1F *fHth; // distribution before 716 TH1F *fHthaft; // distribution after renormalization 717 718 Int_t frefcol; // number of the column (count from 0) containing 719 // the variable for which the target distribution is given 720 Int_t fnbins; // histogram parameters for thsh 721 Float_t ffrombin; // 722 Float_t ftobin; // 723 Float_t fdbin; // 724 725 TArrayF fnormfac; // array of normalization factors 726 TMatrix fMnew; // matrix fM having the requested distribution 727 // and the requested number of rows; 728 // this is the matrix to be used in the g/h separation 729 730 731 //=================================================================== 732 //--------------------------------------------------------- 733 // Calculate normalization factors 734 // 735 736 fHthsh = thsh; 737 738 Int_t fNrows = fM.GetNrows(); 739 Int_t fNcols = fM.GetNcols(); 740 741 // print characteristics 742 // 743 //Int_t fColLwb = fM.GetColLwb(); 744 //Int_t fRowLwb = fM.GetRowLwb(); 745 //*fLog << "MHMatrix::CalcNormFactors; Event matrix (input): " << endl; 746 //*fLog << " fNrows, fNcols, fColLwb, fRowLwb = " << fNrows << ", " 747 // << fNcols << ", " << fColLwb << ", " << fRowLwb << endl; 748 749 750 //--------------------------------------------------------- 751 // 752 // if refcolumn < 0 : select reference events randomly 753 // i.e. set the normaliztion factotrs equal to 1.0 754 // refcol is the column number starting at 0; it is >= 0 755 756 if (refcolumn<0) 757 { 758 frefcol = -refcolumn - 1; 759 } 760 else 761 { 762 frefcol = refcolumn - 1; 763 } 764 765 766 //--------------------------------------------------------- 767 768 if (fHthsh == NULL) 769 { 770 *fLog << "MHMatrix::DefRefMatrix; target distribution has to be defined" 771 << endl; 772 return kFALSE; 773 } 774 775 fnbins = fHthsh->GetNbinsX(); 776 ffrombin = fHthsh->GetBinLowEdge(1); 777 ftobin = fHthsh->GetBinLowEdge(fnbins+1); 778 fdbin = (ftobin-ffrombin)/fnbins; 779 //*fLog << "CalcNormFactors; Reference column (count from 0) = " 780 // << frefcol << endl; 781 //*fLog << "CalcNormFactors; Histo params : " << fnbins << ", " << ffrombin 782 // <<", "<< ftobin << endl; 783 784 785 //--------------------------------------------------------- 786 // ===== set up the real histogram 787 // 788 fHth = new TH1F("th", "data at input", fnbins, ffrombin, ftobin); 789 for (Int_t j=1; j<=fNrows; j++) 790 { 791 fHth->Fill(fM(j-1,frefcol)); 792 //*fLog << "j, frefcol, fM(j-1,frefcol) = " << j << ", " << frefcol 793 // << ", " << fM(j-1,frefcol) << endl; 794 } 795 796 //--------------------------------------------------------- 797 // ===== obtain correction factors 798 // 799 fHthd = new TH1F ("thd", "correction factors", fnbins, ffrombin, ftobin); 800 801 Double_t cmax = 0.; 802 Float_t a; 803 Float_t b; 804 Float_t c; 805 for (Int_t j=1; j<=fnbins; j++) 806 { 807 a = fHth->GetBinContent(j); 808 809 // if refcolumn < 0 set the correction factors equal to 1 810 if ( refcolumn>=0 ) 811 b = fHthsh->GetBinContent(j); 812 else 813 b = a; 814 815 if ( a<0.0 || b<0.0 ) 686 // rest a TMatrix conatining the resulting (not choosen) 687 // columns of the primary matrix. Maybe NULL if you 688 // are not interested in this 689 // 690 Bool_t MHMatrix::DefRefMatrix(const TH1F &thsh, const Int_t refcolumn, 691 const Int_t nmaxevts, TMatrix *rest) 692 { 693 if (!fM.IsValid()) 694 { 695 *fLog << err << dbginf << "Matrix not initialized" << endl; 696 return kFALSE; 697 } 698 699 // 700 // if refcolumn < 0 : select reference events randomly 701 // i.e. set the normaliztion factotrs equal to 1.0 702 // refcol is the column number starting at 0; it is >= 0 703 // 704 // number of the column (count from 0) containing 705 // the variable for which the target distribution is given 706 // 707 const Int_t refcol = refcolumn<0 ? -refcolumn - 1 : refcolumn - 1; 708 709 // 710 // Calculate normalization factors 711 // 712 const int nbins = thsh.GetNbinsX(); 713 const float frombin = thsh.GetBinLowEdge(1); 714 const float tobin = thsh.GetBinLowEdge(nbins+1); 715 const float dbin = thsh.GetBinWidth(1); 716 const Int_t nrows = fM.GetNrows(); 717 const Int_t ncols = fM.GetNcols(); 718 719 // 720 // set up the real histogram (distribution before) 721 // 722 TH1F hth("th", "data at input", nbins, frombin, tobin); 723 for (Int_t j=0; j<nrows; j++) 724 hth.Fill(fM(j, refcol)); 725 726 // 727 // ===== obtain correction factors (normalization factors) 728 // 729 TH1F hthd("thd", "correction factors", nbins, frombin, tobin); 730 731 Double_t cmax = 0.; 732 for (Int_t j=1; j<=nbins; j++) 733 { 734 const float a = hth.GetBinContent(j); 735 736 // if refcolumn < 0 set the correction factors equal to 1 737 const float b = refcolumn>=0 ? thsh.GetBinContent(j) : a; 738 739 if (a<0 || b<0) 740 { 741 *fLog << err << dbginf << "renormalization of input matrix is not possible" << endl; 742 *fLog << " a, b = " << a << ", " << b << endl; 743 return kFALSE; 744 } 745 746 const float c = a==0 ? 0 : b/a; 747 748 hthd.SetBinContent(j, c); 749 750 if (cmax < c) 751 cmax = c; 752 } 753 754 if (hthd.GetMaximum() <= 0) 755 { 756 *fLog << err << dbginf << "Maximum ratio is LE zero" << endl; 757 return kFALSE; 758 } 759 760 hthd.Scale(1/hthd.GetMaximum()); 761 762 // 763 // ==== compress matrix fM into Mnew 764 // 765 766 // 767 // get random access 768 // 769 TArrayF ranx(nrows); 770 771 TRandom3 rnd(0); 772 for (Int_t i=0; i<nrows; i++) 773 ranx[i] = rnd.Rndm(i); 774 775 TArrayI ind(nrows); 776 TMath::Sort(nrows, ranx.GetArray(), ind.GetArray(), kTRUE); 777 778 // 779 // define new matrix 780 // 781 Int_t evtcount1 = -1; 782 Int_t evtcount2 = 0; 783 TMatrix mnewtmp(nrows, ncols); 784 TMatrix mrest(nrows, ncols); 785 786 TArrayF cumulweight(nrows); // keep track for each bin how many events 787 788 TMatrix hindref(fM); 789 hindref -= frombin; 790 hindref *= 1/dbin; 791 792 // 793 // select events (distribution after renormalization) 794 // 795 for (Int_t ir=0; ir<nrows; ir++) 796 { 797 const Int_t indref = (Int_t)hindref(ind[ir], refcol); 798 799 cumulweight[indref] += hthd.GetBinContent(indref+1); 800 if (cumulweight[indref]<=0.5) 801 { 802 TVector vold(fM.GetNcols()); 803 TMatrixRow(mrest, evtcount2++) = vold = TMatrixRow(fM, ind[ir]); 804 continue; 805 } 806 807 cumulweight[indref] -= 1.; 808 if (++evtcount1 >= nmaxevts) 809 { 810 for (; ir<nrows; ir++) 811 { 812 TVector vold(fM.GetNcols()); 813 TMatrixRow(mrest, evtcount2++) = vold = TMatrixRow(fM, ind[ir]); 814 } 815 break; 816 } 817 818 TVector vold(fM.GetNcols()); 819 TMatrixRow(mnewtmp, evtcount1) = vold = TMatrixRow(fM, ind[ir]); 820 } 821 822 // 823 // reduce size 824 // 825 // matrix fM having the requested distribution 826 // and the requested number of rows; 827 // this is the matrix to be used in the g/h separation 828 // 829 830 fM.ResizeTo(evtcount1, ncols); 831 fNumRow = evtcount1; 832 for (Int_t ir=0; ir<evtcount1; ir++) 833 { 834 TVector vold(fM.GetNcols()); 835 TMatrixRow(fM, ir) = vold = TMatrixRow(mnewtmp, ir); 836 } 837 838 if (evtcount1 < nmaxevts) 839 *fLog << warn << "The reference sample contains less events (" << evtcount1 << ") than required (" << nmaxevts << ")" << endl; 840 841 if (!rest) 842 return kTRUE; 843 844 rest->ResizeTo(evtcount2, ncols); 845 for (Int_t ir=0; ir<evtcount1; ir++) 846 { 847 TVector vold(fM.GetNcols()); 848 TMatrixRow(*rest, ir) = vold = TMatrixRow(mrest, ir); 849 } 850 851 return kTRUE; 852 853 /* 854 TMatrix mnew(evtcount, nconl); 855 for (Int_t ir=0; ir<evtcount; ir++) 856 for (Int_t ic=0; ic<fNcols; ic++) 857 fM(ir,ic) = mnewtmp(ir,ic); 858 859 // 860 // test: print new matrix (part) 861 // 862 *fLog << "DefRefMatrix: Event matrix (output) :" << endl; 863 *fLog << "DefRefMatrix: Nrows, Ncols = " << mnew.GetNrows(); 864 *fLog << " " << mnew.GetNcols() << endl; 865 866 for (Int_t ir=0;ir<10; ir++) 816 867 { 817 *fLog << "MHMatrix::DefRefMatrix; renormalization of input matrix is not possible" << endl; 818 *fLog << " a, b = " << a << ", " << b << endl; 819 return kFALSE; 868 *fLog <<ir <<" "; 869 for (Int_t ic=0; ic<mnew.GetNcols(); ic++) 870 cout<<Mnew(ir,ic)<<" "; 871 *fLog <<endl; 820 872 } 821 822 if (a == 0.0) 823 c = 0.0; 824 else 825 c = b/a; 826 827 fHthd->SetBinContent(j, c); 828 if (cmax < c) cmax = c; 829 //*fLog << j << ", " << a << ", " << b << ", " << c << endl; 830 } 831 832 //*fLog << "MHMatrix::CalcNormFactors; maximum ratio between histogram and target " 833 // << cmax << endl; 834 835 if (cmax <= 0.0) 836 { 837 *fLog << "MHMatrix::CalcNormFactors; maximum ratio is LE zero" << endl; 838 return kFALSE; 839 } 840 841 fnormfac.Set(fnbins); 842 Float_t minbin = 1.0; 843 for (Int_t j=1; j<=fnbins; j++) 844 { 845 float c = (fHthd->GetBinContent(j)) / cmax; 846 fnormfac[j-1] = c; 847 if (minbin > c && c>0) minbin = c; 848 fHthd->SetBinContent(j, c); 849 } 850 //*fLog << "MHMatrix::CalcNormFactors; maximum weight = 1, minimum non-zero weight = " 851 // << minbin <<endl; 852 853 854 //--------------------------------------------------------- 855 //=================================================================== 856 857 858 // ==== compress matrix fM into Mnew 859 // 860 // get random access 861 TArrayF ranx(fNrows); 862 TRandom3 *fRnd = new TRandom3(0); 863 864 for (Int_t i=0;i<fNrows;i++) ranx[i] = fRnd->Rndm(i); 865 866 TArrayI ind(fNrows); 867 TMath::Sort(fNrows, ranx.GetArray(), ind.GetArray(),kTRUE); 868 869 //*fLog << "MHMatrix::DefRefMatrix; permuting array " <<endl; 870 //for (Int_t i=0; i<10; i++) cout << ind[i] << " "; 871 //*fLog << endl; 872 873 //--------------------------------------------------------- 874 // go through matrix and keep events depending on content of 875 // reference column 876 // 877 //*fLog << "MHMatrix::DefRefMatrix; pass at most " << nmaxevts 878 // << " events, in random order " << endl; 879 880 881 //--------------------------------------------------------- 882 // define new matrix 883 Int_t evtcount = 0; 884 TMatrix Mnew_tmp (fNrows,fNcols); 885 886 TArrayF cumul_weight(fNrows); // keep track for each bin how many events 887 888 for (Int_t ir=0; ir<fNrows; ir++) cumul_weight[ir]=0; 889 890 //-------------------------------- 891 // select events 892 // 893 fHthaft = new TH1F ("thaft","data after selection", fnbins, ffrombin, ftobin); 894 for (Int_t ir=0; ir<fNrows; ir++) 895 { 896 Int_t indref = (Int_t) ((fM(ind[ir],frefcol)-ffrombin)/fdbin); 897 cumul_weight[indref] += fnormfac[indref]; 898 if (cumul_weight[indref]>0.5) 873 */ 874 875 /* 876 // test print new bin contents 877 *fLog << "MHMatrix::DefRefMatrix; new histogram: " << endl; 878 for (Int_t j=1; j<=fnbins; j++) 899 879 { 900 cumul_weight[indref] -= 1.;901 if (evtcount++ < nmaxevts)902 {903 fHthaft->Fill(fM(ind[ir],frefcol));904 for (Int_t ic=0; ic<fNcols; ic++)905 Mnew_tmp(evtcount-1,ic) = fM(ind[ir],ic);906 }907 else break;908 }909 }910 evtcount--;911 //*fLog << "MHMatrix::DefRefMatrix; passed " << evtcount912 // << " events" << endl;913 914 //--------------------------------915 // reduce size916 917 TMatrix Mnew(evtcount, fNcols);918 fM.ResizeTo (evtcount, fNcols);919 fNumRow = evtcount;920 921 for (Int_t ir=0;ir<evtcount; ir++)922 for (Int_t ic=0;ic<fNcols;ic++)923 {924 Mnew(ir,ic) = Mnew_tmp(ir,ic);925 fM(ir,ic) = Mnew_tmp(ir,ic);926 }927 928 if (evtcount < nmaxevts)929 {930 *fLog << "MHMatrix::DefRefMatrix; Warning : The reference sample contains less events (" << evtcount << ") than required (" << nmaxevts << ")" << endl;931 }932 933 934 //---------------------------------------------------------935 // test: print new matrix (part)936 *fLog << "MHMatrix::DefRefMatrix; Event matrix (output) :" << endl;937 *fLog << "MHMatrix::DefRefMatrix; Nrows, Ncols = " << Mnew.GetNrows()938 << " " << Mnew.GetNcols() << endl;939 for (Int_t ir=0;ir<10; ir++)940 {941 *fLog <<ir <<" ";942 for (Int_t ic=0; ic<Mnew.GetNcols(); ic++) cout<<Mnew(ir,ic)<<" ";943 *fLog <<endl;944 }945 946 // test print new bin contents947 *fLog << "MHMatrix::DefRefMatrix; new histogram: " << endl;948 for (Int_t j=1; j<=fnbins; j++)949 {950 880 float a = fHthaft->GetBinContent(j); 951 881 *fLog << j << " "<< a << " "; 952 } 953 *fLog <<endl; 954 955 //--------------------------------------------------------- 956 // ==== plot four histograms 957 TCanvas *th1 = new TCanvas (fName, fName, 1); 958 th1->Divide(2,2); 959 960 th1->cd(1); 961 ((TH1F*)fHthsh)->DrawCopy(); // target 962 963 th1->cd(2); 964 ((TH1F*)fHth)->DrawCopy(); // real histogram before 965 966 th1->cd(3); 967 ((TH1F*)fHthd)->DrawCopy(); // correction factors 968 969 th1->cd(4); 970 ((TH1F*)fHthaft)->DrawCopy(); // histogram after 971 972 //--------------------------------------------------------- 973 974 return kTRUE; 882 } 883 *fLog <<endl; 884 */ 885 886 /* 887 //--------------------------------------------------------- 888 // ==== plot four histograms 889 TCanvas *th1 = new TCanvas (fName, fName, 1); 890 th1->Divide(2,2); 891 892 th1->cd(1); 893 ((TH1F*)fHthsh)->DrawCopy(); // target 894 895 th1->cd(2); 896 ((TH1F*)fHth)->DrawCopy(); // real histogram before 897 898 th1->cd(3); 899 ((TH1F*)fHthd)->DrawCopy(); // correction factors 900 901 th1->cd(4); 902 ((TH1F*)fHthaft)->DrawCopy(); // histogram after 903 904 //--------------------------------------------------------- 905 */ 906 //return kTRUE; 975 907 } 976 908 … … 989 921 990 922 // -------------------------------------------------------------------------- 991 992 993 994 995 996 997 -
trunk/MagicSoft/Mars/mhist/MHMatrix.h
r1762 r1826 99 99 Int_t Read(const char *name); 100 100 101 Bool_t DefRefMatrix (const Int_t refcolumn=-1, const TH1F *thsh=NULL,102 const Int_t nmaxevts=0);101 Bool_t DefRefMatrix(const TH1F &thsh, const Int_t refcolumn=-1, 102 const Int_t nmaxevts=0, TMatrix *mrest=NULL); 103 103 104 104 ClassDef(MHMatrix, 1) // Multidimensional Matrix to store events
Note:
See TracChangeset
for help on using the changeset viewer.