Logo ROOT  
Reference Guide
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 
14 This class is designed to aid in the construction of RooDataSets and RooArgSets,
15 particularly those naturally arising in fitting operations. Typically, the usage
16 of this class is as follows:
17 
18 1. create DetailedOutputAggregator instance
19 2. use AppendArgSet to add value sets to be stored as one row of the dataset
20 3. call CommitSet when an entire row's worth of values has been added
21 4. repeat steps 2 and 3 until all rows have been added
22 5. 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 
36 namespace RooStats {
37 
39  // destructor
40  if (fResult != NULL) delete fResult;
41  if (fBuiltSet != NULL) 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 
50  RooArgSet * DetailedOutputAggregator::GetAsArgSet(RooFitResult *result, TString prefix, bool withErrorsAndPulls) {
51 
52  RooArgSet *detailedOutput = new RooArgSet;
53  const RooArgList &detOut = result->floatParsFinal();
54  const RooArgList &truthSet = result->floatParsInit();
55  TIterator *it = detOut.createIterator();
56  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>(it->Next())) {
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  delete it;
75 
76  // monitor a few more variables
77  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("minNLL"), TString().Append(prefix).Append("minNLL"), result->minNll() ) );
78  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("fitStatus"), TString().Append(prefix).Append("fitStatus"), result->status() ) );
79  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("covQual"), TString().Append(prefix).Append("covQual"), result->covQual() ) );
80  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("numInvalidNLLEval"), TString().Append(prefix).Append("numInvalidNLLEval"), result->numInvalidNLL() ) );
81  return detailedOutput;
82  }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// For each variable in aset, prepend prefix to its name and add
86 /// to the internal store. Note this will not appear in the produced
87 /// dataset unless CommitSet is called.
88 
90 
91  if (aset == NULL) {
92  // silently ignore
93  //std::cout << "Attempted to append NULL" << endl;
94  return;
95  }
96  if (fBuiltSet == NULL) {
97  fBuiltSet = new RooArgList();
98  }
99  TIterator* iter = aset->createIterator();
100  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>( iter->Next() ) ) {
101  TString renamed(TString::Format("%s%s", prefix.Data(), v->GetName()));
102  if (fResult == NULL) {
103  // we never committed, so by default all columns are expected to not exist
104  RooAbsArg* var = v->createFundamental();
105  assert(var != NULL);
106  (RooArgSet(*var)) = RooArgSet(*v);
107  var->SetName(renamed);
108  if (RooRealVar* rvar= dynamic_cast<RooRealVar*>(var)) {
109  if (v->getAttribute("StoreError")) var->setAttribute("StoreError");
110  else rvar->removeError();
111  if (v->getAttribute("StoreAsymError")) var->setAttribute("StoreAsymError");
112  else rvar->removeAsymError();
113  }
114  if (fBuiltSet->addOwned(*var)) continue; // OK - can skip past setting value
115  }
116  if (RooAbsArg* var = fBuiltSet->find(renamed)) {
117  // we already committed an argset once, so we expect all columns to already be in the set
118  var->SetName(v->GetName());
119  (RooArgSet(*var)) = RooArgSet(*v); // copy values and errors
120  var->SetName(renamed);
121  }
122  }
123  delete iter;
124  }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Commit to the result RooDataSet.
128 
130  if (fResult == NULL) {
131  // Store dataset as a tree - problem with VectorStore and StoreError (bug #94908)
132  RooRealVar wgt("weight","weight",1.0);
133  fResult = new RooDataSet("", "", RooArgSet(*fBuiltSet,wgt), RooFit::WeightVar(wgt));
134  }
135  fResult->add(RooArgSet(*fBuiltSet), weight);
137  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>( iter->Next() ) ) {
138  if (RooRealVar* var= dynamic_cast<RooRealVar*>(v)) {
139  // Invalidate values in case we don't set some of them next time round (eg. if fit not done)
140  var->setVal(std::numeric_limits<Double_t>::quiet_NaN());
141  var->removeError();
142  var->removeAsymError();
143  }
144  }
145  delete iter;
146  }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Returns all detailed output as a dataset.
150 /// Ownership of the dataset is transferred to the caller.
151 
153  RooDataSet* temp = NULL;
154  if( fResult ) {
155  temp = fResult;
156  fResult = NULL; // we no longer own the dataset
157  temp->SetNameTitle( name.Data(), title.Data() );
158  }else{
159  RooRealVar wgt("weight","weight",1.0);
160  temp = new RooDataSet(name.Data(), title.Data(), RooArgSet(wgt), RooFit::WeightVar(wgt));
161  }
162  delete fBuiltSet;
163  fBuiltSet = NULL;
164 
165  return temp;
166  }
167 
168 
169 } // end namespace RooStats
170 
RooStats::DetailedOutputAggregator::CommitSet
void CommitSet(double weight=1.0)
Commit to the result RooDataSet.
Definition: DetailedOutputAggregator.cxx:129
RooStats::DetailedOutputAggregator::AppendArgSet
void AppendArgSet(const RooAbsCollection *aset, TString prefix="")
For each variable in aset, prepend prefix to its name and add to the internal store.
Definition: DetailedOutputAggregator.cxx:89
RooStats::DetailedOutputAggregator::GetAsArgSet
static RooArgSet * GetAsArgSet(RooFitResult *result, TString prefix="", bool withErrorsAndPulls=false)
static function to translate the given fit result to a RooArgSet in a generic way.
Definition: DetailedOutputAggregator.cxx:50
RooFitResult::floatParsInit
const RooArgList & floatParsInit() const
Definition: RooFitResult.h:106
TString::Data
const char * Data() const
Definition: TString.h:369
DetailedOutputAggregator.h
RooArgList
Definition: RooArgList.h:21
RooFit::WeightVar
RooCmdArg WeightVar(const char *name, Bool_t reinterpretAsWeight=kFALSE)
Definition: RooGlobalFunc.cxx:123
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooAbsArg::SetName
void SetName(const char *name)
Set the name of the TNamed.
Definition: RooAbsArg.cxx:2218
RooFitResult::numInvalidNLL
Int_t numInvalidNLL() const
Definition: RooFitResult.h:90
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:813
RooArgSet::add
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooPullVar
Definition: RooPullVar.h:24
TString::Format
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:2311
TString
Definition: TString.h:136
v
@ v
Definition: rootcling_impl.cxx:3635
RooDataSet.h
TIterator
Definition: TIterator.h:30
RooFitResult
Definition: RooFitResult.h:40
RooDataSet::add
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0) override
Add a data point, with its coordinates specified in the 'data' argset, to the data set.
Definition: RooDataSet.cxx:1164
RooStats::DetailedOutputAggregator::GetAsDataSet
RooDataSet * GetAsDataSet(TString name, TString title)
Returns all detailed output as a dataset.
Definition: DetailedOutputAggregator.cxx:152
RooFitResult::status
Int_t status() const
Definition: RooFitResult.h:77
RooPullVar.h
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
RooDataSet::SetNameTitle
void SetNameTitle(const char *name, const char *title) override
Change the title of this dataset into the given name.
Definition: RooDataSet.cxx:2001
RooFitResult::covQual
Int_t covQual() const
Definition: RooFitResult.h:86
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:118
RooAbsCollection
Definition: RooAbsCollection.h:30
RooRealVar.h
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
RooAbsCollection::addOwned
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:389
RooFitResult.h
TIterator::Next
virtual TObject * Next()=0
RooStats::DetailedOutputAggregator::~DetailedOutputAggregator
virtual ~DetailedOutputAggregator()
Definition: DetailedOutputAggregator.cxx:38
RooStats::DetailedOutputAggregator::fBuiltSet
RooArgList * fBuiltSet
Definition: DetailedOutputAggregator.h:61
RooStats
Definition: Asimov.h:19
RooAbsArg::setAttribute
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:259
RooStats::DetailedOutputAggregator::fResult
RooDataSet * fResult
Definition: DetailedOutputAggregator.h:60
name
char name[80]
Definition: TGX11.cxx:110
RooFitResult::floatParsFinal
const RooArgList & floatParsFinal() const
Definition: RooFitResult.h:110
RooDataSet
Definition: RooDataSet.h:33
RooAbsArg
Definition: RooAbsArg.h:73
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooRealVar
Definition: RooRealVar.h:36
RooFitResult::minNll
Double_t minNll() const
Definition: RooFitResult.h:98
RooArgSet
Definition: RooArgSet.h:28