MultiBinAnaHelper Class Reference

#include <MultiBinAnaHelper.h>

List of all members.

Public Member Functions

 MultiBinAnaHelper ()
virtual ~MultiBinAnaHelper ()
void Rebin2DHist (TH2D *h, Int_t nx, Double_t *x, Int_t ny, Double_t *y)
void Rebin3DHist (TH3D *&h, Int_t nx, Double_t *x, Int_t ny, Double_t *y, Int_t nz, Double_t *z)
void CopyYtoZ (TH2D *h2, TH3D *&h3)

Detailed Description

Definition at line 14 of file MultiBinAnaHelper.h.


Constructor & Destructor Documentation

MultiBinAnaHelper::MultiBinAnaHelper (  ) 

Definition at line 5 of file MultiBinAnaHelper.cxx.

00006 {
00007 }

MultiBinAnaHelper::~MultiBinAnaHelper (  )  [virtual]

Definition at line 8 of file MultiBinAnaHelper.cxx.

00009 {
00010 }


Member Function Documentation

void MultiBinAnaHelper::CopyYtoZ ( TH2D *  h2,
TH3D *&  h3 
)

Definition at line 313 of file MultiBinAnaHelper.cxx.

00313                                                     {
00314 
00315   //Takes in filled h2 and empty h3
00316   //Copies X and Y axes of h2 to h3 AND Y axis of h2 to Z axis of h3
00317   //Fills h3 with contents of h2 such that YZ proj of h3 is diagonal
00318 
00319   if (!h2) {
00320     cout << "The TH2D pointer is null! Quitting..." << endl;
00321     return;
00322   }
00323 
00324   int nx = h2->GetNbinsX();
00325   int ny = h2->GetNbinsY();
00326   int ix,iy;
00327   double *x = new double[nx+1];
00328   double *y = new double[ny+1];
00329 
00330   for(ix = 0; ix < nx + 1; ix++) x[ix] = h2->GetXaxis()->GetBinLowEdge(ix+1);
00331   for(iy = 0; iy < ny + 1; iy++) y[iy] = h2->GetYaxis()->GetBinLowEdge(iy+1);
00332   
00333   string histname = string(h2->GetName());
00334   histname += "_CopyYtoZ";
00335 
00336   h3 = new TH3D(histname.c_str(),"",nx,x,ny,y,ny,y);
00337   double bincont,binerr;
00338 
00339   for (ix = 1; ix <= nx; ix++) {
00340     for (iy = 1; iy <= ny; iy++) {
00341       bincont = h2->GetBinContent(ix,iy);
00342       binerr = h2->GetBinError(ix,iy);
00343       h3->SetBinContent(ix,iy,iy,bincont);
00344       h3->SetBinError(ix,iy,iy,binerr);
00345     }
00346   }
00347 
00348   delete [] x;
00349   delete [] y;
00350 
00351 }

void MultiBinAnaHelper::Rebin2DHist ( TH2D *  h,
Int_t  nx,
Double_t *  x,
Int_t  ny,
Double_t *  y 
)

Definition at line 12 of file MultiBinAnaHelper.cxx.

References MuELoss::e.

Referenced by Extrapolate2D_Simple::ReadNDDataFile(), ErrorCalc::ReadSysFiles_Appearance(), ErrorCalc::ReadSysFiles_Appearance_AllRuns(), ErrorCalc_Joint::ReadSysFiles_Appearance_AllRuns(), ErrorCalc::ReadSysFiles_FNExtrap(), ErrorCalc_Joint::ReadSysFiles_FNExtrap_AllRuns(), ErrorCalc::ReadSysFiles_FNExtrap_AllRuns(), Extrapolate2D_Simple::RebinInputHists(), and Extrapolate2D::RebinInputHists().

00013 {
00014   //Be careful using this function!! It only works correctly if you give it correct input.  It is not smart enough to make sure that your new bins can really be constructed out of the old ones.
00015   
00016   Bool_t rebinx=true,rebiny=true;
00017   if(!x || nx==0)
00018   {
00019     rebinx = false;
00020   }
00021   if(!y || ny==0)
00022   {
00023     rebiny = false;
00024   }
00025   
00026   if(!rebinx && !rebiny)
00027   {
00028     //do nothing
00029     return;
00030   }
00031   
00032   Int_t i,ix,iy,jx,jy;
00033   
00034   Int_t noldx = h->GetNbinsX();
00035   Int_t noldy = h->GetNbinsY();
00036   double *oldxb = new double[noldx+1];
00037   double *oldyb = new double[noldy+1];
00038   for(ix=0;ix<noldx+1;ix++)
00039   {
00040     oldxb[ix] = h->GetXaxis()->GetBinLowEdge(ix+1);
00041   }
00042   for(iy=0;iy<noldy+1;iy++)
00043   {
00044     oldyb[iy] = h->GetYaxis()->GetBinLowEdge(iy+1);
00045   }
00046   
00047   Int_t nnewx = nx;
00048   Int_t nnewy = ny;
00049   if(!rebinx)
00050   {
00051     nnewx = noldx;
00052   }
00053   if(!rebiny)
00054   {
00055     nnewy = noldy;
00056   }
00057   
00058   double *xb = new double[nnewx+1];
00059   double *yb = new double[nnewy+1];
00060   for(i=0;i<nnewx+1;i++)
00061   {
00062     if(rebinx)
00063     {
00064       xb[i] = x[i];
00065     }
00066     else
00067     {
00068       xb[i] = h->GetXaxis()->GetBinLowEdge(i+1);
00069     }
00070   }
00071   for(i=0;i<nnewy+1;i++)
00072   {
00073     if(rebiny)
00074     {
00075       yb[i] = y[i];
00076     }
00077     else
00078     {
00079       yb[i] = h->GetYaxis()->GetBinLowEdge(i+1);
00080     }
00081   }
00082   
00083   if(nnewx>noldx)
00084   {
00085     cout<<"Error in MultiBinAnaHelper::Rebin2DHist(): number of new x bins is greater than the number of original x bins!  Quitting..."<<endl;
00086     return;
00087   }
00088   if(nnewy>noldy)
00089   {
00090     cout<<"Error in MultiBinAnaHelper::Rebin2DHist(): number of new y bins is greater than the number of original y bins!  Quitting..."<<endl;
00091     return;
00092   }
00093   
00094   double dx = h->GetXaxis()->GetBinWidth(1)*1e-4;
00095   double dy = h->GetYaxis()->GetBinWidth(1)*1e-4;
00096   
00097   string name;
00098   
00099   TH2D *old = (TH2D*)h->Clone();
00100   name = h->GetName();
00101   name += "_old";
00102   old->SetNameTitle(name.c_str(),"");
00103 //   cout<<old->Integral()<<endl;
00104   
00105   h->Reset();
00106   h->SetBins(nnewx,xb,nnewy,yb);
00107   
00108   name = h->GetName();
00109   name += "_mid";
00110   TH2D *mid = new TH2D(name.c_str(),"",nnewx,xb,noldy,oldyb);
00111   mid->Sumw2();
00112   
00113   TH1D *proj;
00114   Double_t s,ds;
00115   
00116   if(rebinx)
00117   {
00118     for(iy=0;iy<noldy;iy++)
00119     {
00120       name = old->GetName();
00121       name += "_px";
00122       proj = old->ProjectionX(name.c_str(),iy+1,iy+1);
00123       //should be able to do a proj->Rebin(x) here but it doesn't work
00124       for(ix=0;ix<nnewx;ix++)
00125       {
00126         s=0;
00127         ds=0;
00128         for(jx=0;jx<noldx;jx++)
00129         {
00130           if(TMath::Abs(xb[ix]-oldxb[jx])<dx || (oldxb[jx]>xb[ix] && oldxb[jx]<xb[ix+1]))//low edges of old and new bin matches or low edge of old bin is in between new bin edges
00131           {
00132             s+=proj->GetBinContent(jx+1);
00133             ds+=proj->GetBinError(jx+1)*proj->GetBinError(jx+1);
00134           }
00135         }
00136         ds = TMath::Sqrt(ds);
00137         
00138         mid->SetBinContent(ix+1,iy+1,s);
00139         mid->SetBinError(ix+1,iy+1,ds);
00140       }
00141       proj->Reset();
00142     }
00143   }
00144   else
00145   {
00146     mid->Add(old);
00147   }
00148   
00149 //   cout<<mid->Integral()<<endl;
00150   
00151   double tot = 0;
00152   
00153   if(rebiny)
00154   {
00155     for(ix=0;ix<nnewx;ix++)
00156     {
00157       name = old->GetName();
00158       name += "_py";
00159       proj = mid->ProjectionY(name.c_str(),ix+1,ix+1);
00160       //should be able to do a proj->Rebin(y) here but it doesn't work
00161       for(iy=0;iy<nnewy;iy++)
00162       {
00163 //       cout<<"new bin "<<iy<<endl;
00164         s=0;
00165         ds=0;
00166         for(jy=0;jy<noldy;jy++)
00167         {
00168           if(TMath::Abs(yb[iy]-oldyb[jy])<dy || (oldyb[jy]>yb[iy] && oldyb[jy]<yb[iy+1]))//low edges of old and new bin matches or low edge of old bin is in between new bin edges
00169           {
00170             s+=proj->GetBinContent(jy+1);
00171             ds+=proj->GetBinError(jy+1)*proj->GetBinError(jy+1);
00172 //          cout<<"old bin "<<jy<<endl;
00173           }
00174         }
00175         ds = TMath::Sqrt(ds);
00176         
00177         h->SetBinContent(ix+1,iy+1,s);
00178         h->SetBinError(ix+1,iy+1,ds);
00179         tot+=s;
00180       }
00181       proj->Reset();
00182     }
00183   }
00184   else
00185   {
00186     h->Add(mid);
00187   }
00188   
00189   mid->Delete();
00190   
00191   delete [] oldxb;
00192   delete [] oldyb;
00193   delete [] xb;
00194   delete [] yb;
00195   
00196 //   cout<<h->Integral()<<endl;
00197 //   cout<<tot<<endl;
00198   return;
00199 }

void MultiBinAnaHelper::Rebin3DHist ( TH3D *&  h,
Int_t  nx,
Double_t *  x,
Int_t  ny,
Double_t *  y,
Int_t  nz,
Double_t *  z 
)

Definition at line 201 of file MultiBinAnaHelper.cxx.

Referenced by Extrapolate2D_Simple::RebinInputHists(), and Extrapolate2D::RebinInputHists().

00202 {
00203   //Be careful using this function!! It only works correctly if you give it correct input.  It is not smart enough to make sure that your new bins can really be constructed out of the old ones.
00204   
00205   Bool_t rebinx=true,rebiny=true,rebinz=true;
00206   if(!x || nx==0) rebinx = false;
00207   if(!y || ny==0) rebiny = false;
00208   if(!z || nz==0) rebinz = false;
00209 
00210   if(!rebinx && !rebiny && !rebinz) return; //do nothing
00211   
00212   Int_t i,ix,iy,iz;
00213   
00214   Int_t noldx = h->GetNbinsX();
00215   Int_t noldy = h->GetNbinsY();
00216   Int_t noldz = h->GetNbinsZ();
00217   double *oldxb = new double[noldx+1];
00218   double *oldyb = new double[noldy+1];
00219   double *oldzb = new double[noldz+1];
00220   for(ix = 0; ix < noldx + 1; ix++) oldxb[ix] = h->GetXaxis()->GetBinLowEdge(ix+1);
00221   for(iy = 0; iy < noldy + 1; iy++) oldyb[iy] = h->GetYaxis()->GetBinLowEdge(iy+1);
00222   for(iz = 0; iz < noldz + 1; iz++) oldzb[iz] = h->GetZaxis()->GetBinLowEdge(iz+1);
00223   
00224   Int_t nnewx = nx;
00225   Int_t nnewy = ny;
00226   Int_t nnewz = nz;
00227   if(!rebinx) nnewx = noldx;
00228   if(!rebiny) nnewy = noldy;
00229   if(!rebinz) nnewz = noldz;
00230   
00231   double *xb = new double[nnewx+1];
00232   double *yb = new double[nnewy+1];
00233   double *zb = new double[nnewz+1];
00234   for(i = 0; i < nnewx + 1; i++) {
00235     if(rebinx) xb[i] = x[i];
00236     else xb[i] = h->GetXaxis()->GetBinLowEdge(i+1);
00237   }
00238   for(i = 0; i < nnewy + 1; i++) {
00239     if(rebiny) yb[i] = y[i];
00240     else yb[i] = h->GetYaxis()->GetBinLowEdge(i+1);
00241   }
00242   for(i = 0; i < nnewz + 1; i++) {
00243     if(rebinz) zb[i] = z[i];
00244     else zb[i] = h->GetZaxis()->GetBinLowEdge(i+1);
00245   }
00246   
00247   if(nnewx > noldx) {
00248     cout<<"Error in MultiBinAnaHelper::Rebin3DHist(): number of new x bins is greater than the number of original x bins!  Quitting..."<<endl;
00249     return;
00250   }
00251   if(nnewy > noldy) {
00252     cout<<"Error in MultiBinAnaHelper::Rebin3DHist(): number of new y bins is greater than the number of original y bins!  Quitting..."<<endl;
00253     return;
00254   }
00255   if(nnewz > noldz) {
00256     cout<<"Error in MultiBinAnaHelper::Rebin3DHist(): number of new z bins is greater than the number of original z bins!  Quitting..."<<endl;
00257     return;
00258   }
00259   
00260   string origname = h->GetName();
00261   bool issumw2 = h->GetSumw2N();
00262 
00263   string name;
00264 
00265   TH3D *old = (TH3D*)h->Clone();
00266   name = origname + "_old";
00267   old->SetNameTitle(name.c_str(),"");
00268 
00269   int ik;
00270   double jx,jy,jz;
00271   double ds;
00272   double bincont,binerr;
00273 
00274   delete h;
00275   h = new TH3D(origname.c_str(),"",nnewx,xb,nnewy,yb,nnewz,zb);
00276   if (issumw2) h->Sumw2();
00277 
00278   for (iz = 1; iz <= noldz; iz++) {
00279     for (iy = 1; iy <= noldy; iy++) {
00280       for (ix = 1; ix <= noldx; ix++) {
00281 
00282         jx = old->GetXaxis()->GetBinCenter(ix);
00283         jy = old->GetYaxis()->GetBinCenter(iy);
00284         jz = old->GetZaxis()->GetBinCenter(iz);
00285         bincont = old->GetBinContent(ix,iy,iz);
00286         binerr = old->GetBinError(ix,iy,iz);
00287 
00288         ik = h->FindBin(jx,jy,jz);
00289         ds = h->GetBinError(ik);
00290         ds *= ds;
00291         ds += binerr*binerr;
00292         ds = TMath::Sqrt(ds);
00293         h->AddBinContent(ik,bincont);
00294         h->SetBinError(ik,ds);
00295 
00296       }
00297     }
00298   }
00299  
00300   //  cout << "MBH says integral was " << old->Integral() << ", now is " << h->Integral() << endl;
00301   delete old;
00302   
00303   delete [] oldxb;
00304   delete [] oldyb;
00305   delete [] oldzb;
00306   delete [] xb;
00307   delete [] yb;
00308   delete [] zb;
00309   
00310   return;
00311 }


The documentation for this class was generated from the following files:

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1