ROOT  6.06/09
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 // implementation file of DetailedOutputAggregator
12 
13 #include <limits>
14 
15 
16 #include "RooFitResult.h"
17 #include "RooPullVar.h"
18 #include "RooRealVar.h"
19 #include "RooDataSet.h"
20 
22 
23 namespace RooStats {
24 
26  // destructor
27  if (fResult != NULL) delete fResult;
28  if (fBuiltSet != NULL) delete fBuiltSet;
29  }
30 
31 
33  // static function to translate the given fit result to a RooArgSet in a generic way.
34  // Prefix is prepended to all variable names.
35  // LM: caller is responsible to delete the returned list and eventually also the content of the list
36  // Note that the returned list is not owning the returned content
37  RooArgSet *detailedOutput = new RooArgSet;
38  const RooArgList &detOut = result->floatParsFinal();
39  const RooArgList &truthSet = result->floatParsInit();
40  TIterator *it = detOut.createIterator();
41  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>(it->Next())) {
42  RooAbsArg* clone = v->cloneTree(TString().Append(prefix).Append(v->GetName()));
43  clone->SetTitle( TString().Append(prefix).Append(v->GetTitle()) );
44  RooRealVar* var = dynamic_cast<RooRealVar*>(v);
45  if (var) clone->setAttribute("StoreError");
46  detailedOutput->add(*clone);
47 
48  if( withErrorsAndPulls && var ) {
49  clone->setAttribute("StoreAsymError");
50 
51  TString pullname = TString().Append(prefix).Append(TString::Format("%s_pull", var->GetName()));
52  // TString pulldesc = TString::Format("%s pull for fit %u", var->GetTitle(), fitNumber);
53  RooRealVar* truth = dynamic_cast<RooRealVar*>(truthSet.find(var->GetName()));
54  RooPullVar pulltemp("temppull", "temppull", *var, *truth);
55  RooRealVar* pull = new RooRealVar(pullname, pullname, pulltemp.getVal());
56  detailedOutput->add(*pull);
57  }
58  }
59  delete it;
60 
61  // monitor a few more variables
62  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("minNLL"), TString().Append(prefix).Append("minNLL"), result->minNll() ) );
63  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("fitStatus"), TString().Append(prefix).Append("fitStatus"), result->status() ) );
64  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("covQual"), TString().Append(prefix).Append("covQual"), result->covQual() ) );
65  detailedOutput->add( *new RooRealVar(TString().Append(prefix).Append("numInvalidNLLEval"), TString().Append(prefix).Append("numInvalidNLLEval"), result->numInvalidNLL() ) );
66  return detailedOutput;
67  }
68 
70  // For each variable in aset, prepend prefix to its name and add
71  // to the internal store. Note this will not appear in the produced
72  // dataset unless CommitSet is called.
73 
74  if (aset == NULL) {
75  // silently ignore
76  //std::cout << "Attempted to append NULL" << endl;
77  return;
78  }
79  if (fBuiltSet == NULL) {
80  fBuiltSet = new RooArgList();
81  }
82  TIterator* iter = aset->createIterator();
83  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>( iter->Next() ) ) {
84  TString renamed(TString::Format("%s%s", prefix.Data(), v->GetName()));
85  if (fResult == NULL) {
86  // we never commited, so by default all columns are expected to not exist
87  RooAbsArg* var = v->createFundamental();
88  assert(var != NULL);
89  (RooArgSet(*var)) = RooArgSet(*v);
90  var->SetName(renamed);
91  if (RooRealVar* rvar= dynamic_cast<RooRealVar*>(var)) {
92  if (v->getAttribute("StoreError")) var->setAttribute("StoreError");
93  else rvar->removeError();
94  if (v->getAttribute("StoreAsymError")) var->setAttribute("StoreAsymError");
95  else rvar->removeAsymError();
96  }
97  if (fBuiltSet->addOwned(*var)) continue; // OK - can skip past setting value
98  }
99  if (RooAbsArg* var = fBuiltSet->find(renamed)) {
100  // we already commited an argset once, so we expect all columns to already be in the set
101  var->SetName(v->GetName());
102  (RooArgSet(*var)) = RooArgSet(*v); // copy values and errors
103  var->SetName(renamed);
104  }
105  }
106  delete iter;
107  }
108 
109  // Commit to the result RooDataSet.
111  if (fResult == NULL) {
112  // Store dataset as a tree - problem with VectorStore and StoreError (bug #94908)
113  RooRealVar wgt("weight","weight",1.0);
114  fResult = new RooDataSet("", "", RooArgSet(*fBuiltSet,wgt), RooFit::WeightVar(wgt));
115  }
116  fResult->add(RooArgSet(*fBuiltSet), weight);
118  while(RooAbsArg* v = dynamic_cast<RooAbsArg*>( iter->Next() ) ) {
119  if (RooRealVar* var= dynamic_cast<RooRealVar*>(v)) {
120  // Invalidate values in case we don't set some of them next time round (eg. if fit not done)
121  var->setVal(std::numeric_limits<Double_t>::quiet_NaN());
122  var->removeError();
123  var->removeAsymError();
124  }
125  }
126  delete iter;
127  }
128 
129 
131  // Returns all detailed output as a dataset.
132  // Ownership of the dataset is transferred to the caller.
133  RooDataSet* temp = NULL;
134  if( fResult ) {
135  temp = fResult;
136  fResult = NULL; // we no longer own the dataset
137  temp->SetNameTitle( name.Data(), title.Data() );
138  }else{
139  RooRealVar wgt("weight","weight",1.0);
140  temp = new RooDataSet(name.Data(), title.Data(), RooArgSet(wgt), RooFit::WeightVar(wgt));
141  }
142  delete fBuiltSet;
143  fBuiltSet = NULL;
144 
145  return temp;
146  }
147 
148 
149 } // end namespace RooStats
150 
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:265
const RooArgList & floatParsFinal() const
Definition: RooFitResult.h:109
static int pull(FILE *fp, struct mg_connection *conn, char *buf, int len)
Definition: civetweb.c:1954
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
#define assert(cond)
Definition: unittest.h:542
Basic string class.
Definition: TString.h:137
Iterator abstract base class.
Definition: TIterator.h:32
const char * Data() const
Definition: TString.h:349
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:2334
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
TIterator * createIterator(Bool_t dir=kIterForward) const
TString & Append(const char *cs)
Definition: TString.h:492
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void AppendArgSet(const RooAbsCollection *aset, TString prefix="")
RooAbsArg * find(const char *name) const
Find object with given name in list.
SVector< double, 2 > v
Definition: Dict.h:5
void SetName(const char *name)
Change (i.e.
Definition: RooAbsArg.cxx:2389
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooDataSet * GetAsDataSet(TString name, TString title)
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set...
Int_t numInvalidNLL() const
Definition: RooFitResult.h:89
Namespace for the RooStats classes.
Definition: Asimov.h:20
Int_t status() const
Definition: RooFitResult.h:76
Double_t minNll() const
Definition: RooFitResult.h:97
const RooArgList & floatParsInit() const
Definition: RooFitResult.h:105
RooCmdArg WeightVar(const char *name, Bool_t reinterpretAsWeight=kFALSE)
#define name(a, b)
Definition: linkTestLib0.cpp:5
void SetNameTitle(const char *name, const char *title)
Change the title of this dataset into the given name.
Int_t covQual() const
Definition: RooFitResult.h:85
virtual TObject * Next()=0
#define NULL
Definition: Rtypes.h:82
static RooArgSet * GetAsArgSet(RooFitResult *result, TString prefix="", bool withErrorsAndPulls=false)
double result[121]
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448