Skip to content

Commit

Permalink
All nodes requiring input now specify the variable name (#34)
Browse files Browse the repository at this point in the history
* All nodes requiring input now specify the variable name

Closes #2

* Simplify node_evaluate visitor
  • Loading branch information
nsmith- committed Feb 16, 2021
1 parent a3f8a57 commit 712e571
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 72 deletions.
8 changes: 8 additions & 0 deletions data/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def build_discrbinning(sf):
return Binning.parse_obj(
{
"nodetype": "binning",
"input": "discriminant",
"edges": edges,
"content": [
build_formula(sf[(sf["discrMin"] >= lo) & (sf["discrMax"] <= hi)])
Expand All @@ -59,6 +60,7 @@ def build_ptbinning(sf):
return Binning.parse_obj(
{
"nodetype": "binning",
"input": "pt",
"edges": edges,
"content": [
build_discrbinning(sf[(sf["ptMin"] >= lo) & (sf["ptMax"] <= hi)])
Expand All @@ -73,6 +75,7 @@ def build_etabinning(sf):
return Binning.parse_obj(
{
"nodetype": "binning",
"input": "abseta",
"edges": edges,
"content": [
build_ptbinning(sf[(sf["etaMin"] >= lo) & (sf["etaMax"] <= hi)])
Expand All @@ -87,6 +90,7 @@ def build_flavor(sf):
return Category.parse_obj(
{
"nodetype": "category",
"input": "flavor",
"content": [
{"key": key, "value": build_etabinning(sf[sf["jetFlavor"] == key])}
for key in keys
Expand All @@ -100,6 +104,7 @@ def build_systs(sf):
return Category.parse_obj(
{
"nodetype": "category",
"input": "systematic",
"content": [
{"key": key, "value": build_flavor(sf[sf["sysType"] == key])}
for key in keys
Expand Down Expand Up @@ -141,6 +146,7 @@ def build_syst(sf):
return Category.parse_obj(
{
"nodetype": "category",
"input": "systematic",
"content": [
{"key": "nominal", "value": sf["value"]},
{"key": "up", "value": sf["value"] + sf["error"]},
Expand Down Expand Up @@ -174,6 +180,7 @@ def build_pts(sf):
return Binning.parse_obj(
{
"nodetype": "binning",
"input": "pt",
"edges": edges,
"content": content,
}
Expand All @@ -197,6 +204,7 @@ def build_etas(sf):
return Binning.parse_obj(
{
"nodetype": "binning",
"input": "eta",
"edges": edges,
"content": content,
}
Expand Down
10 changes: 7 additions & 3 deletions data/schemav2.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"parser": {
"title": "Parser",
"const": "TFormula_v1",
"const": "TFormula",
"type": "string"
},
"variables": {
Expand All @@ -94,8 +94,7 @@
"nodetype",
"expression",
"parser",
"variables",
"parameters"
"variables"
],
"additionalProperties": false
},
Expand Down Expand Up @@ -224,6 +223,10 @@
"const": "binning",
"type": "string"
},
"input": {
"title": "Input",
"type": "string"
},
"edges": {
"title": "Edges",
"type": "array",
Expand Down Expand Up @@ -257,6 +260,7 @@
},
"required": [
"nodetype",
"input",
"edges",
"content"
],
Expand Down
29 changes: 16 additions & 13 deletions include/correction.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ constexpr int evaluator_version { 2 };

class Variable {
public:
enum class VarType {string, integer, real};
typedef std::variant<int, double, std::string> Type;

Variable(const rapidjson::Value& json);
std::string name() const { return name_; };
std::string description() const { return description_; };
std::string type() const;
VarType type() const { return type_; };
std::string typeStr() const;
void validate(const Type& t) const;

private:
enum class VarType {string, integer, real};
std::string name_;
std::string description_;
VarType type_;
Expand All @@ -48,7 +49,7 @@ class Formula {
private:
std::string expression_;
ParserType type_;
std::vector<int> variableIdx_;
std::vector<size_t> variableIdx_;

static std::map<ParserType, peg::parser> parsers_;
static std::mutex parsers_mutex_; // could be one per parser, but this is good enough
Expand Down Expand Up @@ -87,33 +88,35 @@ class Formula {
class Binning {
public:
Binning(const rapidjson::Value& json, const std::vector<Variable>& inputs);
const Content& child(const std::vector<Variable>& inputs, const std::vector<Variable::Type>& values, const int depth) const;
const Content& child(const std::vector<Variable::Type>& values) const;

private:
std::vector<double> edges_;
std::vector<Content> content_;
std::vector<std::tuple<double, Content>> bins_;
size_t variableIdx_;
};

class MultiBinning {
public:
MultiBinning(const rapidjson::Value& json, const std::vector<Variable>& inputs);
int ndimensions() const { return edges_.size(); };
const Content& child(const std::vector<Variable>& inputs, const std::vector<Variable::Type>& values, const int depth) const;
int ndimensions() const { return axes_.size(); };
const Content& child(const std::vector<Variable::Type>& values) const;

private:
std::vector<std::vector<double>> edges_;
std::vector<size_t> dim_strides_;
// variableIdx, stride, edges
std::vector<std::tuple<size_t, size_t, std::vector<double>>> axes_;
std::vector<Content> content_;
};

class Category {
public:
Category(const rapidjson::Value& json, const std::vector<Variable>& inputs);
const Content& child(const std::vector<Variable>& inputs, const std::vector<Variable::Type>& values, const int depth) const;
const Content& child(const std::vector<Variable::Type>& values) const;

private:
std::map<int, Content> int_map_;
std::map<std::string, Content> str_map_;
typedef std::map<int, Content> IntMap;
typedef std::map<std::string, Content> StrMap;
std::variant<IntMap, StrMap> map_;
size_t variableIdx_;
};

class Correction {
Expand Down
Loading

0 comments on commit 712e571

Please sign in to comment.