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

NuZBeamReweight.cxx

Go to the documentation of this file.
00001 
00002 
00003 // Coded by Jeff Hartnell July/2006       
00004 //                                                                    
00005 // Contact: j.j.hartnell@rl.ac.uk
00007 
00008 #include "TClonesArray.h"
00009 #include "TDirectory.h"
00010 
00011 #include "Conventions/BeamType.h"
00012 #include "MessageService/MsgService.h"
00013 #include "MCNtuple/NtpMCTruth.h"
00014 #include "CandNtupleSR/NtpSREvent.h"
00015 #include "TruthHelperNtuple/NtpTHEvent.h"
00016 #include "StandardNtuple/NtpStRecord.h"
00017 #include "Conventions/ReleaseType.h"
00018 
00019 #include "MCReweight/MCReweight.h"
00020 #include "MCReweight/NeugenWeightCalculator.h"
00021 #include "MCReweight/ReweightHelpers.h"
00022 #include "MCReweight/SKZPWeightCalculator.h"
00023 #include "MCReweight/Zbeam.h"
00024 #include "MCReweight/Zfluk.h"
00025 
00026 #include "NtupleUtils/NuUtilities.h"
00027 #include "NtupleUtils/NuEvent.h"
00028 #include "NtupleUtils/NuZBeamReweight.h"
00029 
00030 CVSID("$Id: NuZBeamReweight.cxx,v 1.32 2010/02/09 00:06:46 ahimmel Exp $");
00031 
00032 //......................................................................
00033 
00034 NuZBeamReweight::NuZBeamReweight()
00035 {
00036   MSG("NuZBeamReweight",Msg::kDebug)
00037     <<"Running NuZBeamReweight Constructor..."<<endl;
00038 
00039 
00040   MSG("NuZBeamReweight",Msg::kDebug)
00041     <<"Finished NuZBeamReweight Constructor"<<endl;
00042 }
00043 
00044 //......................................................................
00045 
00046 NuZBeamReweight::~NuZBeamReweight()
00047 {
00048   MSG("NuZBeamReweight",Msg::kDebug)
00049     <<"Running NuZBeamReweight Destructor..."<<endl;
00050   
00051 
00052   MSG("NuZBeamReweight",Msg::kDebug)
00053     <<"Finished NuZBeamReweight Destructor"<<endl;
00054 }
00055 
00056 //......................................................................
00057 
00058 void NuZBeamReweight::ExtractZBeamReweight(NuEvent& nu, bool useCustom) const
00059 {
00060 
00061   MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00062     <<"Running ExtractZBeamReweight..."<<endl;
00063   
00064   if (!this->EventPreCheck(nu)) return;
00065   
00066 //  // Use NA49 NuMu-only weights for RHC
00067 //  if (nu.hornIsReverse || 
00068 //      nu.runPeriod == 4 || 
00069 //      nu.beamType == BeamType::kL010z185i_rev) {
00070 //    MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00071 //    <<"Using numu-only weights for RHC"<<endl;
00072 //    useCustom = true;
00073 //  }
00074   
00075   //get a reference
00076   SKZPWeightCalculator* wc;
00077   if (useCustom) {
00078     wc = this->GetSKZPWeightCalculatorCustom();
00079   }
00080   else {
00081     wc = this->GetSKZPWeightCalculator(nu);
00082   }
00083     
00084 
00085   // If runPeriod == 0, then treat as Daikon04 and earlier
00086   if (nu.runPeriod == 0) {
00087     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00088     <<"Getting old-style weights for nu.runPeriod="<< nu.runPeriod <<endl;
00089 
00090     //calc the weights for runI
00091     GetWeights(nu, *wc, SKZPWeightCalculator::kRunI);
00092     nu.trkEnWeightRunI                = nu.trkEnWeight;
00093     nu.shwEnWeightRunI                = nu.shwEnWeight;
00094     nu.beamWeightRunI                 = nu.beamWeight;
00095     nu.fluxErrHadProdAfterTuneRunI    = nu.fluxErrHadProdAfterTune;
00096     nu.fluxErrTotalErrorPreTuneRunI   = nu.fluxErrTotalErrorPreTune;
00097     nu.fluxErrTotalErrorAfterTuneRunI = nu.fluxErrTotalErrorAfterTune;
00098     nu.detectorWeightNMBRunI          = nu.detectorWeightNMB;
00099     nu.detectorWeightNMRunI           = nu.detectorWeightNM;
00100     
00101     
00102     //calc the weights for runII
00103     GetWeights(nu, *wc, SKZPWeightCalculator::kRunII);
00104     nu.trkEnWeightRunII                = nu.trkEnWeight;
00105     nu.shwEnWeightRunII                = nu.shwEnWeight;
00106     nu.beamWeightRunII                 = nu.beamWeight;
00107     nu.fluxErrHadProdAfterTuneRunII    = nu.fluxErrHadProdAfterTune;
00108     nu.fluxErrTotalErrorPreTuneRunII   = nu.fluxErrTotalErrorPreTune;
00109     nu.fluxErrTotalErrorAfterTuneRunII = nu.fluxErrTotalErrorAfterTune;
00110     nu.detectorWeightNMBRunII          = nu.detectorWeightNMB;
00111     nu.detectorWeightNMRunII           = nu.detectorWeightNM;
00112     
00113     
00114     //calc the weights for the weighted average
00115     GetRFWeights(nu, *wc);
00116   }
00117   else {
00118     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00119     <<"Getting new-style weights for nu.runPeriod="<< nu.runPeriod <<endl;
00120 
00121     GetWeights(nu, *wc);
00122   }
00123 }
00124 
00125 //......................................................................
00126 
00127 void NuZBeamReweight::ExtractZBeamReweightCustom(NuEvent& nu) const
00128 { 
00129   this->ExtractZBeamReweight(nu, true);
00130 }
00131 
00132 
00133 //......................................................................
00134 
00135 void NuZBeamReweight::ExtractZBeamReweight(const NtpStRecord& /*ntp*/,
00136                                            const NtpSREvent& /*evt*/,
00137                                            NuEvent& nu) const
00138 { 
00139   //get the weights
00140   this->ExtractZBeamReweight(nu);
00141 }
00142 
00143 //......................................................................
00144 
00145 void NuZBeamReweight::ExtractZBeamReweight(const NtpStRecord& /*ntp*/,
00146                                            NuEvent& nu) const
00147 { 
00148   MAXMSG("NuZBeamReweight",Msg::kError,2000)
00149     <<"NuZBeamReweight::ExtractZBeamReweight(const NtpStRecord& ntp,NuEvent& nu) const"
00150     <<" is deprecated, passing off to ExtractZBeamReweight(NuEvent& nu)!"<<endl;
00151   this->ExtractZBeamReweight(nu);  
00152 }
00153 
00154 
00155 //......................................................................
00156 
00157 void NuZBeamReweight::GetBeamWeightsOnly(NuEvent& nu) const
00158 {
00159   
00160   MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00161   <<"Running GetZBeamReweight..."<<endl;
00162   
00163   if (!this->EventPreCheck(nu)) return;
00164   
00165   //get a reference
00166   SKZPWeightCalculator* wc=this->GetSKZPWeightCalculator(nu);
00167   
00168   // If runPeriod == 0, then treat as Daikon04 and earlier
00169   if (nu.runPeriod == 0) {
00170     //calc the weights for runI
00171     GetWeights(nu, *wc, SKZPWeightCalculator::kRunI, true);
00172     nu.beamWeightRunI                 = nu.beamWeight;
00173     nu.fluxErrHadProdAfterTuneRunI    = nu.fluxErrHadProdAfterTune;
00174     nu.fluxErrTotalErrorPreTuneRunI   = nu.fluxErrTotalErrorPreTune;
00175     nu.fluxErrTotalErrorAfterTuneRunI = nu.fluxErrTotalErrorAfterTune;
00176     
00177     
00178     //calc the weights for runII
00179     GetWeights(nu, *wc, SKZPWeightCalculator::kRunII, true);
00180     nu.beamWeightRunII                 = nu.beamWeight;
00181     nu.fluxErrHadProdAfterTuneRunII    = nu.fluxErrHadProdAfterTune;
00182     nu.fluxErrTotalErrorPreTuneRunII   = nu.fluxErrTotalErrorPreTune;
00183     nu.fluxErrTotalErrorAfterTuneRunII = nu.fluxErrTotalErrorAfterTune;
00184     
00185     
00186     //calc the weights for the weighted average
00187     GetRFWeights(nu, *wc);
00188   }
00189   else {
00190     GetWeights(nu, *wc, true);
00191   }
00192 }
00193 
00194 //......................................................................
00195 
00196 SKZPWeightCalculator* NuZBeamReweight::
00197 GetSKZPWeightCalculator(const NuEvent& nu) const
00198 {
00199   
00200   MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00201   <<"Loading normal weights"<<endl;
00202 
00203   //make a weight calculator, defining the skzp version to use
00204   static SKZPWeightCalculator* pwc=0;
00205   
00206   //create the weight calculator on the first call
00207   if (pwc==0) {
00208     //get a string of the reweightVersion
00209     string sReweightVersion=this->AsString
00210       (static_cast<SKZPWeightCalculator::SKZPConfig_t>
00211        (nu.reweightVersion));
00212     MSG("NuZBeamReweight",Msg::kInfo)
00213       <<"To access database, using the string="<<sReweightVersion<<endl;
00214 
00215     TDirectory* tmpd = gDirectory;
00216     MSG("NuZBeamReweight",Msg::kDebug)
00217       <<"TDirectory before Reweight is:"<<endl;
00218     //tmpd->Print();
00219     
00220     //create the weight calculator
00221     //read constants from DB
00222     pwc=new SKZPWeightCalculator(sReweightVersion,true);
00223 
00224     // Can always do this since it will only be accessed when getting RFWeights
00225     //define and set the numbers of POTs in each run period
00226     //numbers from Andy Blakes minos-doc-3727
00227     Float_t potRunI=1.269e20;
00228     //runIIa=1.229
00229     //runIIb=0.721
00230     Float_t potRunII=1.950e20;
00231     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00232     <<"ExtractZBeamReweight: SetRunFrac w/ potRunI="<<potRunI
00233     <<", potRunII="<<potRunII<<endl;
00234     
00235     Int_t Ibeam;
00236     if (nu.beamType == BeamType::kL010z185i_rev) {
00237       MAXMSG("NuZBeamReweight",Msg::kInfo,1) 
00238       << "Changing beamtype from kL010z185i_rev to kL010z185i for SKZP weighting" << endl;
00239       Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(BeamType::kL010z185i));
00240     }
00241     else {
00242       Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(nu.beamType));
00243     }
00244     
00245     
00246     pwc->SetRunFrac(Ibeam,SKZPWeightCalculator::kRunI,potRunI);
00247     pwc->SetRunFrac(Ibeam,SKZPWeightCalculator::kRunII,potRunII);       
00248     
00249     //print the config
00250     pwc->PrintReweightConfig(cout);
00251 
00252     MSG("NuZBeamReweight",Msg::kDebug)
00253       <<"TDirectory after building Zbeam and Zflux is:"<<endl;
00254     //gDirectory->Print();
00255     MSG("NuZBeamReweight",Msg::kDebug)
00256       <<"After reseting gDirectory its location is:"<<endl;
00257     gDirectory=tmpd;
00258     //gDirectory->Print();
00259   }
00260   
00261   return pwc;
00262 }
00263 
00264 //......................................................................
00265 
00266 SKZPWeightCalculator* NuZBeamReweight::
00267 GetSKZPWeightCalculatorCustom() const
00268 {
00269   MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00270   <<"Loading numu-only weights"<<endl;
00271 
00272   //make a weight calculator, defining the skzp version to use
00273   static SKZPWeightCalculator* pwc=0;
00274   
00275   //create the weight calculator on the first call
00276   if (pwc==0) {
00277     
00278     TDirectory* tmpd = gDirectory;
00279     MSG("NuZBeamReweight",Msg::kDebug) <<"TDirectory before Reweight is:"<<endl;
00280     
00281     //create the weight calculator
00282     //read constants from DB
00283     pwc=new SKZPWeightCalculator("DetXs",true);
00284     
00285     pwc->PrintReweightConfig(cout);
00286     
00287     std::vector<double> beam;
00288     std::vector<double> fluk;
00289     std::vector<double> det;
00290     
00291     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00292     MSG("NuZBeamReweight",Msg::kInfo) << "Setting new target positions." << std::endl;
00293     
00294     // target positions
00295     beam.push_back(1.52410);
00296     beam.push_back(-3.34533);
00297     beam.push_back(-3.68550);
00298     beam.push_back(0.177135);
00299     beam.push_back(-1.73917);
00300     beam.push_back(-0.0842292);
00301     
00302     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00303     MSG("NuZBeamReweight",Msg::kInfo) << "Setting new hadron production parameters." << std::endl;
00304     
00305     // pi+
00306     fluk.push_back(0.53363);
00307     fluk.push_back(-4.09962);
00308     fluk.push_back(0.959969);
00309     fluk.push_back(0.464735);
00310     fluk.push_back(0.948545);
00311     fluk.push_back(1.22209);
00312     
00313     // K+
00314     fluk.push_back(5.69124);
00315     fluk.push_back(17.4408);
00316     fluk.push_back(1.49750);
00317     fluk.push_back(0.972495);
00318     fluk.push_back(0.968975);
00319     fluk.push_back(2.19318);
00320     
00321     // pi-
00322     fluk.push_back(1.05039);
00323     fluk.push_back(-0.867787);
00324     
00325     // K-
00326     fluk.push_back(0.842913);
00327     fluk.push_back(0.0595505);
00328     
00329     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00330     MSG("NuZBeamReweight",Msg::kInfo) << "Setting new detector parameters." << std::endl;
00331     
00332     // detector parameters
00333     det.push_back(-0.0183836);
00334     det.push_back(-0.244669);
00335     det.push_back(0.0); // numubar NC at nominal
00336     det.push_back(1.0); // numubar CC xsec at nominal
00337     det.push_back(0.342342);
00338     det.push_back(0.00517944);
00339     det.push_back(0.389318);
00340     det.push_back(-0.136436);
00341     
00342     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00343     MSG("NuZBeamReweight",Msg::kInfo) << "Calling change parameters." << std::endl;
00344     
00345     pwc->ChangeParameters(&beam,&fluk,&det);
00346     
00347     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00348     MSG("NuZBeamReweight",Msg::kInfo) << "Changed parameters, new configuration:" << std::endl;
00349     MSG("NuZBeamReweight",Msg::kInfo) << std::endl;
00350       
00351     
00352     // Can always do this since iy will only be accessed when getting RFWeights
00353     //define and set the numbers of POTs in each run period
00354     //numbers from Andy Blakes minos-doc-3727
00355     Float_t potRunI=1.269e20;
00356     //runIIa=1.229
00357     //runIIb=0.721
00358     Float_t potRunII=1.950e20;
00359     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00360     <<"ExtractZBeamReweight: SetRunFrac w/ potRunI="<<potRunI
00361     <<", potRunII="<<potRunII<<endl;
00362     const Int_t Ibeam=BeamType::ToZarko(BeamType::kL010z185i);
00363     pwc->SetRunFrac(Ibeam,SKZPWeightCalculator::kRunI,potRunI);
00364     pwc->SetRunFrac(Ibeam,SKZPWeightCalculator::kRunII,potRunII);   
00365     
00366     
00367     //print the config
00368     pwc->PrintReweightConfig(cout);
00369     
00370     MSG("NuZBeamReweight",Msg::kDebug)
00371     <<"TDirectory after building Zbeam and Zflux is:"<<endl;
00372     //gDirectory->Print();
00373     MSG("NuZBeamReweight",Msg::kDebug)
00374     <<"After reseting gDirectory its location is:"<<endl;
00375     gDirectory=tmpd;
00376     //gDirectory->Print();
00377   }
00378   
00379   return pwc;
00380 }
00381 
00382 //......................................................................
00383 
00384 void NuZBeamReweight::CalcGeneratorReweight(const NtpStRecord& ntp,
00385                                             const NtpSREvent& evt,
00386                                             NuEvent& nu) const
00387 {
00391 
00392   //return if not MC
00393   if (nu.simFlag!=SimFlag::kMC) {
00394     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00395       <<"Not doing CalcGeneratorReweight"
00396       <<"for simFlag="<<nu.simFlag<<endl;
00397     
00398     //set the weights to 1 that would otherwise be set below
00399     nu.generatorWeight=1;//no reweighting
00400     return;
00401   }
00402  
00403   if (!nu.useGeneratorReweight) {
00404     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00405       <<"CalcGeneratorReweight: not calculating Generator reweight"
00406       <<" since nu.useGeneratorReweight="<<nu.useGeneratorReweight
00407       <<endl;
00408     return;
00409   }
00410 
00411   if (ReleaseType::IsDaikon(nu.releaseType)) {
00412     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00413       <<"Not calculating Generator weight for Daikon, not required"
00414       <<endl;
00415     return;
00416   }
00417   
00418   MAXMSG("NuZBeamReweight",Msg::kDebug,100)
00419     <<"CalcGeneratorReweight:"<<endl
00420     <<"  generator:config_name="<<nu.sGeneratorConfigName
00421     <<"  generator:config_no="<<nu.generatorConfigNo
00422     <<endl;
00423   
00424   //declare the registry and create it only once
00425   static Registry* rwtconfig=this->CreateNeugenRegistry(nu);
00426 
00427   //make a WeightCalculator and add it to the MCReweight singleton
00428   this->AddNeugenWeightCalculator();
00429 
00430   //get the MC info from the ntp
00431   TClonesArray& thevtTca=*ntp.thevt;
00432   const NtpTHEvent* the=
00433     dynamic_cast<NtpTHEvent*>(thevtTca[evt.index]);
00434   Int_t thidx=the->neumc;
00435 
00436   //fill MCEventInfo to do MODBYRS3;
00437   MCEventInfo ei;
00438   ei.UseStoredXSec(true);
00439   NtpStRecord* st=const_cast<NtpStRecord*>(&ntp);//nasty!
00440   ReweightHelpers::MCEventInfoFilla(&ei,st,thidx);
00441 
00442   //a second one for a sanity check on the values in the NuEvent
00443   MCEventInfo ei2;
00444   ei2.UseStoredXSec(true);
00445   this->MCEventInfoFilla(&ei2,nu);
00446   this->CompareMCEventInfo(ei,ei2);
00447   
00448   //Compute the MODBYRS3 weight
00449   Double_t generatorweight=1.;
00450   NuParent* np=0;
00451   if (nu.useGeneratorReweight){
00452     MCReweight* mcr=&MCReweight::Instance();
00453     generatorweight=mcr->ComputeWeight(&ei,np,rwtconfig);
00454   }
00455   
00456   //set the output of the function
00457   nu.generatorWeight=generatorweight;
00458 }
00459 
00460 //......................................................................
00461 
00462 void NuZBeamReweight::CalcGeneratorReweight(NuEvent& nu) const
00463 {
00464   if (!nu.useGeneratorReweight) {
00465     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00466       <<"CalcGeneratorReweight(nu): not calculating Generator reweight"
00467       <<" since nu.useGeneratorReweight="<<nu.useGeneratorReweight
00468       <<endl;
00469     return;
00470   }
00471 
00472   if (ReleaseType::IsDaikon(nu.releaseType)) {
00473     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00474       <<"Not doing CalcGeneratorReweight(nu) for Daikon, not required"
00475       <<endl;
00476     return;
00477   }
00478 
00479   MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00480     <<"CalcGeneratorReweight(nu):"<<endl
00481     <<"  generator:config_name="<<nu.sGeneratorConfigName
00482     <<"  generator:config_no="<<nu.generatorConfigNo
00483     <<endl;
00484   
00485   //declare the registry and create it only once
00486   static Registry* rwtconfig=this->CreateNeugenRegistry(nu);
00487 
00488   //make a WeightCalculator and add it to the MCReweight singleton
00489   this->AddNeugenWeightCalculator();
00490   
00491   //fill MCEventInfo
00492   MCEventInfo ei;
00493   ei.UseStoredXSec(true);
00494   //ReweightHelpers::MCEventInfoFilla(&ei,st,thidx);
00495   this->MCEventInfoFilla(&ei,nu);
00496   
00497   //Compute the MODBYRS3 weight
00498   Double_t generatorweight=1.;
00499   NuParent* np=0;
00500   if (nu.useGeneratorReweight){
00501     MCReweight* mcr=&MCReweight::Instance();
00502     generatorweight=mcr->ComputeWeight(&ei,np,rwtconfig);
00503   }
00504   
00505   //set the output of the function
00506   nu.generatorWeight=generatorweight;
00507 }
00508 
00509 
00510 //......................................................................
00511 
00512 double NuZBeamReweight::GetWeightHelium(NuEvent &nu) const
00513 {
00514   if (ReleaseType::IsDaikon(nu.releaseType) && 
00515       ReleaseType::GetMCSubVersion(nu.releaseType) > 4) {
00516     MAXMSG("NuZBeamReweight",Msg::kWarning, 10) 
00517     << "Asking for He weights for releaseType = " 
00518     << NuUtilities::PrintRelease(nu.releaseType) 
00519     << " -- He should already be accounted for.  Returning 1." << endl;
00520     return 1;
00521   }
00522   
00523   // It doesn't matter which weight calculator we get
00524   SKZPWeightCalculator *wc = this->GetSKZPWeightCalculatorCustom();
00525 
00526   int inu = nu.inu;
00527   double E = nu.neuEnMC;
00528   int det = nu.detector;
00529   int Ibeam;
00530   if (nu.beamType == BeamType::kL010z185i_rev) {
00531     MAXMSG("NuZBeamReweight",Msg::kInfo,1) 
00532     << "Changing beamtype from kL010z185i_rev to kL010z185i for SKZP weighting" << endl;
00533     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(BeamType::kL010z185i));
00534     inu *= -1;
00535   }
00536   else {
00537     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(nu.beamType));
00538   } 
00539   
00540   
00541   return wc->GetHeliumWeight(det, Ibeam, inu, E);
00542 }
00543 
00544 
00545 
00546 //......................................................................
00547 
00548 bool NuZBeamReweight::EventPreCheck(NuEvent &nu) const
00549 {
00550   if (nu.simFlag!=SimFlag::kMC || 
00551       nu.reweightVersion==SKZPWeightCalculator::kUnknown ||
00552       nu.runPeriod == -1) {
00553     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00554     <<"Not doing ExtractZBeamReweight, reweightVersion="
00555     << nu.reweightVersion << ", simFlag=" << nu.simFlag
00556     << ", runPeriod=" << nu.runPeriod << endl;
00557     
00558     //set the weights to 1 that would otherwise be set below
00559     nu.trkEnWeight=1;//no reweighting
00560     nu.shwEnWeight=1;//no reweighting
00561     nu.beamWeight=1;//no reweighting
00562     nu.fluxErrHadProdAfterTune=-1;
00563     nu.fluxErrTotalErrorPreTune=-1;
00564     nu.fluxErrTotalErrorAfterTune=-1;
00565     nu.detectorWeightNMB=1;//no reweighting
00566     nu.detectorWeightNM=1;//no reweighting
00567     
00568     nu.trkEnWeightRunI=1;//no reweighting
00569     nu.shwEnWeightRunI=1;//no reweighting
00570     nu.beamWeightRunI=1;//no reweighting
00571     nu.fluxErrHadProdAfterTuneRunI=-1;
00572     nu.fluxErrTotalErrorPreTuneRunI=-1;
00573     nu.fluxErrTotalErrorAfterTuneRunI=-1;
00574     nu.detectorWeightNMBRunI=1;//no reweighting
00575     nu.detectorWeightNMRunI=1;//no reweighting
00576   
00577     nu.trkEnWeightRunII=1;//no reweighting
00578     nu.shwEnWeightRunII=1;//no reweighting
00579     nu.beamWeightRunII=1;//no reweighting
00580     nu.fluxErrHadProdAfterTuneRunII=-1;
00581     nu.fluxErrTotalErrorPreTuneRunII=-1;
00582     nu.fluxErrTotalErrorAfterTuneRunII=-1;
00583     nu.detectorWeightNMBRunII=1;//no reweighting
00584     nu.detectorWeightNMRunII=1;//no reweighting
00585     
00586     return false;
00587   }
00588   
00589   return true;
00590 }
00591 
00592 
00593 //......................................................................
00594 
00595 void NuZBeamReweight::GetWeights(NuEvent &nu, SKZPWeightCalculator& wc, bool beamOnly) const
00596 {  
00597   if (nu.runPeriod <= 0) {
00598     MSG("NuZBeamReweight",Msg::kError) << "Run period not specified in NuEvent -- GetWeights failing." << endl;
00599     return;
00600   }
00601   
00602   SKZPWeightCalculator::RunPeriod_t rp = SKZPWeightCalculator::kNone;
00603   if (nu.runPeriod == 1) rp = SKZPWeightCalculator::kRunI;
00604   if (nu.runPeriod == 2) rp = SKZPWeightCalculator::kRunII;
00605   if (nu.runPeriod == 3) rp = SKZPWeightCalculator::kRunIII;
00606   if (nu.runPeriod == 4) {
00607     rp = SKZPWeightCalculator::kRunI;
00608     MAXMSG("NuZBeamReweight",Msg::kInfo,1) << "Reweighting RunIV as if it were Run I." << endl;
00609   }
00610   
00611   this->GetWeights(nu, wc, rp, beamOnly); 
00612 }
00613 
00614 //......................................................................
00615 
00616 void NuZBeamReweight::GetWeights(NuEvent &nu, SKZPWeightCalculator& wc, 
00617                                  SKZPWeightCalculator::RunPeriod_t rp,
00618                                  bool beamOnly) const
00619 {
00620   //fill MCEventInfo
00621   MCEventInfo ei;
00622   if (!beamOnly) {
00623     ei.UseStoredXSec(true);
00624     this->MCEventInfoFilla(&ei,nu);
00625   }
00626   
00627   //convert the std BeamType into Zarko's convention
00628   //was 2=le010z185i (Zbeam.h)
00629   Int_t Ibeam;
00630   if (nu.beamType == BeamType::kL010z185i_rev) {
00631     MAXMSG("NuZBeamReweight",Msg::kInfo,1) 
00632     << "Changing beamtype from kL010z185i_rev to kL010z185i for SKZP weighting" << endl;
00633     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(BeamType::kL010z185i));
00634   }
00635   else {
00636     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(nu.beamType));
00637   }
00638   
00639   //copied from SKZPWeightCalculator
00640   Int_t ccnc=nu.iaction;
00641   Double_t true_enu=nu.neuEnMC;//p4neu[3]
00642   Double_t true_eshw=nu.shwEnMC;//p4shw[3]
00643   Int_t inu=nu.inu; 
00644   Float_t nsigma=1;
00645   //FUTURE VERSIONS may use this:
00646   //parenttype = fi->ptype;
00647   //parentpz = 1.*fi->pppz;
00648   //parentpy = 1.*fi->ppdydz*parentpz;
00649   //parentpx = 1.*fi->ppdxdz*parentpz;
00650   Int_t parenttype = nu.tptype;
00651   Double_t parentpz = nu.tpz;
00652   Double_t parentpy = nu.tpy;
00653   Double_t parentpx = nu.tpx;
00654   Double_t pt = sqrt(parentpy*parentpy+parentpx*parentpx);
00655   
00656   //create variables that are passed by reference to function to fill
00657   Double_t new_reco_emu=0;
00658   Double_t new_reco_eshw=0;
00659   Double_t beamweight=0;
00660   Double_t detweightNMB=0;
00661   Double_t detweightNM=0;
00662   Float_t trkEnWeight=1;
00663   Float_t shwEnWeight=1;
00664   
00665   if (beamOnly) {
00666     // Get only the beam weights
00667     beamweight = wc.GetBeamWeight(nu.detector, Ibeam, parenttype, pt,
00668                                   parentpz, true_enu, inu, rp);
00669   }
00670   else {
00671     // Get the skzp weights (beam and detector)
00672     wc.SetSampleSelection("NuMuBar");
00673     wc.GetWeight(nu.detector,Ibeam,
00674                  parenttype,pt,parentpz,ccnc,true_enu,inu,
00675                  nu.trkEn,nu.shwEn,new_reco_emu,new_reco_eshw,
00676                  beamweight,detweightNMB,rp,true_eshw,&ei);
00677     
00678     
00679     
00680     MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00681     <<endl<<"iaction="<<nu.iaction
00682     <<", nu.trkEn="<<nu.trkEn<<", nu.shwEn="<<nu.shwEn<<endl;
00683     
00684     MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00685     <<"NMB: newEmu="<<new_reco_emu
00686     <<", newEShw="<<new_reco_eshw
00687     <<", detNMB="<<detweightNMB
00688     <<", beamW="<<beamweight<<endl;
00689     
00690     //get the skzp weights (beam and detector)
00691     wc.SetSampleSelection("NuMu");
00692     wc.GetWeight(nu.detector,Ibeam,
00693                  parenttype,pt,parentpz,ccnc,true_enu,inu,
00694                  nu.trkEn,nu.shwEn,new_reco_emu,new_reco_eshw,
00695                  beamweight,detweightNM,rp,true_eshw,&ei);
00696     
00697     MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00698     <<"NM:  newEmu="<<new_reco_emu
00699     <<", newEshw="<<new_reco_eshw
00700     <<",  detNM="<<detweightNM
00701     <<", beamW="<<beamweight<<endl;
00702     
00703     //new for kDetXs
00704     //double GetWeight(int det, int Ibeam,
00705     //     int tptype, double pt, double pz,    
00707     //     double reco_emu, double reco_eshw,
00708     //     double &new_reco_emu, double &new_reco_eshw,
00709     //     double &beamweight, double &detweight,
00710     //     RunPeriod_t rp=kRunI,double true_eshw=-1,MCEventInfo *ei=0);
00711     
00712     if (nu.trkEn) trkEnWeight=new_reco_emu/nu.trkEn;
00713     if (nu.shwEn) shwEnWeight=new_reco_eshw/nu.shwEn;
00714   }
00715   
00716   //get the flux errors
00717   Float_t fluxErrHadProdAfterTune = 
00718   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00719                   SKZPWeightCalculator::kHadProdAfterTune,
00720                   nsigma);
00721   Float_t fluxErrTotalErrorPreTune = 
00722   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00723                   SKZPWeightCalculator::kTotalErrorPreTune,
00724                   nsigma);
00725   Float_t fluxErrTotalErrorAfterTune = 
00726   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00727                   SKZPWeightCalculator::kTotalErrorAfterTune,
00728                   nsigma);
00729   
00730   
00731   
00732   MAXMSG("NuZBeamReweight",Msg::kDebug,300)
00733   << "Wgts for run " << (int)rp
00734   << ", b="   << beamweight 
00735   << ", dNMB="<< detweightNMB 
00736   << ", dNM=" << detweightNM
00737   << ", trk=" << trkEnWeight 
00738   << ", shw=" << shwEnWeight 
00739   << endl
00740   << "energyMC="  << nu.neuEnMC
00741   << ", iaction=" << nu.iaction
00742   << ", inu="     << nu.inu
00743   << endl
00744   << "fluxErrs: " << " HadProdAfterTune   =" << fluxErrHadProdAfterTune << endl
00745   << "          " << " TotalErrorPreTune  =" << fluxErrTotalErrorPreTune << endl
00746   << "          " << " TotalErrorAfterTune=" << fluxErrTotalErrorAfterTune << endl 
00747   << endl;  
00748   
00749   nu.beamWeight                 = beamweight;
00750   nu.fluxErrHadProdAfterTune    = fluxErrHadProdAfterTune;
00751   nu.fluxErrTotalErrorPreTune   = fluxErrTotalErrorPreTune;
00752   nu.fluxErrTotalErrorAfterTune = fluxErrTotalErrorAfterTune;
00753   
00754   if (!beamOnly) {
00755     nu.trkEnWeight                = trkEnWeight;
00756     nu.shwEnWeight                = shwEnWeight;
00757     nu.detectorWeightNMB          = detweightNMB;
00758     nu.detectorWeightNM           = detweightNM;
00759   }
00760   
00761 }
00762 
00763 
00764 //......................................................................
00765 
00766 void NuZBeamReweight::GetRFWeights(NuEvent &nu, SKZPWeightCalculator& wc) const
00767 {
00768 
00769   //fill MCEventInfo
00770   MCEventInfo ei;
00771   ei.UseStoredXSec(true);
00772   this->MCEventInfoFilla(&ei,nu);
00773   //convert the std BeamType into Zarko's convention
00774   //was 2=le010z185i (Zbeam.h)
00775   
00776   Int_t Ibeam;
00777   if (nu.beamType == BeamType::kL010z185i_rev) {
00778     MAXMSG("NuZBeamReweight",Msg::kInfo,1) 
00779     << "Changing beamtype from kL010z185i_rev to kL010z185i for SKZP weighting" << endl;
00780     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(BeamType::kL010z185i));
00781   }
00782   else {
00783     Ibeam = BeamType::ToZarko(static_cast<BeamType::BeamType_t>(nu.beamType));
00784   }
00785 
00786   
00787   
00788   //copied from SKZPWeightCalculator
00789   Int_t ccnc=nu.iaction;
00790   Double_t true_enu=nu.neuEnMC;//p4neu[3]
00791   //Double_t true_eshw=nu.shwEnMC;//p4shw[3]
00792   Int_t inu=nu.inu; 
00793   Float_t nsigma=1;
00794   //FUTURE VERSIONS may use this:
00795   //parenttype = fi->ptype;
00796   //parentpz = 1.*fi->pppz;
00797   //parentpy = 1.*fi->ppdydz*parentpz;
00798   //parentpx = 1.*fi->ppdxdz*parentpz;
00799   Int_t parenttype = nu.tptype;
00800   Double_t parentpz = nu.tpz;
00801   Double_t parentpy = nu.tpy;
00802   Double_t parentpx = nu.tpx;
00803   Double_t pt = sqrt(parentpy*parentpy+parentpx*parentpx);
00804 
00805   //create variables that are passed by reference to function to fill
00806   Double_t new_reco_emu=0;
00807   Double_t new_reco_eshw=0;
00808   Double_t beamweight=0;
00809   Double_t detweightNMB=0;
00810   Double_t detweightNM=0;
00811   
00812   //get the skzp weights (beam and detector)
00813   wc.SetSampleSelection("NuMuBar");
00814   wc.GetRFWWeight(nu.detector,Ibeam,
00815                   parenttype,pt,parentpz,ccnc,true_enu,inu,
00816                   nu.trkEn,nu.shwEn,new_reco_emu,new_reco_eshw,
00817                   beamweight,detweightNMB);
00818   
00819   MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00820   <<endl<<"iaction="<<nu.iaction
00821   <<", nu.trkEn="<<nu.trkEn<<", nu.shwEn="<<nu.shwEn<<endl;
00822   
00823   MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00824   <<"NMB: newEmu="<<new_reco_emu
00825   <<", newEShw="<<new_reco_eshw
00826   <<", detNMB="<<detweightNMB
00827   <<", beamW="<<beamweight<<endl;
00828   
00829   //get the skzp weights (beam and detector)
00830   wc.SetSampleSelection("NuMu");
00831   wc.GetRFWWeight(nu.detector,Ibeam,
00832                   parenttype,pt,parentpz,ccnc,true_enu,inu,
00833                   nu.trkEn,nu.shwEn,new_reco_emu,new_reco_eshw,
00834                   beamweight,detweightNM);
00835   
00836   MAXMSG("NuZBeamReweight",Msg::kDebug,200)
00837   <<"NM:  newEmu="<<new_reco_emu
00838   <<", newEshw="<<new_reco_eshw
00839   <<",  detNM="<<detweightNM
00840   <<", beamW="<<beamweight<<endl;
00841   
00842   //new for kDetXs
00843   //double GetWeight(int det, int Ibeam,
00844   //       int tptype, double pt, double pz,    
00846   //       double reco_emu, double reco_eshw,
00847   //       double &new_reco_emu, double &new_reco_eshw,
00848   //       double &beamweight, double &detweight,
00849   //       RunPeriod_t rp=kRunI,double true_eshw=-1,MCEventInfo *ei=0);
00850   
00851   Float_t trkEnWeight=1;
00852   Float_t shwEnWeight=1;
00853   if (nu.trkEn) trkEnWeight=new_reco_emu/nu.trkEn;
00854   if (nu.shwEn) shwEnWeight=new_reco_eshw/nu.shwEn;
00855   
00856   //get the flux errors
00857   Float_t fluxErrHadProdAfterTune = 
00858   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00859                   SKZPWeightCalculator::kHadProdAfterTune,
00860                   nsigma);
00861   Float_t fluxErrTotalErrorPreTune = 
00862   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00863                   SKZPWeightCalculator::kTotalErrorPreTune,
00864                   nsigma);
00865   Float_t fluxErrTotalErrorAfterTune = 
00866   wc.GetFluxError(nu.detector,Ibeam,inu,true_enu,
00867                   SKZPWeightCalculator::kTotalErrorAfterTune,
00868                   nsigma);
00869   
00870   
00871   
00872   MAXMSG("NuZBeamReweight",Msg::kDebug,300)
00873   << "Wgts for run " << "I+II Avg"
00874   << ", b="   << beamweight 
00875   << ", dNMB="<< detweightNMB 
00876   << ", dNM=" << detweightNM
00877   << ", trk=" << trkEnWeight 
00878   << ", shw=" << shwEnWeight 
00879   << endl
00880   << "energyMC="  << nu.neuEnMC
00881   << ", iaction=" << nu.iaction
00882   << ", inu="     << nu.inu
00883   << endl
00884   << "fluxErrs: " << " HadProdAfterTune   =" << fluxErrHadProdAfterTune << endl
00885   << "          " << " TotalErrorPreTune  =" << fluxErrTotalErrorPreTune << endl
00886   << "          " << " TotalErrorAfterTune=" << fluxErrTotalErrorAfterTune << endl 
00887   << endl;  
00888   
00889   
00890   nu.trkEnWeight                = trkEnWeight;
00891   nu.shwEnWeight                = shwEnWeight;
00892   nu.beamWeight                 = beamweight;
00893   nu.fluxErrHadProdAfterTune    = fluxErrHadProdAfterTune;
00894   nu.fluxErrTotalErrorPreTune   = fluxErrTotalErrorPreTune;
00895   nu.fluxErrTotalErrorAfterTune = fluxErrTotalErrorAfterTune;
00896   nu.detectorWeightNMB          = detweightNMB;
00897   nu.detectorWeightNM           = detweightNM;
00898   
00899 }
00900 
00901 
00902 
00903 
00904 //......................................................................
00905 
00906 void NuZBeamReweight::AddNeugenWeightCalculator() const
00907 {
00909   Bool_t firstTime=true;
00910   if (firstTime) {
00911     firstTime=false;
00912     MAXMSG("NuZBeamReweight",Msg::kInfo,1)
00913       <<"Adding NeugenWeightCalculator to MCReweight singleton"<<endl;
00914     static NeugenWeightCalculator *n=new NeugenWeightCalculator();
00915     MCReweight* mcr=&MCReweight::Instance();    
00916     mcr->AddWeightCalculator(n);
00917   }
00918 }
00919 
00920 //......................................................................
00921 
00922 Registry* NuZBeamReweight::CreateNeugenRegistry(const NuEvent& nu) const
00923 {
00924   static Registry* rwtconfig=0;
00925   
00926   //create it the first time
00927   if (rwtconfig==0){
00928     
00929     //fill the registry to tell MCReweight to do MODBYRS3
00930     rwtconfig=new Registry();
00931     rwtconfig->UnLockValues();
00932     rwtconfig->UnLockKeys();
00933     rwtconfig->Clear();
00934     rwtconfig->Set("neugen:config_name",nu.sGeneratorConfigName.c_str());
00935     rwtconfig->Set("neugen:config_no",nu.generatorConfigNo);
00936     rwtconfig->LockValues();
00937     rwtconfig->LockKeys();
00938   }
00939   return rwtconfig;
00940 }
00941 
00942 //......................................................................
00943 
00944 void NuZBeamReweight::CompareMCEventInfo(const MCEventInfo& ei,
00945                                          const MCEventInfo& ei2) const
00946 {
00947   if (ei.initial_state!=ei2.initial_state ||
00948       ei.nucleus!=ei2.nucleus ||
00949       ei.had_fs!=ei2.had_fs){
00950     MAXMSG("NuZBeamReweight",Msg::kError,300)
00951       <<"Ahhh, CompareMCEventInfo difference found:"<<endl
00952       <<"1st: initial_state="<<ei.initial_state
00953       <<", nucleus="<<ei.nucleus
00954       <<", had_fs="<<ei.had_fs
00955       <<endl
00956       <<"2nd: initial_state="<<ei2.initial_state
00957       <<", nucleus="<<ei2.nucleus
00958       <<", had_fs="<<ei2.had_fs
00959       <<endl
00960       <<"NOTE: This discrepancy has to be understood if we"
00961       <<" are to trust the generator reweighting"
00962       <<endl;
00963   }
00964 }
00965 
00966 //......................................................................
00967 
00968 void NuZBeamReweight::MCEventInfoFilla(MCEventInfo* ei,
00969                                        const NuEvent& nu) const
00970 {
00973 
00974   ei->nuE=nu.neuEnMC;
00975   ei->nuPx=nu.neuPxMC;
00976   ei->nuPy=nu.neuPyMC;
00977   ei->nuPz=nu.neuPzMC;
00978   
00979   ei->tarE=nu.tgtEnMC;
00980   ei->tarPx=nu.tgtPxMC;
00981   ei->tarPy=nu.tgtPyMC;
00982   ei->tarPz=nu.tgtPzMC;
00983   
00984   ei->y=nu.yMC;
00985   ei->x=nu.xMC;
00986   ei->q2=nu.q2MC;
00987   ei->w2=nu.w2MC;
00988   
00989   ei->iaction=nu.iaction;
00990   ei->inu=nu.inu;
00991   ei->iresonance=nu.iresonance;
00992   ei->initial_state=nu.initialStateMC;
00993   
00994   ei->nucleus=nu.nucleusMC;
00995   
00996   ei->had_fs=nu.hadronicFinalStateMC;
00997 
00998 
00999   MAXMSG("NuZBeamReweight",Msg::kDebug,100)
01000     <<endl<<endl
01001     <<", ei->nuE="<<ei->nuE
01002     <<", ei->nuPx="<<ei->nuPx
01003     <<", ei->nuPy="<<ei->nuPy
01004     <<", ei->nuPz="<<ei->nuPz
01005     <<endl
01006     <<", ei->tarE="<<ei->tarE
01007     <<", ei->tarPx="<<ei->tarPx
01008     <<", ei->tarPy="<<ei->tarPy
01009     <<", ei->tarPz="<<ei->tarPz
01010     <<endl
01011     <<", ei->y="<<ei->y<<endl
01012     <<", ei->x="<<ei->x<<endl
01013     <<", ei->q2="<<ei->q2<<endl
01014     <<", ei->w2="<<ei->w2<<endl
01015     <<endl
01016     <<", ei->iaction="<<ei->iaction<<endl
01017     <<", ei->inu="<<ei->inu<<endl
01018     <<", ei->iresonance="<<ei->iresonance<<endl
01019     <<", ei->initial_state="<<ei->initial_state<<endl
01020     <<", ei->nucleus="<<ei->nucleus<<endl
01021     <<", ei->had_fs="<<ei->had_fs<<endl
01022     <<endl;
01023 }
01024 
01025 //......................................................................
01026 
01027 const Char_t* NuZBeamReweight::
01028 AsString(SKZPWeightCalculator::SKZPConfig_t c) const
01029 {
01030   // return char string in cannonical offline form
01031   switch (c) {
01032     case SKZPWeightCalculator::kPRL:     return "PRL";
01033     case SKZPWeightCalculator::kBoston:  return "Boston";
01034     case SKZPWeightCalculator::kPiMinus: return "PiMinus_CedarDaikon";
01035     case SKZPWeightCalculator::kDetXs:   return "DetXs";
01036     case SKZPWeightCalculator::kDogwood1_Daikon07: return "Dogwood1_Daikon07";
01037     case SKZPWeightCalculator::kDogwood1_Daikon07_v2: return "Dogwood1_Daikon07_v2";
01038       case SKZPWeightCalculator::kUnknown: return "Unknown"; break;
01039     default:         return "!?Bad Value Passed?!"; break;
01040   }
01041 }
01042 
01043 //......................................................................

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