ATLAS Offline Software
TrigPassBits_v1.icc
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // $Id: TrigPassBits_v1.icc 773869 2016-09-19 15:27:05Z krasznaa $
8 #ifndef XAODTRIGGER_VERSIONS_TRIGPASSBITS_V1_ICC
9 #define XAODTRIGGER_VERSIONS_TRIGPASSBITS_V1_ICC
10 
11 // System include(s):
12 #include <stdexcept>
13 
14 // xAOD include(s):
15 #include "AthContainers/normalizedTypeinfoName.h"
16 #include "xAODCore/CLASS_DEF.h"
17 
18 namespace xAOD {
19 
20  /// This function is used to initialise or reset the object, to describe
21  /// a target container. It sets up all the internal variables of the object
22  /// so that subsequent maskPassing/isPassing calls would succeed.
23  ///
24  /// @param container Pointer to the container that this object should
25  /// describe
26  /// @param containerKey A unique key describing the target container.
27  /// (The sub-type index from the trigger navigation.)
28  ///
29  template< class CONT >
30  void TrigPassBits_v1::reset( const CONT* container, uint32_t containerKey ) {
31 
32  // Set all internal variables:
33  m_container = static_cast< const void* >( container );
34  setSize( container->size() );
35  if( container->size() == 0 ) {
36  setPassBits( std::vector< uint32_t >() );
37  } else {
38  setPassBits( std::vector< uint32_t >( ( ( container->size() - 1 ) /
39  32 ) + 1 ) );
40  }
41  setContainerKey( containerKey );
42  setContainerClid( ClassID_traits< CONT >::ID() );
43 
44  return;
45  }
46 
47  /// Tried to use const references in the arguments, but then the function
48  /// overload broke. So instead of providing template and non-template
49  /// functions with slightly different names, decided to use pointers in
50  /// the interface instead.
51  ///
52  /// @param obj The object whose state should be set
53  /// @param container The container that the object is part of
54  /// @param passed The "passing state" of the specified object
55  ///
56  template< class OBJ, class CONT >
57  void TrigPassBits_v1::markPassing( const OBJ* obj, const CONT* container,
58  bool passed ) {
59 
60  // Check if the CLID of the specified container is the same as the
61  // container that the information was collected for originally:
62  if( ClassID_traits< CONT >::ID() != containerClid() ) {
63  throw std::runtime_error( "The CLID of the passed container does "
64  "not match that of the original container" );
65  }
66 
67  // Check that we received the correct container:
68  if( m_container && ( m_container != container ) ) {
69  throw std::runtime_error( "Function called with a wrong container" );
70  }
71 
72  // Find the element in the container:
73  auto itr = std::find( container->begin(), container->end(), obj );
74  if( itr == container->end() ) {
75  throw std::runtime_error( "Specified object is not part of the "
76  "specified container" );
77  }
78 
79  // Call the non-templated function to set the bit:
80  markPassing( itr - container->begin(), passed );
81 
82  return;
83  }
84 
85  /// The same comment applied as for the <code>markPassing</code> function.
86  /// In order to avoid clashes with the non-template function, I had to use
87  /// pointer arguments for the function.
88  ///
89  /// @param obj The objects whose state we should check
90  /// @param container The container that the object is part of
91  /// @returns The "passing state" of the object
92  ///
93  template< class OBJ, class CONT >
94  bool TrigPassBits_v1::isPassing( const OBJ* obj,
95  const CONT* container ) const {
96 
97  // Check if the CLID of the specified container is the same as the
98  // container that the information was collected for originally:
99  if( ClassID_traits< CONT >::ID() != containerClid() ) {
100  throw std::runtime_error( "The CLID of the passed container does "
101  "not match that of the original container" );
102  }
103 
104  // Find the element in the container:
105  auto itr = std::find( container->begin(), container->end(), obj );
106  if( itr == container->end() ) {
107  throw std::runtime_error( "Specified object is not part of the "
108  "specified container" );
109  }
110 
111  // Call the non-templated function to check the status of the bit:
112  return isPassing( itr - container->begin() );
113  }
114 
115  /// This function can be used to check whether an element of a container
116  /// passed the hypothesis selections. While checking that we use the
117  /// container with the right key.
118  ///
119  /// @param obj The objects whose state we should check
120  /// @param container The container that the object is part of
121  /// @param key The hashed key of the container we are checking
122  /// @returns The "passing state" of the object
123  ///
124  template< class OBJ, class CONT >
125  bool TrigPassBits_v1::isPassing( const OBJ* obj,
126  const CONT* container,
127  uint32_t key ) const {
128 
129  // Check if the key of the container matches the recorded one:
130  if( key != containerKey() ) {
131  throw std::runtime_error( "The specified key doesn't match the one "
132  "stored in the object" );
133  }
134 
135  // Let the other function do the rest:
136  return isPassing( obj, container );
137  }
138 
139  /// This function can be used to check whether an element of a container
140  /// passed the hypothesis selections. While checking that we use the
141  /// container with the right key.
142  ///
143  /// @param obj The objects whose state we should check
144  /// @param container The container that the object is part of
145  /// @param key The key of the container we are checking
146  /// @returns The "passing state" of the object
147  ///
148  template< class OBJ, class CONT >
149  bool TrigPassBits_v1::isPassing( const OBJ* obj,
150  const CONT* container,
151  const std::string& key ) const {
152 
153  // Let the other function do the heavy lifting:
154  return isPassing( obj, container, hash( key ) );
155  }
156 
157  /// This function is an analogue of the HLT::makeTrigPassBits function,
158  /// written for the original TrigPassBits type. It behaves much the
159  /// same way, it just makes it clear in its interface that the user
160  /// is supposed to take ownership of the received object.
161  ///
162  /// @param container Pointer to the container that this object should
163  /// describe
164  /// @param containerKey A unique key describing the target container.
165  /// (The sub-type index from the trigger navigation.)
166  ///
167  template< class CONT >
168  std::unique_ptr< TrigPassBits_v1 >
169  makeTrigPassBits( const CONT* container, uint32_t containerKey ) {
170 
171  // Create the new object:
172  std::unique_ptr< TrigPassBits_v1 > result( new TrigPassBits_v1() );
173  // Give it a private auxiliary store:
174  result->makePrivateStore();
175  // Set it up:
176  result->reset( container, containerKey );
177  // And now return it:
178  return result;
179  }
180 
181  /// This function is an analogue of the HLT::makeTrigPassBits function,
182  /// written for the original TrigPassBits type. It behaves much the
183  /// same way, it just makes it clear in its interface that the user
184  /// is supposed to take ownership of the received object.
185  ///
186  /// It's a more convenient way for hypo algorithm developers to specify
187  /// a key/label for the container that this TrigPassBits object should
188  /// be describing.
189  ///
190  /// @param container Pointer to the container that this object should
191  /// describe
192  /// @param containerKey A unique key describing the target container.
193  /// (The label of the container in most cases.)
194  ///
195  template< class CONT >
196  std::unique_ptr< TrigPassBits_v1 >
197  makeTrigPassBits( const CONT* container, const std::string& containerKey ) {
198 
199  // Create the new object:
200  std::unique_ptr< TrigPassBits_v1 > result( new TrigPassBits_v1() );
201  // Give it a private auxiliary store:
202  result->makePrivateStore();
203  // Set it up:
204  result->reset( container, xAOD::TrigPassBits_v1::hash( containerKey ) );
205  // And now return it:
206  return result;
207  }
208 
209 } // namespace xAOD
210 
211 #endif // XAODTRIGGER_VERSIONS_TRIGPASSBITS_V1_ICC