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

Zbeam.cxx

Go to the documentation of this file.
00001 // $Author: med $ 
00002 //
00003 // $Revision: 1.29 $
00004 // 
00005 // $Name:  $
00006 //
00007 // $Id: Zbeam.cxx,v 1.29 2009/10/31 20:42:08 med Exp $
00008 //
00009 // $Log: Zbeam.cxx,v $
00010 // Revision 1.29  2009/10/31 20:42:08  med
00011 // Change kInfo to kFatal for non-existant beamsys_XYZ root file and change check to IsZombie()
00012 //
00013 // Revision 1.28  2008/02/22 16:31:08  rearmstr
00014 // Added an option in BeamSys.h, Zbeam, and SKZPWeightCalculator.h
00015 // to get the total error from focusing.
00016 //
00017 // Revision 1.27  2007/11/11 07:22:47  rhatcher
00018 // Part of the ongoing purge of "DetectorType" in favor of "Detector"
00019 // (the former is a synonym of the latter ...)
00020 //
00021 // Revision 1.26  2007/06/06 15:14:45  vahle
00022 // Make Zbeam::SetReweightConfig return the user to the TDirectory they started in
00023 //
00024 // Revision 1.25  2007/05/13 13:47:21  vahle
00025 // Adding new histogram files for Zbeam systematics.  Old hist file only worked with bleeding edge root (TDirectoryFiles are written in the new root instead of TDirectories).  Updated Zbeam to read in 4 separate files instead of 1 file with 4 directories.  Added different run periods to SKZPWeightCalculator to apply different weights depending on supplied run period.  Default is runI which does the same thing as it always used to.  If RunI is specified, weights are applied to ME and HE to correct for beam spot size effects.  If RunII is specified, LE010 beam is weighted to le9 beam.  Also supplied a function to supply a total weight which is the exposure weighted average of the two run periods.  Finally, the macro TestSKZPWeight has been updated to demonstrate the use of all these new features.
00026 //
00027 // Revision 1.24  2007/05/03 16:35:05  zarko
00028 // Added nue hadprod error.
00029 //
00030 // Revision 1.23  2007/04/28 19:46:12  zarko
00031 // *** empty log message ***
00032 //
00033 // Revision 1.22  2007/04/26 16:23:41  zarko
00034 // *** empty log message ***
00035 //
00036 // Revision 1.21  2007/04/24 21:54:17  zarko
00037 // Added BeamSys enumerator. Made Zbeam read from one root file
00038 // instead of infinite amount of .vec files.
00039 //
00040 // Revision 1.20  2007/04/14 03:32:59  zarko
00041 // Updated Zbeam to include target z correction for le10 (effect=15) and added the
00042 // PiMinus_CedarDaikon hadron production errors.
00043 // note: The target z correction takes -1 to give the weights for the le9 beam
00044 //
00045 // Revision 1.19  2007/03/16 17:05:15  vahle
00046 // Added PiMinus weighting code to SKZPWeightCalculator
00047 //
00048 // Revision 1.18  2007/03/04 07:06:49  zarko
00049 // Updated code to return latest hadron production error envelope.
00050 //
00051 // Revision 1.17  2007/01/10 16:02:15  zarko
00052 // Renamed "PiMinus" config into "Boston".
00053 //
00054 // Revision 1.16  2007/01/05 23:21:49  zarko
00055 // Added hadron production error envelope for Boston fit (this includes pi-).
00056 // Use SetReweightConfig(config) to set either PRL or Boston fit
00057 // hadron production error envelope (config="PRL" or "PiMinus")
00058 //
00059 // Revision 1.15  2006/10/03 16:00:56  zarko
00060 // Updated code to reweight numus and numubars
00061 //
00062 // Revision 1.14  2006/09/20 21:18:51  zarko
00063 // Added le150z200i beam.
00064 //
00065 // Revision 1.13  2006/06/03 03:42:33  zarko
00066 // *** empty log message ***
00067 //
00068 // Revision 1.12  2006/05/27 15:40:10  zarko
00069 // Added horn off beam.
00070 //
00071 // Revision 1.11  2006/04/15 04:28:18  zarko
00072 //
00073 // Last commit contained one unintendend change, so this is correcting for that.
00074 //
00075 // Revision 1.10  2006/04/14 02:36:24  zarko
00076 // Zbeam now looks into SRT_PUBLIC_CONTEXT if the data cannot be found in SRT_PRIVATE_CONTEXT
00077 //
00078 // Revision 1.9  2006/02/28 05:50:58  zarko
00079 // Calling GetWeight with nEffect=10 or nEffect=11 will now return the total
00080 // error (all effects summed in quadrature). nEffect=10 includes hadron production
00081 // error before hadron reweighting and nEffect=11 includes hadron production
00082 // error after reweighting.
00083 //
00084 // Revision 1.8  2006/02/26 15:35:59  zarko
00085 // Added hadron productin error after reweighting the MC as nEffect==8.
00086 // Added le010z170i and le010z200i beams as iBeam==6 and iBeam==7.
00087 //
00088 // Revision 1.7  2006/02/22 01:22:46  zarko
00089 // Added the reweighting for low intensity LE10 (Ibeam=5)
00090 // During this run beam was narrower than in current MC,
00091 // so as for ME and HE setting ns=0 for beamwidth will
00092 // reweight to narrower beam.
00093 //
00094 // Revision 1.6  2006/01/30 20:53:52  zarko
00095 // Updated beam width error vectors for ME and HE
00096 // Put back hadron production error as nEffect=7
00097 //
00098 // Revision 1.5  2006/01/29 00:44:26  zarko
00099 // Added beam width effect for ME and HE.
00100 // Removed hadron production from Zbeam.
00101 //
00102 // Revision 1.4  2006/01/08 10:05:13  gmieg
00103 // "#include <cassert>" added so it will compile.
00104 //
00105 // Revision 1.3  2005/12/28 19:34:33  brebel
00106 // set the "Zbeam reading files from: " message to kDebug level and set the
00107 // stream to Zbeam
00108 //
00109 // Revision 1.2  2005/12/18 17:45:40  kordosky
00110 // reacting to instructions from zarko, modify weight function to return 1+deviation (e.g. a weight).
00111 //
00112 // Revision 1.1  2005/12/18 16:36:21  kordosky
00113 // Z. Pavlovic zbeam code added . see macros/test_zbeam.C
00114 //
00115 
00116 #include "MCReweight/Zbeam.h"
00117 #include "MessageService/MsgService.h"
00118 #include "Conventions/Detector.h"
00119 #include "Conventions/BeamType.h"
00120 #include "MCReweight/BeamSys.h"
00121 
00122 #include <fstream>
00123 #include <string>
00124 #include <iostream>
00125 #include <cmath>
00126 #include <cstdlib>
00127 #include <cassert>
00128 
00129 #include "TSystem.h"
00130 #include "TFile.h"
00131 #include "TH1D.h"
00132 
00133 ClassImp(Zbeam)
00134   
00135 CVSID("$Id: Zbeam.cxx,v 1.29 2009/10/31 20:42:08 med Exp $");
00136 
00137 Zbeam::Zbeam(const char* dir)
00138 {
00139   // fTopDir is where the beamsys_hists.root is
00140   fTopDir=dir; // user may set location of input data
00141   if(fTopDir=="") { // by default, this code looks in a standard place
00142     fTopDir="MCReweight/data";
00143     std::string base="";
00144     base=getenv("SRT_PRIVATE_CONTEXT");
00145     if (base!="" && base!=".")
00146       {
00147         // check if directory exists in SRT_PRIVATE_CONTEXT
00148         std::string path = base + "/" + fTopDir;
00149         void *dir_ptr = gSystem -> OpenDirectory(path.c_str());
00150         if(!dir_ptr) base=getenv("SRT_PUBLIC_CONTEXT"); // if it doesn't exist then use SRT_PUBLIC_CONTEXT
00151       }
00152     else base=getenv("SRT_PUBLIC_CONTEXT");
00153     
00154     if(base=="") {
00155       MSG("MCReweight",Msg::kFatal)<<"No SRT_PUBLIC_CONTEXT set"<<std::endl;
00156       assert(false);
00157     }
00158     fTopDir = base+ "/" + fTopDir;
00159   }
00160   MSG("Zbeam",Msg::kDebug) <<"Zbeam getting hists from: "<<fTopDir<<"/beamsys_hists.root"<<std::endl;
00161    
00162   SetReweightConfig("PRL"); //default config
00163 }
00164 
00165 Zbeam::~Zbeam() {
00166 
00167 }
00168 
00169 void Zbeam::SetReweightConfig(std::string cnf)
00170 {
00171   if ( cnf == fCurrentConfig) return;
00172   TDirectory *save = gDirectory;
00173 
00174   fBeamSysFile = new TFile(Form("%s/beamsys_%s.root",fTopDir.c_str(),cnf.c_str()));
00175   if (fBeamSysFile->IsZombie()) {
00176     MSG("Zbeam",Msg::kFatal)<<"Don't recognize "
00177                             <<cnf
00178                             <<" configuration."
00179                             <<" Config set to :"
00180                             <<fCurrentConfig<<endl;
00181     
00182     return;
00183   }
00184   
00185   fBeamSysMap.clear();
00186   fCurrentConfig = cnf;
00187   std::string nus[]={"NuMu","NuMuBar","NuE","NuEBar"};
00188   int ntype[]    ={    56,       55  , 53  , 52};
00189   for (int inu=0;inu<4;inu++) {
00190     for (int eff=BeamSys::kFirst;eff<BeamSys::kEndOfList;eff++) {
00191       for (int beam=BeamType::kUnknown;beam<BeamType::kEndOfList;beam++) {
00192         for (int det=Detector::kNear;det<=Detector::kFar;det++) {
00193           std::string hname=nus[inu]+"_"+
00194             BeamSys::AsString(BeamSys::BeamSys_t(eff))+"_"+
00195             BeamType::AsString(BeamType::BeamType_t(beam))+"_"+
00196             Detector::AsString(Detector::Detector_t(det));
00197           TH1D* hist=dynamic_cast<TH1D*> (fBeamSysFile->Get(hname.c_str()));
00198           //      cout<<eff<<" hname is "<<hname<<endl;
00199           if (hist) FillVector(hist,ntype[inu],eff,beam,det);
00200         }
00201         //for far/near error bar internally use Detector::kUnknown
00202         std::string hname=nus[inu]+"_"+
00203           BeamSys::AsString(BeamSys::BeamSys_t(eff))+"_"+
00204           BeamType::AsString(BeamType::BeamType_t(beam))+"_FN";
00205         TH1D* hist=dynamic_cast<TH1D*> (fBeamSysFile->Get(hname.c_str()));
00206         if (hist) FillVector(hist,ntype[inu],eff,beam,Detector::kUnknown);
00207       }
00208     }
00209   }
00210   
00211   fBeamSysFile->Close();
00212   delete fBeamSysFile;
00213   fBeamSysFile = 0;
00214   save->cd();
00215 }
00216 
00217 double Zbeam::GetWeight(ZbeamData_t zbmdata, BeamSys::BeamSys_t sys, double nSigma)
00218 {
00219   int ntype  = zbmdata.ntype;
00220   double Enu = zbmdata.true_enu;
00221   Detector::Detector_t det = zbmdata.detector;
00222   BeamType::BeamType_t beam = zbmdata.beam;
00223 
00224   int bin;
00225   bin=int(2*Enu);
00226   if (bin<0) bin=0;
00227   if (bin>99) bin=99;
00228 
00229   if (ntype == 14) ntype = 56;
00230   else if (ntype == -14) ntype = 55;
00231   else if (ntype == 12) ntype = 53;
00232   else if (ntype == -12) ntype = 52;
00233 
00234   double weight = 1. ;
00235   double totError=0.;
00236   BeamSys::BeamSys_t first=sys; 
00237   BeamSys::BeamSys_t last=sys;
00238   if (sys == BeamSys::kTotalBefore || sys == BeamSys::kTotalAfter) //Total Error
00239     {
00240       first = BeamSys::kFirst;
00241       last = BeamSys::kEndOfList; 
00242     }
00243   else if(sys == BeamSys::kTotalFocusing)
00244     {
00245       first = BeamSys::kFirst;
00246       last = BeamSys::kHornIDist;
00247     } 
00248 
00249   std::map<int, DetMap_t>::iterator bsmit = fBeamSysMap.find(ntype);   
00250   DetMap_t::iterator dmit;
00251   BeamMap_t::iterator bmit;
00252   bool beam_it_exists = true;
00253   if (bsmit == fBeamSysMap.end()) {
00254     weight = 0.; 
00255     beam_it_exists = false;
00256   } else {
00257     dmit = (bsmit->second).find(det);
00258     if (dmit == (bsmit->second).end() ) {
00259       weight = 0.; 
00260       beam_it_exists = false;
00261     } else {
00262       bmit = (dmit->second).find(beam);
00263       if (bmit == (dmit->second).end() ) {
00264         weight = 0.; 
00265         beam_it_exists = false;
00266       } 
00267     }
00268   }
00269 
00270   if (beam_it_exists) {
00271     for (int eff=first;eff<=last;eff++) {    
00272       if (BeamSys::EBeamSys(eff) != BeamSys::kTotalBefore && 
00273           BeamSys::EBeamSys(eff) != BeamSys::kTotalAfter &&
00274           BeamSys::EBeamSys(eff) != BeamSys::kHadProdBefore && 
00275           BeamSys::EBeamSys(eff) != BeamSys::kHadProdAfter &&
00276           BeamSys::EBeamSys(eff) != BeamSys::kTotalFocusing )
00277         {
00278           EffMap_t::iterator effit = (bmit->second).find(eff);
00279           weight =  ( effit == (bmit->second).end() ) ?  0. : ((*effit).second)[bin];
00280           totError += weight*weight;
00281         }
00282     } // loop over all effects 
00283     
00284     if ((sys == BeamSys::kTotalBefore || sys == BeamSys::kHadProdBefore )) {
00285       EffMap_t::iterator effit = (bmit->second).find(BeamSys::kHadProdBefore);
00286       weight =  ( effit == (bmit->second).end() ) ?  0. : ((*effit).second)[bin];
00287       totError += weight*weight;
00288     }
00289     if ((sys == BeamSys::kTotalAfter || sys == BeamSys::kHadProdAfter )) {
00290       EffMap_t::iterator effit = (bmit->second).find(BeamSys::kHadProdAfter);
00291       weight =  ( effit == (bmit->second).end() ) ?  0. : ((*effit).second)[bin];
00292       totError += weight*weight;
00293     }
00294   }
00295 
00296   totError=sqrt(totError);
00297   
00298   if (!(sys == BeamSys::kTotalBefore || sys == BeamSys::kTotalAfter 
00299         || sys == BeamSys::kTotalFocusing))  totError *= ( (weight>0.) - (weight<0.) );
00300   
00301   double nS = (sys == BeamSys::kBeamWidth) ? nSigma + 1. : nSigma; // by default reweights pME, pHE and lowint 
00302 
00303   //some of the effects are symmetric and only abs(nS) makes sense
00304   totError = ((sys == BeamSys::kHorn1Offset) ||
00305               (sys == BeamSys::kBaffleScraping)) ? totError*fabs(nS)+1.0 :  totError*nS+1.; 
00306  
00307   return totError;  
00308 } 
00309 
00310 double Zbeam::GetWeight(ZbeamData_t zbmdata,std::map<BeamSys::BeamSys_t, double> nSigma)
00311 { 
00312   double wgh=1.;
00313   std::map<BeamSys::BeamSys_t, double>::const_iterator bsit;
00314 
00315   for (bsit=nSigma.begin();bsit != nSigma.end() ;bsit++)
00316     {
00317       wgh*=GetWeight(zbmdata,bsit->first, bsit->second); 
00318     }
00319   return wgh; 
00320 } 
00321 
00322 double Zbeam::GetWeight(int iDet, int iBeam, int nEffect,  
00323                         double nSigma, int ntype, double Enu) 
00324 {
00325   ZbeamData_t zbm;
00326   zbm.beam = BeamType::FromZarko(iBeam);
00327   zbm.detector = (iDet != 3) ?  Detector::EDetector(iDet) : Detector::kUnknown;
00328   zbm.ntype = ntype;
00329   zbm.true_enu = Enu; 
00330   BeamSys::BeamSys_t sys = BeamSys::EBeamSys(nEffect);
00331 
00332   return GetWeight(zbm,sys, nSigma);
00333 }
00334 
00335 double Zbeam::GetWeight(int iDet, int iBeam,  
00336                         std::vector<double> nSigma, int ntype, double Enu) 
00337 {
00338   ZbeamData_t zbm;
00339   zbm.beam = BeamType::FromZarko(iBeam);
00340   zbm.detector = (iDet != 3) ?  Detector::EDetector(iDet) : Detector::kUnknown;
00341   zbm.ntype = ntype;
00342   zbm.true_enu = Enu;
00343 
00344   std::map<BeamSys::BeamSys_t, double> sgm;
00345   for (unsigned int nEffect=0; nEffect<nSigma.size();nEffect++)
00346     {
00347       BeamSys::BeamSys_t sys = BeamSys::EBeamSys(nEffect+1);
00348       const std::pair<BeamSys::BeamSys_t, double> sp(sys, nSigma[nEffect]);
00349       sgm.insert(sp);
00350     }
00351   return GetWeight(zbm, sgm);
00352 }
00353 
00354 void Zbeam::MakeHistogram(std::string name, std::vector<double> vec)
00355 {
00356   //Function creates a histogram using a vector vec (assumes 0.5GeV bins)
00357   //Check if the name contains the detector, beam and effect
00358   //This doesn't guarantee that the name is correct. 
00359   //The name should look like this NuMu_Horn1Offset_L010z185i_Near ...
00360   bool validname=false;
00361   std::string numu="NuMu";
00362   std::string numubar="NuMuBar";
00363   std::string nue="NuE";
00364   std::string nuebar="NuEBar";
00365   
00366   for (int det=Detector::kNear;det<=Detector::kFar;det++) {
00367     for (int beam=BeamType::kUnknown;beam<BeamType::kEndOfList;beam++) {
00368       for (int eff=BeamSys::kFirst;eff<BeamSys::kEndOfList;eff++) {
00369         if (((name.find(Detector::AsString(Detector::Detector_t(det))) != string::npos)||name.find("FN") != string::npos) &&
00370             (name.find(BeamType::AsString(BeamType::BeamType_t(beam))) != string::npos) &&
00371             (name.find(BeamSys::AsString(BeamSys::BeamSys_t(eff))) != string::npos) &&
00372             ((name.find(numu) != string::npos) || (name.find(numubar) != string::npos) ||
00373              (name.find(nue) != string::npos) || (name.find(nuebar) != string::npos))) {
00374           validname=true;
00375         } 
00376       }
00377     }
00378   }
00379   if (!validname) {
00380     cout<<"Invalid histogram name! "<<name <<endl;
00381     cout<<"Name should include beam (as in BeamType), detector (as in Detector)" 
00382         <<" and beam systematics type"<<endl;
00383     return;
00384   }
00385   cout<<"Creating "<<name<<" histogram"<<endl;
00386   unsigned int nbins=vec.size()-1;
00387   double maxx=0.5*nbins;
00388 
00389   TH1D* hist=new TH1D(name.c_str(),name.c_str(),nbins,0.,maxx);
00390   for (unsigned int i=1;i<nbins+1;i++)
00391     hist->SetBinContent(i,vec[i-1]);
00392   
00393   fHistMap[name]=hist;
00394 }
00395 
00396 void Zbeam::SaveHistogram(std::string name, std::string config, std::string filename)
00397 {
00398   map<std::string, TH1D* >::iterator it = fHistMap.find(name);
00399   if ( it == fHistMap.end() ) {
00400     cout << name <<" does not exist!"<<endl;
00401     return;
00402   }
00403   TFile* f=new TFile(filename.c_str(),"UPDATE");
00404   if (!f->GetListOfKeys()->Contains(config.c_str())) {
00405     cout << "Creating directory for " << config << " configuration" << endl;
00406     f->mkdir(config.c_str());
00407   }
00408   f->cd(config.c_str());
00409   fHistMap[name]->Write();
00410   f->Close();
00411   delete f;
00412   f=0;
00413 }
00414 
00415 void Zbeam::DeleteHistogram(std::string name, std::string config, std::string filename)
00416 {
00417   TFile* f=new TFile(filename.c_str(),"UPDATE");
00418   f->cd(config.c_str());
00419   f->rmdir(name.c_str());
00420   f->Close();
00421   delete f;
00422   f=0;
00423 }
00424 
00425 void Zbeam::FillVector(TH1D* hist, int ntype, int eff, int beam, int det)
00426 {
00427   EffMap_t map1;
00428   BeamMap_t map2;
00429   DetMap_t map3;
00430 
00431   const int NBINS=hist->GetNbinsX();
00432   double* vec=new double[NBINS];
00433 
00434   for (int i=1; i<hist->GetNbinsX()+1;i++) vec[i-1]=hist->GetBinContent(i);
00435 
00436   map1.insert(EffMap_t::value_type(eff, vec));
00437   map2.insert(BeamMap_t::value_type(beam, map1));
00438   map3.insert(DetMap_t::value_type(det, map2));
00439 
00440   std::map<int, DetMap_t>::iterator bsmit = fBeamSysMap.find(ntype);
00441 
00442   if (bsmit == fBeamSysMap.end()) {
00443     fBeamSysMap.insert(std::map<int, DetMap_t>::value_type(ntype, map3));
00444   } else {
00445     DetMap_t::iterator dmit = (bsmit->second).find(det);
00446     if (dmit == (bsmit->second).end() ) {
00447       ((*bsmit).second).insert(DetMap_t::value_type(det,map2));
00448     } else {
00449       BeamMap_t::iterator bmit = (dmit->second).find(beam);
00450       if (bmit == (dmit->second).end() ) {
00451         ((*dmit).second).insert(BeamMap_t::value_type(beam,map1));
00452       } else {
00453         EffMap_t::iterator effit = (bmit->second).find(eff);
00454         if (effit == (bmit->second).end() ) {
00455           ((*bmit).second).insert(EffMap_t::value_type(eff,vec));
00456         } else {
00457           cout << "Already loaded vector for: " 
00458                << "beam "<<beam<< " det "<<det<<" nu "<<ntype<<" eff "<<eff<<endl;
00459           return;
00460         }
00461       }
00462     }
00463   } 
00464 }

Generated on Mon Feb 15 11:07:51 2010 for loon by  doxygen 1.3.9.1