50 _nll(
"input",
"-log(L) function",this,nllIn)
93 target->
setVal(var->getVal()) ;
104 std::cout <<
"Error: Must initialize data before initializing cache" << std::endl;
105 throw std::runtime_error(
"Uninitialized Data");
108 std::cout <<
"Error: Must initialize model pdf before initializing cache" << std::endl;
109 throw std::runtime_error(
"Uninitialized model pdf");
113 std::map< std::string, std::vector<double> > ChannelBinDataMap;
119 RooArgSet* obsSet = std::unique_ptr<RooArgSet>{
_pdf->getObservables(*
_data)}.release();
122 if( obsTerms.
empty() ) {
123 std::cout <<
"Error: Found no observable terms with pdf: " <<
_pdf->GetName()
124 <<
" using dataset: " <<
_data->GetName() << std::endl;
127 if( constraints.
empty() ) {
128 std::cout <<
"Error: Found no constraint terms with pdf: " <<
_pdf->GetName()
129 <<
" using dataset: " <<
_data->GetName() << std::endl;
135 auto channelCat =
static_cast<RooCategory const*
>(&simPdf->indexCat());
136 for (
const auto& nameIdx : *channelCat) {
140 RooAbsPdf* channelPdf = simPdf->getPdf(nameIdx.first.c_str());
141 std::string channel_name = channelPdf->
GetName();
147 if( ! hasStatUncert ) {
149 std::cout <<
"Channel: " << channel_name
150 <<
" doesn't have statistical uncertainties"
156 if(verbose) std::cout <<
"Found ParamHistFunc: " << param_func->
GetName() << std::endl;
163 int num_bins = param_func->
numBins();
168 std::vector<BarlowCache> temp_cache( num_bins );
169 bool channel_has_stat_uncertainty=
false;
171 for(
Int_t bin_index = 0; bin_index < num_bins; ++bin_index ) {
178 if(!gamma_stat)
throw std::runtime_error(
"ParamHistFunc contains non-RooRealVar, not supported in RooBarlowBeestonLL");
180 if(verbose) std::cout <<
"Ignoring constant gamma: " << gamma_stat->
GetName() << std::endl;
185 channel_has_stat_uncertainty=
true;
186 cache.
gamma = gamma_stat;
204 if( !tau || !pois_mean ) {
205 std::cout <<
"Failed to find pois mean or tau parameter for " << gamma_stat->
GetName() << std::endl;
209 std::cout <<
"Found pois mean and tau for parameter: " << gamma_stat->
GetName() <<
" tau: " << tau->
GetName()
210 <<
" " << tau->
getVal() <<
" pois_mean: " << pois_mean->
GetName() <<
" " << pois_mean->
getVal()
220 if( sum_pdf ==
nullptr ) {
221 std::cout <<
"Failed to find RooRealSumPdf in channel " << channel_name
222 <<
", therefor skipping this channel for analytic uncertainty minimization"
224 channel_has_stat_uncertainty=
false;
230 if( ChannelBinDataMap.find(channel_name) == ChannelBinDataMap.end() ) {
231 std::cout <<
"Error: channel with name: " << channel_name
232 <<
" not found in BinDataMap" << std::endl;
233 throw std::runtime_error(
"BinDataMap");
235 double nData = ChannelBinDataMap[channel_name].at(bin_index);
238 temp_cache.at(bin_index) = cache;
243 if( channel_has_stat_uncertainty ) {
244 std::cout <<
"Adding channel: " << channel_name
245 <<
" to the barlow cache" << std::endl;
303 bool stripDisconnected)
const {
309 for (
auto const& arg : outputSet) {
315 toRemove.
add( *arg );
319 for(
auto& arg : toRemove) outputSet.
remove( *arg,
true );
321 return errorInBaseCall ||
false;
329const RooArgSet& RooStats::HistFactory::RooBarlowBeestonLL::bestFitParams() const
332 return _paramAbsMin ;
338const RooArgSet& RooStats::HistFactory::RooBarlowBeestonLL::bestFitObs() const
396 std::map< std::string, std::vector< BarlowCache > >::iterator iter_cache;
399 std::string channel_name = (*iter_cache).first;
400 std::vector< BarlowCache >& channel_cache = (*iter_cache).second;
415 for(
unsigned int i = 0; i < channel_cache.size(); ++i ) {
421 std::vector< double > nu_b_vec( channel_cache.size() );
422 for(
unsigned int i = 0; i < channel_cache.size(); ++i ) {
432 nu_b_vec.at(i) = nu_b;
437 for(
unsigned int i = 0; i < channel_cache.size(); ++i ) {
443 std::vector< double > nu_b_stat_vec( channel_cache.size() );
444 for(
unsigned int i = 0; i < channel_cache.size(); ++i ) {
453 double nu_b_stat = sum_pdf->
getVal(*obsSet)*sum_pdf->
expectedEvents(*obsSet)*binVolume - nu_b_vec.at(i);
454 nu_b_stat_vec.at(i) = nu_b_stat;
465 for(
unsigned int i = 0; i < channel_cache.size(); ++i ) {
489 double nu_b = nu_b_vec.at(i);
490 double nu_b_stat = nu_b_stat_vec.at(i);
492 double tau_val = tau->
getVal();
493 double nData = bin_cache.
nData;
494 double m_val = pois_mean->
getVal();
497 double gamma_hat_hat = 1.0;
500 if(nu_b_stat > 0.00000001) {
502 double A = nu_b_stat*nu_b_stat + tau_val*nu_b_stat;
503 double B = nu_b*tau_val + nu_b*nu_b_stat - nData*nu_b_stat - m_val*nu_b_stat;
504 double C = -1*m_val*nu_b;
506 double discrim = B*B-4*A*C;
509 std::cout <<
"Warning: Discriminant (B*B - 4AC) < 0" << std::endl;
510 std::cout <<
"Warning: Taking B*B - 4*A*C == 0" << std::endl;
515 std::cout <<
"Warning: A <= 0" << std::endl;
516 throw std::runtime_error(
"BarlowBeestonLL::evaluate() : A < 0");
519 gamma_hat_hat = ( -1*B + std::sqrt(discrim) ) / (2*A);
525 gamma_hat_hat = m_val/tau_val;
530 std::cout <<
"ERROR: gamma hat hat is NAN" << std::endl;
531 throw std::runtime_error(
"BarlowBeestonLL::evaluate() : gamma hat hat is NAN");
534 if( gamma_hat_hat <= 0 ) {
535 std::cout <<
"WARNING: gamma hat hat <= 0. Setting to 0" << std::endl;
552 gamma->setVal( gamma_hat_hat );
582void RooStats::HistFactory::RooBarlowBeestonLL::validateAbsMin() const
584 // Check if constant status of any of the parameters have changed
588 while((par=(RooAbsArg*)_piter->Next())) {
589 if (_paramFixed[par->GetName()] != par->isConstant()) {
590 cxcoutI(Minimization) << "RooStats::HistFactory::RooBarlowBeestonLL::evaluate(" << GetName() << ") constant status of parameter " << par->GetName() << " has changed from "
591 << (_paramFixed[par->GetName()]?"fixed":"floating") << " to " << (par->isConstant()?"fixed":"floating")
592 << ", recalculating absolute minimum" << std::endl ;
593 _absMinValid = false ;
600 // If we don't have the absolute minimum w.r.t all observables, calculate that first
603 cxcoutI(Minimization) << "RooStats::HistFactory::RooBarlowBeestonLL::evaluate(" << GetName() << ") determining minimum likelihood for current configurations w.r.t all observable" << std::endl ;
606 // Save current values of non-marginalized parameters
607 std::unique_ptr<RooArgSet> obsStart{(RooArgSet*) _obs.snapshot(false)};
609 // Start from previous global minimum
610 if (_paramAbsMin.size()>0) {
611 const_cast<RooSetProxy&>(_par).assignValueOnly(_paramAbsMin) ;
613 if (_obsAbsMin.size()>0) {
614 const_cast<RooSetProxy&>(_obs).assignValueOnly(_obsAbsMin) ;
617 // Find minimum with all observables floating
618 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",false) ;
621 // Save value and remember
623 _absMinValid = true ;
625 // Save parameter values at abs minimum as well
626 _paramAbsMin.removeAll() ;
628 // Only store non-constant parameters here!
629 std::unique_ptr<RooArgSet> tmp{_par.selectByAttrib("Constant",false)};
630 _paramAbsMin.addClone(*tmp) ;
632 _obsAbsMin.addClone(_obs) ;
634 // Save constant status of all parameters
637 while((par=(RooAbsArg*)_piter->Next())) {
638 _paramFixed[par->GetName()] = par->isConstant() ;
641 if (dologI(Minimization)) {
642 cxcoutI(Minimization) << "RooStats::HistFactory::RooBarlowBeestonLL::evaluate(" << GetName() << ") minimum found at (" ;
647 while ((arg=(RooAbsReal*)_oiter->Next())) {
648 ccxcoutI(Minimization) << (first?"":", ") << arg->GetName() << "=" << arg->getVal() ;
651 ccxcoutI(Minimization) << ")" << std::endl ;
654 // Restore original parameter values
655 const_cast<RooSetProxy&>(_obs) = *obsStart ;
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
int Int_t
Signed integer 4 bytes (int).
A class which maps the current values of a RooRealVar (or a set of RooRealVars) to one of a number of...
const RooArgSet * get(Int_t masterIdx) const
RooAbsReal & getParameter() const
double binVolume(std::size_t i) const
bool isConstant() const
Check if the "Constant" attribute is set.
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
void reserve(Storage_t::size_type count)
Abstract interface for all probability density functions.
virtual double expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
RooAbsReal()
coverity[UNINIT_CTOR] Default constructor
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Object to represent discrete states.
Variable that can be changed from the outside.
void setVal(double value) override
Set value of variable to 'value'.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsReal * nom_pois_mean
void SetBinCenter() const
double evaluate() const override
Optimized implementation of createProfile for profile likelihoods.
bool getParameters(const RooArgSet *depList, RooArgSet &outputSet, bool stripDisconnected=true) const override
Fills a list with leaf nodes in the arg tree starting with ourself as top node that don't match any o...
std::set< std::string > _statUncertParams
std::map< std::string, bool > _paramFixed
Parameter constant status at last time of use.
std::map< std::string, std::vector< BarlowCache > > _barlowCache
void initializeBarlowCache()
RooBarlowBeestonLL(const char *name, const char *title, RooAbsReal &nll)
RooRealProxy _nll
Input -log(L) function.
const char * GetName() const override
Returns name of object.
bool getStatUncertaintyFromChannel(RooAbsPdf *channel, ParamHistFunc *¶mfunc, RooArgList *gammaList)
void FactorizeHistFactoryPdf(const RooArgSet &, RooAbsPdf &, RooArgList &, RooArgList &)
void getDataValuesForObservables(std::map< std::string, std::vector< double > > &ChannelBinDataMap, RooAbsData *data, RooAbsPdf *simPdf)
RooAbsPdf * getSumPdfFromChannel(RooAbsPdf *channel)
int getStatUncertaintyConstraintTerm(RooArgList *constraints, RooRealVar *gamma_stat, RooAbsReal *&pois_mean, RooRealVar *&tau)