8 #include "TLeafObject.h" 13 for(std::string::iterator it = str.begin();
30 const std::string &to_type,
const std::string &from_type):
38 const std::string &to_type):
86 const size_t file,
const size_t num_files,
88 if(file>=num_files)
throw std::logic_error(
"Bad file number");
89 for(
int i = 0; i < chain.GetListOfLeaves()->GetSize(); ++i){
90 std::string type_name(static_cast<TLeafObject*>((chain.GetListOfLeaves()->At(i)))->GetTypeName());
91 const std::string var_name(static_cast<TLeaf*>((chain.GetListOfLeaves()->At(i)))->GetBranch()->GetName());
94 bool needs_ptr(
false);
95 for(
size_t j(type_name.find(
"vector"));
97 j=type_name.find(
"vector",j+6)){
98 type_name.replace(j,6,
"std::vector");
101 for(
size_t j(type_name.find(
"string"));
102 j!=std::string::npos;
103 j=type_name.find(
"string",j+6)){
104 type_name.replace(j,6,
"std::string");
107 if(needs_ptr) type_name = type_name +
"*";
109 if(vars.find(var_name) == vars.end()){
111 typedef std::vector<std::pair<bool, std::string> > OneVar;
112 typedef std::pair<std::string, OneVar > Inserter;
113 vars.insert(Inserter(var_name, OneVar(num_files)));
116 if(vars[var_name].size()!=num_files){
117 throw std::logic_error(
"Number of files does not match.");
120 if(vars[var_name].at(file)!=std::pair<bool, std::string>(
false,
"")
121 && vars[var_name].at(file)!=std::pair<bool, std::string>(is_chain_a, type_name)){
122 throw std::runtime_error(
"Conflicting definition found for " 123 +type_name+
" "+var_name+
" in this file.");
126 vars[var_name].at(file)=std::pair<bool, std::string>(is_chain_a, type_name);
131 std::vector<Variable> &base_vars, std::vector<Variable> &virtual_vars,
132 std::vector<std::vector<Variable> > &file_vars){
134 if(all_vars.size()) file_vars.resize(all_vars.begin()->second.size());
135 for(Dictionary::const_iterator var = all_vars.begin(); var!= all_vars.end(); ++var){
136 const std::string &
name = var->first;
137 if(var->second.size() == 0)
throw std::logic_error(
"No variable definitions found!");
139 const bool base_is_chain_a = var->second.at(0).first;
140 std::string best_type = var->second.at(0).second;
142 bool all_same_chain =
true, all_same_type =
true;
143 for(
size_t idef = 0; idef < var->second.size(); ++idef){
144 const bool this_is_chain_a = var->second.at(idef).first;
145 const std::string this_type = var->second.at(idef).second;
147 if(this_is_chain_a!=base_is_chain_a){
148 all_same_chain =
false;
151 if(this_type != best_type){
152 all_same_type =
false;
154 best_type = this_type;
155 }
else if(this_type==
""){
157 }
else if((best_type==
"std::vector<float>*" && this_type==
"std::vector<bool>*")
158 || (best_type==
"std::vector<bool>*" && this_type==
"std:vector<float>*")){
159 best_type =
"std::vector<bool>*";
161 throw std::runtime_error(
"Unknown type preference.");
166 if(all_same_chain && all_same_type){
167 base_vars.push_back(
Variable(name, base_is_chain_a, best_type));
170 virtual_vars.push_back(
Variable(name,
true, best_type));
172 for(
size_t idef = 0; idef < var->second.size(); ++idef){
173 const bool this_is_chain_a = var->second.at(idef).first;
174 const std::string this_type = var->second.at(idef).second;
177 Variable this_var(name, this_is_chain_a, best_type, this_type);
178 file_vars.at(idef).push_back(this_var);
186 const std::vector<Variable> &virtual_vars){
187 std::ofstream hpp_file(
"inc/cfa_base.hpp");
188 hpp_file <<
"#ifndef H_CFA_BASE\n";
189 hpp_file <<
"#define H_CFA_BASE\n\n";
191 hpp_file <<
"#include <vector>\n";
192 hpp_file <<
"#include <string>\n\n";
194 hpp_file <<
"#include \"TChain.h\"\n\n";
196 hpp_file <<
"class cfa_base{\n";
197 hpp_file <<
"public:\n";
198 hpp_file <<
" explicit cfa_base(const std::string &file);\n\n";
200 hpp_file <<
" long TotalEntries() const;\n";
201 hpp_file <<
" virtual void GetEntry(const long entry);\n";
202 hpp_file <<
" short GetVersion() const;\n";
203 hpp_file <<
" const std::string& SampleName() const;\n";
204 hpp_file <<
" const std::string& SampleName(const std::string &sample_name);\n";
205 hpp_file <<
" void SetFile(const std::string &file_name);\n";
206 hpp_file <<
" void AddFiles(const std::string& files);\n\n";
208 hpp_file <<
" virtual ~cfa_base();\n\n";
211 for(std::vector<Variable>::const_iterator var = base_vars.begin();
212 var != base_vars.end();
214 if(var->Converted()){
215 throw std::logic_error(
"Should not have converted variables in base class");
217 hpp_file <<
" " << var->ToType() <<
" const & " << var->Name() <<
"() const;\n";
222 for(std::vector<Variable>::const_iterator var = virtual_vars.begin();
223 var != virtual_vars.end();
225 hpp_file <<
" __attribute__((noreturn)) virtual " << var->ToType() <<
" const & " 226 << var->Name() <<
"() const;\n";
230 hpp_file <<
"protected:\n";
231 hpp_file <<
" void PrepareNewChains();\n";
232 hpp_file <<
" virtual void InitializeA();\n";
233 hpp_file <<
" virtual void InitializeB();\n\n";
235 hpp_file <<
" TChain chainA_, chainB_;\n";
236 hpp_file <<
" long entry_;\n\n";
238 hpp_file <<
"private:\n";
239 hpp_file <<
" void CalcVersion();\n\n";
241 hpp_file <<
" std::string sample_name_;\n";
242 hpp_file <<
" mutable long total_entries_;\n";
243 hpp_file <<
" short cfa_version_;\n";
244 hpp_file <<
" mutable bool cached_total_entries_;\n\n";
246 for(std::vector<Variable>::const_iterator var = base_vars.begin();
247 var != base_vars.end();
249 hpp_file <<
" " << var->ToType() <<
' ' << var->Name() <<
"_;\n";
250 if(var->Converted()){
251 throw std::runtime_error(
"Converted variable in base class.");
253 hpp_file <<
" TBranch *b_" << var->Name() <<
"_;\n";
254 hpp_file <<
" mutable bool c_" << var->Name() <<
"_;\n";
258 hpp_file <<
"};\n\n";
260 hpp_file <<
"#endif\n";
266 const std::vector<Variable> &virtual_vars){
267 std::ofstream cpp_file(
"src/cfa_base.cpp");
269 cpp_file <<
"#include \"cfa_base.hpp\"\n\n";
271 cpp_file <<
"#include <string>\n";
272 cpp_file <<
"#include <vector>\n";
273 cpp_file <<
"#include <sstream>\n";
274 cpp_file <<
"#include <stdexcept>\n\n";
276 cpp_file <<
"#include \"TChain.h\"\n\n";
278 cpp_file <<
"cfa_base::cfa_base(const std::string &file):\n";
279 cpp_file <<
" chainA_(\"chainA\"),\n";
280 cpp_file <<
" chainB_(\"chainB\"),\n";
281 cpp_file <<
" sample_name_(file),\n";
282 cpp_file <<
" total_entries_(0),\n";
283 cpp_file <<
" cfa_version_(-1),\n";
284 if(base_vars.size()){
285 cpp_file <<
" cached_total_entries_(false),\n";
286 std::vector<Variable>::const_iterator last(base_vars.end());
288 for(std::vector<Variable>::const_iterator var = base_vars.begin();
291 cpp_file <<
" " << var->Name() <<
"_(0),\n";
292 if(var->Converted()){
293 cpp_file <<
" v_" << var->Name() <<
"_(0),\n";
295 cpp_file <<
" b_" << var->Name() <<
"_(NULL),\n";
296 cpp_file <<
" c_" << var->Name() <<
"_(false),\n";
298 cpp_file <<
" " << base_vars.back().Name() <<
"_(0),\n";
299 if(base_vars.back().Converted()){
300 cpp_file <<
" v_" << base_vars.back().Name() <<
"_(0),\n";
302 cpp_file <<
" b_" << base_vars.back().Name() <<
"_(NULL),\n";
303 cpp_file <<
" c_" << base_vars.back().Name() <<
"_(false){\n";
305 cpp_file <<
" cached_total_entries_(false){\n";
307 cpp_file <<
" CalcVersion();\n";
308 cpp_file <<
" AddFiles(file);\n";
309 cpp_file <<
" PrepareNewChains();\n";
312 cpp_file <<
"long cfa_base::TotalEntries() const{\n";
313 cpp_file <<
" if(!cached_total_entries_){\n";
314 cpp_file <<
" const long nEntriesA(chainA_.GetEntries()), nEntriesB(chainB_.GetEntries());\n";
315 cpp_file <<
" if (nEntriesA!=nEntriesB){\n";
316 cpp_file <<
" total_entries_=-1;\n";
317 cpp_file <<
" }else{\n";
318 cpp_file <<
" total_entries_=nEntriesA;\n";
320 cpp_file <<
" cached_total_entries_=true;\n";
322 cpp_file <<
" return total_entries_;\n";
325 cpp_file <<
"void cfa_base::GetEntry(const long entry){\n";
327 for(std::vector<Variable>::const_iterator var = base_vars.begin();
328 var != base_vars.end();
330 cpp_file <<
" c_" << var->Name() <<
"_ = false;\n";
332 cpp_file <<
" const long entry_a = chainA_.LoadTree(entry);\n";
333 cpp_file <<
" const long entry_b = chainB_.LoadTree(entry);\n";
334 cpp_file <<
" if(entry_a!=entry_b) throw std::runtime_error(\"Entry is in different trees for chains A and B\");\n";
335 cpp_file <<
" entry_ = entry_a;\n";
338 cpp_file <<
"short cfa_base::GetVersion() const{\n";
339 cpp_file <<
" return cfa_version_;\n";
342 cpp_file <<
"const std::string& cfa_base::SampleName() const{\n";
343 cpp_file <<
" return sample_name_;\n";
346 cpp_file <<
"const std::string& cfa_base::SampleName(const std::string &sample_name){\n";
347 cpp_file <<
" sample_name_=sample_name;\n";
348 cpp_file <<
" return sample_name_;\n";
351 cpp_file <<
"void cfa_base::SetFile(const std::string &file_name){\n";
352 cpp_file <<
" cached_total_entries_=false;\n";
353 cpp_file <<
" chainA_.Reset(); chainB_.Reset();\n";
354 cpp_file <<
" AddFiles(file_name);\n";
357 cpp_file <<
"cfa_base::~cfa_base(){\n";
360 cpp_file <<
"void cfa_base::CalcVersion(){\n";
361 cpp_file <<
" size_t pos = sample_name_.rfind(\"_v\");\n";
362 cpp_file <<
" if(pos != std::string::npos && pos < sample_name_.size()-2){\n";
363 cpp_file <<
" std::istringstream iss(sample_name_.substr(pos+2));\n";
364 cpp_file <<
" iss >> cfa_version_;\n";
365 cpp_file <<
" if(iss.fail() || iss.bad()){\n";
366 cpp_file <<
" cfa_version_ = -1;\n";
371 cpp_file <<
"void cfa_base::AddFiles(const std::string &file_name){\n";
372 cpp_file <<
" cached_total_entries_=false;\n";
373 cpp_file <<
" std::string dir_name = \"/cfA\";\n";
375 cpp_file <<
" if(file_name.find(\"cfa_file_8\")!=std::string::npos) dir_name = \"/configurableAnalysis\";\n";
376 cpp_file <<
" chainA_.Add((file_name+dir_name+\"/eventA\").c_str());\n";
377 cpp_file <<
" chainB_.Add((file_name+dir_name+\"/eventB\").c_str());\n";
380 cpp_file <<
"void cfa_base::PrepareNewChains(){\n";
381 cpp_file <<
" InitializeA();\n";
382 cpp_file <<
" InitializeB();\n";
385 cpp_file <<
"void cfa_base::InitializeA(){\n";
386 cpp_file <<
" chainA_.SetMakeClass(1);\n";
388 for(std::vector<Variable>::const_iterator var = base_vars.begin();
389 var != base_vars.end();
392 if(var->FromChainA()){
393 cpp_file <<
" chainA_.SetBranchAddress(\"" << var->Name() <<
"\", &" 394 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
395 if(var->Converted()){
396 cpp_file <<
" chainA_.SetBranchAddress(\"" << var->Name() <<
"\", &v_" 397 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
403 cpp_file <<
"void cfa_base::InitializeB(){\n";
404 cpp_file <<
" chainB_.SetMakeClass(1);\n";
406 for(std::vector<Variable>::const_iterator var = base_vars.begin();
407 var != base_vars.end();
410 if(!var->FromChainA()){
411 cpp_file <<
" chainB_.SetBranchAddress(\"" << var->Name() <<
"\", &" 412 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
413 if(var->Converted()){
414 cpp_file <<
" chainB_.SetBranchAddress(\"" << var->Name() <<
"\", &v_" 415 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
422 for(std::vector<Variable>::const_iterator var = base_vars.begin();
423 var != base_vars.end();
425 if(var->Converted()){
426 throw(
"Should not have converted variables in base class");
432 for(std::vector<Variable>::const_iterator var = virtual_vars.begin();
433 var!=virtual_vars.end();
435 cpp_file << var->ToType() <<
" const & cfa_base::" << var->Name() <<
"() const{\n";
436 cpp_file <<
" throw std::runtime_error(\"" << var->Name()
437 <<
" does not exist in this cfA version.\");\n";
445 const std::vector<Variable> &vars){
446 std::ofstream hpp_file((
"inc/"+class_name+
".hpp").c_str());
448 hpp_file <<
"#ifndef H_" <<
AllCaps(class_name) <<
"\n";
449 hpp_file <<
"#define H_" <<
AllCaps(class_name) <<
"\n\n";
451 hpp_file <<
"#include \"cfa_base.hpp\"\n\n";
453 hpp_file <<
"#include <vector>\n";
454 hpp_file <<
"#include <string>\n\n";
456 hpp_file <<
"#include \"TChain.h\"\n\n";
458 hpp_file <<
"class " << class_name <<
": public cfa_base{\n";
459 hpp_file <<
"public:\n";
460 hpp_file <<
" explicit " << class_name <<
"(const std::string &file);\n\n";
462 hpp_file <<
" virtual void GetEntry(const long entry);\n\n";
464 hpp_file <<
" virtual ~" << class_name <<
"();\n\n";
467 for(std::vector<Variable>::const_iterator var = vars.begin();
470 hpp_file <<
" virtual " << var->ToType() <<
" const & " << var->Name() <<
"() const;\n";
474 hpp_file <<
"private:\n";
475 hpp_file <<
" virtual void InitializeA();\n";
476 hpp_file <<
" virtual void InitializeB();\n\n";
479 for(std::vector<Variable>::const_iterator var = vars.begin();
482 hpp_file <<
" " << var->ToType() <<
' ' << var->Name() <<
"_;\n";
483 if(var->Converted()){
484 hpp_file <<
" " << var->FromType() <<
" v_" << var->Name() <<
"_;\n";
486 hpp_file <<
" TBranch *b_" << var->Name() <<
"_;\n";
487 hpp_file <<
" mutable bool c_" << var->Name() <<
"_;\n";
489 hpp_file <<
"};\n\n";
491 hpp_file <<
"#endif\n";
497 const std::vector<Variable> &vars){
498 std::ofstream cpp_file((
"src/"+class_name+
".cpp").c_str());
500 cpp_file <<
"#include \"" << class_name <<
".hpp\"\n\n";
502 cpp_file <<
"#include <string>\n";
503 cpp_file <<
"#include <vector>\n";
504 cpp_file <<
"#include <sstream>\n";
505 cpp_file <<
"#include <stdexcept>\n\n";
507 cpp_file <<
"#include \"TChain.h\"\n\n";
509 cpp_file << class_name <<
"::" << class_name <<
"(const std::string &file):\n";
511 cpp_file <<
" cfa_base(file),\n";
513 std::vector<Variable>::const_iterator last(vars.end());
515 for(std::vector<Variable>::const_iterator var = vars.begin();
518 cpp_file <<
" " << var->Name() <<
"_(0),\n";
519 if(var->Converted()){
520 cpp_file <<
" v_" << var->Name() <<
"_(0),\n";
522 cpp_file <<
" b_" << var->Name() <<
"_(NULL),\n";
523 cpp_file <<
" c_" << var->Name() <<
"_(false),\n";
525 cpp_file <<
" " << vars.back().Name() <<
"_(0),\n";
526 if(vars.back().Converted()){
527 cpp_file <<
" v_" << vars.back().Name() <<
"_(0),\n";
529 cpp_file <<
" b_" << vars.back().Name() <<
"_(NULL),\n";
530 cpp_file <<
" c_" << vars.back().Name() <<
"_(false){\n";
532 cpp_file <<
" cfa_base(file){\n";
535 cpp_file <<
" PrepareNewChains();\n";
538 cpp_file <<
"void " << class_name <<
"::GetEntry(const long entry){\n";
540 for(std::vector<Variable>::const_iterator var = vars.begin();
543 cpp_file <<
" c_" << var->Name() <<
"_ = false;\n";
545 cpp_file <<
" cfa_base::GetEntry(entry);\n";
548 cpp_file <<
"void " << class_name <<
"::InitializeA(){\n";
549 cpp_file <<
" cfa_base::InitializeA();\n";
551 for(std::vector<Variable>::const_iterator var = vars.begin();
555 if(var->FromChainA()){
556 cpp_file <<
" chainA_.SetBranchAddress(\"" << var->Name() <<
"\", &" 557 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
558 if(var->Converted()){
559 cpp_file <<
" chainA_.SetBranchAddress(\"" << var->Name() <<
"\", &v_" 560 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
566 cpp_file <<
"void " << class_name <<
"::InitializeB(){\n";
567 cpp_file <<
" cfa_base::InitializeB();\n";
569 for(std::vector<Variable>::const_iterator var = vars.begin();
573 if(!var->FromChainA()){
574 cpp_file <<
" chainB_.SetBranchAddress(\"" << var->Name() <<
"\", &" 575 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
576 if(var->Converted()){
577 cpp_file <<
" chainB_.SetBranchAddress(\"" << var->Name() <<
"\", &v_" 578 << var->Name() <<
"_, &b_" << var->Name() <<
"_);\n";
584 cpp_file << class_name <<
"::~" << class_name <<
"(){\n";
588 for(std::vector<Variable>::const_iterator var = vars.begin();
599 for(std::vector<Variable>::const_iterator var = vars.begin();
602 typelist.insert(std::pair<std::string, std::string>(var->Name(), var->ToType()));
607 file << var.
ToType() <<
" const & " << class_name <<
"::" << var.
Name() <<
"() const{\n";
608 file <<
" if(!c_" << var.
Name() <<
"_ && b_"<< var.
Name() <<
"_){\n";
609 file <<
" b_" << var.
Name() <<
"_->GetEntry(entry_);\n";
611 if(var.
FromType()==
"std::vector<float>*" && var.
ToType()==
"std::vector<bool>*"){
612 file <<
" " << var.
Name() <<
"_->resize(v_" << var.
Name() <<
"_->size());\n";
613 file <<
" for(size_t i = 0; i < " << var.
Name() <<
"_->size(); ++i){\n";
614 file <<
" " << var.
Name() <<
"_->at(i) = static_cast<bool>(v_" 615 << var.
Name() <<
"_->at(i));\n";
618 throw std::logic_error(
"Cannot write accessor for requested type conversion");
621 file <<
" c_" << var.
Name() <<
"_ = true;\n";
623 file <<
" return " << var.
Name() <<
"_;\n";
630 std::string new_word;
631 std::vector<std::string> rep_list;
632 std::ifstream file(
"txt/mux_rules.cfg");
633 while(file >> new_word){
634 if(new_word.find(
"--") != std::string::npos)
break;
637 while(file >> new_word){
641 for(
unsigned i = 0; i < 2 && file >> temp; ++i){
642 rep_list.push_back(temp);
645 reps.push_back(std::make_pair(new_word, rep_list));
652 for(Typelist::const_iterator var = typelist.begin();
653 var != typelist.end();
655 const std::string&
name = var->first;
656 const std::string& type = var->second;
658 for(RepList::const_iterator pat = pats.begin();
662 for(std::vector<std::string>::const_iterator to_match = pat->second.begin();
663 to_match != pat->second.end();
665 const std::string::size_type match_loc = name.find(*to_match);
669 const std::string prefix = name.substr(0, match_loc);
670 const std::string suffix = name.substr(match_loc+to_match->size(), name.size());
673 const std::string out_name = prefix + pat->first + suffix;
674 const std::pair<std::string, std::string> key_val(out_name, type);
677 std::vector<std::string> out_vars(pat->second.size(),
"");
678 for(
unsigned i = 0; i < out_vars.size(); ++i){
679 const std::string test_name = prefix + pat->second.at(i) + suffix;
680 if(typelist.find(std::make_pair(test_name, type)) != typelist.end()){
682 out_vars.at(i) = test_name;
687 if(reps.find(key_val) != reps.end() && reps.find(key_val)->second!=out_vars){
688 throw std::runtime_error(
"Auto-generating the same function in multiple ways ("+type+
" "+out_name+
")");
691 reps.insert(std::make_pair(key_val, out_vars));
702 for(RepMap::const_iterator it = replacements.begin();
703 it != replacements.end();
705 if(typelist.find(it->first) != typelist.end()){
706 overwritten.insert(*it);
708 new_funcs.insert(*it);
714 std::ofstream hpp_file(
"inc/cfa.hpp");
716 hpp_file <<
"#ifndef H_CFA_MERGED\n";
717 hpp_file <<
"#define H_CFA_MERGED\n\n";
719 hpp_file <<
"#include <vector>\n";
720 hpp_file <<
"#include <string>\n";
721 hpp_file <<
"#include <typeinfo>\n\n";
723 hpp_file <<
"#include \"TChain.h\"\n\n";
725 hpp_file <<
"class cfa_base;\n\n";
727 hpp_file <<
"class cfa{\n";
728 hpp_file <<
"public:\n";
729 hpp_file <<
" explicit cfa(const std::string &file, const bool is_8TeV = false);\n\n";
731 hpp_file <<
" long TotalEntries() const;\n";
732 hpp_file <<
" virtual void GetEntry(const long entry);\n";
733 hpp_file <<
" short GetVersion() const;\n";
734 hpp_file <<
" const std::string& SampleName() const;\n";
735 hpp_file <<
" const std::string& SampleName(const std::string &sample_name);\n";
736 hpp_file <<
" void SetFile(const std::string &file, bool is_8TeV = false);\n";
737 hpp_file <<
" void AddFiles(const std::string &file);\n";
738 hpp_file <<
" const std::type_info& Type() const;\n\n";
740 hpp_file <<
" virtual ~cfa();\n\n";
743 for(Typelist::const_iterator it = typelist.begin();
744 it != typelist.end();
746 const std::string
name = it->first;
747 const std::string type = it->second;
749 if(overwritten.find(*it) != overwritten.end()){
750 hpp_file <<
" " << type <<
" const & " << name <<
"(const bool mux = true) const;\n";
752 hpp_file <<
" " << type <<
" const & " << name <<
"() const;\n";
756 for(RepMap::const_iterator it = new_funcs.begin();
757 it != new_funcs.end();
759 hpp_file <<
" " << it->first.second <<
" const & " << it->first.first <<
"() const;\n";
763 hpp_file <<
"private:\n";
764 hpp_file <<
" cfa_base* cfa_;\n";
765 hpp_file <<
"};\n\n";
767 hpp_file <<
"#endif\n";
773 const std::vector<std::string>& class_names){
774 std::ofstream cpp_file(
"src/cfa.cpp");
776 cpp_file <<
"#include \"cfa.hpp\"\n\n";
778 cpp_file <<
"#include <string>\n";
779 cpp_file <<
"#include <vector>\n";
780 cpp_file <<
"#include <sstream>\n";
781 cpp_file <<
"#include <stdexcept>\n";
782 cpp_file <<
"#include <typeinfo>\n\n";
784 cpp_file <<
"#include \"TChain.h\"\n\n";
786 cpp_file <<
"#include \"cfa_base.hpp\"\n";
787 cpp_file <<
"#include \"cfa_8.hpp\"\n";
788 cpp_file <<
"#include \"cfa_13.hpp\"\n\n";
790 cpp_file <<
"cfa::cfa(const std::string &file, const bool is_8TeV):\n";
791 cpp_file <<
" cfa_( is_8TeV ? static_cast<cfa_base*>(new cfa_8(file)) : static_cast<cfa_base*>(new cfa_13(file)) ){\n";
794 cpp_file <<
"long cfa::TotalEntries() const{\n";
795 cpp_file <<
" return cfa_->TotalEntries();\n";
798 cpp_file <<
"void cfa::GetEntry(const long entry){\n";
799 cpp_file <<
" cfa_->GetEntry(entry);\n";
802 cpp_file <<
"short cfa::GetVersion() const{\n";
803 cpp_file <<
" return cfa_->GetVersion();\n";
806 cpp_file <<
"const std::string& cfa::SampleName() const{\n";
807 cpp_file <<
" return cfa_->SampleName();\n";
810 cpp_file <<
"const std::string& cfa::SampleName(const std::string &sample_name){\n";
811 cpp_file <<
" return cfa_->SampleName(sample_name);\n";
814 cpp_file <<
"void cfa::SetFile(const std::string &file, const bool is_8TeV){\n";
815 cpp_file <<
" delete cfa_; cfa_=NULL;\n";
816 cpp_file <<
" cfa_ = ( is_8TeV ? static_cast<cfa_base*>(new cfa_8(file)) : static_cast<cfa_base*>(new cfa_13(file)) );\n";
819 cpp_file <<
"void cfa::AddFiles(const std::string &file){\n";
820 cpp_file <<
" cfa_->AddFiles(file);\n";
823 cpp_file <<
"const std::type_info& cfa::Type() const{\n";
824 cpp_file <<
" return typeid(*cfa_);\n";
827 cpp_file <<
"cfa::~cfa(){\n";
828 cpp_file <<
" delete cfa_; cfa_=NULL;\n";
832 for(Typelist::const_iterator it = typelist.begin();
833 it != typelist.end();
835 const std::string
name = it->first;
836 const std::string type = it->second;
838 const RepMap::const_iterator over_it = overwritten.find(*it);
839 if(over_it == overwritten.end()){
841 cpp_file << type <<
" const & cfa::" << name <<
"() const{\n";
842 cpp_file <<
" return cfa_->" << name <<
"();\n";
846 const std::vector<std::string>& funcs = over_it->second;
847 if(funcs.size() != class_names.size())
throw std::runtime_error(
"Could not match class names to muxes");
849 cpp_file << type <<
" const & cfa::" << name <<
"(const bool mux) const{\n";
851 cpp_file <<
" if(mux){\n";
852 for(
unsigned i = 0; i < funcs.size(); ++i){
853 cpp_file <<
" " << (i?
"}else ":
"") <<
"if(typeid(*cfa_)==typeid(" << class_names.at(i) <<
")){\n";
855 cpp_file <<
" return cfa_->" << funcs.at(i) <<
"();\n";
857 cpp_file <<
" throw std::logic_error(\"Function lookup unknown for class \"+std::string(typeid(*cfa_).name()));\n";
860 cpp_file <<
" }else{\n";
861 cpp_file <<
" throw std::logic_error(\"Function lookup unknown for class \"+std::string(typeid(*cfa_).name()));\n";
865 cpp_file <<
" return cfa_->" << name <<
"();\n";
871 for(RepMap::const_iterator it = new_funcs.begin();
872 it != new_funcs.end();
874 const std::string&
name = it->first.first;
875 const std::string& type = it->first.second;
876 const std::vector<std::string>& funcs = it->second;
877 if(funcs.size() != class_names.size())
throw std::runtime_error(
"Could not match class names to muxes");
879 bool all_blank =
true;
880 std::string good_name =
"";
882 cpp_file << type <<
" const & cfa::" << name <<
"() const{\n";
883 for(
unsigned i = 0; i < funcs.size(); ++i){
884 if(all_blank && funcs.at(i)!=
""){
886 good_name = funcs.at(i);
888 cpp_file <<
" " << (i?
"}else ":
"") <<
"if(typeid(*cfa_)==typeid(" << class_names.at(i) <<
")){\n";
890 cpp_file <<
" return cfa_->" << funcs.at(i) <<
"();\n";
892 cpp_file <<
" throw std::logic_error(\"Function lookup unknown for class \"+std::string(typeid(*cfa_).name()));\n";
895 cpp_file <<
" }else{\n";
896 cpp_file <<
" throw std::logic_error(\"Function lookup unknown for class \"+std::string(typeid(*cfa_).name()));\n";
898 cpp_file <<
" return cfa_->" << good_name <<
"();\n";
902 if(all_blank)
throw std::runtime_error(
"No valid function call available for "+type+name);
std::set< std::pair< std::string, std::string > > Typelist
void GetVariables(TChain &chain, const bool is_chain_a, const size_t file, const size_t num_files, Dictionary &vars)
void WriteBaseHeader(const std::vector< Variable > &base_vars, const std::vector< Variable > &virtual_vars)
std::vector< std::pair< std::string, std::vector< std::string > > > RepList
void WriteMergedSource(const Typelist &typelist, const RepMap &overwritten, const RepMap &new_funcs, const std::vector< std::string > &class_names)
void WriteDerivedSource(const std::string &class_name, const std::vector< Variable > &vars)
void WriteBaseSource(const std::vector< Variable > &base_vars, const std::vector< Variable > &virtual_vars)
void GetOverwritten(const Typelist &typelist, const RepMap &replacements, RepMap &overwritten, RepMap &new_funcs)
const std::string & Name() const
void AddToTypelist(const std::vector< Variable > &vars, Typelist &typelist)
void PrintAccessor(const Variable &var, std::ofstream &file, const std::string &class_name)
std::map< std::pair< std::string, std::string >, std::vector< std::string > > RepMap
void GetReplacements(const Typelist &typelist, const RepList &pats, RepMap &reps)
void WriteMergedHeader(const Typelist &typelist, const RepMap &overwritten, const RepMap &new_funcs)
void GetRules(RepList &reps)
void ClassifyVariables(const Dictionary &all_vars, std::vector< Variable > &base_vars, std::vector< Variable > &virtual_vars, std::vector< std::vector< Variable > > &file_vars)
const std::string & ToType() const
void WriteDerivedHeader(const std::string &class_name, const std::vector< Variable > &vars)
const bool & FromChainA() const
std::string AllCaps(std::string str)
std::map< std::string, std::vector< std::pair< bool, std::string > > > Dictionary
const std::string & FromType() const