11 #include "OscLib/IOscCalc.h"
17 #include "Minuit2/StackAllocator.h"
18 #include "TObjArray.h"
24 #include "TObjString.h"
25 #include "TCollection.h"
34 return islog ? ax->GetBinCenterLog(bin) : ax->GetBinCenter(bin);
42 const std::vector<const IFitVar*>& profVars,
43 const std::vector<const ISyst*>& profSysts,
44 const std::map<
const IFitVar*, std::vector<double>>& seedPts,
45 const std::vector<SystShifts>& systSeedPts,
48 : fParallel(parallel), fPrec(prec), fLogX(xax.islog), fLogY(yax.islog)
54 for(
unsigned int i = 0; i < profVars.size()+profSysts.size(); ++i){
56 if(i < profVars.size())
59 title = profSysts[i-profVars.size()]->LatexName();
69 if(!profVars.empty() || !profSysts.empty()){
70 progTitle +=
" (profiling ";
72 for(
const IFitVar* v: profVars) {
73 progTitle += v->ShortName() +
", ";
76 for(
const ISyst*
s: profSysts)
77 progTitle +=
s->ShortName() +
", ";
80 progTitle.resize(progTitle.size()-2);
84 FillSurface(progTitle, expt, calc, xax, yax, profVars, profSysts, seedPts, systSeedPts);
88 int minx = xax.
nbins/2;
89 int miny = yax.
nbins/2;
90 for(
int x = 1;
x < xax.
nbins+1; ++
x){
91 for(
int y = 1;
y < yax.
nbins+1; ++
y){
92 const int bin = (
y-1)*
fHist->GetNbinsX() + (
x-1);
95 const double chi =
fHist->GetBinContent(
x,
y);
96 if(chi < minchi && !isnan(chi) && !isinf(chi)){
104 std::vector<const IFitVar*> allVars = {xax.
var, yax.
var};
105 allVars.insert(allVars.end(), profVars.begin(), profVars.end());
106 Fitter fit(expt, allVars, profSysts);
117 for(
int x = 0;
x < xax.
nbins+2; ++
x){
118 for(
int y = 0;
y < yax.
nbins+2; ++
y){
123 fHist->SetMinimum(0);
129 const IFitVar* xvar,
int nbinsx,
double xmin,
double xmax,
130 const IFitVar* yvar,
int nbinsy,
double ymin,
double ymax,
131 const std::vector<const IFitVar*>& profVars,
132 const std::vector<const ISyst*>& profSysts,
133 const std::map<
const IFitVar*, std::vector<double>>& seedPts,
134 const std::vector<SystShifts>& systSeedPts,
138 FitAxis(xvar, nbinsx, xmin, xmax),
139 FitAxis(yvar, nbinsy, ymin, ymax),
140 profVars, profSysts, seedPts, systSeedPts, parallel, prec)
149 const std::vector<const IFitVar*>& profVars,
150 const std::vector<const ISyst*>& profSysts,
151 const std::map<
const IFitVar*, std::vector<double>>& seedPts,
152 const std::vector<SystShifts>& systSeedPts)
154 if(
fParallel && !(profVars.empty() && profSysts.empty())){
158 ROOT::Minuit2::StackAllocatorHolder::Get();
175 const int Nx =
fHist->GetNbinsX();
176 const int Ny =
fHist->GetNbinsY();
184 if((Nx*Ny)%step == 0) step = 1;
199 const int x = bin%Nx+1;
200 const int y = bin/Nx+1;
214 <<
" has penalty of " << xax.
var->
Penalty(xv, calc)
215 <<
" that could have been applied in surface. "
216 <<
"This should never happen." << std::endl;
220 <<
" has penalty of " << yax.
var->
Penalty(yv, calc)
221 <<
" that could have been applied in surface. "
222 <<
"This should never happen." << std::endl;
229 profVars, profSysts, seedPts, systSeedPts);
234 profVars, profSysts, seedPts, systSeedPts);
236 prog->
SetProgress(neval*grid_stride/
double(Nx*Ny));
239 if(grid_stride > 1)
fBinMask.push_back(bin);
241 for(
int i = 0; i < grid_stride; ++i){
242 bin = (bin + step) % (Nx * Ny);
269 std::cout <<
"Surface::OscCalcNoHash not copyable." << std::endl;
272 double P(
int a,
int b,
double E)
override {
return fCalc->P(a, b, E);}
281 void Set##v(const double& x) override {fCalc->Set##v(x);}\
282 double Get##v() const override {return fCalc->Get##v();}
283 F(Dmsq21);
F(Dmsq32);
F(Th12);
F(Th13);
F(Th23);
F(dCP);
295 const std::vector<const IFitVar*>& profVars,
296 const std::vector<const ISyst*>& profSysts,
297 const std::map<
const IFitVar*, std::vector<double>>& seedPts,
298 const std::vector<SystShifts>& systSeedPts)
316 if(profVars.empty() && profSysts.empty()){
320 Fitter fitter(expt, profVars, profSysts);
325 for(
unsigned int i = 0; i < profVars.size(); ++i){
326 fProfHists[i]->Fill(x, y, profVars[i]->GetValue(calc));
328 for(
unsigned int j = 0; j < profSysts.size(); ++j){
333 fHist->Fill(x, y, chi);
348 if(gPad && !gPad->GetListOfPrimitives()->IsEmpty())
return;
362 const TAxis* ax =
fHist->GetXaxis();
363 const TAxis* ay =
fHist->GetYaxis();
364 const double Nx = ax->GetNbins();
365 const double Ny = ay->GetNbins();
370 TString::Format(
";%s;%s",
371 ax->GetTitle(), ay->GetTitle()),
378 axes->SetMinimum(
fHist->GetMinimum());
379 axes->SetMaximum(
fHist->GetMaximum());
382 axes->SetTitle(
fHist->GetTitle());
383 axes->GetXaxis()->SetLabelSize(ax->GetLabelSize());
384 axes->GetYaxis()->SetLabelSize(ay->GetLabelSize());
385 axes->GetXaxis()->SetLabelOffset(ax->GetLabelOffset());
386 axes->GetYaxis()->SetLabelOffset(ay->GetLabelOffset());
387 axes->GetXaxis()->CenterTitle();
388 axes->GetYaxis()->CenterTitle();
390 if(
fLogX) gPad->SetLogx();
391 if(
fLogY) gPad->SetLogy();
401 fHist->Draw(
"colz same");
415 TMarker* mark =
new TMarker(
fMinX,
fMinY, marker);
416 mark->SetMarkerSize(1.5);
417 mark->SetMarkerColor(color);
427 std::vector<TGraph*> ret;
429 if(minchi < 0) minchi =
fMinChi;
433 TVirtualPad* bak = gPad;
435 const bool wasbatch = gROOT->IsBatch();
439 gStyle->SetOptStat(0);
441 const double level = minchi-
fMinChi;
442 surf->SetContour(1, &level);
443 surf->Draw(
"cont list");
448 gROOT->SetBatch(wasbatch);
455 TCollection* specs = gROOT->GetListOfSpecials();
457 TIter nextSpec(specs);
458 while(TObject* spec = nextSpec()){
459 if(!spec->InheritsFrom(TObjArray::Class()))
continue;
460 TObjArray* conts = (TObjArray*)spec;
462 if(conts->IsEmpty())
continue;
464 if(!conts->At(0)->InheritsFrom(TList::Class()))
continue;
465 TList* cont = (TList*)conts->At(0);
469 while(TObject* obj = nextObj()){
470 if(!obj->InheritsFrom(TGraph::Class()))
continue;
472 ret.push_back((TGraph*)obj->Clone(
UniqueName().c_str()));
486 std::vector<TGraph*> gs =
GetGraphs(fc, minchi);
490 g->SetLineStyle(style);
491 g->SetLineColor(color);
504 TH2F* ret =
new TH2F(*
fHist);
507 for(
int x = 0;
x < ret->GetNbinsX()+2; ++
x){
508 for(
int y = 0;
y < ret->GetNbinsY()+2; ++
y){
509 ret->SetBinContent(
x,
y, ret->GetBinContent(
x,
y)+
fMinChi-minchi);
520 fHist->SetTitle(str);
529 for(
int x = 0;
x < h->GetNbinsX()+2; ++
x)
530 for(
int y = 0;
y < h->GetNbinsY()+2; ++
y)
531 h->SetBinContent(
x,
y, level);
561 TDirectory* tmp = gDirectory;
563 TObjString(
"Surface").Write(
"type");
569 v.Write(
"minValues");
571 fHist->Write(
"hist");
576 TDirectory* profDir = dir->mkdir(
"profHists");
580 it->Write( TString::Format(
"hist%d", idx++));
585 TVectorD
m(tmp.size(), &tmp[0]);
590 TObjString(
fLogX ?
"yes" :
"no").Write(
"logx");
591 TObjString(
fLogY ?
"yes" :
"no").Write(
"logy");
601 TObjString* tag = (TObjString*)dir->Get(
"type");
603 assert(tag->GetString() ==
"Surface");
605 const TVectorD v = *(TVectorD*)dir->Get(
"minValues");
606 const TVectorD
s = *(TVectorD*)dir->Get(
"seeds");
607 const TVectorD*
m = (TVectorD*)dir->Get(
"mask");
609 std::unique_ptr<Surface> surf(
new Surface);
610 surf->fHist = (TH2F*)dir->Get(
"hist");
611 surf->fMinChi = v[0];
615 for(
int idx = 0; idx < s.GetNrows(); ++idx){
616 surf->fSeedValues.push_back(s[idx]);
619 for(
int idx = 0; ; ++idx){
620 TH2*
h = (TH2*)dir->Get(TString::Format(
"profHists/hist%d", idx));
621 if(h) surf->fProfHists.push_back(h);
else break;
625 for(
int idx = 0; idx < m->GetNrows(); ++idx)
626 surf->fBinMask.push_back((*m)[idx]);
629 const TObjString* logx = (TObjString*)dir->Get(
"logx");
630 const TObjString* logy = (TObjString*)dir->Get(
"logy");
632 surf->fLogX = (logx && logx->GetString() ==
"yes");
633 surf->fLogY = (logy && logy->GetString() ==
"yes");
642 std::vector<std::unique_ptr<Surface>> surfs;
643 for(TFile* f: files) {
647 int Nx = surfs[0]->fHist->GetNbinsX();
648 int Ny = surfs[0]->fHist->GetNbinsY();
649 size_t nbins = Nx * Ny;
650 std::vector<int> binMask;
653 for(
size_t i = 0; i < surfs.size(); ++i) {
656 assert(
false &&
"Repeated bin found in surfaces being merged.");
657 binMask.push_back(
bin);
660 if(binMask.size() != nbins) {
661 std::cout <<
"Missing bins found in surfaces being merged. "
662 <<
"Are you sure you included all files for this surface?"
668 std::unique_ptr<Surface> ret(
new Surface);
669 ret->fHist =
new TH2F(*(TH2F*)surfs[0]->
ToTH2(0));
670 ret->fMinChi = surfs[0]->fMinChi;
671 ret->fMinX = surfs[0]->fMinX;
672 ret->fMinY = surfs[0]->fMinY;
673 ret->fLogX = surfs[0]->fLogX;
674 ret->fLogY = surfs[0]->fLogY;
675 ret->fSeedValues = surfs[0]->fSeedValues;
677 ret->fProfHists.push_back((TH2F*)
h->Clone());
680 for(
size_t i = 1; i < surfs.size(); ++i) {
681 ret->fHist->Add(surfs[i]->
ToTH2(0));
682 if(surfs[i]->fMinChi < ret->
fMinChi) {
683 ret->fMinChi = surfs[i]->fMinChi;
684 ret->fMinX = surfs[i]->fMinX;
685 ret->fMinY = surfs[i]->fMinY;
687 for(
size_t j = 0; j < ret->fProfHists.size(); ++j)
688 ret->fProfHists[j]->Add(surfs[i]->fProfHists[j]);
692 for(
int x = 0;
x < ret->fHist->GetNbinsX()+2; ++
x){
693 for(
int y = 0;
y < ret->fHist->GetNbinsY()+2; ++
y){
694 ret->fHist->SetBinContent(
x,
y, ret->fHist->GetBinContent(
x,
y)-ret->fMinChi);
698 ret->fHist->SetMinimum(0);
707 std::vector<std::string> fileNames =
Wildcard(wildcard);
708 std::vector<TFile*> files;
709 for(std::string
fileName : fileNames){
710 files.push_back(TFile::Open(
fileName.c_str()));
713 for(TFile* f: files)
delete f;
721 std::cout <<
"Cannot call " << func <<
"() on a partial surface!" << std::endl;
double BinCenter(const TAxis *ax, int bin, bool islog)
void Finish()
Wait for all threads to complete before returning.
TH2 * Gaussian2Sigma1D(const Surface &s)
Up-value surface for 2 sigma confidence in 1D in gaussian approximation.
TH2 * Gaussian2Sigma2D(const Surface &s)
Up-value surface for 2 sigma confidence in 2D in gaussian approximation.
virtual std::string LatexName() const =0
void DrawBestFit(Color_t color, Int_t marker=kFullCircle) const
Draw the best fit point.
virtual double GetValue(const osc::IOscCalcAdjustable *osc) const =0
virtual std::string ShortName() const =0
Helper for Surface::FillSurfacePoint.
process_name opflash particleana ie x
BEGIN_PROLOG could also be cerr
TMD5 * GetParamsHash() const override
Marks this calc "unhashable" so the cacheing won't occur.
Log-likelihood scan across two parameters.
TH2 * Gaussian90Percent1D(const Surface &s)
Up-value surface for 90% confidence in 1D in gaussian approximation.
void SaveTo(TDirectory *dir) const
std::vector< int > fBinMask
Simple record of shifts applied to systematic parameters.
virtual double ChiSq(osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const =0
void FillSurfacePoint(const IExperiment *expt, osc::IOscCalcAdjustable *calc, const FitAxis &xax, double x, const FitAxis &yax, double y, const std::vector< const IFitVar * > &profVars, const std::vector< const ISyst * > &profSysts, const std::map< const IFitVar *, std::vector< double >> &seedPts, const std::vector< SystShifts > &systSeedPts)
osc::IOscCalcAdjustable * fCalc
TH2 * Gaussian95Percent2D(const Surface &s)
Up-value surface for 95% confidence in 2D in gaussian approximation.
void DrawContour(TH2 *fc, Style_t style, Color_t color, double minchi=-1)
double Fit(osc::IOscCalcAdjustable *seed, SystShifts &bestSysts=junkShifts, const std::map< const IFitVar *, std::vector< double >> &seedPts={}, const std::vector< SystShifts > &systSeedPts={}, Verbosity verb=kVerbose) const
TH2 * Gaussian68Percent1D(const Surface &s)
Up-value surface for 68% confidence in 1D in gaussian approximation.
TH2 * Gaussian68Percent2D(const Surface &s)
Up-value surface for 68% confidence in 2D in gaussian approximation.
void SetRho(double x) override
std::vector< std::string > Wildcard(const std::string &wildcardString)
void SetPrecision(Precision prec)
process_name opflashCryoW ana
TH2 * Gaussian90Percent2D(const Surface &s)
Up-value surface for 90% confidence in 2D in gaussian approximation.
static SystShifts Nominal()
void FillSurface(const std::string &progTitle, const IExperiment *expt, osc::IOscCalcAdjustable *calc, const FitAxis &xax, const FitAxis &yax, const std::vector< const IFitVar * > &profVars, const std::vector< const ISyst * > &profSysts, const std::map< const IFitVar *, std::vector< double >> &seedPts, const std::vector< SystShifts > &systSeedPts)
Encapsulate code to systematically shift a caf::StandardRecord.
tuple m
now if test mode generate materials, CRT shell, world, gdml header else just generate CRT shell for u...
Perform MINUIT fits in one or two dimensions.
TH2 * Gaussian95Percent1D(const Surface &s)
Up-value surface for 95% confidence in 1D in gaussian approximation.
TH2 * Gaussian3Sigma1D(const Surface &s)
Up-value surface for 3 sigma confidence in 1D in gaussian approximation.
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
TH2 * Gaussian5Sigma2D(const Surface &s)
Up-value surface for 5 sigma confidence in 2D in gaussian approximation.
process_name pandoraGausCryo1 vertexChargeCryo1 vertexStubCryo1 xmin
static std::unique_ptr< Surface > LoadFromMulti(const std::vector< TFile * > &files, const std::string &label)
TH2 * Gaussian99Percent2D(const Surface &s)
Up-value surface for 99% confidence in 2D in gaussian approximation.
TH2F * ExpandedHistogram(const std::string &title, int nbinsx, double xmin, double xmax, bool xlog, int nbinsy, double ymin, double ymax, bool ylog)
OscCalcNoHash(osc::IOscCalcAdjustable *c)
process_name opflash particleana ie ie y
virtual void SetValue(osc::IOscCalcAdjustable *osc, double val) const =0
static std::unique_ptr< Surface > LoadFrom(TDirectory *dir)
TH2 * Gaussian3Sigma2D(const Surface &s)
Up-value surface for 3 sigma confidence in 2D in gaussian approximation.
virtual double Penalty(double, osc::IOscCalcAdjustable *) const
void AddMemberTask(T *obj, M meth, A...args)
Add member function task, with arguments.
TH2 * Gaussian99Percent1D1Sided(const Surface &s)
Up-value surface for 99% confidence in 1D in 1-sided gaussian approxiamtion.
TH2 * Gaussian90Percent1D1Sided(const Surface &s)
Up-value surface for 90% confidence in 1D in 1-sided gaussian approximation.
TH2 * Gaussian95Percent1D1Sided(const Surface &s)
Up-value surface for 95% confidence in 1D in 1-sided gaussian approximation.
void CheckMask(const std::string &func) const
double GetShift(const ISyst *syst) const
osc::IOscCalcAdjustable * Copy() const override
std::vector< double > fSeedValues
void SetProgress(double frac)
Update the progress fraction between zero and one.
then echo File list $list not found else cat $list while read file do echo $file sed s
services TFileService fileName
TH2 * Gaussian3Sigma1D1Sided(const Surface &s)
Up-value surface for 3 sigma confidence in 1D in 1-sided gaussian approximation.
std::vector< TH2 * > fProfHists
Base class defining interface for experiments.
TH2 * Gaussian5Sigma1D1Sided(const Surface &s)
Up-value surface for 5 sigma confidence in 1D in 1-sided gaussian approximation.
void Draw() const
Draw the surface itself.
A very simple thread pool for use by Surface.
A simple ascii-art progress bar.
void ShowProgress(const std::string &title)
Interface definition for fittable variables.
TH2F * ToTH2(double minchi=-1) const
then echo ***************************************echo Variable FHICL_FILE_PATH not found echo You porbably haven t set up larsoft echo Try setup uboonecode vXX_XX_XX q e10
void SetL(double x) override
TH2 * Flat(double level, const Surface &s)
Helper function for the gaussian approximation surfaces.
std::size_t count(Cont const &cont)
Prevent histograms being added to the current directory.
void Done()
Call this when action is completed.
TH2 * Gaussian99Percent1D(const Surface &s)
Up-value surface for 99% confidence in 1D in gaussian approximation.
std::string UniqueName()
Return a different string each time, for creating histograms.
std::string LatexName(int pdgcode)
Convert PDG code to a latex string (root-style)
BEGIN_PROLOG could also be cout
std::vector< TGraph * > GetGraphs(TH2 *fc, double minchi=-1)
For expert use, custom painting of contours.
Collect information describing the axis of a fit variable.
void SetTitle(const char *str)
double P(int a, int b, double E) override