6 #include "boost/format.hpp"
13 std::string
const& root_pattern)
14 : text_pattern_(text_pattern),
15 root_pattern_(root_pattern),
16 wildcard_masses_({
"*"}),
32 std::vector<std::string>
const& masses) {
33 wildcard_masses_ = masses;
37 auto CardWriter::BuildMap(std::string
const& pattern,
41 cmb.cp().mass(wildcard_masses_,
false)
44 std::string key = Compile(pattern, obj);
45 if (f_map.count(key))
return;
46 std::set<std::string> mappings;
50 std::string maps_pattern = Compile(pattern, obj,
true);
52 for (
auto m : wildcard_masses_) {
53 std::string tmp = maps_pattern;
54 boost::replace_all(tmp,
"$MASS", m);
57 f_map[key] = mappings;
59 auto masses = cmb.cp().mass(wildcard_masses_,
false).mass_set();
60 cmb.cp().mass(wildcard_masses_,
true)
63 std::string key = Compile(pattern, obj,
true);
64 for (
auto m : masses) {
65 std::string full_key = key;
66 boost::replace_all(full_key,
"$MASS", m);
67 if (f_map.count(full_key))
continue;
68 std::set<std::string> mappings;
70 std::string wildcard_key = key;
71 boost::replace_all(wildcard_key,
"$MASS", obj->
mass());
72 mappings.insert(wildcard_key);
73 f_map[full_key] = mappings;
84 void CardWriter::MakeDirs(PatternMap
const& map)
const {
85 std::set<boost::filesystem::path> mk_dirs;
86 for (
auto const& f : map) {
87 mk_dirs.insert(boost::filesystem::path(f.first).parent_path());
89 for (
auto & dir : mk_dirs) {
90 FNLOGC(std::cout, v_ > 0) <<
"Creating dir " << dir.string() <<
"\n";
92 boost::filesystem::create_directories(dir);
114 auto f_map = BuildMap(root_pattern_, cmb);
116 if (create_dirs_) MakeDirs(f_map);
123 std::map<Object const*, std::string> root_map;
125 root_map[obj] = Compile(root_pattern_, obj);
127 std::map<Object const*, std::string> text_map;
129 text_map[obj] = Compile(text_pattern_, obj);
132 std::map<std::string, CombineHarvester> datacards;
133 for (
auto const& f : f_map) {
135 FNLOGC(std::cout, v_ > 0) <<
"Creating file " << f.first <<
"\n";
136 TFile file(f.first.c_str(),
"RECREATE");
146 auto d_map = BuildMap(text_pattern_, f_cmb);
149 if (create_dirs_) MakeDirs(d_map);
152 for (
auto const& d : d_map) {
158 FNLOGC(std::cout, v_ > 0) <<
"Creating datacard " << d.first <<
"\n";
160 datacards[d.first] = d_cmb;
166 std::string CardWriter::Compile(std::string pattern,
ch::Object const* obj,
167 bool skip_mass)
const {
168 #ifdef TIME_FUNCTIONS
171 boost::replace_all(pattern,
"$TAG", tag_);
172 boost::replace_all(pattern,
"$BINID",
173 boost::lexical_cast<std::string>(obj->
bin_id()));
174 boost::replace_all(pattern,
"$BIN", obj->
bin());
175 boost::replace_all(pattern,
"$PROCESS", obj->
process());
176 if (!skip_mass) boost::replace_all(pattern,
"$MASS", obj->
mass());
177 boost::replace_all(pattern,
"$ERA", obj->
era());
178 boost::replace_all(pattern,
"$CHANNEL", obj->
channel());
179 boost::replace_all(pattern,
"$ANALYSIS", obj->
analysis());
#define LAUNCH_FUNCTION_TIMER(x, y)
Conveniently initialise a ch::FnTimer instance.
Automates the writing of datacards into directory structures.
CardWriter & CreateDirectories(bool flag)
Control whether directories can be created if missing.
CardWriter & SetWildcardMasses(std::vector< std::string > const &masses)
Redefine the mass values that should be treated as wildcards.
std::map< std::string, CombineHarvester > WriteCards(std::string const &tag, ch::CombineHarvester &cmb) const
Write datacards according to patterns, substituting $TAG for tag
CardWriter & SetVerbosity(unsigned v)
Set >= 1 for verbose output, otherwise silent.
CardWriter(std::string const &text_pattern, std::string const &root_pattern)
Must be constructed with text and ROOT file patterns.
void WriteDatacard(std::string const &name, std::string const &root_file)
CombineHarvester & FilterAll(Function func)
CombineHarvester cp()
Creates and returns a shallow copy of the CombineHarvester instance.
void ForEachObj(Function func)
virtual std::string const & process() const
virtual std::string const & bin() const
virtual int bin_id() const
virtual std::string const & analysis() const
virtual std::string const & era() const
virtual std::string const & mass() const
virtual std::string const & channel() const
bool contains(const Range &r, T p)