Repository navigation
Expand file tree
/
Copy pathTestMarginalization.C
More file actions
129 lines (115 loc) · 4.39 KB
/
Copy pathTestMarginalization.C
File metadata and controls
129 lines (115 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This should be run on a THardLogLikelihood with a smallish number of
// dimensions (or the plots become unreasonable).
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <TFile.h>
#include <TH1F.h>
#include <TH2D.h>
#include <TProfile.h>
#include <TList.h>
void TestMarginalization() {
// Find the tree in the file.
TList *list = gFile->GetListOfKeys();
TIter iter(list->MakeIterator());
std::string name;
while (TObject *obj = iter()) {
TKey *key = (TKey*)obj;
if (std::string(key->GetClassName()) != "TTree") continue;
name = key->GetName();
}
// Get the tree out of the file.
TTree *inputTree = (TTree*) gFile->Get(name.c_str());
std::cout << "Input Tree Name: " << inputTree->GetName() << std::endl;
int entries = inputTree->GetEntries();
std::cout << "Entries: " << entries << std::endl;\
std::vector<double> *accepted = NULL;
// Get an entry to collect information about the tree.
inputTree->SetBranchAddress("Accepted",&accepted);
inputTree->GetEntry(0);
std::size_t dim = accepted->size();
std::size_t allDim = dim;
if (dim > 10) {
std::cout << "I think you're making a mistake, only doing 10"
<< std::endl;
dim = 10;
}
// Define the range for the plots... This could be A LOT more intelligent.
std::vector<std::pair<double,double>> range;
for (int i= 0; i< allDim; ++i) {
range.push_back(std::make_pair(accepted->at(i),accepted->at(i)));
}
double absMin = 1E+20;
double absMax = -absMin;
for (int entry = 0; entry < entries; ++entry) {
inputTree->GetEntry(entry);
for (int i= 0; i< allDim; ++i) {
range[i].first = std::min(range[i].first,accepted->at(i));
range[i].second = std::max(range[i].second,accepted->at(i));
absMin = std::min(absMin,accepted->at(i));
absMax = std::max(absMax,accepted->at(i));
}
entry += 0.001*entries;
}
std::cout << "Dimensions: " << dim << std::endl;
std::vector<TH1F*> marginalized;
for (int i= 0; i< allDim; ++i) {
std::ostringstream name;
std::ostringstream title;
name << "marginalized" << i;
title << "Marginalization: Dimension " << i;
TH1F* hist = new TH1F(name.str().c_str(),title.str().c_str(),
100, absMin, absMax);
marginalized.push_back(hist);
std::cout << range[i].first << " " << range[i].second << std::endl;
}
std::vector<TH2F*> correlations;
for (int i= 0; i< dim; ++i) {
for (int j= 0; j< dim; ++j) {
std::ostringstream name;
std::ostringstream title;
name << "correlation" << i << "_" << j;
title << "Dimensions " << j << " vs " << i;
double ri = 0.05*(range[i].second-range[i].first);
double rj = 0.05*(range[j].second-range[j].first);
TH2F* hist = new TH2F(name.str().c_str(),title.str().c_str(),
50, range[i].first-ri, range[i].second+ri,
50, range[j].first-rj, range[j].second+rj);
correlations.push_back(hist);
}
}
for (int entry = 0; entry < entries; ++entry) {
inputTree->GetEntry(entry);
if ((entry % 1000) == 0) std::cout << "entry " << entry << std::endl;
for (int i = 0; i<allDim; ++i) {
marginalized[i]->Fill(accepted->at(i));
if (i < dim) {
for (int j = 0; j<dim; ++j) {
correlations[dim*i+j]->Fill(accepted->at(i),
accepted->at(j));
}
}
}
}
marginalized[0]->SetTitle("Marginalized Distributions (all dimensions)");
marginalized[0]->Draw("C");
marginalized[0]->SetLineWidth(3.0);
for (int i = 1; i<allDim; ++i) {
marginalized[i]->Draw("same,C");
}
marginalized[dim-1]->SetLineWidth(3.0);
marginalized[dim-1]->SetLineColor(kRed);
gPad->Print("marginalized.png");
TCanvas *zoned = new TCanvas("zoned","multipads",1000,1000);
gStyle->SetOptStat(0);
zoned->Divide(dim,dim,0,0);
for (int i = 0; i<dim; ++i) {
for (int j = 0; j<dim; ++j) {
zoned->cd(dim*i+j+1);
correlations[dim*i+j]->Draw("colz");
}
}
zoned->cd(0);
gPad->Print("correlations.png");
}