hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
Bus.h
Go to the documentation of this file.
1#ifndef FRAMEWORK_BUS_H
2#define FRAMEWORK_BUS_H
3
4#include <iostream>
5#include <map>
6#include <memory>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11// ROOT
12#include "TBranchElement.h"
13#include "TTree.h"
14
15namespace framework {
16
29class Bus {
30 public:
42 template <typename BaggageType>
43 const BaggageType& get(const std::string& name) {
44 return getRef<BaggageType>(name).get();
45 }
46
64 template <typename BaggageType>
65 void board(const std::string& name) {
66 passengers_[name] = std::make_unique<Passenger<BaggageType>>();
67 passengers_[name]->clear(); // make sure 'default' state is well defined
68 }
69
82 template <typename BaggageType>
83 void update(const std::string& name, const BaggageType& obj) {
84 getRef<BaggageType>(name).update(obj);
85 }
86
107 TBranch* attach(TTree* tree, const std::string& name, bool can_create) {
108 return passengers_[name]->attach(tree, name, can_create);
109 }
110
117 bool isOnBoard(const std::string& name) {
118 return passengers_.find(name) != passengers_.end();
119 }
120
126 void clear() {
127 for (auto& [n, handle] : passengers_) handle->clear();
128 }
129
143 void clear(const std::string& name) {
144 auto it = passengers_.find(name);
145 if (it != passengers_.end()) it->second->clear();
146 }
147
158 void everybodyOff() { passengers_.clear(); }
159
168 void stream(std::ostream& s) const {
169 for (auto& [n, handle] : passengers_)
170 s << n << " : " << handle << std::endl;
171 }
172
182 friend std::ostream& operator<<(std::ostream& s, const framework::Bus& b) {
183 b.stream(s);
184 return s;
185 }
186
187 private:
198 class Seat {
199 public:
206 virtual ~Seat() {}
207
223 virtual TBranch* attach(TTree* tree, const std::string& branch_name,
224 bool can_create) = 0;
225
234 virtual void clear() = 0;
235
241 virtual void stream(std::ostream& s) const = 0;
242
256 friend std::ostream& operator<<(
257 std::ostream& s, const std::unique_ptr<framework::Bus::Seat>& seat) {
258 seat->stream(s);
259 return s;
260 }
261
262 }; // Seat
263
264 private:
304 template <typename BaggageType>
305 class Passenger : public Seat {
306 public:
317 Passenger() : Seat(), baggage_{new BaggageType} {}
318
336 virtual ~Passenger() { delete baggage_; }
337
361 virtual TBranch* attach(TTree* tree, const std::string& branch_name,
362 bool can_create) {
363 return attach(the_type<BaggageType>{}, tree, branch_name, can_create);
364 }
365
370 const BaggageType& get() const { return *baggage_; }
371
383 void update(const BaggageType& updated_obj) {
384 *baggage_ = updated_obj;
386 }
387
395 virtual void clear() { clear(the_type<BaggageType>{}); }
396
404 virtual void stream(std::ostream& s) const {
406 }
407
418 friend std::ostream& operator<<(
419 std::ostream& s, const framework::Bus::Passenger<BaggageType>& p) {
420 p.stream(s);
421 return s;
422 }
423
424 private:
429 template <typename>
430 struct the_type {};
431
432 private: // specializations of attach
443 class DeleteObjectStatus : public TBranchElement {
446
447 public:
448 static int bit() { return TBranchElement::EStatusBits::kDeleteObject; }
449 };
450
475 template <typename T>
476 TBranch* attach(the_type<T> t, TTree* tree, const std::string& branch_name,
477 bool can_create) {
478 TBranch* branch = tree->GetBranch(branch_name.c_str());
479 if (branch) {
493 if (dynamic_cast<TBranchElement*>(branch)) {
494 branch->SetBit(DeleteObjectStatus::bit(), false);
495 }
496 branch->SetObject(baggage_);
497 } else if (can_create) {
506 branch = tree->Branch((branch_name + ".").c_str(), baggage_, 100000, 3);
507 }
508 return branch;
509 }
510
535 TBranch* attachBasic(TTree* tree, const std::string& branch_name,
536 bool can_create) {
537 TBranch* branch = tree->GetBranch(branch_name.c_str());
538 if (branch) {
539 // branch already exists
540 // set the object the branch should read/write from/to
541 branch->SetAddress(baggage_);
542 } else if (can_create) {
543 static const std::map<std::string, std::string> cpp_to_root_type_name =
544 {{"b", "O"}, {"s", "S"}, {"i", "I"},
545 {"l", "L"}, {"f", "F"}, {"d", "D"}};
546 // branch doesnt exist and we are allowed to make a new one
547 std::string cpp_type = typeid(*baggage_).name();
548 branch = tree->Branch(
549 branch_name.c_str(), baggage_,
550 (branch_name + "/" + cpp_to_root_type_name.at(cpp_type)).c_str());
551 }
552 return branch;
553 }
554
566 TBranch* attach(the_type<bool> t, TTree* tree,
567 const std::string& branch_name, bool can_create) {
568 return attachBasic(tree, branch_name, can_create);
569 }
570
582 TBranch* attach(the_type<short> t, TTree* tree,
583 const std::string& branch_name, bool can_create) {
584 return attachBasic(tree, branch_name, can_create);
585 }
586
598 TBranch* attach(the_type<int> t, TTree* tree,
599 const std::string& branch_name, bool can_create) {
600 return attachBasic(tree, branch_name, can_create);
601 }
602
614 TBranch* attach(the_type<long> t, TTree* tree,
615 const std::string& branch_name, bool can_create) {
616 return attachBasic(tree, branch_name, can_create);
617 }
618
630 TBranch* attach(the_type<float> t, TTree* tree,
631 const std::string& branch_name, bool can_create) {
632 return attachBasic(tree, branch_name, can_create);
633 }
634
646 TBranch* attach(the_type<double> t, TTree* tree,
647 const std::string& branch_name, bool can_create) {
648 return attachBasic(tree, branch_name, can_create);
649 }
650
651 private: // specializations of clear
656 void clear(the_type<bool> t) { *baggage_ = false; }
657
663 *baggage_ = std::numeric_limits<BaggageType>::min();
664 }
665
671 *baggage_ = std::numeric_limits<BaggageType>::min();
672 }
673
679 *baggage_ = std::numeric_limits<BaggageType>::min();
680 }
681
687 *baggage_ = std::numeric_limits<BaggageType>::min();
688 }
689
695 *baggage_ = std::numeric_limits<BaggageType>::min();
696 }
697
708 template <typename T>
710 baggage_->Clear();
711 }
712
717 template <typename Content>
718 void clear(the_type<std::vector<Content>> t) {
719 baggage_->clear();
720 }
721
726 template <typename Key, typename Val>
727 void clear(the_type<std::map<Key, Val>> t) {
728 baggage_->clear();
729 }
730
731 private: // specializations of post_update
736 template <typename T>
738
751
752 private: // specializations of stream
764 template <typename T>
765 void stream(the_type<T> t, std::ostream& s) const {
766 // s << *baggagage_;
767 }
768
778 template <typename Content>
779 void stream(the_type<std::vector<Content>> t, std::ostream& s) const {
780 s << baggage_->size();
781 /*
782 s << "[ ";
783 for (auto const& entry : *baggage_) s << entry << " ";
784 s << "]";
785 */
786 }
787
798 template <typename Key, typename Val>
799 void stream(the_type<std::map<Key, Val>> t, std::ostream& s) const {
800 s << baggage_->size();
801 /*
802 s << "{ ";
803 for (auto const& [k, v] : *baggage_) {
804 s << k << " -> " << v << " ";
805 }
806 s << "}";
807 */
808 }
809
810 private:
812 BaggageType* baggage_;
813 }; // Passenger
814
815 private:
831 template <typename BaggageType>
832 Passenger<BaggageType>& getRef(const std::string& name) {
833 return dynamic_cast<Passenger<BaggageType>&>(*passengers_[name]);
834 }
835
836 private:
843 std::unordered_map<std::string, std::unique_ptr<Seat>> passengers_;
844
845}; // Bus
846
847} // namespace framework
848
849#endif // FRAMEWORK_BUS_H
DeleteObjectStatus(const DeleteObjectStatus &)=delete
void clear(the_type< short > t)
Definition Bus.h:662
virtual void stream(std::ostream &s) const
Definition Bus.h:404
void clear(the_type< float > t)
Definition Bus.h:686
void clear(the_type< int > t)
Definition Bus.h:670
void clear(the_type< std::vector< Content > > t)
Definition Bus.h:718
TBranch * attach(the_type< int > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:598
void clear(the_type< bool > t)
Definition Bus.h:656
void clear(the_type< std::map< Key, Val > > t)
Definition Bus.h:727
void clear(the_type< long > t)
Definition Bus.h:678
TBranch * attach(the_type< T > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:476
virtual TBranch * attach(TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:361
virtual ~Passenger()
Definition Bus.h:336
void stream(the_type< std::vector< Content > > t, std::ostream &s) const
Definition Bus.h:779
void stream(the_type< T > t, std::ostream &s) const
Definition Bus.h:765
TBranch * attach(the_type< float > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:630
TBranch * attach(the_type< short > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:582
const BaggageType & get() const
Definition Bus.h:370
TBranch * attachBasic(TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:535
void stream(the_type< std::map< Key, Val > > t, std::ostream &s) const
Definition Bus.h:799
friend std::ostream & operator<<(std::ostream &s, const framework::Bus::Passenger< BaggageType > &p)
Definition Bus.h:418
void post_update(the_type< T > t)
Definition Bus.h:737
virtual void clear()
Definition Bus.h:395
TBranch * attach(the_type< bool > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:566
void clear(the_type< double > t)
Definition Bus.h:694
void clear(the_type< T > t)
Definition Bus.h:709
BaggageType * baggage_
A pointer to the baggage we own and created.
Definition Bus.h:812
TBranch * attach(the_type< long > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:614
TBranch * attach(the_type< double > t, TTree *tree, const std::string &branch_name, bool can_create)
Definition Bus.h:646
void update(const BaggageType &updated_obj)
Definition Bus.h:383
virtual void stream(std::ostream &s) const =0
virtual void clear()=0
virtual ~Seat()
Definition Bus.h:206
virtual TBranch * attach(TTree *tree, const std::string &branch_name, bool can_create)=0
friend std::ostream & operator<<(std::ostream &s, const std::unique_ptr< framework::Bus::Seat > &seat)
Definition Bus.h:256
void everybodyOff()
Definition Bus.h:158
friend std::ostream & operator<<(std::ostream &s, const framework::Bus &b)
Definition Bus.h:182
void board(const std::string &name)
Definition Bus.h:65
TBranch * attach(TTree *tree, const std::string &name, bool can_create)
Definition Bus.h:107
void stream(std::ostream &s) const
Definition Bus.h:168
std::unordered_map< std::string, std::unique_ptr< Seat > > passengers_
Definition Bus.h:843
void update(const std::string &name, const BaggageType &obj)
Definition Bus.h:83
void clear(const std::string &name)
Definition Bus.h:143
Passenger< BaggageType > & getRef(const std::string &name)
Definition Bus.h:832
void clear()
Definition Bus.h:126
bool isOnBoard(const std::string &name)
Definition Bus.h:117
const BaggageType & get(const std::string &name)
Definition Bus.h:43