Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve summary output #44

Merged
merged 2 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/example_files.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'data.root':
type: data
legend: 'Data'
pretty-name: 'Data'

'MC_sample1.root':
type: mc
sample-name: "TTJets_TuneCUETP8M1_amcatnloFXFX_25ns_15c5fa1_c106444"
pretty-name: 'MC sample 1'
cross-section: 245.8
generated-events: 2167
fill-color: "#D95B43"
Expand All @@ -14,6 +15,7 @@

'MC_sample2.root':
type: mc
pretty-name: 'MC sample 2'
cross-section: 666.3
generated-events: 2404
fill-color: '#53777A'
Expand Down
8 changes: 4 additions & 4 deletions external/build-external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

# YAML

curl -O https://yaml-cpp.googlecode.com/files/yaml-cpp-0.5.1.tar.gz
tar xf yaml-cpp-0.5.1.tar.gz
curl -O -J -L https://github.com/jbeder/yaml-cpp/archive/release-0.5.3.tar.gz
tar xf yaml-cpp-release-0.5.3.tar.gz

cd yaml-cpp-0.5.1
cd yaml-cpp-release-0.5.3
mkdir build
cd build

Expand All @@ -15,7 +15,7 @@ make -j4
make install

cd ../..
rm yaml-cpp-0.5.1.tar.gz
rm yaml-cpp-release-0.5.3.tar.gz

# TCLAP
curl -L "http://downloads.sourceforge.net/project/tclap/{tclap-1.2.1.tar.gz}?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Ftclap%2Ffiles%2F&ts=1431017326&use_mirror=freefr" -o "#1"
Expand Down
2 changes: 1 addition & 1 deletion include/TH1Plotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace plotIt {
plotter(plotIt) {
}

virtual bool plot(TCanvas& c, Plot& plot);
virtual boost::optional<Summary> plot(TCanvas& c, Plot& plot);
virtual bool supports(TObject& object);

private:
Expand Down
20 changes: 20 additions & 0 deletions include/colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <iostream>

namespace Color {
enum Code {
RESET = 0,
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_MAGENTA = 35,
FG_CYAN = 36,
FG_WHITE = 37
};

std::ostream& operator<<(std::ostream& os, Code code) {
return os << "\033[" << static_cast<int>(code) << "m";
}
}
5 changes: 4 additions & 1 deletion include/plotter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <plotIt.h>
#include <summary.h>

#include <boost/optional.hpp>

class TCanvas;
class TObject;
Expand All @@ -15,7 +18,7 @@ namespace plotIt {
}


virtual bool plot(TCanvas& c, Plot& plot) = 0;
virtual boost::optional<Summary> plot(TCanvas& c, Plot& plot) = 0;
virtual bool supports(TObject& object) = 0;

protected:
Expand Down
7 changes: 5 additions & 2 deletions include/plotters.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#pragma once

#include <TH1Plotter.h>
#include <summary.h>

#include <boost/optional.hpp>

namespace plotIt {
static std::vector<std::shared_ptr<plotter>> s_plotters;
void createPlotters(plotIt& plotIt) {
s_plotters.push_back(std::make_shared<TH1Plotter>(plotIt));
}

bool plot(const File& file, TCanvas& c, Plot& plot) {
boost::optional<Summary> plot(const File& file, TCanvas& c, Plot& plot) {
for (auto& plotter: s_plotters) {
if (plotter->supports(*file.object))
return plotter->plot(c, plot);
}

return false;
return boost::none;
}
}
46 changes: 46 additions & 0 deletions include/summary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <types.h>

#include <map>
#include <string>
#include <vector>

namespace plotIt {

struct SummaryItem {
std::string name;

float events = 0;
float events_uncertainty = 0;

float efficiency = 0;
float efficiency_uncertainty = 0;
};

class Summary {
public:
void add(const Type& type, const SummaryItem& item);
void addSystematics(const Type& type, const SummaryItem& item);

std::vector<SummaryItem> get(const Type& type) const;
std::vector<SummaryItem> getSystematics(const Type& type) const;

private:
std::map<Type, std::vector<SummaryItem>> m_items;
std::map<Type, std::vector<SummaryItem>> m_systematics_items;
};

class SummaryPrinter {
public:
virtual void print(const Summary& summary) const = 0;
};

class ConsoleSummaryPrinter: public SummaryPrinter {
public:
virtual void print(const Summary& summary) const override;

private:
void printItems(const Type& type, const Summary& summary) const;
};
}
33 changes: 18 additions & 15 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <defines.h>
#include <uuid.h>

#include <yaml-cpp/yaml.h>

#include <TObject.h>
#include <TFile.h>
#include <TChain.h>
Expand All @@ -21,6 +23,21 @@ namespace plotIt {
DATA
};

inline std::string type_to_string(const Type& type) {
switch (type) {
case MC:
return "Simulation";

case SIGNAL:
return "Signal";

case DATA:
return "Data";
}

return "Unknown";
}

enum ErrorsType {
Normal = 0,
Poisson = 1,
Expand Down Expand Up @@ -59,25 +76,11 @@ namespace plotIt {
VERTICAL
};

struct Summary {
float n_events = 0;
float n_events_error = 0;

float efficiency = 0;
float efficiency_error = 0;

void clear() {
n_events = n_events_error = efficiency = efficiency_error = 0;
}
};

struct Systematic {
std::string path;
TObject* object = nullptr;
std::map<std::string, TObject*> objects;
std::shared_ptr<TFile> handle;

Summary summary;
};

struct LineStyle {
Expand Down Expand Up @@ -111,6 +114,7 @@ namespace plotIt {

struct File {
std::string path;
std::string pretty_name;

// For MC and Signal
float cross_section = 1.;
Expand All @@ -130,7 +134,6 @@ namespace plotIt {
std::vector<Systematic> systematics;

int16_t order = std::numeric_limits<int16_t>::min();
Summary summary;

std::shared_ptr<TChain> chain;
std::shared_ptr<TFile> handle;
Expand Down
10 changes: 10 additions & 0 deletions include/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,14 @@ namespace plotIt {
globfree(&glob_result);
return ret;
}

inline std::string truncate(const std::string& str, size_t max_len) {
if (str.length() > max_len - 1) {
std::string ret = str;
ret.resize(max_len - 1);
return ret + u8"…";
} else {
return str;
}
}
}
54 changes: 38 additions & 16 deletions src/TH1Plotter.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <TH1Plotter.h>

#include <TCanvas.h>
#include <TEfficiency.h>
#include <TF1.h>
#include <TFitResult.h>
#include <TLatex.h>
Expand Down Expand Up @@ -61,9 +62,11 @@ namespace plotIt {
return object.InheritsFrom("TH1");
}

bool TH1Plotter::plot(TCanvas& c, Plot& plot) {
boost::optional<Summary> TH1Plotter::plot(TCanvas& c, Plot& plot) {
c.cd();

Summary global_summary;

// Rescale and style histograms
for (File& file: m_plotIt.getFiles()) {
setHistogramStyle(file);
Expand All @@ -77,20 +80,35 @@ namespace plotIt {
factor *= m_plotIt.getConfiguration().scale * file.scale;
}

float n_entries = h->Integral();
file.summary.efficiency = n_entries / file.generated_events;
file.summary.efficiency_error = sqrt( (file.summary.efficiency * (1 - file.summary.efficiency)) / file.generated_events );
SummaryItem summary;
summary.name = file.pretty_name;

file.summary.n_events = n_entries * factor;
file.summary.n_events_error = m_plotIt.getConfiguration().luminosity * file.cross_section * file.branching_ratio * file.summary.efficiency_error;
if (! m_plotIt.getConfiguration().ignore_scales) {
file.summary.n_events_error *= m_plotIt.getConfiguration().scale * file.scale;
}
double integral_error = 0;
double integral = h->IntegralAndError(h->GetXaxis()->GetFirst(), h->GetXaxis()->GetLast(), integral_error);

summary.events = integral * factor;
summary.events_uncertainty = integral_error * factor;

// FIXME: Probably invalid in case of weights...

// Bayesian efficiency
// Taken from https://root.cern.ch/doc/master/TEfficiency_8cxx_source.html#l02428

// Use a flat prior (equivalent to Beta(1, 1))
float alpha = 1.;
float beta = 1.;

summary.efficiency = TEfficiency::BetaMean(integral + alpha, file.generated_events - integral + beta);
summary.efficiency_uncertainty = TEfficiency::Bayesian(file.generated_events, integral, 0.68, alpha, beta, true) - summary.efficiency;

global_summary.add(file.type, summary);

h->Scale(factor);
} else {
file.summary.n_events = h->Integral();
file.summary.n_events_error = 0;
SummaryItem summary;
summary.name = file.pretty_name;
summary.events = h->Integral();
global_summary.add(file.type, summary);
}

h->Rebin(plot.rebin);
Expand Down Expand Up @@ -225,8 +243,8 @@ namespace plotIt {
for (Systematic& syst: file.systematics) {
// This histogram should contains syst errors
// in percent
float total_syst_error = 0;
TH1* h = dynamic_cast<TH1*>(syst.object);
float total_syst_error = 0;
for (uint32_t i = 1; i <= (uint32_t) mc_histo_syst_only->GetNbinsX(); i++) {
float total_error = mc_histo_syst_only->GetBinError(i);
float syst_error_percent = h->GetBinError(i);
Expand All @@ -237,9 +255,13 @@ namespace plotIt {
mc_histo_syst_only->SetBinError(i, std::sqrt(total_error * total_error + syst_error * syst_error));
}

syst.summary.n_events = file.summary.n_events;
syst.summary.n_events_error = std::sqrt(total_syst_error);
SummaryItem summary;
summary.name = fs::path(syst.path).stem().native();
summary.events_uncertainty = std::sqrt(total_syst_error);

global_summary.addSystematics(file.type, summary);
}

}

// Propagate syst errors to the stat + syst histogram
Expand All @@ -265,7 +287,7 @@ namespace plotIt {

if (!toDraw.size()) {
std::cerr << "Error: nothing to draw." << std::endl;
return false;
return boost::none;
};

// Sort object by minimum
Expand Down Expand Up @@ -610,7 +632,7 @@ namespace plotIt {
if (hi_pad.get())
hi_pad->cd();

return true;
return global_summary;
}

void TH1Plotter::setHistogramStyle(const File& file) {
Expand Down
Loading