Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NCExtrapolationNS.cxx

Go to the documentation of this file.
00001 
00002 //$Id: NCExtrapolationNS.cxx,v 1.40 2008/01/11 15:53:31 bckhouse Exp $
00003 //
00004 //NCExtrapolationNS.cxx
00005 //
00006 //class to hold pdfs for doing nccc separation
00007 //
00008 //B. Rebel 2/2007
00010 
00011 #include "NCUtils/Extrapolation/NCExtrapolationNS.h"
00012 #include "NCUtils/Extrapolation/NCEnergyBin.h"
00013 #include "NCUtils/Cuts/NCAnalysisCutsNC.h"
00014 #include "MessageService/MsgService.h"
00015 #include "AnalysisNtuples/ANtpAnalysisInfo.h"
00016 #include "AnalysisNtuples/ANtpBeamInfo.h"
00017 #include "AnalysisNtuples/ANtpRecoInfo.h"
00018 #include "AnalysisNtuples/ANtpHeaderInfo.h"
00019 #include "AnalysisNtuples/ANtpTruthInfoBeam.h"
00020 
00021 #include "TH2.h"
00022 #include "TSystem.h"
00023 #include "TDirectory.h"
00024 #include "TMath.h"
00025 #include <cassert>
00026 
00027 using namespace NCType;
00028 
00029 // These three values correspond to shower_shift loops//
00030 const int     n1     = 21;
00031 const int     n0     = 0;
00032 const double  les    = 0.80;
00033 const double  hes    = 1.20;
00034 const int     n1d    = 20;
00035 const double size_e=(hes-les)/n1d;
00036 // These three values correspond to nc shift loops//
00037 const int     nc1    = 21;
00038 const int     nc0    = 0;
00039 const double  lnc    = 0.20;
00040 const double  hnc    = 1.80;
00041 const int     ncd    = 20;
00042 const double size_nc=(hnc-lnc)/ncd;
00043 // These three values correspond to normalization shift loops//
00044 const int     nr1    = 11;//21;
00045 const int     nr0    = 10;//0;
00046 const double  lnr    = 0.95;
00047 const double  hnr    = 1.05;
00048 const int     nrd    = 20;
00049 const double size_nr=(hnr-lnr)/nrd;
00050 
00051 // These are the sigmas//
00052 const double signorm = NCType::kParameterSigmas[NCType::kNormalization];
00053 const double siges   = NCType::kParameterSigmas[NCType::kShowerEnergy];
00054 const double signc   = NCType::kParameterSigmas[NCType::kNCBackground];
00055 
00056 //far and near detector masses
00057 const double kmass_f    = 52.97*486*0.0254*7.874+52.97*484*0.011*1.19879;
00058 const double kmass_n_cc = 3.141592654*1.*1.*(1.710210*7.874+0.746800*1.20317); //reference to cc analysis
00059 const double kmass_n    = 2.4930144*(1.710210*7.874+0.746800*1.20317)*(4.7368-1.728)/4.; //for nc analysis
00060 const double kscalef    = 1.00;
00061 
00062 CVSID("$Id: NCExtrapolationNS.cxx,v 1.40 2008/01/11 15:53:31 bckhouse Exp $");
00063 
00064 //......................................................................
00065 NCExtrapolationNS::NCExtrapolationNS() :
00066   NCExtrapolation::NCExtrapolation()
00067 {
00068 }
00069 
00070 //.......................................................................
00071 NCExtrapolationNS::NCExtrapolationNS(NCAnalysisUtils *utils,
00072                                      std::map<Int_t,double>& dataNear,
00073                                      std::map<Int_t,double>& mcNear,
00074                                      std::map<Int_t,double>& dataFar,
00075                                      std::map<Int_t,double>& mcFar,
00076                                      bool useCC,
00077                                      bool useNC) :
00078   NCExtrapolation(utils, dataNear, mcNear, dataFar, mcFar, useCC, useNC)
00079 {
00080 
00081   fExtrapolationType = NCType::kExtrapolationNS;
00082 
00083   //set up the chi^2 histograms
00084   fchisqall   = new TH2F("chisqall","",
00085                          NCType::kNumUMu3SqrBins,
00086                          NCType::kUMu3SqrStart,
00087                          NCType::kUMu3SqrEnd,
00088                          NCType::kNumDeltaMSqrBins,
00089                          NCType::kDeltaMSqrStart,
00090                          NCType::kDeltaMSqrEnd);
00091   fchisqallnc = new TH2F("chisqallnc","",
00092                          NCType::kNumUS3SqrBins,
00093                          NCType::kUS3SqrStart,
00094                          NCType::kUS3SqrEnd,
00095                          NCType::kNumDeltaMSqrBins,
00096                          NCType::kDeltaMSqrStart,
00097                          NCType::kDeltaMSqrEnd);
00098 
00099 }
00100 
00101 //----------------------------------------------------------------------
00102 NCExtrapolationNS::~NCExtrapolationNS()
00103 {
00104 }
00105 
00106 //----------------------------------------------------------------------
00107 //this method allows the user to pass in appropriate settings for the
00108 //extrapolation such as locations of files, whether to generate the files
00109 //again, etc.  the registry comes from the job module GetConfig method
00110 void NCExtrapolationNS::Prepare(const Registry& r)
00111 {
00112   MSG("NCExtrapolationNS", Msg::kWarning) << "NCExtrapolationNS::Prepare(const Registry& r)" << endl;
00113   int         tmpb;  // a temp bool. See comment under DefaultConfig...
00114   int         tmpi;  // a temp int.
00115   double      tmpd;  // a temp double.
00116   const char* tmps;  // a temp string
00117 
00118   NCExtrapolation::Prepare(r);
00119 
00120   if(r.Get("Decay",                    tmpi)) fDecay                    = tmpi;
00121   if(r.Get("DoMC",                     tmpi)) fDoMC                     = tmpi;
00122   if(r.Get("BeamFileName",             tmps)) fBeamFileName             = tmps;
00123   if(r.Get("BeamFileNamePostShutDown", tmps)) fBeamFileNamePostShutDown = tmps;
00124   if(r.Get("TrueCorrectionHistNear",   tmps)) fTrueCorrectionHistNear   = tmps;
00125   if(r.Get("TrueCorrectionHistFar",    tmps)) fTrueCorrectionHistFar    = tmps;
00126   if(r.Get("FarTrueCorrectionFile",    tmps)) fFarTrueCorrectionFile    = tmps;
00127   if(r.Get("NearTrueCorrectionFile",   tmps)) fNearTrueCorrectionFile   = tmps;
00128 
00129   TString topDir="NCUtils/data";
00130   TString base="";
00131   base=getenv("SRT_PRIVATE_CONTEXT");
00132   if(base!="" && base!="."){
00133     // check if directory exists in SRT_PRIVATE_CONTEXT
00134     TString path = base + "/" + topDir;
00135     void *dir_ptr = gSystem->OpenDirectory(path);
00136     if(!dir_ptr) base=getenv("SRT_PUBLIC_CONTEXT");
00137   }
00138   // if it doesn't exist then use SRT_PUBLIC_CONTEXT
00139   else base=getenv("SRT_PUBLIC_CONTEXT");
00140 
00141   if(base==""){
00142     MSG("NCExtrapolationNS",Msg::kFatal) << "NCExtrapolationNS: No SRT_PUBLIC_CONTEXT set"
00143                                          << endl;
00144     assert(false);
00145   }
00146   topDir += "/";
00147   topDir.Prepend(base+"/");
00148   MSG("NCExtrapoationNS", Msg::kInfo)
00149     << "NCExtrapoationNS reading files from: " << topDir << endl;
00150 
00151   fBeamFileName.Prepend(topDir);
00152   fBeamFileNamePostShutDown.Prepend(topDir);
00153   fFarTrueCorrectionFile.Prepend(topDir);
00154   fNearTrueCorrectionFile.Prepend(topDir);
00155 
00156   MakeHistograms();
00157 
00158   tmpb = 0;
00159   tmpi = 0;
00160   tmpd = 0.;
00161   tmps = "";
00162 
00163   return;
00164 }
00165 
00166 //----------------------------------------------------------------------
00167 void NCExtrapolationNS::MakeHistograms()
00168 {
00169   fbinvect[0]=0.;
00170   fbinvect[1]=0.5;
00171   for(int i=2;i<200;i++){
00172     fbinvect[i]=0.25+(i)*0.25;
00173     if(i==199) fbinvect[i]=200.;
00174   }
00175 
00176   //save the current directory before opening some files
00177   TDirectory *savedir = gDirectory;
00178 
00179   TFile beamfile(fBeamFileName,"READ");
00180   fbmat_norm_r2 = dynamic_cast<TH2D *>(beamfile.Get("bmat_norm_r2"));
00181   fbmat_norm_r2->SetDirectory(savedir);
00182   TFile beamfile_post(fBeamFileNamePostShutDown,"READ");
00183   fbmat_norm_r2_post = dynamic_cast<TH2D *>(beamfile.Get("bmat_norm_r2"));
00184   fbmat_norm_r2_post->SetDirectory(savedir);
00185 
00186   savedir->cd();
00187 
00188   //these histograms will be made by the method now
00189   fSingleMCNearNuCC = new TH1D("singleMCNearNuCC", "", fnbins, fbinvect);
00190   fSingleMCFarNuCC = new TH1D("singleMCFarNuCC", "", fnbins, fbinvect);
00191   fSingleMCNearAllCC = new TH1D("singleMCNearAllCC", "", fnbins, fbinvect);
00192   fSingleMCFarAllCC = new TH1D("singleMCFarAllCC", "", fnbins, fbinvect);
00193   fMCNearNuCC = new TH1D("mcNearNuCC", "", fnbins, fbinvect);
00194   fMCFarNuCC = new TH1D("mcFarNuCC", "", fnbins, fbinvect);
00195   fMCNearAllCC = new TH1D("mcNearAllCC", "", fnbins, fbinvect);
00196   fMCFarAllCC = new TH1D("mcFarAllCC", "", fnbins, fbinvect);
00197   //LLH deprecating fcorf = new TH1D(fTrueCorrectionHistFar, "", fnbins, fbinvect); //for cc only - LLH
00198   //LLH deprecating fcor = new TH1D(fTrueCorrectionHistNear, "", fnbins, fbinvect); //for cc only - LLH
00199   fcorall = new TH1D("fcorall","",fnbins,fbinvect);  //for cc and nc? - LLH
00200   fcorfall = new TH1D("fcorfall","",fnbins,fbinvect);  //for cc and nc? - LLH
00201 
00202 //   TFile corrf(fFarTrueCorrectionFile,"READ");
00203 //   fcorf = dynamic_cast<TH1D *>(corrf.Get(fTrueCorrectionHistFar));
00204 //   fcorf->SetDirectory(savedir);
00205 //   TFile corr(fNearTrueCorrectionFile,"READ");
00206 //   fcor = dynamic_cast<TH1D *>(corr.Get(fTrueCorrectionHistNear));
00207 //   fcor->SetDirectory(savedir);
00208 
00209   fTrueND = new TH1F("trueND", "", fnbins, fbinvect);
00210   fTrueNDAfterRT = new TH1F("trueNDAfterRT", "", fnbins, fbinvect);
00211   fTrueNDAfterRT_noeffcorr = new TH1F("trueNDAfterRT_noeffcorr", "", fnbins, fbinvect); //LLH adding
00212   fRecoND = new TH1F("recoND", "", fnbins, fbinvect);
00213   fTrueFD = new TH1F("trueFD", "", fnbins, fbinvect);
00214   fTrueFD_nc = new TH1F("trueFD_nc", "", fnbins, fbinvect);
00215   fRecoFD = new TH1F("recoFD", "", fnbins, fbinvect);
00216   fRecoFD_nc = new TH1F("recoFD_nc", "", fnbins, fbinvect);
00217   fTrueFDAfterBM = new TH1F("trueFDAfterBM", "", fnbins, fbinvect);
00218   fTrueFDAfterBM_noacccorr = new TH1F("trueFDAfterBM_noacccorr", "", fnbins, fbinvect); //LLH adding
00219   fTrueFDAfterBM_posteffcorr = new TH1F("trueFDAfterBM_posteffcorr", "", fnbins, fbinvect); //LLH adding
00220   fTrueFDAfterOsc = new TH1F("trueFDAfterOsc", "", fnbins, fbinvect); //LLH adding
00221   fRecoFDAfterBM = new TH1F("recoFDAfterBM", "", fnbins, fbinvect);
00222 //  fdatsel = new TH1F("datselstore", "", fnbins, fbinvect); //LLH adding, store before POT scaling
00223 
00224   //used in efficiency calculation in ND
00225   fallccmc = new TH1F("allccmc","",fnbins,fbinvect);
00226   fallccmcf = new TH1F("allccmcf","",fnbins,fbinvect); //LLH adding for proper eff calc
00227   fallncmcf = new TH1F("allncmcf","",fnbins,fbinvect); //LLH adding for proper eff calc
00228 
00229   // FAR CC AND NC TRUE-RECO
00230 //not used-LLH ftruerecomat = new TH2F("truerecomat", "", fnbins, fbinvect, fnbins, fbinvect);
00231 //not used-LLH ftruerecomatnc = new TH2F("truerecomatnc", "", fnbins, fbinvect, fnbins, fbinvect);
00232 //not used-LLH ftruerecomatb = new TH2F("truerecomatb",  "", fnbins, fbinvect, fnbins, fbinvect);
00233 //not used-LLH ftruerecomatncb = new TH2F("truerecomatncb","", fnbins, fbinvect, fnbins, fbinvect);
00234 
00235   // FAR CC AND NC SELECTION EFFICIENCIES (SIGNAL AND BACKGROUND)
00236   feffic = new TH1F("effic",   "",fnbins, fbinvect);
00237   fefficnc = new TH1F("efficnc", "",fnbins, fbinvect);
00238   fefficbg = new TH1F("efficb",  "",fnbins, fbinvect);
00239   fefficncbg = new TH1F("efficncb","",fnbins, fbinvect);
00240   fratccnc = new TH1F("ratccnc", "",fnbins, fbinvect);
00241   feffic_numcheck = new TH1F("effic_numcheck",   "",fnbins, fbinvect); //LLH adding to check eff calc
00242 
00243   // TAUS
00244   ftruerecomatt = new TH2F("truerecomatt", "", fnbins, fbinvect, fnbins, fbinvect);
00245   ftruerecomatbt = new TH2F("truerecomatbt", "", fnbins, fbinvect, fnbins, fbinvect);
00246   feffict = new TH1F("effict", "", fnbins, fbinvect);
00247   fefficbgt = new TH1F("efficbt", "", fnbins, fbinvect);
00248   frattau = new TH1F("rattau", "", fnbins, fbinvect);
00249 
00250   // NEAR
00251   frecotruemat            = new TH2F("recotruemat","",fnbins,fbinvect,fnbins,fbinvect);
00252   fpurityn                = new TH1F("purityn","",fnbins,fbinvect);
00253   fefficiencyn            = new TH1F("efficiencyn","",fnbins,fbinvect);
00254 
00255   // FAR CC AND NC TRUE-RECO  POST
00256 //not used-LLH  ftruerecomat_post            = new TH2F("truerecomat_post",   "",fnbins,fbinvect,fnbins,fbinvect);
00257 //not used-LLH  ftruerecomatnc_post          = new TH2F("truerecomatnc_post", "",fnbins,fbinvect,fnbins,fbinvect);
00258 //not used-LLH  ftruerecomatb_post           = new TH2F("truerecomatb_post",  "",fnbins,fbinvect,fnbins,fbinvect);
00259 //not used-LLH  ftruerecomatncb_post         = new TH2F("truerecomatncb_post","",fnbins,fbinvect,fnbins,fbinvect);
00260 
00261   // FAR CC AND NC SELECTION EFFICIENCIES (SIGNAL AND BACKGROUND)
00262   feffic_post                  = new TH1F("effic_post",   "",fnbins,fbinvect);
00263   fefficnc_post                = new TH1F("efficnc_post", "",fnbins,fbinvect);
00264   fefficbg_post                 = new TH1F("efficb_post",  "",fnbins,fbinvect);
00265   fefficncbg_post               = new TH1F("efficncb_post","",fnbins,fbinvect);
00266   fratccnc_post                = new TH1F("ratccnc_post", "",fnbins,fbinvect);
00267   // TAUS
00268   ftruerecomatt_post           = new TH2F("truerecomatt_post",   "",fnbins,fbinvect,fnbins,fbinvect);
00269   ftruerecomatbt_post          = new TH2F("truerecomatbt_post",  "",fnbins,fbinvect,fnbins,fbinvect);
00270   feffict_post                 = new TH1F("effict_post",   "",fnbins,fbinvect);
00271   fefficbgt_post                = new TH1F("efficbt_post",  "",fnbins,fbinvect);
00272   frattau_post                 = new TH1F("rattau_post","",fnbins,fbinvect);
00273 
00274   // Near
00275   frecotruemat_post            = new TH2F("recotruemat_post","",fnbins,fbinvect,fnbins,fbinvect);
00276   fpurityn_post                = new TH1F("purityn_post","",fnbins,fbinvect);
00277   fefficiencyn_post            = new TH1F("efficiencyn_post","",fnbins,fbinvect);
00278 
00279   //PREDICTED SPECTRUM CC - bestfit sys unosc
00280   //fnoosctruereal           = new TH1F("noosctruereal","",fnbins,fbinvect); //not being used - LLH
00281   fnoosctrue               = new TH1F("noosctrue","",fnbins,fbinvect); //unosc prediction cc, trueE
00282   fnooscreco               = new TH1F("nooscreco","",fnbins,fbinvect); //unosc prediction cc, recoE
00283   fnooscreconc             = new TH1F("nooscreconc","",fnbins,fbinvect); //unosc prediction nc bg, recoE
00284   fnooscrecotau            = new TH1F("nooscrecotau","",fnbins,fbinvect);
00285 
00286   //PREDICTED SPECTRUM NC - bestfit sys unosc
00287   fnoosctrue_ncfit             = new TH1F("noosctrue_ncfit","",fnbins,fbinvect); //unosc prediction nc, trueE, no sel eff!!!
00288   fnooscreco_ncfit            = new TH1F("nooscreco_ncfit","",fnbins,fbinvect); //unosc preditcion nc, recoE
00289   fnooscrecocc_ncfit             = new TH1F("nooscrecocc_ncfit","",fnbins,fbinvect); //onosc precition cc bg, recoE
00290   fnooscrecotau_ncfit          = new TH1F("nooscrecotau_ncfit","",fnbins,fbinvect);
00291 
00292   //PREDICTED SPECTRUM CC POST
00293   //fnoosctruereal_post          = new TH1F("noosctruereal_post","",fnbins,fbinvect);  //not being used - LLH
00294   fnoosctrue_post              = new TH1F("noosctrue_post","",fnbins,fbinvect);
00295   fnooscreco_post              = new TH1F("nooscreco_post","",fnbins,fbinvect);
00296   fnooscreconc_post            = new TH1F("nooscreconc_post","",fnbins,fbinvect);
00297   fnooscrecotau_post           = new TH1F("nooscrecotau_post","",fnbins,fbinvect);
00298 
00299   //PREDICTED SPECTRUM NC post
00300   fnoosctrue_ncfit_post            = new TH1F("noosctrue_ncfit_post","",fnbins,fbinvect);
00301   fnooscreco_ncfit_post           = new TH1F("nooscreco_ncfit_post","",fnbins,fbinvect);
00302   fnooscrecocc_ncfit_post            = new TH1F("nooscrecocc_ncfit_post","",fnbins,fbinvect);
00303   fnooscrecotau_ncfit_post         = new TH1F("nooscrecotau_ncfit_post","",fnbins,fbinvect);
00304 
00305   // auxilary spectra
00306   fnooscar              = new TH1F("nooscar","",fnbins,fbinvect);
00307   fnooscar_ccselcc      = new TH1F("nooscar_ccselcc","",fnbins,fbinvect);
00308   fnooscar_ccselnc         = new TH1F("nooscar_ccselnc","",fnbins,fbinvect);
00309   fnooscar_ncselnc      = new TH1F("nooscar_ncselnc","",fnbins,fbinvect);
00310   fnooscar_ncselcc         = new TH1F("nooscar_ncselcc","",fnbins,fbinvect);
00311   //Taus
00312   fnooscar_ccselcct        = new TH1F("nooscar_ccselcct","",fnbins,fbinvect);
00313   fnooscar_ccselnct     = new TH1F("nooscar_ccselnct","",fnbins,fbinvect);
00314 
00315   //POST
00316   fnooscar_post                = new TH1F("nooscar_post","",fnbins,fbinvect);
00317   fnooscar_ccselcc_post         = new TH1F("nooscar_ccselcc_post","",fnbins,fbinvect);
00318   fnooscar_ccselnc_post         = new TH1F("nooscar_ccselnc_post","",fnbins,fbinvect);
00319   fnooscar_ncselnc_post         = new TH1F("nooscar_ncselnc_post","",fnbins,fbinvect);
00320   fnooscar_ncselcc_post         = new TH1F("nooscar_ncselcc_post","",fnbins,fbinvect);
00321   //Taus
00322   fnooscar_ccselcct_post       = new TH1F("nooscar_ccselcct_post","",fnbins,fbinvect);
00323   fnooscar_ccselnct_post       = new TH1F("nooscar_ccselnct_post","",fnbins,fbinvect);
00324 
00325   // BEST FIT TRUE AND RECO FROM FIT  CC
00326 
00327   fbestfitreco            = new TH1F("bestfitreco","",fnbins,fbinvect);
00328   fbestfitreconc          = new TH1F("bestfitreconc","",fnbins,fbinvect);
00329   fbestfitrecotau         = new TH1F("bestfitrecotau","",fnbins,fbinvect);
00330 
00331   // BEST FIT TRUE AND RECO FROM FIT NC
00332 
00333   fbestfitreco_ncfit         = new TH1F("bestfitreco_ncfit","",fnbins,fbinvect);
00334   fbestfitrecocc_ncfit          = new TH1F("bestfitrecocc_ncfit","",fnbins,fbinvect);
00335   fbestfitrecotau_ncfit      = new TH1F("bestfitrecotau_ncfit","",fnbins,fbinvect);
00336 
00337   // BEST FIT TRUE AND RECO FROM FIT CC
00338 
00339   fbestfitreco_post         = new TH1F("bestfitreco_post","",fnbins,fbinvect);
00340   fbestfitreconc_post       = new TH1F("bestfitreconc_post","",fnbins,fbinvect);
00341   fbestfitrecotau_post      = new TH1F("bestfitrecotau_post","",fnbins,fbinvect);
00342 
00343   // BEST FIT TRUE AND RECO FROM FIT NC
00344 
00345   fbestfitreco_ncfit_post      = new TH1F("bestfitreco_ncfit_post","",fnbins,fbinvect);
00346   fbestfitrecocc_ncfit_post       = new TH1F("bestfitrecocc_ncfit_post","",fnbins,fbinvect);
00347   fbestfitrecotau_ncfit_post   = new TH1F("bestfitrecotau_ncfit_post","",fnbins,fbinvect);
00348 
00349   // HELP ON FITTING CC
00350   ftemptrue               = new TH1F("temptrue","",fnbins,fbinvect);
00351   ftempreco               = new TH1F("tempreco","",fnbins,fbinvect);
00352   ftempreconc             = new TH1F("tempreconc","",fnbins,fbinvect);
00353   ftemprecotau            = new TH1F("temprecotau","",fnbins,fbinvect);
00354 
00355   // HELP ON FITTING NC
00356   ftemptrue_ncfit            = new TH1F("temptrue_ncfit","",fnbins,fbinvect);
00357   ftempreco_ncfit            = new TH1F("tempreco_ncfit","",fnbins,fbinvect);
00358   ftemprecocc_ncfit             = new TH1F("temprecocc_ncfit","",fnbins,fbinvect);
00359   ftemprecotau_ncfit         = new TH1F("temprecotau_ncfit","",fnbins,fbinvect);
00360 
00361   // HELP ON FITTING CC
00362   ftemptrue_post            = new TH1F("temptrue_post","",fnbins,fbinvect);
00363   ftempreco_post            = new TH1F("tempreco_post","",fnbins,fbinvect);
00364   ftempreconc_post          = new TH1F("tempreconc_post","",fnbins,fbinvect);
00365   ftemprecotau_post         = new TH1F("temprecotau_post","",fnbins,fbinvect);
00366 
00367   // HELP ON FITTING NC
00368   ftemptrue_ncfit_post         = new TH1F("temptrue_ncfit_post","",fnbins,fbinvect);
00369   ftempreco_ncfit_post         = new TH1F("tempreco_ncfit_post","",fnbins,fbinvect);
00370   ftemprecocc_ncfit_post          = new TH1F("temprecocc_ncfit_post","",fnbins,fbinvect);
00371   ftemprecotau_ncfit_post      = new TH1F("temprecotau_ncfit_post","",fnbins,fbinvect);
00372 
00373   int nbins1 = 15;
00374   if(fDoMC) nbins1 = 39;
00375 
00376   float binvect1[40];
00377   binvect1[0]=0;
00378   binvect1[1]=1.;
00379   for (int i=2; i < nbins1+1; i++){
00380     if(fDoMC > 0 && i <= 19)  binvect1[i]=0.5+(i)*0.5;
00381     if(fDoMC > 0 && i > 19)   binvect1[i]=10.+(i-19)*1.;
00382     if(fDoMC < 1 && i <= 10)  binvect1[i]=(i)*1.;
00383     if(fDoMC < 1 && i > 10)   binvect1[i]=10.+(i-10)*2.;
00384   }
00385 
00386   fbestfitrecotau_rebinned        = new TH1F("bestfitrecotau_rebinned","",
00387                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00388   fbestfitreco_rebinned           = new TH1F("bestfitreco_rebinned","",
00389                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00390   fbestfitreconc_rebinned         = new TH1F("bestfitreconc_rebinned","",
00391                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00392   foscreco_rebinned                  = new TH1F("oscreco_rebinned","",
00393                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00394   ftempreco_rebinned              = new TH1F("tempreco_rebinned","",
00395                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00396   ftempreconc_rebinned            = new TH1F("tempreconc_rebinned","",
00397                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00398   fnooscreco_rebinned             = new TH1F("nooscreco_rebinned","",
00399                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00400   fnooscreconc_rebinned           = new TH1F("nooscreconc_rebinned","",
00401                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00402   fnooscrecotau_rebinned          = new TH1F("nooscrecotau_rebinned","",
00403                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00404 
00405   fbestfitrecotau_rebinned_post       = new TH1F("bestfitrecotau_rebinned_post","",
00406                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00407   fbestfitreco_rebinned_post          = new TH1F("bestfitreco_rebinned_post","",
00408                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00409   fbestfitreconc_rebinned_post        = new TH1F("bestfitreconc_rebinned_post","",
00410                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00411   foscreco_rebinned_post                     = new TH1F("oscreco_rebinned_post","",
00412                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00413   ftempreco_rebinned_post             = new TH1F("tempreco_rebinned_post","",
00414                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00415   ftempreconc_rebinned_post           = new TH1F("tempreconc_rebinned_post","",
00416                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00417   fnooscreco_rebinned_post            = new TH1F("nooscreco_rebinned_post","",
00418                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00419   fnooscreconc_rebinned_post          = new TH1F("nooscreconc_rebinned_post","",
00420                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00421   fnooscrecotau_rebinned_post         = new TH1F("nooscrecotau_rebinned_post","",
00422                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00423 
00424   //NC
00425   fbestfitrecotau_ncfit_rebinned     = new TH1F("bestfitrecotau_ncfit_rebinned","",
00426                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00427   fbestfitreco_ncfit_rebinned        = new TH1F("bestfitreco_ncfit_rebinned","",
00428                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00429   fbestfitrecocc_ncfit_rebinned         = new TH1F("bestfitrecocc_ncfit_rebinned","",
00430                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00431   foscreco_ncfit_rebinned            = new TH1F("oscreco_ncfit_rebinned","",
00432                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00433   ftempreco_ncfit_rebinned           = new TH1F("tempreco_ncfit_rebinned","",
00434                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00435   ftemprecocc_ncfit_rebinned            = new TH1F("temprecocc_ncfit_rebinned","",
00436                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00437   fnooscreco_ncfit_rebinned          = new TH1F("nooscreco_ncfit_rebinned","",
00438                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00439   fnooscrecocc_ncfit_rebinned           = new TH1F("nooscrecocc_ncfit_rebinned","",
00440                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00441   fnooscrecotau_ncfit_rebinned        = new TH1F("nooscrecotau_ncfit_rebinned","",
00442                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00443 
00444 
00445   fbestfitrecotau_ncfit_rebinned_post    = new TH1F("bestfitrecotau_ncfit_rebinned_post","",
00446                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00447   fbestfitreco_ncfit_rebinned_post       = new TH1F("bestfitreco_ncfit_rebinned_post","",
00448                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00449   fbestfitrecocc_ncfit_rebinned_post        = new TH1F("bestfitrecovc_rebinned_post","",
00450                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00451   foscreco_ncfit_rebinned_post       = new TH1F("oscreco_ncfit_rebinned_post","",
00452                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00453   ftempreco_ncfit_rebinned_post          = new TH1F("tempreco_ncfit_rebinned_post","",
00454                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00455   ftemprecocc_ncfit_rebinned_post           = new TH1F("temprecocc_ncfit_rebinned_post","",
00456                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00457   fnooscreco_ncfit_rebinned_post         = new TH1F("nooscreco_ncfit_rebinned_post","",
00458                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00459   fnooscrecocc_ncfit_rebinned_post          = new TH1F("nooscrecocc_ncfit_rebinned_post","",
00460                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00461   fnooscrecotau_ncfit_rebinned_post       = new TH1F("nooscrecotau_ncfit_rebinned_post","",
00462                                         NCType::kNumEnergyBinsFar, NCType::kEnergyBinsFar);
00463 
00464   return;
00465 }
00466 
00467 //----------------------------------------------------------------------
00468 //add events to the histograms used for the weighting
00469 void NCExtrapolationNS::AddEvent(ANtpHeaderInfo    *headerInfo,
00470                                  ANtpBeamInfo      *beamInfo,
00471                                  ANtpRecoInfo      *recoInfo,
00472                                  ANtpAnalysisInfo  *analysisInfo,
00473                                  ANtpTruthInfoBeam *truthInfo,
00474                                  int runType,
00475                                  bool useMCAsData,
00476                                  int fileType)
00477 {
00478 
00479   //++++Energies are set to the correct NC/CC values in the NCExtrapolationModule
00480   //before being passed into the extrapolations
00481 
00482   //only use the L010z185i beam
00483   if(beamInfo->beamType != BeamType::ToZarko(BeamType::kL010z185i))
00484     return;
00485 
00486   if(headerInfo->detector == (int)Detector::kNear)
00487     AddNearEvent(headerInfo, beamInfo, recoInfo, analysisInfo,
00488                  truthInfo, runType, useMCAsData, fileType);
00489 
00490   if(headerInfo->detector == (int)Detector::kFar)
00491     AddFarEvent(headerInfo, beamInfo, recoInfo, analysisInfo,
00492                 truthInfo, runType, useMCAsData, fileType);
00493 
00494   return;
00495 }
00496 
00497 //----------------------------------------------------------------------
00498 void NCExtrapolationNS::AddNearEvent(ANtpHeaderInfo *headerInfo,
00499                                      ANtpBeamInfo *beamInfo,
00500                                      ANtpRecoInfo *recoInfo,
00501                                      ANtpAnalysisInfo *analysisInfo,
00502                                      ANtpTruthInfoBeam *truthInfo,
00503                                      int runType,
00504                                      bool useMCAsData,
00505                                      int fileType)
00506 {
00507 
00508   //fill histogram for all MC events with a true vertex in the fiducial volume
00509 
00510   //Note: if this code is run on stripped uDST files, it will miss events that were reco'd
00511   //reco'd outside the FV!  Make sure to run on unstripped files or find a way to apply
00512   //a correction... This should be a small effect - LLH
00513   if(headerInfo->dataType == (int)SimFlag::kMC && !useMCAsData){
00514     NCAnalysisCutsNC cuts;
00515     cuts.SetInfoObjects(headerInfo, beamInfo, 0, 0, 0, truthInfo);
00516     if( cuts.InFiducialVolumeTrue() && headerInfo->events != 0
00517         && truthInfo->interactionType == 1
00518         && TMath::Abs(truthInfo->nuFlavor) == 14){
00519 
00520       if(truthInfo->nuFlavor > 0) fallccmc->Fill(truthInfo->nuEnergy, recoInfo->weight);
00521 
00522       //check if this is a new snarl
00523 //       if(headerInfo->newSnarl > 0) AddSingleMCEventsToHistogram(Detector::kNear);
00524 
00526       //Following lines were for fcorf calculation, since we can't do
00527       //this from uDST files, comment out for now - LLH
00529 
00530 //       fTrueNuEnergy.push_back(truthInfo->nuEnergy);
00531 //       fTrueNuFlavor.push_back(truthInfo->nuFlavor);
00532 //       fTrueNuComplete.push_back(truthInfo->eventCompleteness);
00533     }//end if true vertex is in fiducial volume
00534 
00535   }//end if mc
00536 
00537   //Here apply FV and cleaning cuts!
00538   NCExtrapolation::AddEvent(headerInfo, beamInfo, recoInfo, analysisInfo,
00539                             truthInfo, runType, useMCAsData, fileType);
00540 
00541   //dont bother with events outside the fiducial volume
00542 //   if(recoInfo->inFiducialVolume < 1) return;
00543 
00544 //   if(headerInfo->dataType == (int)SimFlag::kMC && !useMCAsData){
00545 
00546 //     feshnd.push_back(recoInfo->showerEnergyCC);
00547 //     femund.push_back(recoInfo->muEnergy);
00548 //     fenutnd.push_back(truthInfo->nuEnergy);
00549 //     fccnd.push_back(truthInfo->interactionType);
00550 //     if(analysisInfo->isNC > 0)
00551 //       fpidpnd.push_back(NCType::kNC);
00552 //     else if(analysisInfo->isCC > 0)
00553 //       fpidpnd.push_back(NCType::kCC);
00554 //     fflavnd.push_back(truthInfo->nuFlavor);
00555 //     finisnd.push_back(truthInfo->initialState);
00556 //     fweind.push_back(recoInfo->weight);
00557 
00558 //   }//end if MC
00559 
00560   return;
00561 }
00562 
00563 //----------------------------------------------------------------------
00564 //all events passed to this method are garaunteed to be cc events, have
00565 //their true interaction vertex within the fiducial volume and are either
00566 //numu or numubar
00567 
00568 //Note (9/10/07) This is deprecated for the moment since uDST's don't save
00569 //info on generated MC events that weren't found by reconstruction - LLH
00570 void NCExtrapolationNS::AddSingleMCEventsToHistogram(Detector::Detector_t det)
00571 {
00572   //double loop over mc events in snarl and see if there are any repeats
00573   //for each entry with duplicates, store the number of duplicates found with j>i
00574   //store only instances where number of additional duplicates is 0
00575   //Because we are saving true energies, there's no need to worry about event completeness
00576 
00577   //note that if cleaning cut are applied to all events in the strip file,
00578   //then this method will not find any duplicates! - LLH
00579   vector<int> repeated;
00580 
00581 
00582   for(uint i = 0; i < fTrueNuEnergy.size(); ++i){
00583     repeated.push_back(-1);
00584     uint duplicatesCtr = 0; //for each duplicate found for entry i, increment this by 1
00585 
00586     for(uint j = i+1; j < fTrueNuEnergy.size(); ++j) //only search through j > i
00587     {
00588        if(fTrueNuEnergy[i] == fTrueNuEnergy[j])
00589           duplicatesCtr++;
00590 
00591     }//end inner loop
00592 
00593     repeated[i] = duplicatesCtr; //storing number of duplicates found after entry i
00594 
00595     MSG("NCExtrapolationNS", Msg::kInfo) << "i= " << i  //LLH changed from kDebug, 100
00596                                          << ", repeated[i] = " << repeated[i]
00597                                          << ", duplicatesCtr = " << duplicatesCtr
00598                                          <<", energy = " << fTrueNuEnergy[i]
00599                                          << endl;
00600 
00601   }//end outer loop
00602 
00603   //loop over the array one more time and fill the histogram
00604   //with single events or the event with the largest completeness
00605   //if it is a duplicate
00606   for(uint i = 0; i < fTrueNuEnergy.size(); ++i){
00607 
00608     if(det == Detector::kNear){
00609        if(repeated[i] ==0) fSingleMCNearAllCC->Fill(fTrueNuEnergy[i]);  //all cc-like
00610        fMCNearAllCC->Fill(fTrueNuEnergy[i]);
00611        if(fTrueNuFlavor[i] == 14){
00612           if(repeated[i] == 0) fSingleMCNearNuCC->Fill(fTrueNuEnergy[i]);  //true numu cc-like
00613           fMCNearNuCC->Fill(fTrueNuEnergy[i]);
00614       }//end if numu
00615     }//end if near
00616 
00617     else if(det == Detector::kFar){
00618       if(repeated[i] ==0) fSingleMCFarAllCC->Fill(fTrueNuEnergy[i]);  //all cc-like
00619       fSingleMCFarAllCC->Fill(fTrueNuEnergy[i]);
00620       if(fTrueNuFlavor[i] == 14){
00621         if(repeated[i] == 0) fSingleMCFarNuCC->Fill(fTrueNuEnergy[i]);  //true numu cc-like
00622         fMCFarNuCC->Fill(fTrueNuEnergy[i]);
00623       }//end if numu
00624     }//end if far
00625   }//end loop over events
00626 
00627   //reset the arrays (for next snarl)
00628   fTrueNuEnergy.clear();
00629   fTrueNuFlavor.clear();
00630   fTrueNuComplete.clear();
00631 
00632   return;
00633 }
00634 
00635 //----------------------------------------------------------------------
00636 void NCExtrapolationNS::AddFarEvent(ANtpHeaderInfo *headerInfo,
00637                                     ANtpBeamInfo *beamInfo,
00638                                     ANtpRecoInfo *recoInfo,
00639                                     ANtpAnalysisInfo *analysisInfo,
00640                                     ANtpTruthInfoBeam *truthInfo,
00641                                     int runType,
00642                                     bool useMCAsData,
00643                                     int fileType)
00644 {
00645    MAXMSG("NCExtrapolationNS", Msg::kInfo, 1) << "AddFarEvent " << endl;
00646 
00647   //fill histogram for all MC events with a true vertex in the fiducial volume
00648    if(headerInfo->dataType == (int)SimFlag::kMC && !useMCAsData){
00649 
00650       //don't double count and make sure event passed reco because
00651       //reconstruction efficiency is accounted for in the corf function
00652       //(eventually take this out)
00653       if(headerInfo->newSnarl && headerInfo->events!=0)
00654       {
00655          //Far detector efficiency corrections must be wrt all (numu) events in far detector
00656          if(truthInfo->nuFlavor == 14)
00657          {
00658             if(truthInfo->interactionType == 1) fallccmcf->Fill(truthInfo->nuEnergy, recoInfo->weight);
00659 
00661             //Following lines were for fcorf calculation, since we can't do
00662             //this from uDST files, comment out for now - LLH
00664 
00665             //check if this is a new snarl
00666 //      if(headerInfo->newSnarl > 0) AddSingleMCEventsToHistogram(Detector::kFar);
00667 //       fTrueNuEnergy.push_back(truthInfo->nuEnergy);
00668 //       fTrueNuFlavor.push_back(truthInfo->nuFlavor);
00669 //       fTrueNuComplete.push_back(truthInfo->eventCompleteness);
00670          }//end if true vertex is a numu
00671 
00672          //For NC events store all nu types
00673          if(truthInfo->interactionType == 0) fallncmcf->Fill(truthInfo->nuEnergy, recoInfo->weight);
00674       } //end if nonzero events and is new snarl
00675 
00676   }//end if mc
00677 
00678 
00679   NCExtrapolation::AddEvent(headerInfo, beamInfo, recoInfo, analysisInfo,
00680                             truthInfo, runType, useMCAsData, fileType);
00681 
00682   return;
00683 }
00684 
00685 //----------------------------------------------------------------------
00686 //just have to multiply total MC events with true vertex in the
00687 //fiducial volume by the correction factor for split events
00688 void NCExtrapolationNS::PrepareNearDetector()
00689 {
00690   //get the correction histograms for the single events
00691 //  fcor->Divide(fSingleMCNearNuCC, fMCNearNuCC);
00692 //  fcorall->Divide(fSingleMCNearAllCC, fMCNearAllCC);
00693 
00694    cout <<"In PrepareNearDetector" << endl;
00695 
00696   //For now read in fcor and fcorall from Niki's files, since we don't have the
00697   //capability to do this from uDST's
00698 //  TFile *corr      = new TFile("$MINOS_DATA/d84/niki/mad_cedar_daikon/near/correction_plots/near/norm_fix_near.root","READ");
00699    TFile *corr      = new TFile("/afs/fnal.gov/files/data/minos/d79/llhsu/eventFindingEff/norm_fix_near_uDST_strip.root","READ"); //using my own file - LLH with zero event cut
00700   fcor = (TH1F*)(corr->Get("cor_neu"));
00701 
00702   //Correct the true CC events for reconstruction efficiencies and event splitting
00703   fallccmc->Multiply(fcor);
00704   return;
00705 }
00706 
00707 //----------------------------------------------------------------------
00708 //this is where you call the guts of the code to do the prediction and
00709 //fit
00710 void NCExtrapolationNS::PredictSpectrum()
00711 {
00712 
00713    cout <<"In NCExtrapolationNS::PredictSpectrum!" << endl;
00714 
00715   //make the correction histograms used by the method
00716 //LLH deprecating  fcorf->Divide(fSingleMCFarNuCC, fMCFarNuCC);
00717 //LLH deprecating  fcorfall->Divide(fSingleMCFarAllCC, fMCFarAllCC);
00718   TFile *corrf      = new TFile("$MINOS_DATA/d84/niki/mad_cedar_daikon/near/correction_plots/far/norm_fix_far.root","READ");
00719   fcorf = (TH1F*)(corrf->Get("cor_neuf"));
00720 
00721   //find the position in the beam vector
00722   int runI = NCType::kRunI;
00723   int runII = runI;//NCType::kRunII; Temp! FIXME - LLH
00724   int farBeamI = NCExtrapolation::FindBeamIndex(Detector::kFar, BeamType::kL010z185i, runI);
00725   int farBeamII = NCExtrapolation::FindBeamIndex(Detector::kFar, BeamType::kL010z185i, runII);
00726   int nearBeamI = NCExtrapolation::FindBeamIndex(Detector::kNear, BeamType::kL010z185i, runI);
00727   int nearBeamII = NCExtrapolation::FindBeamIndex(Detector::kNear, BeamType::kL010z185i, runII);
00728   if(farBeamI > 100 || farBeamII > 100
00729      || nearBeamI > 100 || nearBeamII > 100){
00730     MSG("NCExtrapolationNS", Msg::kWarning) << "could not find "
00731                                             << "FD/ND beam in vector - bail"
00732                                             << " far: " << farBeamI << " " << farBeamII
00733                                             << " near: " << nearBeamI << " " << nearBeamII
00734                                             << endl;
00735     return;
00736   }
00737 
00738   //the next 2 lines just make everything look good without doing any fits
00739   //ie just the nominal mc and data
00740   //return;
00741 
00742   //get near and far histograms
00743   //datsel histograms are near data
00744   TH1F *datsel = new TH1F("datsel", "", fnbins, fbinvect);
00745   TH1F *datselpost = new TH1F("datselpost", "", fnbins, fbinvect);
00746   fdatselFD = new TH1F("datselstoreFD", "", fnbins, fbinvect); //LLH added
00747   fdatselFD_nc = new TH1F("datselstoreFD_nc", "", fnbins, fbinvect); //LLH added
00748   //TH1F *datselnc = fBeams[nearBeamI].GetDataHistogram(NCType::kNC);
00749   //TH1F *datselncpost = fBeams[nearBeamII].GetDataHistogram(NCType::kNC);
00750   FillDataHistogramFromEnergyBins(nearBeamI, NCType::kCC, datsel);
00751   FillDataHistogramFromEnergyBins(nearBeamII, NCType::kCC, datselpost);
00752   FillDataHistogramFromEnergyBins(farBeamI, NCType::kCC, fdatselFD); //LLH added
00753   FillDataHistogramFromEnergyBins(farBeamI, NCType::kNC, fdatselFD_nc); //LLH added
00754 
00755   //put far data histograms into Niki's names for the fitting
00756   FillDataHistogramFromEnergyBins(farBeamI, NCType::kCC, foscreco_rebinned);
00757   FillDataHistogramFromEnergyBins(farBeamII, NCType::kCC, foscreco_rebinned_post);
00758   FillDataHistogramFromEnergyBins(farBeamI, NCType::kNC, foscreco_ncfit_rebinned);
00759   FillDataHistogramFromEnergyBins(farBeamII, NCType::kNC, foscreco_ncfit_rebinned_post);
00760 
00761   //this method needs to know the relative scaling between near and far
00762   //exposures.  the matrix extrapolates based on the ND exposure
00763   //scale near detector exposure to be same as far detector
00764   double potNear = fBeams[nearBeamI].GetDataPOT();
00765   double potFar = fBeams[farBeamI].GetDataPOT();
00766 
00767   fNearExposureScale = potFar/potNear;
00768   cout <<"Scaling datsel!!!!!!! scale factor = " << fNearExposureScale
00769        <<"\npotFar = " << potFar
00770        <<"\npotNear = " << potNear
00771        << endl; //LLH
00772   fdatselND = (TH1F*)datsel->Clone("datselstoreND"); //LLH unmodified datsel
00773   datsel->Scale(fNearExposureScale);
00774   datselpost->Scale(fNearExposureScale);
00775 
00776   double scale_factNC = 1.;
00777   double nc_shift  = 1.;
00778   double enu_shift = 1.;
00779 
00780   // fit loop
00781   float dmsqmin     = 0.;
00782   float s2tmin      = 0.;
00783   float s2smin      = 0.;
00784   float chisqmin    = 9999.;
00785   float norm_min    = 9999.;
00786   float enu_shiftmin= 9999.;
00787   float nc_shiftmin = 9999.;
00788   int best_enu      = 9999;
00789   int best_nc       = 9999;
00790 
00791   int ndf = 39 + 39 - 3;
00792   if(fDoMC < 1) ndf=15+15-3;
00793 
00794   vector <vector <vector <double> > > chisqarraymins;
00795   chisqarraymins.resize(NCType::kNumDeltaMSqrBins);
00796   for(int i = 0; i < NCType::kNumDeltaMSqrBins; i++){
00797     chisqarraymins[i].resize(NCType::kNumUS3SqrBins);
00798     for(int j = 0; j < NCType::kNumUS3SqrBins; j++){
00799       chisqarraymins[i][j].resize(NCType::kNumUMu3SqrBins);
00800       for(int k = 0; k < NCType::kNumUMu3SqrBins; k++){
00801         chisqarraymins[i][j][k] = 1.e12;
00802       }
00803     }
00804   }
00805 
00806   float dmsqfit = 0.;
00807   float s2tfit = 0.;
00808   float s2sfit = 0.;
00809 
00810   TString math;
00811 
00812   TString noosc;
00813 
00814   noosctruearray.resize(n1);
00815   noosctruearraypost.resize(n1);
00816 
00817   //initializing arrays
00818   for(int i = 0; i < n1; ++i){
00819     math = TString::Format("truerecomat%d",i);
00820     matarray.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00821     math = TString::Format("truerecomatnc%d",i);
00822     matarraync.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00823     math = TString::Format("truerecomatb%d",i);
00824     matarrayb.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00825     math = TString::Format("truerecomatncb%d",i);
00826     matarrayncb.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00827     //TAUS
00828     math = TString::Format("truerecomatt%d",i);
00829     matarrayt.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00830     math = TString::Format("truerecomatbt%d",i);
00831     matarraybt.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00832 
00833     math = TString::Format("truerecomat_post%d",i);
00834     matarray_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00835     math = TString::Format("truerecomatnc_post%d",i);
00836     matarraync_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00837     math = TString::Format("truerecomatb_post%d",i);
00838     matarrayb_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00839     math = TString::Format("truerecomatncb_post%d",i);
00840     matarrayncb_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00841     //TAUS
00842     math = TString::Format("truerecomatt_post%d",i);
00843     matarrayt_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00844     math = TString::Format("truerecomatbt_post%d",i);
00845     matarraybt_post.push_back(new TH2F(math,"",fnbins,fbinvect,fnbins,fbinvect));
00846 
00847     for(int j = 0; j < nc1; ++j){
00848 
00849       noosc = TString::Format("noosctrue_%d_%d",i,j);
00850       noosctruearray[i].push_back(new TH1F(noosc,"",fnbins,fbinvect));
00851       noosc = TString::Format("noosctrue_post_%d_%d",i,j);
00852       noosctruearraypost[i].push_back(new TH1F(noosc,"",fnbins,fbinvect));
00853     }
00854   }//end initialization of arrays
00855 
00856   //Getting efficiencies (Far only)
00857   GetEff(feffic, fefficnc, fefficbg, fefficncbg, fratccnc, frattau, farBeamI);
00858   GetEff(feffic_post, fefficnc_post, fefficbg_post, fefficncbg_post, fratccnc_post, frattau_post, farBeamII);
00859   if(fDoMC==0){
00860     GetEfft(feffict, fefficbgt, farBeamI);
00861     GetEfft(feffict_post, fefficbgt_post, farBeamII);
00862   }
00863 
00864   double purbin = 0.;
00865   double newbin = 0.;
00866 
00867   // loop for truereco mat efficiency and purity For Far and Near
00868   int start = n1d/2;
00869   int end = start + 1;
00870   int startnc = ncd/2;
00871   int endnc = startnc + 1;
00872 
00873   if(fSystematicsToUse[NCType::kShowerEnergy]){
00874     start   = n0;
00875     end     = n1;
00876   }
00877   if(fSystematicsToUse[NCType::kNCBackground]){
00878     startnc = nc0;
00879     endnc   = nc1;
00880   }
00881 
00882   //use TH1::Copy function to copy the ftruerecomat* matricies into the
00883   //array of matricies matarray*
00884 
00885   for(int ii = start; ii < end; ii++){
00886     // SHOWER LOOP
00887     enu_shift = les+ii*size_e;
00888     MSG("NCExtrapolationNS", Msg::kInfo) << " enu  shift " << enu_shift << endl;
00889     cout <<"First Shower Loop!" << endl;
00890 
00891     RecoTrueEffPur(matarray[ii], matarraync[ii], matarrayb[ii],
00892                    matarrayncb[ii], enu_shift, farBeamI);
00893     // here TAUS
00894     if(fDoMC==0)
00895       RecoTrueEffPurTau(matarrayt[ii], matarraybt[ii], enu_shift, farBeamI);
00896 
00897     RecoTruePur(frecotruemat, fpurityn, fefficiencyn, enu_shift, nearBeamI);
00898 
00899     for(int gg = 0; gg < fnbins; gg++){
00900       purbin = fpurityn->GetBinContent(gg+1);
00901       newbin = purbin/(purbin+scale_factNC*(1-purbin));
00902       fpurityn->SetBinContent(gg+1,newbin);
00903     }
00904 
00905     // post shutdown
00906 //     RecoTrueEffPur(matarray_post[ii], matarraync_post[ii], matarrayb_post[ii],
00907 //                 matarrayncb_post[ii], enu_shift, farBeamII);
00908 
00909 //     // here TAUS
00910 //     if(fDoMC==0)
00911 //       RecoTrueEffPurTau(matarrayt_post[ii], matarraybt_post[ii], enu_shift, farBeamII);
00912 
00913 //     RecoTruePur(frecotruemat_post, f_purityn_post, fefficiencyn_post, enu_shift, nearBeamII);
00914 
00915 //     for(int gg = 0; gg < fnbins; gg++){
00916 //       purbin = fpurityn_post->GetBinContent(gg+1);
00917 //       newbin = purbin/(purbin+scale_factNC*(1-purbin));
00918 //       fpurityn_post->SetBinContent(gg+1,newbin);
00919 //     }
00920 
00921     // NC LOOP
00922     for(int jj = startnc; jj < endnc; jj++){
00923       nc_shift = lnc+jj*size_nc;
00924       MSG("NCExtrapolationNS", Msg::kInfo) << " nc  shift " << nc_shift << endl;
00925       cout <<"NC loop within first shower loop" << endl;
00926 
00927       // MAKE PREDICTION UNOSCILLATED //
00928       for(int gg = 0; gg < fnbins; gg++){
00929         purbin = fpurityn->GetBinContent(gg+1);
00930         newbin = purbin/(purbin+nc_shift*(1-purbin));
00931         fpurityn->SetBinContent(gg+1,newbin);
00932       }
00933 
00934       //Temp - Code speedup for debugging only!
00935       Int_t bypass = 1;
00936       if(bypass == 1)
00937       {
00938          cout <<"Bypass 3!" << endl;
00939          TFile* f = new TFile("/local/scratch04/llhsu/tempFiles/L010z185i/extrapolation_fn_NS_16kND488FD_ncfvcor.root","READ");
00940          TString noosctruename = TString::Format("noosctrue_%d_%d", ii, jj);
00941          TH1F* tempHisto = (TH1F*)f->Get("plotsBeamMatrix/"+noosctruename);
00942          noosctruename += "_clone";
00943          noosctruearray[ii][jj] = (TH1F*)tempHisto->Clone(noosctruename);
00944          noosctruearray[ii][jj]->SetDirectory(0);
00945          //       fdatselND = (TH1F*)((f->Get("datselstoreND"))->Clone("datselstoreND"));
00946 //         fpurityn = (TH1F*)((f->Get("purityn"))->Clone("purityn"));
00947 //         fefficiencyn = (TH1F*)((f->Get("efficiencyn"))->Clone("efficiencyn"));
00948 //         frecotruemat = (TH2F*)((f->Get("recotruemat"))->Clone("recotruemat"));
00949          //f->Close();
00950          cout <<"Done 3!" << endl;
00951       }
00952       else
00953       {
00954 
00955       //take ND "data" and generate predicted true far spectrum
00956       MakePrediction(datsel, fpurityn, fefficiencyn,
00957                      frecotruemat, noosctruearray[ii][jj], runI);
00958 
00959      } //remove else brackets with bypass
00960 
00961       // MAKE PREDICTION UNOSCILLATED //
00962 //       for(int gg = 0; gg < fnbins; gg++){
00963 //      purbin = fpuritynp->GetBinContent(gg+1);
00964 //      newbin = purbin/(purbin+nc_shift*(1-purbin));
00965 //      fpuritynp->SetBinContent(gg+1,newbin);
00966 //       }
00967 
00968 //       MakePrediction(datselpost, fpurityn_post, fefficiencyn_post,
00969 //                   frecotruemat_post, noosctruearraypost[ii][jj], runII);
00970 
00971       // restore purity
00972       for(int gg = 0; gg < fnbins; gg++){
00973         purbin = fpurityn->GetBinContent(gg+1);
00974         newbin = (nc_shift*purbin)/(1-purbin+nc_shift*purbin);
00975         fpurityn->SetBinContent(gg+1,newbin);
00976 
00977 //      purbin = fpuritynp->GetBinContent(gg+1);
00978 //      newbin = (nc_shift*purbin)/(1-purbin+nc_shift*purbin);
00979 //      fpuritynp->SetBinContent(gg+1,newbin);
00980       }
00981     }//end nc loop
00982 
00983 //Temp LLH, for 1 iteration only!    frecotruemat->Reset();
00984 //Temp LLH, for 1 iteration only!    fpurityn->Reset();
00985 //Temp LLH, for 1 iteration only!    fefficiencyn->Reset();
00986 
00987 //     frecotruemat_post->Reset();
00988 //     fpurityn_post->Reset();
00989 //     fefficiencyn_post->Reset();
00990 
00991   } // end shower loop
00992 
00993   MSG("NCExtrapolationNS", Msg::kInfo) << " DONE EFF PUR RECO->TRUE NEAR FAR " << endl;
00994 
00995   float chisqtemp1 = 1.e12;
00996   //  float chisqtemp2 = 1.e12;
00997   float chisqtemp3 = 1.e12;
00998   //  float chisqtemp4 = 1.e12;
00999   float chisqmin_norm = 1.e12;
01000 
01001   float nc_min   = 9999.;
01002   float sh_min   = 9999.;
01003   double norm = 1.;
01004   int decayMode = 1;
01005   int decayModeNC = 0;
01006   int decayModeTau = 11;
01007   if(fDecay == 1){
01008     decayMode = -1;
01009     decayModeNC = -1;
01010     decayModeTau = -11;
01011   }
01012 
01013   MSG("NCExtrapolationNS", Msg::kInfo) << " outside loop SHOWER Start "
01014                                        << start << " END " << end
01015                                        << " NC Start " << startnc
01016                                        << " END " << endnc << endl;
01017 
01018 
01019   int maxsin = 1;//99;//NCType::kNumUMu3SqrBins;
01020   int maxdm = 1; //119;//NCType::kNumDeltaMSqrBins;
01021   int maxfs = 1;//NCType::kNumUS3SqrBins;
01022   vector<double> sinSqrDeltaMSqr;
01023 
01024   //Fitting and oscillations??
01025   // SHOWER LOOP // ii
01026   for(int ii = start; ii < end; ii++){
01027     enu_shift = les + ii*size_e;
01028     MSG("NCExtrapolationNS", Msg::kDebug) << " Shower Loop " << ii << endl;
01029     cout <<"starting second shower loop, enu_shift = " << enu_shift << endl;
01030     // NC LOOP  // jj
01031     for(int jj = startnc; jj < endnc; jj++){
01032       nc_shift = lnc + jj*size_nc;
01033       MSG("NCExtrapolationNS", Msg::kDebug) << " NC Loop " << jj
01034                                             << " nc_shift " << nc_shift << endl;
01035       cout  <<"nc loop within second shower loop, nc_shift = " << nc_shift << endl;
01036 
01037       //I need four histos here
01038       //true cc sel as cc, true cc sel as nc,
01039       //true nc sel as nc, true nc sel as cc
01040 
01041       fnooscar->Reset();
01042       fnooscar_post->Reset();
01043       fnooscar_ccselcc->Reset();
01044       fnooscar_ccselcc_post->Reset();
01045       fnooscar_ccselnc->Reset();
01046       fnooscar_ccselnc_post->Reset();
01047       fnooscar_ncselnc->Reset();
01048       fnooscar_ncselnc_post->Reset();
01049       fnooscar_ncselcc->Reset();
01050       fnooscar_ncselcc_post->Reset();
01051       // taus
01052       fnooscar_ccselcct->Reset();
01053       fnooscar_ccselcct_post->Reset();
01054       fnooscar_ccselnct->Reset();
01055       fnooscar_ccselnct_post->Reset();
01056 
01057 //Temp disabling to check fitting      fnooscar->Add(noosctruearray[ii][jj]); //Copying prediction into working histo!!
01058       fnooscar->Add(fallccmcf); //Temp using true FD spectrum (before pid efficiency corr) instead of prediction!
01059 
01060       fTrueFDAfterBM->Add(noosctruearray[ii][jj]);  //for storage
01061 //       fnooscar_post->Add(noosctruearraypost[ii][jj]);
01062 
01063       //PRE
01064       fnooscar_ccselcc->Multiply(fnooscar,feffic);  //truecc, cclike, efficiency correction
01065       fTrueFDAfterBM_posteffcorr=(TH1F*)fnooscar_ccselcc->Clone("trueFDAfterBM_posteffcorr"); //LLH adding to compare to fTrueFD after PID'as applied
01066       fnooscar_ccselnc->Multiply(fnooscar,fefficbg); //truecc, nclike, efficiency correction
01067       //Obtain true NC Spectrum from the NC/CC Ratio
01068       fnooscar_ncselnc->Multiply(fnooscar,fratccnc); //truenc, nclike, cross-section correction
01069       fnooscar_ncselcc->Multiply(fnooscar,fratccnc); //truenc, cclike, cross-section correction
01070       fnooscar_ncselnc->Multiply(fefficnc); //efficiency correction
01071       fnooscar_ncselcc->Multiply(fefficncbg); //efficiency correction
01072 
01073       // POST
01074 //       fnooscar_ccselcc_post->Multiply(fnooscar_post,fefficp);
01075 //       fnooscar_ccselnc_post->Multiply(fnooscar_post,fefficbgp);
01076 //       //Obtain true NC Spectrum from the NC/CC Ratio
01077 //       fnooscar_ncselnc_post->Multiply(fnooscar_post,fratccncp);
01078 //       fnooscar_ncselcc_post->Multiply(fnooscar_post,fratccncp);
01079 //       fnooscar_ncselnc_post->Multiply(fefficncp);
01080 //       fnooscar_ncselcc_post->Multiply(fefficncbgp);
01081 
01082       // taus
01083       if(fDoMC==0){
01084         // PRE
01085         fnooscar_ccselcct->Multiply(fnooscar,frattau);
01086         fnooscar_ccselcct->Multiply(feffict);
01087         fnooscar_ccselnct->Multiply(fnooscar,frattau);
01088         fnooscar_ccselnct->Multiply(fefficbgt);
01089 
01090         // POST
01091 //      fnooscar_ccselcct_post->Multiply(fnooscar_post,frattaup);
01092 //      fnooscar_ccselcct_post->Multiply(feffictp);
01093 //      fnooscar_ccselnct_post->Multiply(fnooscar_post,frattaup);
01094 //      fnooscar_ccselnct_post->Multiply(fefficbgtp);
01095       }
01096 
01097       //Loop over deltamsq
01098       for(int i = 0; i < maxdm; i++){
01099         dmsqfit = (1.*i + 0.5)*NCType::kDeltaDeltaMSqr+NCType::kDeltaMSqrStart;
01100         dmsqfit *= 1.e-3;
01101 
01102         MSG("NCExtrapolationNS", Msg::kInfo) << " on Delta m^2 = " << dmsqfit
01103                                              << " chi^2 min = " << chisqmin << endl;
01104         cout <<"Inside deltamsq loop! dmsqfit = " << dmsqfit << endl;
01105 
01106         sinSqrDeltaMSqr.clear();
01107         //really just need the value of the sin^2(1.27*\Delta m^2 L/E) factor
01108         //at the middle of each bin as the bins are of size 0.25 GeV in the
01109         //region of oscillation interest - that is good enough to get the oscillated
01110         //true value to within 0.0004%
01111         for(int dmb = 0; dmb < fnbins; ++dmb){  //Why was this set to 27?? - LLH
01112           sinSqrDeltaMSqr.push_back(TMath::Power(TMath::Sin(NCType::k127*dmsqfit
01113                                                             *NCType::kBaseLineFar/fnooscreco->GetBinCenter(dmb+1)),2.));
01114         }//end loop over energy bins
01115 
01116         for(int j = 0; j < maxsin; j++){
01117           MSG("NCExtrapolationNS", Msg::kDebug) << " In sin loop " << j <<  endl;
01118           s2tfit = (1.*j)*NCType::kDeltaUMu3Sqr+NCType::kUMu3SqrStart; //Temp! (1.*j + 0.5)*NCType::kDeltaUMu3Sqr+NCType::kUMu3SqrStart;
01119           cout <<"Inside sinsq loop! s2tfit = " << s2tfit << endl;
01120 
01121           for(int k = 0; k < maxfs; k++){
01122             s2sfit = (1.*k + 0.5)*NCType::kDeltaUS3Sqr+NCType::kUS3SqrStart;
01123             cout <<"Inside fsterile loop! s2sfit = " << s2sfit << endl;
01124 
01125             //PRE SHUTDOWN
01126 
01127             //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01128             // LAST 1 IS FOR CC
01129             //oscweight(fnooscar_ccselcc,*ftemptrue,dmsqfit,s2tfit,1,decayMode);
01130             oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselcc,ftemptrue,14,NCType::kCC);
01131             fTrueFDAfterOsc = (TH1F*)ftemptrue->Clone("trueFDAfterOsc"); //should be identical to fTrueFDAfterBM_posteffcorr w/o osc
01132             //truetoreco(fTrueFD,*ftempreco,matarray[ii]);  //Temp! To debug unsmearing - LLH
01133             truetoreco(ftemptrue,*ftempreco,matarray[ii]);
01134             fRecoFDAfterBM->Add(ftempreco);
01135             ftemptrue->Reset();
01136             //Oscillate true nc's selected as cc's after efficiency and turn them to reco
01137             //oscweight (fnooscar_ncselcc,*ftemptrue,dmsqfit,s2sfit,1,decayModeNC);
01138             oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ncselcc,ftemptrue,14,NCType::kNC);
01139             truetoreco(ftemptrue,*ftempreconc,matarrayncb[ii]);
01140             ftempreconc->Scale(nc_shift);
01141             ftemptrue->Reset();
01142             //Oscillate true nc's selected as nc's after efficiency and turn them to reco
01143             //oscweight(fnooscar_ncselnc,*ftemptrue,dmsqfit,s2sfit,1,decayModeNC);
01144             oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ncselnc,ftemptrue,14,NCType::kNC);
01145             truetoreco(ftemptrue,*ftempreco_ncfit,matarraync[ii]);
01146             ftempreco_ncfit->Scale(nc_shift);
01147             ftemptrue->Reset();
01148             //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01149             //oscweight(fnooscar_ccselnc,*ftemptrue,dmsqfit,s2tfit,1,decayMode);
01150             oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselnc,ftemptrue,14,NCType::kCC);
01151             truetoreco(ftemptrue,*ftemprecocc_ncfit,matarrayb[ii]);
01152             ftemptrue->Reset();
01153 
01154             // TAUS  HERE
01155             if(fDoMC==0){
01156               //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01157               // LAST 11 IS FOR CC TAU
01158               //oscweight(fnooscar_ccselcct,*ftemptrue,dmsqfit,s2tfit,1,decayModeTau);
01159               oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselcct,ftemptrue,16,NCType::kCC);
01160               truetoreco(ftemptrue,*ftemprecotau,matarrayt[ii]);
01161               ftemptrue->Reset();
01162 
01163               //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01164               //oscweight(fnooscar_ccselnct,*ftemptrue,dmsqfit,s2tfit,1,decayModeTau);
01165               oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselnct,ftemptrue,16,NCType::kNC);
01166               truetoreco(ftemptrue,*ftemprecotau_ncfit,matarraybt[ii]);
01167               ftemptrue->Reset();
01168             }   //end taus
01169 
01170 //          //POST SHUTDOWN
01171 
01172 //          //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01173 //          // LAST 1 IS FOR CC
01174 //          //oscweight(fnooscar_ccselcc_post,*ftemptrue,dmsqfit,s2tfit,1,decayMode);
01175 //          oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselcc_post,ftemptrue,14,NCType::kCC);
01176 //          truetoreco(ftemptrue,*ftempreco_post,matarray_post[ii]);
01177 //          ftemptrue->Reset();
01178 //          //Oscillate true nc's selected as cc's after efficiency and turn them to reco
01179 //          //oscweight(fnooscar_ncselcc_post,*ftemptrue,dmsqfit,s2sfit,1,decayModeNC);
01180 //          oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ncselcc_post,ftemptrue,14,NCType::kNC);
01181 //          truetoreco(ftemptrue,*ftempreconc_post,matarrayncb_post[ii]);
01182 //          ftempreconc_post->Scale(nc_shift);
01183 //          ftemptrue->Reset();
01184 
01185 //          //Oscillate true nc's selected as nc's after efficiency and turn them to reco
01186 //          //oscweight(fnooscar_ncselnc_post,*ftemptrue,dmsqfit,s2sfit,1,decayModeNC);
01187 //          oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ncselnc_post,ftemptrue,14,NCType::kNC);
01188 //          truetoreco(ftemptrue,*ftempreco_ncfit_post,matarraync_post[ii]);
01189 //          ftempreco_ncfit_post->Scale(nc_shift);
01190 //          ftemptrue->Reset();
01191 //          //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01192 //          //oscweight(fnooscar_ccselnc_post,*ftemptrue,dmsqfit,s2tfit,1,decayMode);
01193 //          oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselnc_post,ftemptrue,14,NCType::kCC);
01194 //          truetoreco(ftemptrue,*ftemprecocc_ncfit_post,matarrayb_post[ii]);
01195 //          ftemptrue->Reset();
01196 
01197 //          // TAUS  HERE
01198 //          if(fDoMC==0){
01199 //            //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01200 //            // LAST 11 IS FOR CC TAU
01201 //            //oscweight(fnooscar_ccselcct_post,*ftemptrue,dmsqfit,s2tfit,1,decayModeTau);
01202 //            oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselcct_post,ftemptrue,16,NCType::kCC);
01203 //            truetoreco(ftemptrue,*ftemprecotau_post,matarrayt_post[ii]);
01204 //            ftemptrue->Reset();
01205 
01206 //            //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01207 //            //oscweight(fnooscar_ccselnct_post,*ftemptrue,dmsqfit,s2tfit,1,decayModeTau);
01208 //            oscweight(sinSqrDeltaMSqr,s2tfit,s2sfit,fnooscar_ccselnct_post,ftemptrue,16,NCType::kCC);
01209 //            truetoreco(ftemptrue,*ftemprecotau_ncfit_post,matarraybt_post[ii]);
01210 //            ftemptrue->Reset();
01211 //          }
01212 
01213             chisqmin_norm = 1.e12;
01214             // NORMALIZATION // kk
01215             for(int kk = nr0; kk < nr1; kk++){
01216               norm = lnr + kk*size_nr;
01217               // CC
01218               ftempreconc->Scale(norm);
01219               ftempreco->Scale(norm);
01220 //            ftempreconc_post->Scale(norm);
01221 //            ftempreco_post->Scale(norm);
01222               if(fDoMC==0){
01223                 ftemprecotau->Scale(norm);
01224 //              ftemprecotau_post->Scale(norm);
01225               }
01226               // NC
01227               ftemprecocc_ncfit->Scale(norm);
01228               ftempreco_ncfit->Scale(norm);
01229 //            ftemprecocc_ncfit_post->Scale(norm);
01230 //            ftempreco_ncfit_post->Scale(norm);
01231               if(fDoMC==0){
01232                 ftemprecotau_ncfit->Scale(norm);
01233 //              ftemprecotau_ncfit_post->Scale(norm);
01234               }
01235               //ADD THEM ALL CC
01236               ftempreco->Add(ftempreconc);
01237 //            ftempreco_post->Add(ftempreconcp);
01238               //ADD THEM ALL NC
01239               ftempreco_ncfit->Add(ftemprecocc_ncfit);
01240 //            ftempreco_ncfit_post->Add(ftemprecocc_ncfitp);
01241               if(fDoMC == 0){
01242                 ftempreco->Add(ftemprecotau);
01243 //              ftempreco_post->Add(ftemprecotaup);
01244                 ftempreco_ncfit->Add(ftemprecotau_ncfit);
01245 //              ftempreco_ncfit_post->Add(ftemprecotau_ncfitp);
01246               }
01247 
01248               //REBIN
01249               Rebin(ftempreco, *ftempreco_rebinned);
01250               Rebin(ftempreconc, *ftempreconc_rebinned);
01251 //            Rebin(ftempreco_post, *ftempreco_rebinned_post);
01252 //            Rebin(ftempreconc_post, *ftempreconc_rebinned_post);
01253               Rebin(ftempreco_ncfit, *ftempreco_ncfit_rebinned);
01254               Rebin(ftemprecocc_ncfit, *ftemprecocc_ncfit_rebinned);
01255 //            Rebin(ftempreco_ncfit_post, *ftempreco_ncfit_rebinned_post);
01256 //            Rebin(ftemprecocc_ncfit_post, *ftemprecocc_ncfit_rebinned_post);
01257 
01258               // CHISQUARE
01259               chisqtemp1 = chisq(foscreco_rebinned, ftempreco_rebinned, ftempreconc_rebinned);
01260               chisqtemp1 += ( ((1.-norm)/signorm)*((1.-norm)/signorm)
01261                               +((1.-enu_shift)/siges)*((1.-enu_shift)/siges)
01262                               +((1.-nc_shift)/signc)*((1.-nc_shift)/signc) );
01263 //            chisqtemp2 = chisq(foscreco_rebinned_post, ftempreco_rebinned_post, ftempreconc_rebinned_post);
01264 //            chisqtemp2 += ( ((1.-norm)/signorm)*((1.-norm)/signorm)
01265 //                            +((1.-enu_shift)/siges)*((1.-enu_shift)/siges)
01266 //                            +((1.-nc_shift)/signc)*((1.-nc_shift)/signc) );
01267               chisqtemp3 = chisq(foscreco_ncfit_rebinned, ftempreco_ncfit_rebinned, ftemprecocc_ncfit_rebinned);
01268 //            chisqtemp3 += ( ((1.-norm)/signorm)*((1.-norm)/signorm)
01269 //                            +((1.-enu_shift)/siges)*((1.-enu_shift)/siges)
01270 //                            +((1.-nc_shift)/signc)*((1.-nc_shift)/signc) );
01271 //            chisqtemp4 = chisq(foscreco_ncfit_rebinned_post, ftempreco_ncfit_rebinned_post, ftemprecocc_ncfit_rebinned_post);
01272 //            chisqtemp4 += ( ((1.-norm)/signorm)*((1.-norm)/signorm)
01273 //                            +((1.-enu_shift)/siges)*((1.-enu_shift)/siges)
01274 //                            +((1.-nc_shift)/signc)*((1.-nc_shift)/signc) );
01275 
01276               chisqtemp1 += chisqtemp3;//chisqtemp2 + chisqtemp3 + chisqtemp4;
01277 
01278               if(chisqtemp1 < chisqmin_norm) chisqmin_norm = chisqtemp1;
01279 
01280               if(chisqtemp1 < chisqmin){
01281                 chisqmin = chisqtemp1;
01282                 dmsqmin  = dmsqfit;
01283                 s2tmin   = s2tfit;
01284                 s2smin   = s2sfit;
01285                 norm_min = norm;
01286                 nc_min   = nc_shift;
01287                 sh_min   = enu_shift;
01288                 best_enu = ii;
01289                 best_nc  = jj;
01290               }
01291               // Un-Normalize
01292               if(fDoMC == 0){
01293                 ftempreco->Add(ftemprecotau,-1);
01294                 ftemprecotau->Scale(1./norm);
01295 //              ftempreco_post->Add(ftemprecotau_post,-1);
01296 //              ftemprecotau_post->Scale(1./norm);
01297                 ftempreco_ncfit->Add(ftemprecotau_ncfit,-1);
01298                 ftemprecotau_ncfit->Scale(1./norm);
01299 //              ftempreco_ncfit_post->Add(ftemprecotau_ncfit_post,-1);
01300 //              ftemprecotau_ncfit_post->Scale(1./norm);
01301               }
01302               ftempreco->Add(ftempreconc,-1);
01303               ftempreco->Scale(1./norm);
01304               ftempreconc->Scale(1./norm);
01305 //            ftempreco_post->Add(ftempreconc_post,-1);
01306 //            ftempreco_post->Scale(1./norm);
01307 //            ftempreconc_post->Scale(1./norm);
01308               ftempreco_ncfit->Add(ftemprecocc_ncfit,-1);
01309               ftempreco_ncfit->Scale(1./norm);
01310               ftemprecocc_ncfit->Scale(1./norm);
01311 //            ftempreco_ncfit_post->Add(ftemprecocc_ncfit_post,-1);
01312 //            ftempreco_ncfit_post->Scale(1./norm);
01313 //            ftemprecocc_ncfit_post->Scale(1./norm);
01314 
01315             }// NORMALIZATION
01316 
01317             chisqarraymins[i][k][j] = chisqmin_norm;
01318 
01319             MSG("NCExtrapolationNS", Msg::kInfo) << " (dm^2, sin^2 2theta, fsterile) = ("
01320                                                  << dmsqfit << "," << s2tfit << ","
01321                                                  << s2sfit << ") chi^2 = "
01322                                                  << chisqmin_norm << endl;
01323           }// fs
01324         }// sin2theta
01325       }// dm2
01326 
01327       //Unnormalized NC
01328       ftempreconc->Scale(1./nc_shift);
01329       ftempreco_ncfit->Scale(1./nc_shift);
01330 //       ftempreconc_post->Scale(1./nc_shift);
01331 //       ftempreco_ncfit_post->Scale(1./nc_shift);
01332 
01333     }// NC LOOP
01334   }// SHOWER LOOP
01335 
01337 
01338   //set the best fit values
01339   BestUMu3Sqr() = s2tmin;
01340   BestUS3Sqr() = s2smin;
01341   BestDeltaMSqr() = dmsqmin;
01342   fMinChiSqr = chisqmin;
01343 
01344   cout <<"Bestfit values stored as Oscillation Parameters!: "
01345        <<"\nUMu3SqrTheta = " << BestUMu3Sqr()
01346        <<"\nUS3Sqr = " << BestUS3Sqr()
01347        <<"\nDeltaMSqr = " << BestDeltaMSqr()
01348        <<"\nMinChiSqr = " << fMinChiSqr
01349        << endl;
01350 
01351 //   fBeams[farBeamI].SetOscillationParameters(fBestUMu3Sqr, fBestDeltaMSqr, fBestUS3Sqr);
01352 //   fBeams[farBeamII].SetOscillationParameters(fBestUMu3Sqr, fBestDeltaMSqr, fBestUS3Sqr);
01353 
01354   NCExtrapolation::FillDeltaChiSqrMaps(chisqarraymins);
01355 
01356   // Now fill chisq
01357   for(int i = 0; i < maxdm; i++){// dm2
01358     dmsqfit = (1.*i + 0.5)*NCType::kDeltaDeltaMSqr+NCType::kDeltaMSqrStart;
01359 
01360     for(int j = 0; j < maxsin; j++){ //sin2theta
01361       s2tfit = (1.*j + 0.5)*NCType::kDeltaUMu3Sqr+NCType::kUMu3SqrStart;
01362 
01363       double chismin =1e9;
01364       int    kmin    =-1;
01365       for(int k = 0; k < maxfs; k++){ //fsterile
01366         s2sfit = (1.*k + 0.5)*NCType::kDeltaUS3Sqr+NCType::kUS3SqrStart;
01367 
01368         if(chisqarraymins[i][k][j]<chismin){
01369           chismin = chisqarraymins[i][k][j];
01370           kmin = k;
01371         }
01372       } // fs
01373       fchisqall->Fill(s2tfit, dmsqfit, chismin);
01374     } // sin2theta
01375   } // dm2
01376 
01377   double chismin = 1e9;
01378   int    jmin    = -1;
01379 
01380   // Now fill chisq other projection
01381   for(int i = 0; i < maxdm; i++){// dm2
01382     dmsqfit = (1.*i + 0.5)*NCType::kDeltaDeltaMSqr+NCType::kDeltaMSqrStart;
01383 
01384     for(int k = 0; k < maxfs; k++){ //fsterile
01385       s2sfit = (1.*k + 0.5)*NCType::kDeltaUS3Sqr+NCType::kUS3SqrStart;
01386 
01387       chismin = 1e9;
01388       jmin    = -1;
01389 
01390       for(int j = 0; j < maxsin; j++){ //sin2theta
01391         s2tfit = (1.*j + 0.5)*NCType::kDeltaUMu3Sqr+NCType::kUMu3SqrStart;
01392 
01393         if(chisqarraymins[i][k][j] < chismin){
01394           chismin = chisqarraymins[i][k][j];
01395           jmin = j;
01396         }
01397       } // sin2theta
01398       fchisqallnc->Fill(s2sfit, dmsqfit, chismin);
01399     } // fs
01400   } // dm2
01401 
01402   TString name = "";
01403   TString junk = "";
01404   TString domch = "data";
01405   TString dodecayh = "osc";
01406 
01407   if(fDoMC==1) domch = "mc";
01408   if(fDecay==1) dodecayh = "dec";
01409 
01410   int sele = n1d/2;
01411   int selnc = ncd/2;
01412   enu_shiftmin = 1.;
01413   nc_shiftmin  = 1.;
01414   if(fSystematicsToUse[NCType::kShowerEnergy]){
01415     sele = best_enu;
01416     enu_shiftmin = les+best_enu*size_e;
01417   }
01418   if(fSystematicsToUse[NCType::kNCBackground]){
01419     selnc = best_nc;
01420     nc_shiftmin = lnc+best_nc *size_nc;
01421   }
01422 
01423   if(fDoMC==1){
01424     name = "res_fit_";
01425     name += "_" + junk + "_" + domch + "_" + dodecayh;
01426     name += "new.txt";
01427   }
01428   if(fDoMC==0){
01429     name = "res_fit_" + domch + "_"  + dodecayh;
01430     name += "_test_post_part_new.txt";
01431   }
01432 
01433   //Temp! - removal of systematic loops, setting normalizations to 1
01434   norm_min = 1.0;
01435   enu_shiftmin = 1.0;
01436   nc_shiftmin = 1.0;
01437 
01438   ofstream text_file(name);
01439   text_file << " Best fit at dmsq= " << dmsqmin
01440             << " sin2theta= "       << s2tmin
01441             << " fs " << s2smin
01442             << "  chisq=" << chisqmin/ndf
01443             << " norm_min " << norm_min
01444             << " enu_shiftmin " << enu_shiftmin
01445             << " nc_shiftmin " << nc_shiftmin << endl;
01446   text_file.close();
01447 
01448   MSG("NCExtrapolationNS", Msg::kInfo) << " Best fit at dmsq = " << dmsqmin
01449                                        << " sin2theta = " << s2tmin
01450                                        << " fs = " << s2smin << "  chisq = "
01451                                        << chisqmin << " ndf = "  << ndf << " norm_min "
01452                                        << norm_min << " enu_shiftmin "
01453                                        << enu_shiftmin << "nc_shiftmin " << nc_shiftmin
01454                                        <<  endl;
01455   MSG("NCExtrapolationNS", Msg::kInfo) << " Best enu integer= " << best_enu
01456                                        << " best nc integer "  << best_nc << endl;
01457 
01458   // Make best fit histo//
01459   // For that we need best shower energy, best nc value,
01460   //best normalization value and the best fit points//
01461   fnooscar->Reset();
01462 //   fnooscar_post->Reset();
01463   fnooscar_ccselcc->Reset();
01464 //   fnooscar_ccselcc_post->Reset();
01465   fnooscar_ccselnc->Reset();
01466 //   fnooscar_ccselnc_post->Reset();
01467   fnooscar_ncselnc->Reset();
01468 //   fnooscar_ncselnc_post->Reset();
01469   fnooscar_ncselcc->Reset();
01470 //   fnooscar_ncselcc_post->Reset();
01471   // taus
01472   fnooscar_ccselcct->Reset();
01473 //   fnooscar_ccselcct_post->Reset();
01474   fnooscar_ccselnct->Reset();
01475 //   fnooscar_ccselnct_post->Reset();
01476 
01477   //Copying noosctruearray for best showerenergy and nc values
01478 //Temp disabling to check fitting, no looping over showerenergy and nc values at the moment   noosctruearray[sele][selnc]->Copy(*fnooscar);
01479   fallccmcf->Copy(*fnooscar); //temp using true FD spectrum (before pid efficiency corr) instead of prediction!
01480 //   noosctruearraypost[sele][selnc]->Copy(*fnooscar_post);
01481 
01482   // PRE
01483   fnooscar_ccselcc->Multiply(fnooscar,feffic);
01484   fnooscar_ccselnc->Multiply(fnooscar,fefficbg);
01485   fnooscar_ncselnc->Multiply(fnooscar,fratccnc);
01486   fnooscar_ncselcc->Multiply(fnooscar,fratccnc);
01487   fnooscar_ncselnc->Multiply(fefficnc);
01488   fnooscar_ncselcc->Multiply(fefficncbg);
01489 
01490   // POST
01491 //   fnooscar_ccselcc_post->Multiply(fnooscar_post,fefficp);
01492 //   fnooscar_ccselnc_post->Multiply(fnooscar_post,fefficbgp);
01493 //   fnooscar_ncselnc_post->Multiply(fnooscar_post,fratccncp);
01494 //   fnooscar_ncselcc_post->Multiply(fnooscar_post,fratccncp);
01495 //   fnooscar_ncselnc_post->Multiply(fefficncp);
01496 //   fnooscar_ncselcc_post->Multiply(fefficncbgp);
01497 
01498   // taus
01499   if(fDoMC==0){
01500     // PRE
01501     fnooscar_ccselcct->Multiply(fnooscar,frattau);
01502     fnooscar_ccselcct->Multiply(feffict);
01503     fnooscar_ccselnct->Multiply(fnooscar,frattau);
01504     fnooscar_ccselnct ->Multiply(fefficbgt);
01505 
01506     // POST
01507 //     fnooscar_ccselcct_post->Multiply(fnooscar_post,frattaup);
01508 //     fnooscar_ccselcct_post->Multiply(feffictp);
01509 //     fnooscar_ccselnct_post->Multiply(fnooscar_post,frattaup);
01510 //     fnooscar_ccselnct_post->Multiply(fefficbgtp);
01511 
01512   }
01513 
01514   //PRE SHUTDOWN
01515 
01516   for(int dmb = 0; dmb < fnbins; ++dmb){
01517     sinSqrDeltaMSqr.push_back(TMath::Power(TMath::Sin(NCType::k127*dmsqfit
01518                                                       *NCType::kBaseLineFar/fnooscreco->GetBinCenter(dmb+1)),2.));
01519   }//end loop over energy bins
01520 
01521   //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01522   //oscweight(fnooscar_ccselcc,*ftemptrue,dmsqmin,s2tmin,1,decayMode);
01523   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselcc,ftemptrue,14,NCType::kCC);
01524   truetoreco(ftemptrue,*fbestfitreco,matarray[sele]);
01525   ftemptrue->Reset();
01526   //Oscillate true nc's selected as cc's after efficiency and turn them to reco
01527   //oscweight(fnooscar_ncselcc,*ftemptrue,dmsqmin,s2smin,1,decayModeNC);
01528   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ncselcc,ftemptrue,14,NCType::kNC);
01529   truetoreco(ftemptrue,*fbestfitreconc,matarrayncb[sele]);
01530   fbestfitreconc->Scale(nc_shiftmin);
01531   ftemptrue->Reset();
01532   //Oscillate true nc's selected as nc's after efficiency and turn them to reco
01533   //oscweight(fnooscar_ncselnc,*ftemptrue,dmsqmin,s2smin,1,decayModeNC);
01534   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ncselnc,ftemptrue,14,NCType::kNC);
01535   truetoreco(ftemptrue,*fbestfitreco_ncfit,matarraync[sele]);
01536   fbestfitreco_ncfit->Scale(nc_shiftmin);
01537   ftemptrue->Reset();
01538   //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01539   //oscweight(fnooscar_ccselnc,*ftemptrue,dmsqmin,s2tmin,1,decayMode);
01540   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselnc,ftemptrue,14,NCType::kCC);
01541   truetoreco(ftemptrue,*fbestfitrecocc_ncfit,matarrayb[sele]);
01542   ftemptrue->Reset();
01543 
01544   // TAUS  HERE
01545   if(fDoMC==0){
01546     //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01547     // LAST 11 IS FOR CC TAU
01548     //oscweight(fnooscar_ccselcct,*ftemptrue,dmsqmin,s2tmin,1,decayModeTau);
01549     oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselcct,ftemptrue,16,NCType::kCC);
01550     truetoreco(ftemptrue,*fbestfitrecotau,matarrayt[sele]);
01551     ftemptrue->Reset();
01552 
01553     //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01554     //oscweight(fnooscar_ccselnct,*ftemptrue,dmsqmin,s2tmin,1,decayModeTau);
01555     oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselnct,ftemptrue,16,NCType::kCC);
01556     truetoreco(ftemptrue,*fbestfitrecotau_ncfit,matarraybt[sele]);
01557     ftemptrue->Reset();
01558   }
01559 
01560 //   //POST SHUTDOWN
01561 //   //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01562 //   //oscweight(fnooscar_ccselcc_post,*ftemptrue,dmsqmin,s2tmin,1,decayMode);
01563 //   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselcc_post,ftemptrue,14,NCType::kCC);
01564 //   truetoreco(ftemptrue,*fbestfitreco_post,matarray_post[sele]);
01565 //   ftemptrue->Reset();
01566 //   //Oscillate true nc's selected as cc's after efficiency and turn them to reco
01567 //   //oscweight(fnooscar_ncselcc_post,*ftemptrue,dmsqmin,s2smin,1,decayModeNC);
01568 //   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ncselcc_post,ftemptrue,14,NCType::kNC);
01569 //   truetoreco(ftemptrue,*fbestfitreconc_post,matarrayncb_post[sele]);
01570 //   fbestfitreconc_post->Scale(nc_shiftmin);
01571 //   ftemptrue->Reset();
01572 //   //Oscillate true nc's selected as nc's after efficiency and turn them to reco
01573 //   //oscweight(fnooscar_ncselnc_post,*ftemptrue,dmsqmin,s2smin,1,decayModeNC);
01574 //   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ncselnc_post,ftemptrue,14,NCType::kNC);
01575 //   truetoreco(ftemptrue,*fbestfitreco_ncfit_post,matarraync_post[sele]);
01576 //   fbestfitreco_ncfit_post->Scale(nc_shiftmin);
01577 //   ftemptrue->Reset();
01578 //   //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01579 //   //oscweight(fnooscar_ccselnc_post,*ftemptrue,dmsqmin,s2tmin,1,decayMode);
01580 //   oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselnc_post,ftemptrue,14,NCType::kCC);
01581 //   truetoreco(ftemptrue,*fbestfitrecocc_ncfit_post,matarrayb_post[sele]);
01582 //   ftemptrue->Reset();
01583 
01584 //   // TAUS  HERE
01585 //   if(fDoMC==0){
01586 //     //Oscillate true cc's selected as cc's after efficiency and turn them to reco
01587 //     //oscweight(fnooscar_ccselcct_post,*ftemptrue,dmsqmin,s2tmin,1,decayModeTau);
01588 //     oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselcct_post,ftemptrue,16,NCType::kCC);
01589 //     truetoreco(ftemptrue,*fbestfitrecotau_post,matarrayt_post[sele]);
01590 //     ftemptrue->Reset();
01591 
01592 //     //Oscillate true cc's selected as nc's after efficiency and turn them to reco
01593 //     //oscweight(fnooscar_ccselnct_post,*ftemptrue,dmsqmin,s2tmin,1,decayModeTau);
01594 //     oscweight(sinSqrDeltaMSqr,s2tmin,s2smin,fnooscar_ccselnct_post,ftemptrue,16,NCType::kCC);
01595 //     truetoreco(ftemptrue,*fbestfitrecotau_ncfit_post,matarraybt_post[sele]);
01596 //     ftemptrue->Reset();
01597 //   }
01598 
01599   // CC
01600   fbestfitreconc->Scale(norm_min);
01601   fbestfitreco->Scale(norm_min);
01602 //   fbestfitreconc_post->Scale(norm_min);
01603 //   fbestfitreco_post->Scale(norm_min);
01604   fbestfitrecocc_ncfit->Scale(norm_min);
01605   fbestfitreco_ncfit->Scale(norm_min);
01606   fbestfitrecocc_ncfit->Scale(norm_min);
01607 //   fbestfitreco_ncfit_post->Scale(norm_min);
01608   fbestfitreco->Add(fbestfitreconc);
01609 //   fbestfitreco_post->Add(fbestfitreconcp);
01610   fbestfitreco_ncfit->Add(fbestfitrecocc_ncfit);
01611 //   fbestfitreco_ncfit_post->Add(fbestfitrecocc_ncfitp);
01612   if(fDoMC==0){
01613     fbestfitrecotau->Scale(norm_min);
01614 //     fbestfitrecotau_post->Scale(norm_min);
01615     fbestfitrecotau_ncfit->Scale(norm_min);
01616 //     fbestfitrecotau_ncfit_post->Scale(norm_min);
01617     fbestfitreco->Add(fbestfitrecotau);
01618 //     fbestfitreco_post->Add(fbestfitrecotaup);
01619     fbestfitreco_ncfit->Add(fbestfitrecotau_ncfit);
01620 //     fbestfitreco_ncfit_post->Add(fbestfitrecotau_ncfitp);
01621   }
01622 
01623   //REBIN
01624   Rebin(fbestfitreco, *fbestfitreco_rebinned);
01625   Rebin(fbestfitreconc, *fbestfitreconc_rebinned);
01626 //   Rebin(fbestfitreco_post, *fbestfitreco_rebinned_post);
01627 //   Rebin(fbestfitreconc_post, *fbestfitreconc_rebinned_post);
01628   Rebin(fbestfitreco_ncfit, *fbestfitreco_ncfit_rebinned);
01629   Rebin(fbestfitrecocc_ncfit, *fbestfitrecocc_ncfit_rebinned);
01630 //   Rebin(fbestfitreco_ncfit_post, *fbestfitreco_ncfit_rebinned_post);
01631 //   Rebin(fbestfitrecocc_ncfit_post, *fbestfitrecocc_ncfit_rebinned_post);
01632   if(fDoMC==0){
01633     Rebin(fbestfitrecotau, *fbestfitrecotau_rebinned);
01634     Rebin(fbestfitrecotau_ncfit, *fbestfitrecotau_ncfit_rebinned);
01635 //     Rebin(fbestfitrecotau_post, *fbestfitrecotau_rebinned_post);
01636 //     Rebin(fbestfitrecotau_ncfit_post, *fbestfitrecotau_ncfit_rebinned_post);
01637   }
01638 
01639   // PREDICTION UNOSCILLATED //
01640   fnooscar->Copy(*fnoosctrue);
01641 //   fnooscar_post->Copy(*fnoosctruep);
01642   fnoosctrue_ncfit->Multiply(fnooscar,fratccnc);
01643 //   fnoosctrue_ncfit_post->Multiply(fnooscar_post,fratccncp);
01644 
01645   //PRE SHUTDOWN
01646   truetoreco(fnooscar_ccselcc,*fnooscreco,  matarray[sele]);
01647   truetoreco(fnooscar_ncselcc,*fnooscreconc,matarrayncb[sele]);
01648   fnooscreconc->Scale(nc_shiftmin);
01649   truetoreco(fnooscar_ncselnc,*fnooscreco_ncfit,matarraync[sele]);
01650   fnooscreconc->Scale(nc_shiftmin);
01651   truetoreco(fnooscar_ccselnc,*fnooscrecocc_ncfit, matarrayb[sele]);
01652 
01653   //POST SHUTDOWN
01654   truetoreco(fnooscar_ccselcc_post,*fnooscreco_post,matarray_post[sele]);
01655   truetoreco(fnooscar_ncselcc_post,*fnooscreconc_post,matarrayncb_post[sele]);
01656 //   fnooscreconc_post->Scale(nc_shiftmin);
01657   truetoreco(fnooscar_ncselnc_post,*fnooscreco_ncfit_post,matarraync_post[sele]);
01658 //   fnooscreconc_post->Scale(nc_shiftmin);
01659   truetoreco(fnooscar_ccselnc_post,*fnooscrecocc_ncfit_post,matarrayb_post[sele]);
01660 
01661   // CC
01662   fnooscreconc->Scale(norm_min);
01663   fnooscreco->Scale(norm_min);
01664 //   fnooscreconc_post->Scale(norm_min);
01665   fnooscreco_post->Scale(norm_min);
01666   fnooscrecocc_ncfit->Scale(norm_min);
01667   fnooscreco_ncfit->Scale(norm_min);
01668 //   fnooscrecocc_ncfit_post->Scale(norm_min);
01669 //   fnooscreco_ncfit_post->Scale(norm_min);
01670   fnooscreco->Add(fnooscreconc);
01671 //   fnooscreco_post->Add(fnooscreconcp);
01672   fnooscreco_ncfit->Add(fnooscrecocc_ncfit);
01673 //   fnooscreconc_post->Add(fnooscrecocc_ncfitp);
01674   if(fDoMC==0){
01675     fnooscrecotau->Scale(norm_min);
01676 //     fnooscrecotau_post->Scale(norm_min);
01677     fnooscrecotau_ncfit->Scale(norm_min);
01678 //     fnooscrecotau_ncfit_post->Scale(norm_min);
01679     fnooscreco->Add(fnooscrecotau);
01680 //     fnooscreco_post->Add(fnooscrecotaup);
01681     fnooscreco_ncfit->Add(fnooscrecotau_ncfit);
01682 //     fnooscreconc_post->Add(fnooscrecotau_ncfitp);
01683   }
01684 
01685   //REBIN
01686   Rebin(fnooscreco,          *fnooscreco_rebinned);
01687   Rebin(fnooscreconc,        *fnooscreconc_rebinned);
01688 //   Rebin(fnooscreco_post,         *fnooscreco_rebinned_post);
01689 //   Rebin(fnooscreconc_post,       *fnooscreconc_rebinned_post);
01690   Rebin(fnooscreco_ncfit,       *fnooscreco_ncfit_rebinned);
01691   Rebin(fnooscrecocc_ncfit,        *fnooscrecocc_ncfit_rebinned);
01692 //   Rebin(fnooscreco_ncfit_post,      *fnooscreco_ncfit_rebinned_post);
01693 //   Rebin(fnooscrecocc_ncfit_post,       *fnooscrecocc_ncfit_rebinned_post);
01694   if(fDoMC==0){
01695     Rebin(fnooscrecotau, *fnooscrecotau_rebinned);
01696     Rebin(fnooscrecotau_ncfit, *fnooscrecotau_ncfit_rebinned);
01697 //     Rebin(fnooscrecotau_post, *fnooscrecotau_rebinned_post);
01698 //     Rebin(fnooscrecotau_ncfit_post, *fnooscrecotau_ncfit_rebinned_post);
01699   }
01700 
01701 //   //clear all the far beam MC histograms and then add the
01702 //   //results from the extrapolations to the appropriate ones
01703 // LLH uncommented
01704   int farBeams[] = {farBeamI, farBeamII};
01705   for(int b = 0; b < 2; ++b){
01706      for(int i = 0; i < 2; ++i){
01707         fBeams[farBeams[b]].GetMCFitHistogram(i)->Reset();
01708         fBeams[farBeams[b]].GetMCBackgroundHistogram(i)->Reset();
01709         fBeams[farBeams[b]].GetMCFitNuMuToNuTauHistogram(i)->Reset();
01710         fBeams[farBeams[b]].GetMCHistogram(i)->Reset();
01711         fBeams[farBeams[b]].GetMCBackgroundHistogram(i)->Reset();
01712         fBeams[farBeams[b]].GetMCNoFitNuMuToNuTauHistogram(i)->Reset();
01713      }
01714   }
01715 
01716   float scaleFactor = 1.; //what is this?  LLH
01717 
01718 //LLH uncommented
01719    //put the fit spectra into the beam objects
01720    fBeams[farBeamI].GetMCFitHistogram(NCType::kCC)->Add(fbestfitreco_rebinned, 1./scaleFactor);
01721    fBeams[farBeamI].GetMCFitHistogram(NCType::kNC)->Add(fbestfitreco_ncfit_rebinned, 1./scaleFactor);
01722    fBeams[farBeamI].GetMCBackgroundHistogram(NCType::kCC)->Add(fbestfitreconc_rebinned, 1./scaleFactor);
01723    fBeams[farBeamI].GetMCBackgroundHistogram(NCType::kNC)->Add(fbestfitrecocc_ncfit_rebinned, 1./scaleFactor);
01724    fBeams[farBeamI].GetMCFitNuMuToNuTauHistogram(NCType::kCC)->Add(fbestfitrecotau_rebinned, 1./scaleFactor);
01725    fBeams[farBeamI].GetMCFitNuMuToNuTauHistogram(NCType::kNC)->Add(fbestfitrecotau_ncfit_rebinned, 1./scaleFactor);
01726 
01727    //and the nooscillation histograms
01728    fBeams[farBeamI].GetMCHistogram(NCType::kCC)->Add(fnooscreco_rebinned, 1./scaleFactor);
01729    fBeams[farBeamI].GetMCHistogram(NCType::kNC)->Add(fnooscreco_ncfit_rebinned, 1./scaleFactor);
01730    fBeams[farBeamI].GetMCBackgroundHistogram(NCType::kCC)->Add(fnooscreconc_rebinned, 1./scaleFactor);
01731    fBeams[farBeamI].GetMCBackgroundHistogram(NCType::kNC)->Add(fnooscrecocc_ncfit_rebinned, 1./scaleFactor);
01732    fBeams[farBeamI].GetMCNoFitNuMuToNuTauHistogram(NCType::kCC)->Add(fnooscrecotau_rebinned, 1./scaleFactor);
01733    fBeams[farBeamI].GetMCNoFitNuMuToNuTauHistogram(NCType::kNC)->Add(fnooscrecotau_ncfit_rebinned, 1./scaleFactor);
01734 
01735    //put the fit spectra into the beam objects
01736    fBeams[farBeamII].GetMCFitHistogram(NCType::kCC)->Add(fbestfitreco_rebinned_post, 1./scaleFactor);
01737    fBeams[farBeamII].GetMCFitHistogram(NCType::kNC)->Add(fbestfitreco_ncfit_rebinned_post, 1./scaleFactor);
01738    fBeams[farBeamII].GetMCBackgroundHistogram(NCType::kCC)->Add(fbestfitreconc_rebinned_post, 1./scaleFactor);
01739    fBeams[farBeamII].GetMCBackgroundHistogram(NCType::kNC)->Add(fbestfitrecocc_ncfit_rebinned_post, 1./scaleFactor);
01740    fBeams[farBeamII].GetMCFitNuMuToNuTauHistogram(NCType::kCC)->Add(fbestfitrecotau_rebinned_post, 1./scaleFactor);
01741    fBeams[farBeamII].GetMCFitNuMuToNuTauHistogram(NCType::kNC)->Add(fbestfitrecotau_ncfit_rebinned_post, 1./scaleFactor);
01742 
01743    //and the nooscillation histograms
01744    fBeams[farBeamII].GetMCHistogram(NCType::kCC)->Add(fnooscreco_rebinned_post, 1./scaleFactor);
01745    fBeams[farBeamII].GetMCHistogram(NCType::kNC)->Add(fnooscreco_ncfit_rebinned_post, 1./scaleFactor);
01746    fBeams[farBeamII].GetMCBackgroundHistogram(NCType::kCC)->Add(fnooscreconc_rebinned_post, 1./scaleFactor);
01747    fBeams[farBeamII].GetMCBackgroundHistogram(NCType::kNC)->Add(fnooscrecocc_ncfit_rebinned_post, 1./scaleFactor);
01748    fBeams[farBeamII].GetMCNoFitNuMuToNuTauHistogram(NCType::kCC)->Add(fnooscrecotau_rebinned_post, 1./scaleFactor);
01749    fBeams[farBeamII].GetMCNoFitNuMuToNuTauHistogram(NCType::kNC)->Add(fnooscrecotau_ncfit_rebinned_post, 1./scaleFactor);
01750 
01751   return;
01752 }
01753 
01754 //----------------------------------------------------------------------
01755 //write out histograms etc used in the fit
01756 void NCExtrapolationNS::WriteResources()
01757 {
01758 
01759   //fill the histograms in the NCBeam objects in fBeams vector
01760   //before calling WriteResources in the base class
01761 
01762    cout <<"Writing!!!!!!" << endl;
01763 
01764   NCExtrapolation::WriteResources();
01765 
01766   // get a pointer to the current directory
01767   // this is one of the output files
01768   TDirectory* fileDir = gDirectory;
01769   TDirectory* saveDir = gDirectory;
01770   fileDir->cd("plots"+NCType::kExtrapolationNames[fExtrapolationType]);
01771 
01772   fTrueND->Write();
01773   fTrueNDAfterRT->Write();
01774   fTrueNDAfterRT_noeffcorr->Write(); //LLH adding
01775   fRecoND->Write();
01776   fTrueFD->Write();
01777   fTrueFD_nc->Write();
01778   fRecoFD->Write();
01779   fRecoFD_nc->Write();
01780   fTrueFDAfterBM->Write();
01781   fTrueFDAfterBM_noacccorr->Write(); //LLH addng
01782   fTrueFDAfterBM_posteffcorr->Write(); //LLH addng
01783   fTrueFDAfterOsc->Write(); //LLH addng
01784   fRecoFDAfterBM->Write();
01785 
01786   //LLH adding
01787   frecotruemat->Write();
01788   fefficiencyn->Write();
01789   fdatselND->Write();
01790   fdatselFD->Write();
01791   fdatselFD_nc->Write();
01792 
01793   //true to reco matricies
01794   for(uint i = 0; i < matarray.size(); ++i){
01795     matarray[i]->Write();
01796     matarraync[i]->Write();
01797     matarrayb[i]->Write();
01798     matarrayncb[i]->Write();
01799     //TAUS
01800     matarrayt[i]->Write();
01801     matarraybt[i]->Write();
01802 
01803     matarray_post[i]->Write();
01804     matarraync_post[i]->Write();
01805     matarrayb_post[i]->Write();
01806     matarrayncb_post[i]->Write();
01807     //TAUS
01808     matarrayt_post[i]->Write();
01809     matarraybt_post[i]->Write();
01810 
01811     for(uint j = 0; j < noosctruearray[i].size(); ++j){
01812       noosctruearray[i][j]->Write();
01813       noosctruearraypost[i][j]->Write();
01814     }
01815   }//end writing true to reco matricies
01816 
01817 
01818   fnoosctrue->Write();
01819   fnooscreco->Write();
01820   fbestfitreco->Write();
01821   fbestfitreconc->Write();
01822   fbestfitrecotau->Write();
01823 
01824   //LLH adding
01825   fnooscar->Write();
01826   fnooscar_ccselcc->Write();
01827   fnooscar_ccselnc->Write();
01828   fnooscar_ncselnc->Write();
01829   fnooscar_ncselcc->Write();
01830 
01831   fnooscar_ccselcct->Write();
01832   fnooscar_ccselnct->Write();
01833 
01834   foscreco_rebinned->Write();
01835   fnooscreco_rebinned->Write();
01836   fbestfitreco_rebinned->Write();
01837   fbestfitreconc_rebinned->Write();
01838   fbestfitrecotau_rebinned->Write();
01839   //
01840   fnoosctrue_ncfit->Write();
01841   fnooscreco_ncfit->Write();
01842   fnooscrecocc_ncfit->Write();
01843   fnooscrecotau_ncfit->Write();
01844   fbestfitreco_ncfit->Write();
01845   fbestfitrecocc_ncfit->Write();
01846   fbestfitrecotau_ncfit->Write();
01847 
01848   foscreco_ncfit_rebinned->Write();
01849 
01850   fnooscreco_ncfit_rebinned->Write();
01851   fbestfitreco_ncfit_rebinned->Write();
01852   fbestfitrecocc_ncfit_rebinned->Write();
01853   fbestfitrecotau_ncfit_rebinned->Write();
01854 
01855   // POst
01856 
01857   fnoosctrue_post->Write();
01858   fnooscreco_post->Write();
01859   fbestfitreco_post->Write();
01860   fbestfitreconc_post->Write();
01861   fbestfitrecotau_post->Write();
01862 
01863   foscreco_rebinned_post->Write();
01864 
01865   fnooscreco_rebinned_post->Write();
01866   fbestfitreco_rebinned_post->Write();
01867   fbestfitreconc_rebinned_post->Write();
01868   fbestfitrecotau_rebinned_post->Write();
01869 
01870   fnoosctrue_ncfit_post->Write();
01871   fnooscreconc_post->Write();
01872   fbestfitreco_ncfit_post->Write();
01873   fbestfitrecocc_ncfit_post->Write();
01874   fbestfitrecotau_ncfit_post->Write();
01875 
01876   foscreco_ncfit_rebinned_post->Write();
01877 
01878   fnooscreco_ncfit_rebinned_post->Write();
01879   fbestfitreco_ncfit_rebinned_post->Write();
01880   fbestfitrecocc_ncfit_rebinned_post->Write();
01881   fbestfitrecotau_ncfit_rebinned_post->Write();
01882 
01883   //
01884   fchisqall->Write();
01885   fchisqallnc->Write();
01886   fratccnc->Write();
01887 
01888   fcorf->Write();
01889   fcor->Write();
01890   fcorfall->Write();
01891   fcorall->Write();
01892   fSingleMCNearNuCC->Write();
01893   fSingleMCNearAllCC->Write();
01894   fSingleMCFarNuCC->Write();
01895   fSingleMCFarAllCC->Write();
01896   fMCNearNuCC->Write();
01897   fMCNearAllCC->Write();
01898   fMCFarNuCC->Write();
01899   fMCFarAllCC->Write();
01900   fallccmc->Write();
01901   fallccmcf->Write();
01902   fallncmcf->Write();
01903 
01904   fpurityn->Write();
01905   feffic->Write();
01906   fefficnc->Write();
01907   fefficbg->Write();
01908   fefficncbg->Write();
01909   //fratccnc->Write(); //LLH oops in here twice!
01910   frattau->Write();
01911   feffic_numcheck->Write(); //LLH adding
01912 
01913   saveDir->cd();
01914 
01915   return;
01916 }
01917 
01918 //***************************************************************************************
01919 void NCExtrapolationNS::MakePrediction(TH1F *datsel,
01920                                        TH1F* purityn,
01921                                        TH1F* efficiencyn,
01922                                        TH2F *matn_norm,
01923                                        TH1F* prediction,
01924                                        int runType){
01925 
01926   MSG("NCExtrapolationNS", Msg::kInfo) << " in MakePrediction" << endl;
01927 
01928   prediction->Reset();
01929 
01930   TH2D *bmat_norm_r2 = fbmat_norm_r2;
01931   if(runType == 1) bmat_norm_r2 = fbmat_norm_r2_post; //LLH should this be runType == NCType::kRunII?? - fixme
01932 
01933   double ntrue[fnbins] = {0.};
01934   double ftrue[fnbins] = {0.};
01935   double eff[fnbins] = {0.};
01936 
01937   // FIDUCIAL MASS DEFINITIONS
01938   double scalef = kscalef;
01939   double mass_f = kmass_f;
01940   double mass_n = kmass_n;
01941 
01942 //this was old fv for PRL (doMC = 0 is data, doMC=1 is mc)
01943 //   if(fDoMC==-1){ // !!!!!!!!! here change
01944 //     mass_f    = 5.23*1e3+53*484*0.01*1.032; // tons
01945 //     mass_n    = 3.141592654*(1.)*(1.)*((84-17)*0.0254*7.874+(84-17)*0.01*1.032);
01946 //     scalef    = 1.00;
01947 //   }
01948 //commented out, this is the old cc fv volume - LLH
01949 
01950   double  scalemass = mass_f/mass_n;
01951   cout <<"scalemass = " << scalemass
01952        <<"\nmass_f = " << mass_f
01953        <<"\nmass_n = " << mass_n
01954        << endl;
01955 
01956   TH1F *datsel_cor = new TH1F("datsel_cor","",fnbins,fbinvect);
01957   // NEAR TRUE SPECTRA
01958   TH1F *allccmc_obt_2d = new TH1F("allccmc_obt_2d","",fnbins,fbinvect);
01959 
01960   // STEP 1 : CORRECT FOR PURITY THE "DATA SELECTED SPECTGRUM"
01961   datsel_cor->Multiply(datsel,purityn);
01962 
01963   // STEP 2 : TRANSFORM RECO = > TRUE & CORRECT FOR EFFICIENCY
01964   float k = 0.;
01965   for(int i = 0; i < fnbins; i++){
01966     ntrue[i] = 0.;
01967     eff[i] = efficiencyn->GetBinContent(i+1);
01968     k = datsel_cor->GetBinCenter(i+1);
01969 
01970     // TRANSFORMATION USING 2D MATRIX
01971     for(int j = 0; j < fnbins; j++){
01972       ntrue[i] += matn_norm->GetBinContent(j+1,i+1)*datsel_cor->GetBinContent(j+1);
01973     }
01974     if(eff[i] > 0.){
01975        fTrueNDAfterRT_noeffcorr->Fill(k, ntrue[i]); //LLH adding
01976        ntrue[i] /= eff[i];   //efficiency correction!
01977        allccmc_obt_2d->Fill(k, ntrue[i]);
01978        fTrueNDAfterRT->Fill(k, ntrue[i]);
01979     }
01980 
01981 //LLH commenting out
01982 //     MAXMSG("NCExtrapolationNS", Msg::kInfo, fnbins)
01983 //       << "near reco to true: bin center = " << k
01984 //       << ", datsel = " << datsel->GetBinContent(i+1)
01985 //       << ", datsel_cor (purity corr) = " << datsel_cor->GetBinContent(i+1)
01986 //       << ", ntrue (eff and reco->true corr) = " << ntrue[i] << endl;
01987 
01988   }//end loop over bins
01989 
01990   // STEP 3 EXTRAPOLATE & CORRECT FOR ACCEPTANCE
01991   double cor_factf = 0.;
01992 
01993   for(int i = 0; i < fnbins; i++){
01994     // Correction factor for acceptance
01995     cor_factf = fcorf->GetBinContent(i+1);
01996     ftrue[i] = 0.;
01997 
01998     // TRANSFORMATION USING 2D GNUMI MATRIX
01999     for(int j = 0; j < fnbins; j++) {
02000       ftrue[i] += bmat_norm_r2->GetBinContent(j+1,i+1)*allccmc_obt_2d->GetBinContent(j+1);
02001     } //LLH added braces for readability
02002 
02003     //LLH adding
02004     k = fcorf->GetBinCenter(i+1);
02005     fTrueFDAfterBM_noacccorr->Fill(k, ftrue[i]);
02006     //end LLH added
02007 
02008 //LLH    cout <<"cor_factf = " << cor_factf << endl;
02009 
02010     prediction->SetBinContent(i+1,ftrue[i]*cor_factf);  //applying correction for FD acceptance and event recon.
02011   }
02012 
02013   prediction->Scale(scalemass*scalef);
02014 
02015   datsel_cor->Delete();
02016   allccmc_obt_2d->Delete();
02017 }
02018 
02019 //----------------------------------------------------------------------
02020 void NCExtrapolationNS::RecoTruePur(TH2F*recotruemat1,
02021                                     TH1F *purityn1,
02022                                     TH1F *efficiencyn1,
02023                                     double enu_shift,
02024                                     int beam)
02025 {
02026    cout <<"In RecoTruePur! " << endl;
02027 
02028   recotruemat1->Reset();
02029   purityn1->Reset();
02030   efficiencyn1->Reset();
02031 
02032   TH1F selcc_true("selcc_true","",fnbins,fbinvect);
02033   TH1F selcc_reco("selcc_reco","",fnbins,fbinvect);
02034   TH1F sel_reco("sel_reco","",fnbins,fbinvect);
02035 
02036   double mc_weif=1.;
02037   double shower = 0.;
02038   double mumom = 0.;
02039   double reco_ene = 0.;
02040   int    flavour = 0;
02041   double true_enu = 0.;
02042   //double annpid = 0.;
02043   //int cc_nc = 0;
02044   //int initial_state = 0;
02045 
02046   //size of the MC arrays
02047   int sizeSignal =  0;
02048   int sizeBG = 0;
02049 
02050   //loop over bins in the beam
02051   //only use CC events in this stage
02052   NCEnergyBin *bin = 0;
02053   int numBins = fBeams[beam].GetNumberEnergyBins(NCType::kCC);
02054   for(int i = 0; i < numBins; ++i){
02055     bin = fBeams[beam].GetEnergyBin(i, NCType::kCC);
02056     sizeSignal = bin->GetMCSignalVectorSize();
02057     sizeBG = bin->GetMCBackgroundVectorSize();
02058     for(int j = 0; j < sizeSignal; ++j){
02059       bin->GetMCInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02060       shower *= enu_shift;
02061       reco_ene = shower + mumom;
02062       //only use nu_mu, no nu_mubar
02063       if(flavour == 14){
02064         selcc_true.Fill(true_enu,mc_weif);
02065         recotruemat1->Fill(reco_ene,true_enu,mc_weif);
02066         selcc_reco.Fill(reco_ene,mc_weif);
02067         fTrueND->Fill(true_enu, mc_weif); } //LLH temp to check datsel against fRecoND
02068         fRecoND->Fill(reco_ene, mc_weif);
02069 //LLH temp default brace location     }
02070         sel_reco.Fill(reco_ene,mc_weif); //LLH sel_reco includes numu and numubar
02071     }//end loop over signal events
02072 
02073     for(int j = 0; j < sizeBG; ++j){
02074       bin->GetMCBackgroundInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02075       shower *= enu_shift;
02076       reco_ene = shower + mumom;
02077       sel_reco.Fill(reco_ene,mc_weif);
02078       fRecoND->Fill(reco_ene, mc_weif); //LLH temp to check datsel against fRecoND
02079     }//end loop over background events
02080 
02081     //LLH adding
02082     int size_elec = bin->GetMCNuEToNuEVectorSize();
02083     int size_tau = bin->GetMCNuMuToNuTauVectorSize(); //there should be no taus in ND!
02084     for(int j = 0; j < size_tau; ++j){
02085        bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02086        shower *= enu_shift;
02087        reco_ene = shower + mumom;
02088        sel_reco.Fill(reco_ene,mc_weif);
02089        fRecoND->Fill(reco_ene, mc_weif);
02090        cout <<"Found a tau in the ND MC!!!!!!!!!!" << reco_ene << endl;
02091     }
02092     for(int j = 0; j < size_elec; ++j){
02093        bin->GetMCNuEToNuEInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02094        shower *= enu_shift;
02095        reco_ene = shower + mumom;
02096        sel_reco.Fill(reco_ene,mc_weif);
02097        fRecoND->Fill(reco_ene, mc_weif);
02098        cout <<"Found a CC-LIKE nue in the ND MC!!!!!!!!!!" << reco_ene << endl;
02099     }
02100     //end LLH added
02101 
02102   }//end loop over energy bins
02103 
02104 
02105   //for(uint i = 0; i < fweind.size(); ++i){
02106 
02107 //     mc_weif       = fweind[i];
02108 //     shower        = feshnd[i];
02109 //     shower        = shower*enu_shift;
02110 //     mumom         = femund[i];
02111 //     reco_ene      = shower+mumom;
02112 //     annpid        = fpidpnd[i];
02113 //     cc_nc         = fccnd[i];
02114 //     flavour       = fflavnd[i];
02115 //     initial_state = finisnd[i];
02116 //     true_enu      = fenutnd[i];
02117 
02118 //     if(cc_nc == NCType::kCC
02119 //        && flavour == 14
02120 //        && initial_state <= 2){
02121 
02122 //       selcc_true->Fill(true_enu,mc_weif);
02123 //       recotruemat1->Fill(reco_ene,true_enu,mc_weif);
02124 //       selcc_reco->Fill(reco_ene,mc_weif);
02125 //     }
02126 
02127 //     sel_reco->Fill(reco_ene,mc_weif);
02128 
02129 //   }//end loop over near events
02130 
02131   double pur = 1.;
02132   double eff = 1.;
02133   double all = 0.;
02134   double selected = 0.;
02135   for(int i = 0; i < fnbins; i++){
02136     selected = selcc_reco.GetBinContent(i+1);
02137     all = sel_reco.GetBinContent(i+1);
02138     if(all > 0){
02139       pur = selected/all;
02140       purityn1->SetBinContent(i+1,pur);
02141     }
02142 
02143     selected = selcc_true.GetBinContent(i+1);
02144     all = fallccmc->GetBinContent(i+1);
02145     if(all > 0){
02146       eff = selected/all;
02147       efficiencyn1->SetBinContent(i+1,eff);
02148     }
02149   }
02150   //Efficiency check
02151   //---------------WHAT DOES THIS DO?
02152   double check = 1.;
02153   for(int i = 0; i < fnbins; i++){
02154     check=efficiencyn1->GetBinContent(i+1);
02155     if(check == 0 && i < fnbins)
02156       efficiencyn1->SetBinContent(i+1,efficiencyn1->GetBinContent(i+2));
02157   }
02158 
02159   Convert2DMatrixToProbability(recotruemat1);
02160 
02161   return;
02162 }
02163 
02164 //----------------------------------------------------------------------
02165 //Why is this separate from GetEff??
02166 void NCExtrapolationNS::RecoTrueEffPur(TH2F *truerecomat1, //cc sel cc
02167                                        TH2F *truerecomat2, //nc sel nc
02168                                        TH2F *truerecomat1b, //nc sel cc
02169                                        TH2F *truerecomat2b, //cc sel nc
02170                                        double enu_shift,
02171                                        int beam)
02172 {
02173    cout <<"In RecoTrueEffPur! " << endl;
02174 
02175   truerecomat1->Reset();
02176   truerecomat2->Reset();
02177 
02178   truerecomat1b->Reset();
02179   truerecomat2b->Reset();
02180 
02181   double  mc_weif=1.;
02182   double  shower = 1.;
02183   double  mumom = 1.;
02184   double  reco_ene = 0.;
02185   int     flavour = 0;
02186   double  true_enu = 0.;
02187 //   double   showernc = 1.;
02188 //   int     annpid_f1p1 = 1;
02189 //   int     cc_nc = 0;
02190 //   int     initial_state = 0;
02191 
02192 //   bool trcc   = false;
02193 //   bool trcca  = false;
02194 //   bool slcc   = false;
02195 //   bool nslcc  = false;
02196 
02197 //   bool evnc =false;
02198 //   bool slnc =false;
02199 //   bool nslnc=false;
02200 
02201   //size of the MC arrays
02202   int sizeSignal =  0;
02203   int sizeBG = 0;
02204   int sizeElec =0;
02205 
02206   //loop over bins in the beam
02207   NCEnergyBin *bin = 0;
02208   int numBins = fBeams[beam].GetNumberEnergyBins(NCType::kCC);
02209   for(int i = 0; i < numBins; ++i){
02210     bin = fBeams[beam].GetEnergyBin(i, NCType::kCC);
02211     sizeSignal = bin->GetMCSignalVectorSize();
02212     sizeBG = bin->GetMCBackgroundVectorSize();
02213     sizeElec = bin->GetMCNuEToNuEVectorSize();
02214 
02215     //true cc selected as cc
02216     for(int j = 0; j < sizeSignal; ++j){
02217       bin->GetMCInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02218       shower *= enu_shift;
02219       reco_ene = shower + mumom;
02220       //only use nu_mu, no nu_mubar <= original case, LLH is including numubar!
02221       if(abs(flavour) == 14){
02222         truerecomat1->Fill(true_enu, reco_ene, mc_weif);
02223         fTrueFD->Fill(true_enu, mc_weif);
02224         fRecoFD->Fill(reco_ene, mc_weif);
02225         }
02226     }//end loop over signal events
02227 
02228     //true nc selected as cc
02229     for(int j = 0; j < sizeBG; ++j){
02230       bin->GetMCBackgroundInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02231       shower *= enu_shift;
02232       reco_ene = shower + mumom;
02233       truerecomat2b->Fill(true_enu, reco_ene, mc_weif);
02234     }//end loop over background events
02235 
02236     //LLH adding
02237 //    int size_tau = bin->GetMCNuMuToNuTauVectorSize(); //taus must be oscillated before adding, no osc on for debugging, therefore no taus!
02238 //     for(int j = 0; j < size_tau; ++j){
02239 //        bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02240 //        shower *= enu_shift;
02241 //        reco_ene = shower + mumom;
02242 //       fRecoFD->Fill(reco_ene, mc_weif);
02243 //       cout <<"Finding FD tau!!!!" << endl;
02244 //    }
02245 
02246     //true cc elec selected as cc
02247     for(int j = 0; j < sizeElec; ++j){
02248        bin->GetMCNuEToNuEInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02249        shower *= enu_shift;
02250        reco_ene = shower + mumom;
02251        truerecomat2b->Fill(true_enu, reco_ene, mc_weif);
02252 //        fRecoFD->Fill(reco_ene, mc_weif);
02253 //        cout <<"Finding FD nue!!!!" << endl;
02254     }
02255     //end LLH added
02256 
02257   }//end loop over cc energy bins
02258 
02259   numBins = fBeams[beam].GetNumberEnergyBins(NCType::kNC);
02260   for(int i = 0; i < numBins; ++i){
02261     bin = fBeams[beam].GetEnergyBin(i, NCType::kNC);
02262     sizeSignal = bin->GetMCSignalVectorSize();
02263     sizeBG = bin->GetMCBackgroundVectorSize();
02264     sizeElec = bin->GetMCNuEToNuEVectorSize();
02265 
02266     //true nc selected as nc
02267     for(int j = 0; j < sizeSignal; ++j){
02268       bin->GetMCInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02269       shower *= enu_shift;
02270       reco_ene = shower + mumom;
02271       truerecomat2->Fill(true_enu,shower,mc_weif);
02272       fTrueFD_nc->Fill(true_enu, mc_weif);
02273       fRecoFD_nc->Fill(reco_ene, mc_weif);
02274     }//end loop over signal events
02275 
02276     //true cc selected as nc
02277     for(int j = 0; j < sizeBG; ++j){
02278       bin->GetMCBackgroundInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02279       shower *= enu_shift;
02280       reco_ene = shower + mumom;
02281       truerecomat1b->Fill(true_enu,reco_ene,mc_weif);
02282     }//end loop over background events
02283 
02284     //true elec selected as nc, true cc only
02285     for(int j = 0; j < sizeElec; ++j){
02286        bin->GetMCNuEToNuEInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02287        shower *= enu_shift;
02288        reco_ene = shower + mumom;
02289        truerecomat1b->Fill(true_enu, reco_ene, mc_weif);
02290     }
02291 
02292   }//end loop over nc energy bins
02293 
02294 //   for(uint i = 0; i < fwei.size(); i++){
02295 //     mc_weif       = fwei[i];
02296 //     shower        = fesh[i];
02297 //     shower        = shower*enu_shift;
02298 //     showernc      = feshnc[i];
02299 //     showernc      = showernc*enu_shift;
02300 //     mumom         = femu[i];
02301 //     reco_ene      = shower+mumom;
02302 //     annpid_f1p1   = fpid_post[i];
02303 //     cc_nc         = fccfd[i];
02304 //     flavour       = fflavfd[i];
02305 //     initial_state = finis[i];
02306 //     true_enu      = fenut[i];
02307 
02308 //     // CC Selection
02309 //     trcc   = false;
02310 //     trcca  = false;
02311 //     slcc   = false;
02312 //     nslcc  = false;
02313 
02314 //     if(cc_nc == NCType::kCC
02315 //        && initial_state <= 2
02316 //        && flavour == 14)
02317 //       trcc  = true;
02318 //     if(cc_nc == NCType::kCC)
02319 //       trcca = true;
02320 //     if(annpid_f1p1 == NCType::kCC) slcc  = true;
02321 //     if(annpid_f1p1 == NCType::kNC) nslcc = true;
02322 
02323 //     // NC Selection
02324 //     evnc =false;
02325 //     slnc =false;
02326 //     nslnc=false;
02327 
02328 //     if(cc_nc == NCType::kNC) evnc = true;
02329 //     if(annpid_f1p1 == NCType::kNC) slnc = true;
02330 //     if(annpid_f1p1 == NCType::kCC) nslnc= true;
02331 
02332 //     // CC selected as CC
02333 //     if(trcc == true && slcc==true)
02334 //       truerecomat1->Fill(true_enu,reco_ene,mc_weif);
02335 
02336 //     // NC Selected as NC
02337 //     if(evnc == true && slnc==true)
02338 //       truerecomat2->Fill(true_enu,showernc,mc_weif);
02339 
02340 //     //CC selected as NC
02341 //     if(trcca==true && nslcc==true)
02342 //       truerecomat1b->Fill(true_enu,showernc,mc_weif);
02343 
02344 //     //NC selected as CC
02345 //     if(evnc==true && nslnc==true)
02346 //       truerecomat2b->Fill(true_enu,reco_ene,mc_weif);
02347 
02348 //   }
02349 
02350   Convert2DMatrixToProbability(truerecomat1);
02351   Convert2DMatrixToProbability(truerecomat1b);
02352   Convert2DMatrixToProbability(truerecomat2);
02353   Convert2DMatrixToProbability(truerecomat2b);
02354 
02355   return;
02356 }
02357 //**********************************************************************
02358 // TAUS RECO TO TRUE FOR BOTH CC AND ND NAMELY 2 HISTOS
02359 void NCExtrapolationNS::RecoTrueEffPurTau(TH2F *truerecomat1,
02360                                           TH2F *truerecomat1b,
02361                                           double enu_shift,
02362                                           int beam)
02363 {
02364 
02365   truerecomat1->Reset();
02366   truerecomat1b->Reset();
02367 
02368   double  mc_weif=1.;
02369   double   shower = 1.;
02370   double   mumom = 1.;
02371   double   reco_ene = 0.;
02372   int     flavour = 0;
02373   double   true_enu = 0.;
02374 //   double   showernc = 1.;
02375 //   int     annpid_f1p1 = 1;
02376 //   int     cc_nc = 0;
02377 //   int     initial_state = 0;
02378 //   bool    selfl = false;
02379 //   bool    selflnc = false;
02380 
02381   //size of the MC arrays
02382   int sizeTau =  0;
02383 
02384   //loop over bins in the beam
02385   //only true cc taus are included in the tau backgrounds as the nc are just nc events
02386   NCEnergyBin *bin = 0;
02387   int numBins = fBeams[beam].GetNumberEnergyBins(NCType::kCC);
02388   for(int i = 0; i < numBins; ++i){
02389     bin = fBeams[beam].GetEnergyBin(i, NCType::kCC);
02390     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02391 
02392     //true cc tau selected as cc
02393     for(int j = 0; j < sizeTau; ++j){
02394       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02395       shower *= enu_shift;
02396       reco_ene = shower + mumom;
02397       truerecomat1->Fill(true_enu,reco_ene,mc_weif);
02398     }//end loop over tau events
02399   }//end loop over cc energy bins
02400 
02401   numBins = fBeams[beam].GetNumberEnergyBins(NCType::kNC);
02402   for(int i = 0; i < numBins; ++i){
02403     bin = fBeams[beam].GetEnergyBin(i, NCType::kNC);
02404     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02405 
02406     //true cc tau selected as nc
02407     for(int j = 0; j < sizeTau; ++j){
02408       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02409       shower *= enu_shift;
02410       truerecomat1b->Fill(true_enu,shower,mc_weif);
02411     }//end loop over tau events
02412   }//end loop over nc energy bins
02413 
02414 //   for(uint i = 0; i < fweit.size(); i++){
02415 
02416 //     mc_weif       = fweit[i];
02417 //     shower        = fesht[i];
02418 //     shower        = shower*enu_shift;
02419 //     showernc      = feshnct[i];
02420 //     showernc      = showernc*enu_shift;
02421 //     mumom         = femut[i];
02422 //     reco_ene      = shower+mumom;
02423 //     annpid_f1p1   = fpidpt[i];
02424 //     cc_nc         = fccfdt[i];
02425 //     flavour       = fflavfdt[i];
02426 //     initial_state = finist[i];
02427 //     true_enu      = fenutt[i];
02428 
02429 //     selfl = false;
02430 //     selflnc = false;
02431 
02432 //     // CC Selection
02433 //     if(annpid_f1p1 == NCType::kCC) selfl =true;
02434 //     // NC Selection
02435 //     if(annpid_f1p1 == NCType::kNC) selflnc =true;
02436 //     // CCTAUS selected as CC
02437 //     if(cc_nc == NCType::kCC && flavour == 16){
02438 //       if(selfl) truerecomat1->Fill(true_enu,reco_ene,mc_weif);
02439 //     }
02440 //     //CCTAUS selected as NC
02441 //     if(cc_nc == NCType::kCC && flavour == 16){
02442 //       if(selflnc)  truerecomat1b->Fill(true_enu,showernc,mc_weif);
02443 //     }
02444 //   }
02445 
02446   Convert2DMatrixToProbability(truerecomat1);
02447   Convert2DMatrixToProbability(truerecomat1b);
02448 
02449   return;
02450 }
02451 
02452 //********************************************************************************
02453 void NCExtrapolationNS::GetEff(TH1F *effic_noosctrue, //cclike, cc (sig)
02454                                TH1F *effic_noosctrue_ncfit, //nclike, nc (sig)
02455                                TH1F *neffic_noosctrue, //nclike, cc (bg)
02456                                TH1F *neffic_noosctruenc, //cclike, nc (bg)
02457                                TH1F *ratccnc,
02458                                TH1F *rattau,
02459                                int beam)
02460 {
02461   ratccnc->Reset();
02462   rattau->Reset();
02463 
02464   effic_noosctrue->Reset();
02465   effic_noosctrue_ncfit->Reset();
02466 
02467   neffic_noosctrue->Reset();
02468   neffic_noosctruenc->Reset();
02469 
02470 //LLH deprecating  TH1F all("all",   "",fnbins,fbinvect);
02471   TH1F alltau("alltau","",fnbins,fbinvect);
02472 //LLH deprecating  TH1F allnc("allnc", "",fnbins,fbinvect);
02473 
02474   double mc_weif = 1.;
02475   double shower = 0.;
02476   double mumom = 0.;
02477   int    flavour = 0;
02478   double true_enu = 0.;
02479 //   double reco_ene = 0.;
02480 //   int   annpid_f1p1 = 0;
02481 //   int cc_nc = 0;
02482 //   int initial_state = 0;
02483 
02484 //   bool trcc =false;
02485 //   bool trcca=false;
02486 //   bool slcc =false;
02487 //   bool nslcc=false;
02488 //   bool evnc =false;
02489 //   bool slnc =false;
02490 //   bool nslnc=false;
02491 
02492   //size of the MC arrays
02493   int sizeTau =  0;
02494   int sizeSignal =  0;
02495   int sizeBG =  0;
02496   int sizeElec = 0;
02497 
02498   //loop over bins in the beam
02499   //only true cc taus are included in the tau backgrounds as the nc are just nc events
02500   NCEnergyBin *bin = 0;
02501   int numBins = fBeams[beam].GetNumberEnergyBins(NCType::kCC);  //Looping over CC selected
02502   for(int i = 0; i < numBins; ++i){
02503     bin = fBeams[beam].GetEnergyBin(i, NCType::kCC);
02504     sizeSignal = bin->GetMCSignalVectorSize();
02505     sizeBG = bin->GetMCBackgroundVectorSize();
02506     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02507     sizeElec = bin->GetMCNuEToNuEVectorSize();
02508 
02509     //true cc selected as cc
02510     for(int j = 0; j < sizeSignal; ++j){
02511       bin->GetMCInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02512       if(abs(flavour) == 14) effic_noosctrue->Fill(true_enu, mc_weif); //originally only numu, LLH including numubar!
02513     }//end loop over cc events
02514 
02515     //true nc selected as cc
02516     for(int j = 0; j < sizeBG; ++j){
02517       bin->GetMCBackgroundInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02518       neffic_noosctruenc->Fill(true_enu, mc_weif); //all 3 flavours!! (NC true only)
02519     }//end loop over nc events
02520 
02521     //true cc tau selected as cc
02522     for(int j = 0; j < sizeTau; ++j){
02523       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02524       alltau.Fill(true_enu, mc_weif); //fixme! incorrect for fv efficiency? - LLH
02525     }//end loop over tau events
02526 
02527     //true cc elec selected as cc
02528     for(int j = 0; j < sizeElec; ++j){
02529        bin->GetMCNuEToNuEInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02530        neffic_noosctruenc->Fill(true_enu, mc_weif); //fixme! incorrect for fv efficiency? - LLH
02531     }//end loop over electron events
02532   }//end loop over cc energy bins
02533 
02534   numBins = fBeams[beam].GetNumberEnergyBins(NCType::kNC);  //Looping over NC selected
02535   for(int i = 0; i < numBins; ++i){
02536     bin = fBeams[beam].GetEnergyBin(i, NCType::kNC);
02537     sizeSignal = bin->GetMCSignalVectorSize();
02538     sizeBG = bin->GetMCBackgroundVectorSize();
02539     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02540     sizeElec = bin->GetMCNuEToNuEVectorSize();
02541 
02542     //true nc selected as nc
02543     for(int j = 0; j < sizeSignal; ++j){
02544       bin->GetMCInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02545       effic_noosctrue_ncfit->Fill(true_enu, mc_weif); //numu and numubar (and neu neubar? - LLH)
02546     }//end loop over nc events
02547 
02548     //true cc selected as nc
02549     for(int j = 0; j < sizeBG; ++j){
02550       bin->GetMCBackgroundInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02551       neffic_noosctrue->Fill(true_enu, mc_weif);  //numu and numubar
02552     }//end loop over cc events
02553 
02554     //true tau selected as nc
02555     for(int j = 0; j < sizeTau; ++j){
02556       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02557       alltau.Fill(true_enu,mc_weif);  //fixme
02558     }//end loop over tau events
02559 
02560     //true elec selected as nc, true cc only
02561     for(int j = 0; j < sizeElec; ++j){
02562        bin->GetMCNuEToNuEInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02563        neffic_noosctrue->Fill(true_enu, mc_weif); //fixme! incorrect for fv efficiency? - LLH
02564     }//end loop over electron events
02565   }//end loop over nc energy bins
02566 
02567   rattau->Divide(&alltau,fallccmcf);
02568   ratccnc->Divide(fallncmcf,fallccmcf); //nc/cc - takes pred from allccmcf->allccncf, before eff corr
02569 
02570   feffic_numcheck = (TH1F*)effic_noosctrue->Clone("effic_numcheck"); //LLH adding, at this point should be identical to fTrueFD
02571 
02572   MSG("NCExtrapolationNS", Msg::kInfo) << " DONE WITH EFFICIENCIES " << endl;
02573   effic_noosctrue->Divide(fallccmcf);
02574   effic_noosctrue_ncfit->Divide(fallncmcf);
02575 
02576   neffic_noosctrue->Divide(fallccmcf);
02577   neffic_noosctruenc->Divide(fallncmcf);
02578 
02579 }
02580 
02581 //********************************************************
02582 void NCExtrapolationNS::GetEfft(TH1F *effic_noosctrue,
02583                                 TH1F *neffic_noosctrue,
02584                                 int beam)
02585 {
02586   effic_noosctrue->Reset();
02587   neffic_noosctrue->Reset();
02588 
02589   TH1F all("all",  "",fnbins,fbinvect);
02590   //TH1F *allnc   = new TH1F("allnc","",fnbins,fbinvect);
02591 
02592   double  mc_weif=1.;
02593   double  shower = 0.;
02594   double  mumom = 0.;
02595   int     flavour = 0;
02596   double  true_enu = 0.;
02597 //   double  reco_ene = 0.;
02598 //   int    annpid_f1p1 = 0;
02599 //   int    cc_nc = 0;
02600 //   int    initial_state = 0;
02601 
02602 //   bool trcc = false;
02603 //   bool slcc = false;
02604 //   bool slnc = false;
02605 
02606   //size of the MC arrays
02607   int sizeTau =  0;
02608 
02609   //loop over bins in the beam
02610   //only true cc taus are included in the tau backgrounds as the nc are just nc events
02611   NCEnergyBin *bin = 0;
02612   int numBins = fBeams[beam].GetNumberEnergyBins(NCType::kCC);
02613   for(int i = 0; i < numBins; ++i){
02614     bin = fBeams[beam].GetEnergyBin(i, NCType::kCC);
02615     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02616 
02617     //true cc tau selected as cc
02618     for(int j = 0; j < sizeTau; ++j){
02619       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02620       all.Fill(true_enu,mc_weif);
02621       effic_noosctrue->Fill(true_enu,mc_weif);
02622     }//end loop over tau events
02623   }//end loop over cc energy bins
02624 
02625   numBins = fBeams[beam].GetNumberEnergyBins(NCType::kNC);
02626   for(int i = 0; i < numBins; ++i){
02627     bin = fBeams[beam].GetEnergyBin(i, NCType::kNC);
02628     sizeTau = bin->GetMCNuMuToNuTauVectorSize();
02629 
02630     //true cc tau selected as cc
02631     for(int j = 0; j < sizeTau; ++j){
02632       bin->GetMCNuMuToNuTauInformation(true_enu, shower, mumom, mc_weif, flavour, j);
02633       all.Fill(true_enu,mc_weif);
02634       neffic_noosctrue->Fill(true_enu,mc_weif);
02635     }//end loop over tau events
02636   }//end loop over cc energy bins
02637 
02638 //   for(uint i = 0; i < fweit.size(); i++){
02639 
02640 //     mc_weif       = fweit[i];
02641 //     mc_weif       = fweit[i];
02642 //     shower        = fesht[i];
02643 //     mumom         = femut[i];
02644 //     reco_ene      = shower+mumom;
02645 
02646 //     annpid_f1p1   = fpidpt[i];
02647 //     cc_nc         = fccfdt[i];
02648 //     flavour       = fflavfdt[i];
02649 //     initial_state = finist[i];
02650 //     true_enu      = fenutt[i];
02651 
02652 //     trcc = false;
02653 //     slcc = false;
02654 //     slnc = false;
02655 
02656 //     if(cc_nc == NCType::kCC && flavour == 16) trcc = true;
02657 //     if(annpid_f1p1 == NCType::kCC) slcc = true;
02658 //     if(annpid_f1p1 == NCType::kNC) slnc = true;
02659 
02660 //     if(trcc==true)                all->Fill(true_enu,mc_weif);
02661 //     if(slcc==true  && trcc==true) effic_noosctrue->Fill(true_enu,mc_weif);
02662 //     if(slnc==true  && trcc==true) neffic_noosctrue->Fill(true_enu,mc_weif);
02663 //   }
02664 
02665   MSG("NCExtrapolationNS", Msg::kInfo) << " DONE WITH EFFICIENCIES TAU " << endl;
02666   effic_noosctrue->Divide(&all);
02667   neffic_noosctrue->Divide(&all);
02668 
02669   return;
02670 }
02671 
02672 //***************************************************************
02673 
02674 //LLH Note: this only works if inihist has smaller bins that fit exactly inside of rebinhist bins
02675 void NCExtrapolationNS::Rebindata(TH1F *inihist,
02676                                   TH1F &rebinhist)
02677 {
02678   //loop over the output histogram and find the edges of the
02679   //bins.  loop over the input histogram and put all bins that land
02680   //within those edges into the output histogram
02681   double binCenter = 0.;
02682   double binContent = 0.;
02683   double outBinLow = 0.;
02684   double outBinHigh = 0.;
02685   double inBinLow = 0.;
02686   double inBinHigh = 0.;
02687   rebinhist.Reset();
02688   for(int i = 0; i < rebinhist.GetXaxis()->GetNbins(); ++i){
02689     outBinLow = rebinhist.GetXaxis()->GetBinLowEdge(i+1);
02690     outBinHigh = rebinhist.GetXaxis()->GetBinUpEdge(i+1);
02691     for(int j = 0; j < inihist->GetXaxis()->GetNbins(); ++j){
02692       binContent = inihist->GetBinContent(j+1);
02693       binCenter = inihist->GetBinCenter(j+1);
02694       inBinLow = inihist->GetXaxis()->GetBinLowEdge(j+1);
02695       inBinHigh = inihist->GetXaxis()->GetBinUpEdge(j+1);
02696 
02697       //if the input histogram bin is withint the output bin,
02698       //add the content to the output histogram
02699       if(binCenter >= outBinLow && binCenter <= outBinHigh){
02700         if(inBinLow >= outBinLow && inBinHigh <= outBinHigh){
02701           rebinhist.Fill(binCenter, binContent);
02702           MSG("NCExtrapolationNS", Msg::kDebug)
02703             << rebinhist.GetName() << " " << outBinLow
02704             << " " << outBinHigh << " " << inBinLow
02705             << " " << inBinHigh << " " << rebinhist.GetBinContent(i+1)
02706             << " " << binContent << endl;
02707         }
02708         else if(inBinLow >= outBinLow && inBinHigh >= outBinHigh)
02709           MSG("NCExtrapolationNS", Msg::kWarning) << "input bin goes over output bin high edge "
02710                                                   << binCenter << " "
02711                                                   << outBinLow << "/" << inBinLow << " "
02712                                                   << inBinHigh << "/" << outBinHigh << endl;
02713         else if(inBinLow <= outBinLow && inBinHigh <= outBinHigh)
02714           MSG("NCExtrapolationNS", Msg::kWarning) << "input bin goes under output bin low edge "
02715                                                   << binCenter << " "
02716                                                   << outBinLow << "/" << inBinLow << " "
02717                                                   << inBinHigh << "/" << outBinHigh << endl;
02718       }//end if input bin center is within output bin
02719 
02720     }//end loop over input histogram
02721   }//end loop over output histogram
02722 
02723   return;
02724 }
02725 
02726 //----------------------------------------------------------------------
02727 //Rebindata now works for any input histogram as it uses the information
02728 //from the rebinhist and inihist to determine how to combine the bins in
02729 //inihist
02730 void NCExtrapolationNS::Rebin(TH1F *inihist,
02731                               TH1F &rebinhist)
02732 {
02733   Rebindata(inihist, rebinhist);
02734 
02735 //   int kkk=1;
02736 
02737 //   for(int kk=1;kk<200;kk++){
02738 //     if(kk==3){
02739 //       double reco2 =inihist->GetBinContent(kk)+inihist ->GetBinContent(kk-1)+inihist ->GetBinContent(kk-2);
02740 //       rebinhist.SetBinContent(kkk,reco2);
02741 //     }
02742 //     if(kk>3 && kk<=39 && kk%2==1){
02743 //       kkk=kkk+1;
02744 //       double reco2 =inihist->GetBinContent(kk)+inihist ->GetBinContent(kk-1);
02745 //       rebinhist.SetBinContent(kkk,reco2);
02746 //     }
02747 //     if(kk>39 && kk%4==3 && kk<=119){ // 75
02748 //       kkk=kkk+1;
02749 //       double reco2 =inihist->GetBinContent(kk)+inihist ->GetBinContent(kk-1)+inihist->GetBinContent(kk-2)+inihist ->GetBinContent(kk-3);
02750 //       rebinhist.SetBinContent(kkk,reco2);
02751 //     }
02752 
02753 //   }
02754 
02755   return;
02756 }
02757 
02758 //----------------------------------------------------------------------
02759 float NCExtrapolationNS::chisq(TH1F *obs,
02760                                TH1F *exp,
02761                                TH1F *expnc)
02762 {
02763 
02764   // calculate chisq between 2 histograms - use Poisson form
02765 
02766   float chisquare=0;
02767   float nobs = 0.;
02768   float nexp = 0.;
02769   float nexpnc = 0.;
02770 
02771   for(int i = 0; i < obs->GetNbinsX(); i++){
02772     nobs = obs->GetBinContent(i+1);
02773     nexp = exp->GetBinContent(i+1);
02774 
02775     nexpnc = expnc->GetBinContent(i+1);
02776 
02777     if(nexp > 0){
02778       // LOG LIKELIHOOD
02779       if(nobs > 0)  chisquare += 2*(nexp-nobs)+2*nobs*log(nobs/nexp);
02780       if(nobs == 0) chisquare += 2*(nexp-nobs);
02781     }
02782 
02783 // Temp - LLH
02784 //     MSG("NCExtrapolationNS", Msg::kInfo) << "find chi^2 " << obs->GetBinCenter(i+1)
02785 //                                       << " " << nobs << " / " << nexp
02786 //                                       << " " << nexp/nobs << " "
02787 //                                       << " " << chisquare << endl;
02788   }
02789 
02790   return chisquare;
02791 }
02792 
02793 //----------------------------------------------------------------------
02794 void NCExtrapolationNS::truetoreco(TH1F *truth,
02795                                    TH1F& reco,
02796                                    TH2F *matrix)
02797 {
02798   //Adding this back (got lost in translation for some reason) - LLH
02799    reco.Reset();
02800 
02801   // use matrix to convert from true to reco
02802   int nbins = truth->GetNbinsX();
02803 
02804   float count = 0.;
02805   for(int i = 0; i < nbins; i++){
02806     count = 0;
02807     for(int j = 0; j < nbins; j++){
02808       count += truth->GetBinContent(j+1)*matrix->GetBinContent(j+1, i+1);
02809     }
02810     reco.SetBinContent(i+1,count);
02811 
02812 // Temp
02813 //     MAXMSG("NCExtrapolationNS", Msg::kInfo, nbins) << "far true to reco "
02814 //                                                 << ":  bin_center = " << reco.GetBinCenter(i+1) << " "
02815 //                                                 << ", reco = " << count << ", truth = " << truth->GetBinContent(i+1)
02816 //                                                 << endl;
02817   }
02818 
02819   return;
02820 }
02821 
02822 //----------------------------------------------------------------------
02823 //this method uses the value of sin^2(1.27Delta m^2 L/E) appropriate for each bin
02824 //in the provided vector.
02825 void NCExtrapolationNS::oscweight(std::vector<double> &sinSqrDeltaMSqr,
02826                                   double sinSqr2Theta, double fSterile,
02827                                   TH1F *noosc, TH1F *osc, int flavor,
02828                                   int interactionType)
02829 {
02830   double prob = 1.;
02831   double noOscVal = 0.;
02832 
02833   for(uint i = 0; i < sinSqrDeltaMSqr.size(); ++i){
02834     noOscVal = noosc->GetBinContent(i+1);
02835 
02836     if(TMath::Abs(flavor) == 14){
02837       if(interactionType == NCType::kCC)
02838         prob = 1. - sinSqr2Theta*sinSqrDeltaMSqr[i];
02839       else if(interactionType == NCType::kNC)
02840         prob = 1. - sinSqr2Theta*fSterile*sinSqrDeltaMSqr[i];
02841     }
02842     else if(TMath::Abs(flavor) == 16){
02843       if(interactionType == NCType::kCC)
02844         prob = (1. - fSterile)*sinSqr2Theta*sinSqrDeltaMSqr[i];
02845       else if(interactionType == NCType::kNC)
02846         prob = 0.;
02847     }
02848 
02849     osc->SetBinContent(i+1, prob*noOscVal);
02850   }//end loop over energy bins
02851 
02852   return;
02853 }
02854 
02855 //----------------------------------------------------------------------
02856 void NCExtrapolationNS::oscweightOld(TH1F *noosc,
02857                                      TH1F &osc,
02858                                      float dm,
02859                                      float st,
02860                                      int method,
02861                                      int what)
02862 {
02863 
02864   // what = 1 cc disappearance
02865   // what = 2 nc disappearance
02866   // what = 11 taus appearance
02867   // what = -1 decay
02868 
02869   // reweight spectrum with oscillation parameters
02870   // interpolation method:  0 - none,  1 - quadratic,  anything else - linear
02871   //osc.Reset();
02872   float distance = NCType::kBaseLineFar;
02873 
02874   int nbins = noosc->GetNbinsX();
02875   float prob = 0.;
02876   float binsize = 0.25;
02877   float wgt1 = 0.;
02878   float wgt2 = 0.;
02879   float wgt3 = 0.;
02880   float de = 0.;
02881   float de2 = 0.;
02882 
02883   float avgosc = 0.;
02884   float avgosc2 = 0.;
02885   float avgwgt1 = 0.;
02886   float avgwgt2 = 0.;
02887   float enu = 0.;
02888   float delta = 0.;
02889   float linwgt = 0.;
02890   float quadwgt = 0.;
02891 
02892   for (int i = 0; i < nbins; i++) {
02893 
02894     if(what == 1 || what == 0)
02895       prob = 1-st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/noosc->GetBinCenter(i+1)),2);
02896     if(what == 11)
02897       prob = st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/noosc->GetBinCenter(i+1)),2);
02898     if(what == -1)
02899       prob=TMath::Power(st+(1-st)*TMath::Exp(-dm*distance/(2*noosc->GetBinCenter(i+1))),2);
02900     if(what == -11)
02901       prob = 0.;
02902 
02903     binsize = 0.25;
02904     if (i == 0)    binsize = 0.5;
02905     if (i == 199)  binsize = 200.-49.75;
02906 
02907     if(i > nbins-2 || method == 0){
02908       osc.SetBinContent(i+1, noosc->GetBinContent(i+1)*prob);
02909     }
02910     else{
02911 
02912       wgt1 = noosc->GetBinContent(i+1);
02913       wgt2 = noosc->GetBinContent(i+2);
02914       wgt3 = noosc->GetBinContent(i+3);
02915 
02916       if(i == 0){
02917         wgt2 = noosc->GetBinContent(i+2)+noosc->GetBinContent(i+3);
02918         wgt3 = noosc->GetBinContent(i+4)+noosc->GetBinContent(i+5);
02919 
02920       }
02921 
02922       de = wgt2-wgt1;
02923       de2 = wgt3-2*wgt2+wgt1;
02924 
02925       avgosc = 0.;
02926       avgosc2 = 0.;
02927       avgwgt1 = 0.;
02928       avgwgt2 = 0.;
02929 
02930       for(int k = 0; k < 10; k++){
02931 
02932         enu = noosc->GetBinLowEdge(i+1)+k*binsize/10.+binsize/20.;
02933 
02934         delta = enu-noosc->GetBinCenter(i+1);
02935 
02936         linwgt = wgt1+de*delta;
02937         quadwgt = wgt1+de*delta+de2/2*TMath::Power(delta,2);
02938 
02939         if(linwgt < 0) linwgt = 0.;
02940         if(quadwgt < 0) quadwgt = 0.;
02941 
02942         if(what == 1 || what == 0){
02943           avgosc += quadwgt*(1-st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/enu),2));
02944           avgosc2 += linwgt*(1-st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/enu),2));
02945         }
02946         if(what==11){
02947           avgosc += quadwgt*(st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/enu),2));
02948           avgosc2 += linwgt*(st*TMath::Power(TMath::Sin(NCType::k127*dm*distance/enu),2));
02949         }
02950         if(what==-1){
02951           avgosc += quadwgt*TMath::Power(st+(1-st)*exp(-dm*distance/(2*enu)),2);
02952           avgosc2 += linwgt*TMath::Power(st+(1-st)*exp(-dm*distance/(2*enu)),2);
02953         }
02954         if(what==-11){
02955           avgosc += quadwgt*0.;
02956           avgosc2 += linwgt*0.;
02957         }
02958         avgwgt1 += quadwgt;
02959         avgwgt2 += linwgt;
02960       }
02961 
02962       if(method == 1 && avgosc > 0){
02963         if(avgwgt1 > 0) avgosc /= avgwgt1;
02964         else{
02965           MSG("NCExtrapolationNS", Msg::kWarning) << " attempted divide by 0 to get average "
02966                                                   << " oscillation for bin -  not averaged "
02967                                                   << avgosc << " " << avgwgt1 << " "
02968                                                   << osc.GetBinCenter(i+1) << " "
02969                                                   << noosc->GetBinContent(i+1) << endl;
02970         }
02971         osc.SetBinContent(i+1, avgosc*noosc->GetBinContent(i+1));
02972       }
02973       else if(avgwgt2 > 0){
02974         if(avgwgt2 > 0) avgosc2 /= avgwgt2;
02975         else{
02976           MSG("NCExtrapolationNS", Msg::kWarning) << " attempted divide by 0 to get average "
02977                                                   << " oscillation for bin -  not averaged "
02978                                                   << avgosc2 << " " << avgwgt2 << endl;
02979         }
02980         osc.SetBinContent(i+1, avgosc2*noosc->GetBinContent(i+1));
02981       }
02982 
02983     }
02984   }//end loop over energy bins
02985 
02986   if((st == 0 || dm == 0) && (what == 1 || what == 0)){
02987     for(int i = 0; i < nbins; i++) osc.SetBinContent(i+1,noosc->GetBinContent(i+1));
02988   }
02989 
02990   if((st == 0 || dm == 0) && (what == 11 || what == -11)){
02991     for(int i = 0; i < nbins; i++) osc.SetBinContent(i+1,0.);
02992   }
02993 
02994   if(dm == 0 && what == -1){
02995     for(int i = 0; i < nbins; i++) osc.SetBinContent(i+1,noosc->GetBinContent(i+1));
02996   }
02997 
02998   return;
02999 }
03000 
03001 //----------------------------------------------------------------------
03002 //matricies are either reco to true or true to reco.  if reco to true,
03003 //the x axis is reconstructed energy, the y axis is true energy.
03004 //if true to reco, the x axis is true energy, the y axis is reco'ed
03005 //energy
03006 void NCExtrapolationNS::Convert2DMatrixToProbability(TH2F *matrix)
03007 {
03008   double sum[fnbins];
03009   double maxf[fnbins][fnbins];
03010 
03011   //initialize
03012   for(int j = 0; j < fnbins; j++){
03013     sum[j] = 0.;
03014     for(int i = 0; i < fnbins; i++) maxf[i][j] = 0.;
03015   }
03016 
03017   //Make True to reco or reco to true
03018   //this step takes the y bins and puts them in the
03019   //x bins and vice versa
03020   for(int i = 0; i < fnbins; i++){
03021     for(int j = 0; j < fnbins; j++){
03022       maxf[j][i] = matrix->GetBinContent(i+1, j+1);
03023     }
03024   }
03025 
03026   // MAKE 2D MATRIX A PROBABILITY ONE
03027 //LLH in here twice  for(int i = 0; i < fnbins; i++) sum[i] = 0.;
03028 
03029   //sum over the new x bins for each y value
03030   for(int j = 0; j < fnbins; j++){
03031     for(int i = 0; i < fnbins; i++){
03032       sum[j] += maxf[i][j];
03033     }
03034   }
03035 
03036   //each x,y bin is the fraction of total for
03037   //given row in y.  set bin content in matrix to
03038   //be true,reco (reco,true)
03039   for(int i = 0; i < fnbins; i++){
03040     for(int j = 0; j < fnbins; j++){
03041       if(sum[j] > 0.){
03042         maxf[i][j] /= sum[j];
03043         matrix->SetBinContent(j+1, i+1, maxf[i][j]);
03044       }
03045     }
03046   }
03047 
03048   return;
03049 }
03050 
03051 //----------------------------------------------------------------------
03052 void NCExtrapolationNS::FillDataHistogramFromEnergyBins(int beam,
03053                                                         int nccc,
03054                                                         TH1F *hist)
03055 {
03056   hist->Reset();
03057 
03058   NCEnergyBin *bin = 0;
03059   int sizeData = 0;
03060   int numBins = fBeams[beam].GetNumberEnergyBins(nccc);
03061   double energy = 0.;
03062   double weight = 0.;
03063   for(int i = 0; i < numBins; ++i){
03064     bin = fBeams[beam].GetEnergyBin(i, nccc);
03065     sizeData = bin->GetDataVectorSize();
03066     for(int j = 0; j < sizeData; ++j){
03067       bin->GetDataInformation(energy, weight, j);
03068       hist->Fill(energy, weight);
03069     }
03070   }//end loop over bins
03071 
03072   return;
03073 }

Generated on Mon Jun 16 14:57:50 2008 for loon by  doxygen 1.3.9.1