ATLAS Offline Software
Loading...
Searching...
No Matches
TrigComposite.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5//#include <iostream>
6//#include <cmath>
7#include <stdexcept>
9
10
11using namespace std;
12
14 : m_name("dummy"),
15 m_locked(false)
16{
17}
18
20 : m_name(name),
21 m_locked(false)
22{
23}
24
25
26TrigComposite::TrigComposite(const std::string& name, std::string& label1, TrigFeatureLink t1, std::string& label2, TrigFeatureLink t2)
27 : m_name(name),
28 m_locked(false)
29{
30 addObject(label1, t1);
31 addObject(label2, t2);
32}
33
34
35
36template<typename T>
37void TrigComposite::setFormat(const std::vector<std::string>& keys, bool mustBeSet) {
38 for(const std::string& key : keys) {
39 addDetail<T>(key);
40 if (mustBeSet) {
41 mustSet<T>(key);
42 }
43 }
44}
45
46template<class T>
47void TrigComposite::mustSet(const std::string& key) {
48 detailsMustSetList<T>().insert(key);
49}
50
51
52// the implementation is identical as in case of the details
53// the naming is only distinct to emphasize that the TrigFeatureLinks are quite distinct entities
54void TrigComposite::addObject(const std::string& key, TrigFeatureLink link) {
55 addDetail(key, link);
56}
57
58void TrigComposite::setObject(const std::string& key, TrigFeatureLink link) {
59 setDetail(key, link);
60}
61
62
63bool TrigComposite::hasObject(const std::string& key) const {
65}
66
67const TrigFeatureLink& TrigComposite::getObject(const std::string& key) const {
69}
70
72 if ( m_linksToBeSet.empty()
73 and m_floatsToBeSet.empty()
74 and m_intsToBeSet.empty()
75 and m_stringsToBeSet.empty()
76 and m_v_intsToBeSet.empty()
77 and m_v_floatsToBeSet.empty()
78 and m_v_stringsToBeSet.empty() )
79 return true;
80 return false;
81}
82
84
85
86
87
88template<typename T>
89void TrigComposite::addDetail(const std::string& key, const T& value) {
90 if ( isFormatLocked() )
91 throw std::invalid_argument("The format of TrigComposite object has been locked, addDetails can not be used anymore, see unlockFormat or setDetail");
92 if ( hasDetail<T>(key) )
93 throw std::invalid_argument("The TrigCompositeObejct already has key: "+ key);
94
95 // now it needs to fins appropriate collection and add to it the value
96 detailsMap<T>()[key] = value;
97}
98
99
100template<typename T>
101void TrigComposite::setDetail(const std::string& key, const T& value) {
102 if ( hasDetail<T>(key) == false )
103 throw std::invalid_argument("The TrigCompositeObejct does not have key: "+ key);
104
105 // now it needs to fins appropriate collection and set the value
106 detailsMap<T>()[key] = value;
107 detailsMustSetList<T>().erase(key);
108}
109
110
111template<typename T>
112bool TrigComposite::hasDetail(const std::string& key) const {
113 return detailsMap<T>().find(key) != detailsMap<T>().end();
114}
115
116
117
118
119
120template<typename T>
121const T& TrigComposite::getDetail(const std::string& key) const {
122 if ( hasDetail<T>(key) )
123 return detailsMap<T>().find(key)->second;
124 return specimen<T>();
125}
126
127
128template<typename T>
129void TrigComposite::eraseDetail(const std::string& key) {
130 detailsMap<T>().erase(key);
131}
132
133
134template<typename T>
135std::map<std::string, T>& TrigComposite::detailsMap() {
136 // this should never be needed, need to add compile error here
137 [[maybe_unused]]
138 int z =
139 sizeof(struct TrigComposite_does_not_support_that_type_as_a_detail);
140 std::abort();
141}
142
143template<typename T>
144const std::map<std::string, T>& TrigComposite::detailsMap() const {
145 // this should never be needed, need to add compile error here
146 [[maybe_unused]]
147 int z =
148 sizeof(struct TrigComposite_does_not_support_that_type_as_a_detail);
149 std::abort();
150}
151
152// this macro helps instantiating methods for all necessary template arguments
153// disallowing implicit specializations we have a control over detail which at attached
154#define GEN_(type, varaible) \
155 template<> std::map<std::string, type>& TrigComposite::detailsMap() { return varaible; } \
156 template<> const std::map<std::string, type>& TrigComposite::detailsMap() const { return varaible; } \
157 template<> std::set<std::string>& TrigComposite::detailsMustSetList<type>() { return varaible##ToBeSet; } \
158 template<> const type& TrigComposite::specimen<type>() const { static const type x{}; return x;} \
159 template void TrigComposite::addDetail<type>(const std::string&, const type&); \
160 template void TrigComposite::setDetail<type>(const std::string&, const type&); \
161 template bool TrigComposite::hasDetail<type>(const std::string& ) const; \
162 template const type& TrigComposite::getDetail<type>(const std::string& ) const; \
163 template void TrigComposite::eraseDetail<type>(const std::string& ); \
164 template void TrigComposite::setFormat<type>(const std::vector<std::string>&, bool ); \
165 template void TrigComposite::mustSet<type>(const std::string&);
166
167GEN_(TrigFeatureLink, m_links)
168
169// singular details
170GEN_(float, m_floats)
171GEN_(int, m_ints)
172GEN_(std::string, m_strings)
173
174// vector details
175GEN_(std::vector<float>, m_v_floats)
176GEN_(std::vector<int>, m_v_ints)
177GEN_(std::vector<std::string>, m_v_strings)
178// shall more be needed ... easy to add, remember though about persistancy
179
180namespace {
181template<class T>
182MsgStream& print(MsgStream& log, const TrigComposite& d, const std::string& detailsName ) {
183 if ( ! d.allDetails<T>().empty() ) {
184 log << "TrigComposite: Details stored as " << detailsName << " are (key, value): ";
185
186 typedef typename std::map<std::string, T>::value_type key_value;
187 for( const key_value& kv : d.allDetails<T>()) {
188 log << "(" << kv.first << ", " << kv.second << ") ";
189 }
190 log << endmsg;
191 }
192
193 return log;
194}
195}
196
197
198MsgStream& operator<< ( MsgStream& log, const TrigComposite& d ) {
199 log << "TrigComposite object: " << d.name() << endmsg;
200 print<float> (log, d, "floats");
201 print<int> (log, d, "ints");
202 print<std::string> (log, d, "strings");
203
204
205 print<std::vector<float> > (log, d, "vector of floats") ;
206 print<std::vector<int> > (log, d, "vector of ints");
207 print<std::vector<string> > (log, d, "vector of strings");
208
209 if ( ! d.allDetails<TrigFeatureLink>().empty() ) {
210 typedef std::map<std::string, TrigFeatureLink>::value_type key_value;
211 for( const key_value& kv : d.allDetails<TrigFeatureLink>()) {
212 log << "(" << kv.first << ", " << "CLID:" << kv.second.clid() <<") ";
213 }
214
215 } else {
216 log << "There are no objects assocuiated with this TrigComposite object" << endmsg;
217 }
218
219
220 return log;
221}
222
223
224
#define endmsg
void print(char *figname, TCanvas *c1)
#define GEN_(type, varaible)
#define z
The class is meant to store links (of type TrigFeatureLink) to trigger objects and arbitrary details ...
bool hasDetail(const std::string &key) const
Checks if the object contains detail of the type T No verification is made if it is set.
void eraseDetail(const std::string &key)
erases the detail if it existed If the details inder that key did not exist, no action is performed.
~TrigComposite()
Destructor, does nothing special in fact, no links are touched.
void setFormat(const std::vector< std::string > &keys, bool mustBeSet=false)
defines the content (details which must to be stored) The method can be used to enforce certain conte...
const T & specimen() const
void mustSet(const std::string &key)
bool isValid() const
Checks the validity of the format, i.e.
bool hasObject(const std::string &key) const
checks if the object has link to the object under the key
TrigComposite()
Deafault constructor, should not be normally used, needed by the persistency layer.
void setObject(const std::string &key, TrigFeatureLink link)
sets the link
bool isFormatLocked() const
return true if the format can not be further changed (no addDetail can be called)
const TrigFeatureLink & getObject(const std::string &key) const
returns
const T & getDetail(const std::string &key) const
return the value of the detail, if the key is absent the default is returned
void setDetail(const std::string &key, const T &value)
sets the value user the key, if they detail under that key already exists it's value is overwritten,...
MustSetList & detailsMustSetList()
void addDetail(const std::string &key, const T &value=T())
adds the value user the key, if they detail under that key already exists the exception is thrown If ...
std::map< std::string, T > & detailsMap()
void addObject(const std::string &key, TrigFeatureLink link)
adds the link
STL namespace.
ostream & operator<<(ostream &s, const SG::VarHandleKey &m)