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