ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
dump_markov.cxx
Go to the documentation of this file.
1 #include "dump_markov.hpp"
2 
3 #include <cmath>
4 
5 #include <iostream>
6 #include <string>
7 #include <map>
8 #include <utility>
9 #include <algorithm>
10 
11 #include "TFile.h"
12 #include "TIterator.h"
13 #include "TKey.h"
14 #include "TList.h"
15 #include "TH1D.h"
16 #include "TCanvas.h"
17 
18 #include "RooDataSet.h"
19 #include "RooArgSet.h"
20 #include "RooRealVar.h"
21 #include "RooWorkspace.h"
22 
23 #include "RooStats/MarkovChain.h"
24 
25 #include "timer.hpp"
26 
27 using namespace std;
28 
29 int main(int argc, char *argv[]){
30  for(int iarg = 1; iarg < argc; ++iarg){
31  TFile file(argv[iarg], "read");
32  if(!file.IsOpen()) continue;
33  RooWorkspace *w = static_cast<RooWorkspace*>(file.Get("w"));
34  if(w == nullptr) continue;
35  RooDataSet *data = GetData(file);
36  if(data == nullptr) continue;
37 
38  map<string, vector<double> > values;
39  vector<double> weights;
40  GetValues(*w, *data, values, weights);
41  MakePlots(values, weights);
42  }
43 }
44 
45 RooDataSet * GetData(TFile &file){
46  if(!file.IsOpen()) return nullptr;
47  TDirectory *dir = static_cast<TDirectory*>(file.Get("toys"));
48  if(dir == nullptr) return nullptr;
49  TList *keys = dir->GetListOfKeys();
50  if(keys == nullptr) return nullptr;
51  TIter next_key(keys);
52  TKey *key = nullptr;
53  RooDataSet *data = nullptr;
54  while((key = static_cast<TKey*>(next_key()))){
55  if(key->GetClassName() != string("RooStats::MarkovChain")) continue;
56  RooStats::MarkovChain *mcmc = static_cast<RooStats::MarkovChain*>(key->ReadObj());
57  if(mcmc == nullptr) continue;
58  RooDataSet *this_data = static_cast<RooDataSet*>(mcmc->GetAsDataSet());
59  if(this_data == nullptr) continue;
60  if(data == nullptr){
61  data = this_data;
62  }else{
63  data->append(*this_data);
64  }
65  }
66  return data;
67 }
68 
69 void GetValues(RooWorkspace &w,
70  RooDataSet &data,
71  map<string, vector<double> > &values,
72  vector<double> &weights){
73  values.clear();
74  weights.clear();
75 
76  int num_entries = data.numEntries();
77  Timer timer(num_entries, 1.);
78  timer.Start();
79  for(int entry = 0; entry < num_entries; ++entry){
80  timer.Iterate();
81  const RooArgSet *args = static_cast<const RooArgSet*>(data.get(entry));
82  TIterator *iter = args->createIterator();
83  for(; iter != nullptr && *(*iter) != nullptr; iter->Next()){
84  RooRealVar *var = static_cast<RooRealVar*>(*(*iter));
85  if(var == nullptr) continue;
86  RooRealVar *wvar = w.var(var->GetName());
87  if(wvar == nullptr) continue;
88  wvar->setVal(var->getVal());
89 
90  FillValues(values, w);
91  weights.push_back(data.weight());
92  }
93  }
94 }
95 
96 void FillValues(map<string, vector<double> > &values,
97  const RooWorkspace &w){
98  FillValues(values, w.allVars());
99  FillValues(values, w.allFunctions());
100  FillValues(values, w.allPdfs());
101 }
102 
103 void FillValues(map<string, vector<double> > &values,
104  const RooArgSet &args){
105  TIterator *iter = args.createIterator();
106  for(; iter != nullptr && *(*iter) != nullptr; iter->Next()){
107  RooRealVar *var = static_cast<RooRealVar*>(*(*iter));
108  if(var == nullptr) continue;
109  values[var->GetName()].push_back(var->getVal());
110  }
111  if(iter != nullptr){
112  delete iter;
113  iter = 0;
114  }
115 }
116 
117 void MakePlots(const map<string, vector<double> > &values,
118  const vector<double> &weights){
119  for(auto var = values.cbegin();
120  var != values.cend();
121  ++var){
122  MakePlot(var->first, var->second, weights);
123  }
124 }
125 
126 void MakePlot(const string &name, const vector<double> &values,
127  const vector<double> &weights){
128  int num_bins = TMath::Nint(sqrt(static_cast<double>(values.size())));
129  if(num_bins < 1) num_bins = 1;
130  if(num_bins > 100) num_bins = 100;
131  double min_val = *min_element(values.cbegin(), values.cend());
132  double max_val = *max_element(values.cbegin(), values.cend());
133 
134  TH1D h(("hist_"+name).c_str(), (name+";"+name+";").c_str(),
135  num_bins, min_val, max_val);
136  h.Sumw2();
137  size_t num_entries = min(values.size(), weights.size());
138  for(size_t entry = 0; entry < num_entries; ++entry){
139  h.Fill(values.at(entry), weights.at(entry));
140  }
141 
142  TCanvas canvas;
143  h.Draw("e1p");
144  canvas.Print(("hist_"+name+"_lin.pdf").c_str());
145  canvas.SetLogy();
146  h.Draw("e1p");
147  canvas.Print(("hist_"+name+"_log.pdf").c_str());
148 }
void MakePlots(const map< string, vector< double > > &values, const vector< double > &weights)
void FillValues(map< string, vector< double > > &values, const RooWorkspace &w)
Definition: dump_markov.cxx:96
Definition: timer.hpp:6
void MakePlot(const string &name, const vector< double > &values, const vector< double > &weights)
int main(int argc, char *argv[])
Definition: dump_markov.cxx:29
STL namespace.
RooDataSet * GetData(TFile &file)
Definition: dump_markov.cxx:45
void GetValues(RooWorkspace &w, RooDataSet &data, map< string, vector< double > > &values, vector< double > &weights)
Definition: dump_markov.cxx:69
void Iterate()
Definition: timer.cpp:28
void Start()
Definition: timer.cpp:22