Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DetailedOutputAggregator.cxx
Go to the documentation of this file.
1// @(#)root/roostats:$Id$
2// Author: Sven Kreiss, Kyle Cranmer, Lorenzo Moneta Nov 2010
3/*************************************************************************
4 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11/** \class RooStats::DetailedOutputAggregator
12 \ingroup Roostats
13
14This class is designed to aid in the construction of RooDataSets and RooArgSets,
15particularly those naturally arising in fitting operations. Typically, the usage
16of this class is as follows:
17
181. create DetailedOutputAggregator instance
192. use AppendArgSet to add value sets to be stored as one row of the dataset
203. call CommitSet when an entire row's worth of values has been added
214. repeat steps 2 and 3 until all rows have been added
225. call GetAsDataSet to extract result RooDataSet
23
24*/
25
26#include <limits>
27
28
29#include "RooFitResult.h"
30#include "RooPullVar.h"
31#include "RooRealVar.h"
32#include "RooDataSet.h"
33
35
36namespace RooStats {
37
39 // destructor
40 if (fResult != nullptr) delete fResult;
41 if (fBuiltSet != nullptr) delete fBuiltSet;
42 }
43
44////////////////////////////////////////////////////////////////////////////////
45/// static function to translate the given fit result to a RooArgSet in a generic way.
46/// Prefix is prepended to all variable names.
47/// LM: caller is responsible to delete the returned list and eventually also the content of the list
48/// Note that the returned list is not owning the returned content
49
51
52 RooArgSet *detailedOutput = new RooArgSet;
53 const RooArgList &detOut = result->floatParsFinal();
54 const RooArgList &truthSet = result->floatParsInit();
55
56 for (RooAbsArg* v : detOut) {
57 RooAbsArg* clone = v->cloneTree(TString().Append(prefix).Append(v->GetName()));
58 clone->SetTitle( TString().Append(prefix).Append(v->GetTitle()) );
59 RooRealVar* var = dynamic_cast<RooRealVar*>(v);
60 if (var) clone->setAttribute("StoreError");
61 detailedOutput->add(*clone);
62
63 if( withErrorsAndPulls && var ) {
64 clone->setAttribute("StoreAsymError");
65
66 TString pullname = TString().Append(prefix).Append(TString::Format("%s_pull", var->GetName()));
67 // TString pulldesc = TString::Format("%s pull for fit %u", var->GetTitle(), fitNumber);
68 RooRealVar* truth = dynamic_cast<RooRealVar*>(truthSet.find(var->GetName()));
69 RooPullVar pulltemp("temppull", "temppull", *var, *truth);
70 RooRealVar* pull = new RooRealVar(pullname, pullname, pulltemp.getVal());
71 detailedOutput->add(*pull);
72 }
73 }
74
75 // monitor a few more variables
76 detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("minNLL"), TString().Append(prefix).Append("minNLL"), result->minNll() ) );
77 detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("fitStatus"), TString().Append(prefix).Append("fitStatus"), result->status() ) );
78 detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("covQual"), TString().Append(prefix).Append("covQual"), result->covQual() ) );
79 detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("numInvalidNLLEval"), TString().Append(prefix).Append("numInvalidNLLEval"), result->numInvalidNLL() ) );
80 return detailedOutput;
81 }
82
83////////////////////////////////////////////////////////////////////////////////
84/// For each variable in aset, prepend prefix to its name and add
85/// to the internal store. Note this will not appear in the produced
86/// dataset unless CommitSet is called.
87
89
90 if (aset == nullptr) {
91 // silently ignore
92 //std::cout << "Attempted to append nullptr" << endl;
93 return;
94 }
95 if (fBuiltSet == nullptr) {
96 fBuiltSet = new RooArgList();
97 }
98 for (const RooAbsArg* v : *aset) {
99 TString renamed(TString::Format("%s%s", prefix.Data(), v->GetName()));
100 if (fResult == nullptr) {
101 // we never committed, so by default all columns are expected to not exist
102 std::unique_ptr<RooAbsArg> var{v->createFundamental()};
103 assert(var != nullptr);
104 RooArgSet(*var).assign(RooArgSet(*v));
105 var->SetName(renamed);
106 if (RooRealVar* rvar= dynamic_cast<RooRealVar*>(var.get())) {
107 if (v->getAttribute("StoreError")) var->setAttribute("StoreError");
108 else rvar->removeError();
109 if (v->getAttribute("StoreAsymError")) var->setAttribute("StoreAsymError");
110 else rvar->removeAsymError();
111 }
112 if (fBuiltSet->addOwned(std::move(var))) continue; // OK - can skip past setting value
113 }
114 if (RooAbsArg* var = fBuiltSet->find(renamed)) {
115 // we already committed an argset once, so we expect all columns to already be in the set
116 var->SetName(v->GetName());
117 RooArgSet(*var).assign(RooArgSet(*v)); // copy values and errors
118 var->SetName(renamed);
119 }
120 }
121
122 }
123
124////////////////////////////////////////////////////////////////////////////////
125/// Commit to the result RooDataSet.
126
128 if (fResult == nullptr) {
129 // Store dataset as a tree - problem with VectorStore and StoreError (bug #94908)
131 }
132 fResult->add(RooArgSet(*fBuiltSet), weight);
133
134 for (RooAbsArg* v : *fBuiltSet) {
135 if (RooRealVar* var= dynamic_cast<RooRealVar*>(v)) {
136 // Invalidate values in case we don't set some of them next time round (eg. if fit not done)
137 var->setVal(std::numeric_limits<double>::quiet_NaN());
138 var->removeError();
139 var->removeAsymError();
140 }
141 }
142 }
143
144////////////////////////////////////////////////////////////////////////////////
145/// Returns all detailed output as a dataset.
146/// Ownership of the dataset is transferred to the caller.
147
149 RooDataSet* temp = nullptr;
150 if( fResult ) {
151 temp = fResult;
152 fResult = nullptr; // we no longer own the dataset
153 temp->SetNameTitle( name.Data(), title.Data() );
154 }else{
155 temp = new RooDataSet(name.Data(), title.Data(), {}, RooFit::WeightVar());
156 }
157 delete fBuiltSet;
158 fBuiltSet = nullptr;
159
160 return temp;
161 }
162
163
164} // end namespace RooStats
TObject * clone(const char *newname) const override
Definition RooChi2Var.h:9
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
RooAbsArg * find(const char *name) const
Find object with given name in list.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Container class to hold unbinned data.
Definition RooDataSet.h:57
void SetNameTitle(const char *name, const char *title) override
Change the title of this dataset into the given name.
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Represents the pull of a measurement w.r.t.
Definition RooPullVar.h:24
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void AppendArgSet(const RooAbsCollection *aset, TString prefix="")
For each variable in aset, prepend prefix to its name and add to the internal store.
RooDataSet * GetAsDataSet(TString name, TString title)
Returns all detailed output as a dataset.
void CommitSet(double weight=1.0)
Commit to the result RooDataSet.
static RooArgSet * GetAsArgSet(RooFitResult *result, TString prefix="", bool withErrorsAndPulls=false)
Translate the given fit result to a RooArgSet in a generic way.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & Append(const char *cs)
Definition TString.h:572
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
Namespace for the RooStats classes.
Definition Asimov.h:19