00001 #include "NCUtils/Extrapolation/NCExtrapolationPID.h"
00002 #include "NCUtils/Extrapolation/PIDSpectrum.h"
00003 #include "NCUtils/NCUtility.h"
00004
00005 #include "MessageService/MsgService.h"
00006
00007 #include "AnalysisNtuples/ANtpAnalysisInfo.h"
00008 #include "AnalysisNtuples/ANtpBeamInfo.h"
00009 #include "AnalysisNtuples/ANtpRecoInfo.h"
00010 #include "AnalysisNtuples/ANtpHeaderInfo.h"
00011 #include "AnalysisNtuples/ANtpTruthInfoBeam.h"
00012
00013 #include "Conventions/SimFlag.h"
00014
00015 #include "NCExtrapolationModule.h"
00016 #include "NCSpectrumInterpolator.h"
00017
00018 #include "TH1.h"
00019 #include "TFile.h"
00020 #include "TList.h"
00021 #include "TKey.h"
00022 #include "TCanvas.h"
00023
00024 REGISTER_NCEXTRAPOLATION(NCExtrapolationPID, PID)
00025
00026 #include <iostream>
00027
00028 CVSID("$Id: NCExtrapolationPID.cxx,v 1.79 2009/12/18 01:30:21 rodriges Exp $");
00029
00030 ClassImp(NCExtrapolationPID)
00031
00032
00033
00034 NCExtrapolationPID::NCExtrapolationPID()
00035 : fUseCCEnergy(true), fUseSelectionEnergy(true), fTrueBinFactor(5), fNumPIDBins(20),
00036 fPIDmin(0.2), fPIDmax(1.1),
00037 fMCInFilename(""), fMCOutFilename(""),
00038 fDoFarNearCorrection(true), fEmulateFarNear(false)
00039 {
00040 }
00041
00042
00043
00044 NCExtrapolationPID::~NCExtrapolationPID()
00045 {
00046 for (SpecMap::iterator it=fSpectra.begin(); it!=fSpectra.end(); ++it)
00047 delete it->second;
00048 }
00049
00050
00051
00052
00053 void NCExtrapolationPID::AddEvent(NCEventInfo info,
00054 bool useMCAsData,
00055 NCType::EFileType fileType,
00056 NCBeam::Info beamInfo)
00057 {
00058
00059
00060 NCExtrapolation::AddEvent(info, useMCAsData, fileType, beamInfo);
00061
00062
00063
00064 if (fEmulateFarNear &&
00065 !(info.analysis->isNC || info.analysis->isCC)) return;
00066
00067
00068 if (fSpectra.find(beamInfo)==fSpectra.end()) {
00069
00070
00071 assert(fMCInFilename=="");
00072
00073 string name = string("PID_") + beamInfo.GetDescription().Data();
00074 fSpectra[beamInfo]=new PIDSpectrum(name, "title",
00075 fNumPIDBins, fPIDmin, fPIDmax,
00076
00077 fTrueBinFactor, beamInfo,
00078 info.analysis->separationParameterCut);
00079 }
00080
00081
00082
00083
00084 if (fMCInFilename!="" &&
00085 (info.header->detector==Detector::kNear ||
00086 (info.header->dataType==SimFlag::kMC && !useMCAsData))) return;
00087
00088 if(!fUseNC && info.analysis->isNC) return;
00089 if(!fUseCC && info.analysis->isCC) return;
00090
00091 if (!info.reco->inFiducialVolume) return;
00092
00093 Detector::Detector_t det=Detector::Detector_t(info.header->detector);
00094 if (det==Detector::kNear && !info.reco->isSimpleCutsClean) return;
00095
00096 double energy;
00097 if (fUseSelectionEnergy) {
00098 energy=info.reco->nuEnergy;
00099 } else {
00100 energy=fUseCCEnergy ? info.reco->nuEnergyCC : info.reco->nuEnergyNC;
00101 }
00102
00103
00104
00105 if (energy<0 || energy<fEnergyThreshold) return;
00106
00107 if (info.header->dataType==SimFlag::kData || useMCAsData) {
00108
00109 fSpectra[beamInfo]->FillData(det,
00110 info.truth, info.reco, info.analysis, energy,
00111 info.reco->weight);
00112 } else {
00113 fSpectra[beamInfo]->FillMC(det,
00114 info.truth, info.reco, info.analysis, energy,
00115 info.reco->weight);
00116 }
00117
00118 }
00119
00120
00121
00122 void NCExtrapolationPID::FindSpectraForPars(const NC::OscProb::OscPars* pars,
00123 const NC::SystPars& s,
00124 vector<NCBeam::Info> beamsToUse,
00125 vector<TH1*>& exps,
00126 vector<TH1*>& obss)
00127 {
00128 using namespace NC::Utility;
00129
00130 if(fDoSystematicInterpolation) InitializeInterpolator(pars);
00131
00132 if(!fInterpolator){
00133 exps = GetExpHists(beamsToUse, pars);
00134 obss = GetObsHists(beamsToUse);
00135 return;
00136 }
00137
00138
00139 const vector<double> shift = fCoordConv.VectorFromSystPars(s);
00140
00141 vector<TH1*> interp = fInterpolator->GetInterpolatedSpectra(shift);
00142
00143 vector<TH1*> nd_ratios;
00144 assert(interp.size()%2 == 0);
00145
00146 for(unsigned int n = 1; n < interp.size(); n += 2){
00147 nd_ratios.push_back(interp[n]);
00148 }
00149
00150 vector<const TH1*> nd_noms = GetNearMCSpectra(beamsToUse);
00151
00152 assert(nd_ratios.size() == nd_noms.size());
00153
00154 vector<TH1*> nd_spectra;
00155 for(unsigned int n = 0; n < nd_noms.size(); ++n){
00156 nd_spectra.push_back(CloneFast(nd_noms[n]));
00157 MultiplyFast(nd_spectra[n], nd_ratios[n]);
00158 }
00159 assert(nd_spectra.size() == nd_noms.size());
00160
00161
00162 exps = GetExpHists(beamsToUse, pars, nd_spectra);
00163
00164 obss = GetObsHists(beamsToUse);
00165
00166 assert(interp.size() == 2*exps.size());
00167
00168
00169 for(unsigned int n = 0; n < exps.size(); ++n){
00170 MultiplyFast(exps[n], interp[2*n]);
00171 }
00172
00173
00174 for(unsigned int n = 0; n < interp.size(); ++n) delete interp[n];
00175 for(unsigned int n = 0; n < nd_spectra.size(); ++n) delete nd_spectra[n];
00176 }
00177
00178
00179
00180 vector<const TH1*> NCExtrapolationPID::
00181 GetNearMCSpectra(vector<NCBeam::Info> beamsToUse)
00182 {
00183 vector<const TH1*> expHists;
00184 for (int i=0; i<(int)beamsToUse.size(); ++i) {
00185 NCBeam::Info beamInfo=beamsToUse[i];
00186 SpecMap::const_iterator spectrumForBeamInfo=fSpectra.find(beamInfo);
00187 assert(spectrumForBeamInfo!=fSpectra.end());
00188 assert(spectrumForBeamInfo->second);
00189 expHists.push_back(spectrumForBeamInfo->second->GetNearMC());
00190 }
00191
00192 return expHists;
00193 }
00194
00195
00196
00197 void NCExtrapolationPID::CleanupSpectra(vector<TH1*> exp, vector<TH1*> )
00198 {
00199
00200 for (unsigned int n = 0; n < exp.size(); ++n) delete exp[n];
00201 }
00202
00203
00204
00205 void NCExtrapolationPID::
00206 WriteResources(const NC::OscProb::OscPars* trueOscPars)
00207 {
00208
00209 NC::OscProb::OscPars* bestFit=GetBestFitOscPars();
00210 NC::OscProb::OscPars* oscParsNoOsc = new NC::OscProb::NoOscillations;
00211
00212
00213
00214
00215
00216
00217 for(fBeams_t::iterator it=fBeams.begin(); it!=fBeams.end(); ++it){
00218 NCBeam::Info beamInfo=it->first.first;
00219
00220 NCBeam* beam=it->second;
00221 assert(fSpectra.find(beamInfo)!=fSpectra.end());
00222 PIDSpectrum* spectrum=fSpectra[beamInfo];
00223
00224 for(int inccc=NCType::kNC; inccc<=NCType::kCC; ++inccc){
00225
00226 NCType::EEventType nccc=NCType::EEventType(inccc);
00227 TH1D* pseudoNoOsc=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kAll,
00228 oscParsNoOsc);
00229 TH1D* pseudoFit=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kAll,
00230 bestFit);
00231 TH1D* pseudoCC=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kNumu,
00232 bestFit);
00233 TH1D* pseudoBeamNue=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kBeamNue,
00234 bestFit);
00235 TH1D* pseudoOscNue=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kOscNue,
00236 bestFit);
00237 TH1D* pseudoNutau=spectrum->GetPseudoNCCCSpectrum(nccc, PIDSpectrum::kNutau,
00238 bestFit);
00239
00240 TH1D* pseudoData=spectrum->GetPseudoNCCCComponent(spectrum->GetDataHist(), nccc);
00241
00242
00243
00244
00245 TH1D* pseudoBG;
00246 if(nccc==NCType::kNC) pseudoBG=pseudoCC;
00247 else{
00248
00249
00250 pseudoBG=(TH1D*)pseudoFit->Clone("pseudoBG");
00251 pseudoBG->SetDirectory(0);
00252
00253 pseudoBG->Add(pseudoCC, -1);
00254
00255
00256 pseudoBG->Add(pseudoBeamNue, -1);
00257 pseudoBG->Add(pseudoOscNue, -1);
00258 pseudoBG->Add(pseudoNutau, -1);
00259 }
00260
00261 #define COPYBINS(from, to) CopyBinContents(from, \
00262 beam->Get##to##Histogram(nccc))
00263
00264 COPYBINS(pseudoData, Data);
00265 COPYBINS(pseudoNoOsc, MC);
00266 COPYBINS(pseudoFit, MCFit);
00267 COPYBINS(pseudoBG, MCBackground);
00268 COPYBINS(pseudoBeamNue, MCFitNuEToNuE);
00269 COPYBINS(pseudoOscNue, MCFitNuMuToNuE);
00270 COPYBINS(pseudoNutau, MCFitNuMuToNuTau);
00271
00272 #undef COPYBINS
00273
00274 }
00275
00276
00277 beam->MarkHistogramsFilled();
00278 }
00279
00280 delete oscParsNoOsc;
00281
00282 NCExtrapolation::WriteResources(trueOscPars);
00283
00284
00285
00286
00287 gDirectory->mkdir("debug", "debug")->cd();
00288
00289 if(bestFit){
00290 NC::SystPars systs=GetBestFitSysts();
00291
00292 WriteSpectra(bestFit, systs, "bestFit");
00293
00294 if(trueOscPars) WriteSpectra(trueOscPars, systs, "truePars");
00295
00296 }
00297
00298
00299 if (fMCOutFilename!="") WriteMCSpectra(fMCOutFilename.c_str());
00300 }
00301
00302
00303
00304 void NCExtrapolationPID::PrintMap(POTMap& m, const char* name)
00305 {
00306 cout << "Map " << name << endl;
00307 for (POTMap::const_iterator it=m.begin();
00308 it!=m.end(); ++it) {
00309 cout << it->first.GetDescription() << "\t" << it->second << endl;
00310 }
00311 }
00312
00313
00314
00315 void NCExtrapolationPID::PrintSpectraMap()
00316 {
00317 cout << "fSpectra:" << endl;
00318 for (SpecMap::const_iterator it=fSpectra.begin();
00319 it!=fSpectra.end(); ++it) {
00320 cout << it->first.GetDescription() << "\t" << hex << it->second << dec
00321 << "\t" << it->second->GetBeamInfo().GetTitle() << endl;
00322 }
00323 }
00324
00325
00326
00327
00328 vector<TH1*> NCExtrapolationPID::GetExpHists(vector<NCBeam::Info> beamsToUse,
00329 const NC::OscProb::OscPars* pars,
00330 vector<TH1*> ndSpectra) const
00331 {
00332 vector<TH1*> expHists;
00333 for (int i=0; i<(int)beamsToUse.size(); ++i) {
00334 NCBeam::Info beamInfo=beamsToUse[i];
00335 SpecMap::const_iterator spectrumForBeamInfo=fSpectra.find(beamInfo);
00336 assert(spectrumForBeamInfo!=fSpectra.end());
00337 assert(spectrumForBeamInfo->second);
00338 expHists.push_back(spectrumForBeamInfo->second->
00339 GetPredicted(pars,
00340 "",
00341 fDoFarNearCorrection,
00342 ndSpectra.empty() ? 0 : ndSpectra[i]));
00343 }
00344
00345 return expHists;
00346 }
00347
00348
00349
00350 vector<TH1*> NCExtrapolationPID::GetObsHists(vector<NCBeam::Info> beamsToUse) const
00351
00352 {
00353 vector<TH1*> obsHists;
00354 for (int i=0; i<(int)beamsToUse.size(); ++i) {
00355 NCBeam::Info beamInfo=beamsToUse[i];
00356 obsHists.push_back(fSpectra.find(beamInfo)->second->GetDataHist());
00357 }
00358
00359 return obsHists;
00360 }
00361
00362
00363
00364 void NCExtrapolationPID::WriteSpectra(const NC::OscProb::OscPars* coords,
00365 const NC::SystPars& s,
00366 string suffix)
00367 {
00368 if(suffix!="") gDirectory->mkdir(suffix.c_str(), suffix.c_str())->cd();
00369
00370 vector<TH1*> fd_ratios;
00371 vector<TH1*> nd_ratios;
00372
00373 if (fInterpolator) {
00374 const vector<double> shift = fCoordConv.VectorFromSystPars(s);
00375 vector<TH1*> ratios = fInterpolator->GetInterpolatedSpectra(shift);
00376
00377 assert(ratios.size()%2 == 0);
00378
00379 for(unsigned int n = 0; n < ratios.size(); n += 2)
00380 nd_ratios.push_back(ratios[n]);
00381 for(unsigned int n = 1; n < ratios.size(); n += 2)
00382 fd_ratios.push_back(ratios[n]);
00383 }
00384
00385 int specIdx = 0;
00386
00387 for (SpecMap::iterator sit=fSpectra.begin(); sit!=fSpectra.end(); ++sit) {
00388 PIDSpectrum* spec=sit->second;
00389
00390 TString beamName=spec->GetBeamInfo().GetDescription();
00391 assert(beamName!="");
00392 gDirectory->mkdir(beamName, beamName)->cd();
00393
00394 TH1* nd_ratio = 0;
00395 TH1* fd_ratio = 0;
00396
00397
00398
00399
00400
00401 const bool canInterpolate =
00402 fInterpolator &&
00403 spec->GetBeamInfo().IsNominal() &&
00404 spec->GetBeamInfo().GetRunType() != NC::RunUtil::kRunAll;
00405
00406 if(canInterpolate){
00407 nd_ratio=nd_ratios[specIdx];
00408 fd_ratio=fd_ratios[specIdx];
00409
00410
00411 MSG("NCExtrapolationPID", Msg::kDebug)
00412 << "Wanted " << beamName << " found "
00413 << nd_ratio->GetName() << " and "
00414 << fd_ratio->GetName() << endl;
00415
00416 ++specIdx;
00417 }
00418
00419 vector<TCanvas*> v;
00420 v.push_back( spec->DrawSpectrum(coords, fd_ratio, suffix) );
00421
00422 vector<TCanvas*> vNear=spec->DrawNearSpectra(nd_ratio, suffix);
00423 vector<TCanvas*> vE=spec->DrawEnergySlices(coords, fd_ratio, suffix);
00424 vector<TCanvas*> vPID=spec->DrawPIDSlices(coords, fd_ratio, suffix);
00425
00426 v.insert(v.end(), vE.begin(), vE.end());
00427 v.insert(v.end(), vPID.begin(), vPID.end());
00428 v.insert(v.end(), vNear.begin(), vNear.end());
00429
00430 for (vector<TCanvas*>::iterator it=v.begin();
00431 it!=v.end(); ++it) {
00432 (*it)->Write();
00433 }
00434
00435 vector<PIDSpectrum::evtType> evtTypes=spec->EventTypeList();
00436 for (int j=0; j<(int)evtTypes.size(); ++j) {
00437 spec->GetOnePredicted(evtTypes[j], coords, suffix.c_str())->Write();
00438 }
00439
00440 gDirectory->cd("..");
00441 }
00442
00443
00444 gDirectory->cd("..");
00445
00446
00447
00448
00449 }
00450
00451
00452
00453 void NCExtrapolationPID::DoneFilling()
00454 {
00455 cout << "NCExtrapolationPID::DoneFilling()" << endl;
00456
00457 if(fMCInFilename == ""){
00458 for (SpecMap::iterator sit=fSpectra.begin(); sit!=fSpectra.end(); ++sit) {
00459 DoneFillingOneBeam(sit->first);
00460 }
00461 }
00462 }
00463
00464
00465
00466 void NCExtrapolationPID::DoneFillingOneBeam(NCBeam::Info beamInfo)
00467 {
00468 cout << "NCExtrapolationPID::DoneFillingOneBeam("
00469 << beamInfo.GetDescription() << ")" << endl;
00470
00471 cout << "Normalizing true-to-recos" << endl;
00472 fSpectra[beamInfo]->NormalizeTrueToRecos();
00473
00474 }
00475
00476
00477
00478 const Registry& NCExtrapolationPID::DefaultConfig()
00479 {
00480 static Registry r;
00481
00482 r.UnLockValues();
00483
00484
00485
00486 r.Set("PIDFitEnergyType", "CC");
00487
00488
00489 r.Set("PIDFitPIDNbins", 20);
00490
00491 r.Set("PIDFitPIDmin", 0.2);
00492 r.Set("PIDFitPIDmax", 1.1);
00493
00494
00495
00496 r.Set("PIDFitTrueBinFactor", 5);
00497
00498
00499 r.Set("PIDFitWriteMCToFile", "");
00500 r.Set("PIDFitReadMCFromFile", "");
00501
00502
00503 r.Set("PIDFitDoFarNearCorrection", true);
00504
00505
00506
00507 r.Set("PIDFitEmulateFarNear", false);
00508
00509 r.LockValues();
00510 return r;
00511
00512 }
00513
00514
00515
00516 void NCExtrapolationPID::Prepare(const Registry& r)
00517 {
00518 NCExtrapolation::Prepare(r);
00519
00520 cout << "NCExtrapolationPID::Prepare" << endl;
00521 const char* tmps;
00522 int tmpi;
00523 double tmpd;
00524
00525 if (r.Get("PIDFitEnergyType", tmps)) {
00526 string energyType(tmps);
00527 assert(energyType=="NC" || energyType=="CC" || energyType=="Selection");
00528 fUseSelectionEnergy = (energyType=="Selection");
00529 fUseCCEnergy = (energyType=="CC");
00530 }
00531
00532 if (r.Get("PIDFitPIDNbins", tmpi)) fNumPIDBins=tmpi;
00533
00534
00535 if (r.Get("PIDFitPIDmin", tmpd)) fPIDmin=tmpd;
00536 if (r.Get("PIDFitPIDmax", tmpd)) fPIDmax=tmpd;
00537
00538 cout << "fNumPIDBins=" << fNumPIDBins
00539 << " PID range=[" << fPIDmin << ", " << fPIDmax << "]" << endl;
00540
00541 if (r.Get("PIDFitTrueBinFactor", tmpi)) fTrueBinFactor=tmpi;
00542 if (r.Get("PIDFitDoFarNearCorrection", tmpi)) fDoFarNearCorrection=tmpi;
00543
00544 if (r.Get("PIDFitEmulateFarNear", tmpi)) fEmulateFarNear=tmpi;
00545
00546 if (fEmulateFarNear) {
00547
00548
00549
00550 fNumPIDBins=-1;
00551 fUseSelectionEnergy=true;
00552
00553 if (r.Get("PIDFitPIDNbins", tmpi) &&
00554 tmpi!=DefaultConfig().GetInt("PIDFitPIDNbins"))
00555 cout << "WARNING: PIDFitPIDNbins was set, but overridden by PIDFitEmulateFarNear"
00556 << endl;
00557 }
00558
00559
00560 if (r.Get("PIDFitReadMCFromFile", tmps)) fMCInFilename=tmps;
00561 if (r.Get("PIDFitWriteMCToFile", tmps)) fMCOutFilename=tmps;
00562
00563 if (fMCInFilename!="") {
00564 ReadMCSpectra(fMCInFilename.c_str());
00565
00566
00567 for (SpecMap::iterator sit=fSpectra.begin(); sit!=fSpectra.end(); ++sit) {
00568 TH2F* h=sit->second->GetDataHist();
00569 h->Reset();
00570 }
00571 }
00572
00573 }
00574
00575
00576 void NCExtrapolationPID::Reset(bool nearData, bool farData,
00577 bool nearMC, bool farMC)
00578 {
00579 for (SpecMap::iterator sit=fSpectra.begin(); sit!=fSpectra.end(); ++sit) {
00580 if(nearData) sit->second->ResetNearData();
00581 if(farData) sit->second->ResetData();
00582 if(nearMC) sit->second->ResetNearMC();
00583 if(farMC) sit->second->ResetMC();
00584 }
00585 }
00586
00587
00588 bool NCExtrapolationPID::EnableNearToFarExtrapolation(bool enable)
00589 {
00590 const bool ret = fDoFarNearCorrection;
00591 fDoFarNearCorrection = enable;
00592 return ret;
00593 }
00594
00595
00596
00597 void NCExtrapolationPID::WriteMCSpectra(const char* filename)
00598 {
00599 TFile f(filename, "RECREATE");
00600 for (SpecMap::iterator sit=fSpectra.begin(); sit!=fSpectra.end(); ++sit) {
00601 sit->second->Write();
00602 }
00603 }
00604
00605
00606
00607 void NCExtrapolationPID::ReadMCSpectra(const char* filename)
00608 {
00609
00610 cout << "Reading MC spectra from file " << filename << endl;
00611 TFile f(filename);
00612
00613 TList *l = f.GetListOfKeys();
00614 TIter it(l);
00615 TObject *obj;
00616 TKey* k;
00617 while (( k=(TKey*)it() )) {
00618 obj=f.Get(k->GetName());
00619 cout << "Trying object " << obj->GetName() << endl;
00620 if (! (obj->IsA() == PIDSpectrum::Class()) ) {
00621 cerr << "Object with name " << obj->GetName() << " not a PIDSpectrum. Bailing" << endl;
00622 exit(1);
00623 }
00624 cout << "...object is a PIDSpectrum. Adding to list" << endl;
00625 PIDSpectrum* p=(PIDSpectrum*)obj;
00626
00627
00628 if (fEmulateFarNear) assert(p->GetNPIDBins()==2);
00629 else assert(p->GetNPIDBins()==fNumPIDBins);
00630
00631 fSpectra[p->GetBeamInfo()]=p;
00632 }
00633
00634 }
00635
00636
00637
00638 void NCExtrapolationPID::CopyBinContents(TH1D* hFrom, TH1F* hTo) const
00639 {
00640 for(int i = 0; i < kNumEnergyBinsFar; ++i)
00641 hTo->SetBinContent(i+1, hFrom->GetBinContent(i+1));
00642 }