Main Page
Namespaces
Classes
Package Documentation
RecoEgamma
EgammaTools
interface
MultiToken.h
Go to the documentation of this file.
1
#ifndef RecoEgamma_EgammaTools_MultiToken_H
2
#define RecoEgamma_EgammaTools_MultiToken_H
3
4
#include "
DataFormats/Common/interface/Handle.h
"
5
#include "
FWCore/Framework/interface/ConsumesCollector.h
"
6
#include "
FWCore/Framework/interface/Event.h
"
7
#include "
FWCore/ParameterSet/interface/ParameterSet.h
"
8
#include "
FWCore/Utilities/interface/EDGetToken.h
"
9
#include "
FWCore/Utilities/interface/Exception.h
"
10
#include "
FWCore/Utilities/interface/InputTag.h
"
11
12
/*
13
* This class is a wrapper around a vector of EDM tokens, of which at least one
14
* is expected to yield a valid handle.
15
*
16
* The first time you call getValidHandle() or getHandle(), it will go over all
17
* tokens and try to get a valid handle. If no token yields a valid handle, it
18
* will either throw and exception or return the last non-valid handle.
19
*
20
* Once it found a valid handle, it will remember which token was used to get
21
* it and therefore doesn't need to loop over all tokens from there on.
22
*
23
* Example use case: auto-detection of AOD vs. MiniAOD.
24
*
25
* Created by Jonas Rembser on August 3, 2018.
26
*/
27
28
#include <memory>
29
30
template
<
typename
T>
31
class
MultiTokenT
{
32
33
using
GoodIndexType
= std::shared_ptr<std::atomic<int>>;
34
35
public
:
36
37
template
<
typename
... Tags>
38
MultiTokenT
(
edm::ConsumesCollector
&& cc, Tags ... tags)
39
:
isMaster_
(
true
)
40
,
tokens_
({cc.mayConsume<
T
>(
edm::InputTag
(tags))...})
41
,
goodIndex_
(std::make_shared<std::atomic<int>>(-1))
42
{}
43
44
// Constructor which gets the input tags from a config to create the tokens plus master token
45
template
<
typename
S
,
typename
... Tags>
46
MultiTokenT
(
const
MultiTokenT<S>
&
master
,
edm::ConsumesCollector
&& cc, Tags ... tags)
47
:
isMaster_
(
false
)
48
,
tokens_
({cc.mayConsume<
T
>(
edm::InputTag
(tags))...})
49
,
goodIndex_
(
master
.getGoodTokenIndexPtr())
50
{}
51
52
// Constructor which gets the input tags from a config to create the tokens
53
template
<
typename
... Tags>
54
MultiTokenT
(
edm::ConsumesCollector
&& cc,
const
edm::ParameterSet
&
pset
, Tags && ... tags)
55
:
isMaster_
(
true
)
56
,
tokens_
({cc.mayConsume<
T
>(pset.
getParameter
<
edm::InputTag
>(tags))...})
57
,
goodIndex_
(std::make_shared<std::atomic<int>>(-1))
58
{}
59
60
// Constructor which gets the input tags from a config to create the tokens plus master token
61
template
<
typename
S
,
typename
... Tags>
62
MultiTokenT
(
const
MultiTokenT<S>
&
master
,
edm::ConsumesCollector
&& cc,
const
edm::ParameterSet
&
pset
, Tags && ... tags)
63
:
isMaster_
(
false
)
64
,
tokens_
({cc.mayConsume<
T
>(pset.
getParameter
<
edm::InputTag
>(tags))...})
65
,
goodIndex_
(
master
.getGoodTokenIndexPtr())
66
{}
67
68
// Get a handle on the event data, non-valid handle is allowed
69
edm::Handle<T>
getHandle
(
const
edm::Event
&
iEvent
)
const
70
{
71
edm::Handle<T>
handle
;
72
73
// If we already know which token works, take that one
74
if
(*
goodIndex_
>= 0) {
75
iEvent.
getByToken
(
tokens_
[*
goodIndex_
], handle);
76
return
handle
;
77
}
78
79
if
(!
isMaster_
) {
80
throw
cms::Exception
(
"MultiTokenTException"
) <<
81
"Trying to get a handle from a depending MultiToken before the master!"
;
82
}
83
84
// If not, set the good token index parallel to getting the handle
85
handle =
getInitialHandle
(iEvent);
86
87
if
(*
goodIndex_
== -1) {
88
*
goodIndex_
=
tokens_
.size() - 1;
89
}
90
return
handle
;
91
}
92
93
// Get a handle on the event data,
94
// throw exception if no token yields a valid handle
95
edm::Handle<T>
getValidHandle
(
const
edm::Event
&
iEvent
)
const
96
{
97
edm::Handle<T>
handle
;
98
99
// If we already know which token works, take that one
100
if
(*
goodIndex_
>= 0) {
101
iEvent.
getByToken
(
tokens_
[*
goodIndex_
], handle);
102
if
(!handle.
isValid
())
103
throw
cms::Exception
(
"MultiTokenTException"
) <<
104
"Token gave valid handle previously but not anymore!"
;
105
return
handle
;
106
}
107
108
if
(!
isMaster_
) {
109
throw
cms::Exception
(
"MultiTokenTException"
) <<
110
"Trying to get a handle from a depending MultiToken before the master!"
;
111
}
112
113
// If not, set the good token index parallel to getting the handle
114
handle =
getInitialHandle
(iEvent);
115
116
if
(*
goodIndex_
== -1) {
117
throw
cms::Exception
(
"MultiTokenTException"
) <<
"Neither handle is valid!"
;
118
}
119
return
handle
;
120
}
121
122
// get the good token
123
edm::EDGetTokenT<T>
get
(
const
edm::Event
&
iEvent
)
const
124
{
125
// If we already know which token works, take that index
126
if
(*
goodIndex_
>= 0)
127
return
tokens_
[*
goodIndex_
];
128
129
// If this is not a master MultiToken, just return what it got
130
if
(!
isMaster_
) {
131
throw
cms::Exception
(
"MultiTokenTException"
) <<
132
"Trying to get a handle from a depending MultiToken before the master!"
;
133
}
134
135
// Find which token is the good one by trying to get a handle
136
edm::Handle<T>
handle
;
137
for
(
auto
token:
tokens_
) {
138
iEvent
.getByToken(token, handle);
139
if
(handle.
isValid
()) {
140
return
token;
141
}
142
}
143
144
throw
cms::Exception
(
"MultiTokenTException"
) <<
"Neither token is valid!"
;
145
}
146
147
int
getGoodTokenIndex
()
const
148
{
149
return
*
goodIndex_
;
150
}
151
152
GoodIndexType
getGoodTokenIndexPtr
()
const
153
{
154
return
goodIndex_
;
155
}
156
157
private
:
158
159
edm::Handle<T>
getInitialHandle
(
const
edm::Event
&
iEvent
)
const
160
{
161
// Try to retrieve the collection from the event. If we fail to
162
// retrieve the collection with one name, we next look for the one with
163
// the other name and so on.
164
edm::Handle<T>
handle
;
165
for
(
size_t
i
= 0;
i
<
tokens_
.size(); ++
i
) {
166
iEvent.
getByToken
(
tokens_
[
i
], handle);
167
if
(handle.
isValid
()) {
168
*
goodIndex_
=
i
;
169
return
handle
;
170
}
171
}
172
return
handle
;
173
}
174
175
const
bool
isMaster_
;
176
const
std::vector<edm::EDGetTokenT<T>>
tokens_
;
177
const
GoodIndexType
goodIndex_
;
178
};
179
180
#endif
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
funct::master
Master< F > master(const F &f)
Definition:
FunctClone.h:68
MultiTokenT::goodIndex_
const GoodIndexType goodIndex_
Definition:
MultiToken.h:177
mps_fire.i
i
Definition:
mps_fire.py:330
EDGetToken.h
Exception
Definition:
hltDiff.cc:292
MultiTokenT::tokens_
const std::vector< edm::EDGetTokenT< T > > tokens_
Definition:
MultiToken.h:176
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition:
Event.h:517
funct::false
false
Definition:
Factorize.h:36
Event.h
edm::Handle
Definition:
AssociativeIterator.h:47
muonDTDigis_cfi.pset
pset
Definition:
muonDTDigis_cfi.py:27
MultiTokenT::GoodIndexType
std::shared_ptr< std::atomic< int >> GoodIndexType
Definition:
MultiToken.h:33
edm::EDGetTokenT
Definition:
EDGetToken.h:33
ParameterSet.h
funct::true
true
Definition:
Factorize.h:185
iEvent
int iEvent
Definition:
GenABIO.cc:224
MultiTokenT::getValidHandle
edm::Handle< T > getValidHandle(const edm::Event &iEvent) const
Definition:
MultiToken.h:95
MultiTokenT::isMaster_
const bool isMaster_
Definition:
MultiToken.h:175
edm::HandleBase::isValid
bool isValid() const
Definition:
HandleBase.h:74
MultiTokenT::getGoodTokenIndexPtr
GoodIndexType getGoodTokenIndexPtr() const
Definition:
MultiToken.h:152
Exception.h
cms::Exception
Definition:
Exception.h:68
MultiTokenT::MultiTokenT
MultiTokenT(edm::ConsumesCollector &&cc, const edm::ParameterSet &pset, Tags &&...tags)
Definition:
MultiToken.h:54
S
double S(const TLorentzVector &, const TLorentzVector &)
Definition:
Particle.cc:99
cmsBatch.handle
handle
Definition:
cmsBatch.py:286
MultiTokenT::getInitialHandle
edm::Handle< T > getInitialHandle(const edm::Event &iEvent) const
Definition:
MultiToken.h:159
edm::InputTag
Definition:
InputTag.h:15
MultiTokenT::getHandle
edm::Handle< T > getHandle(const edm::Event &iEvent) const
Definition:
MultiToken.h:69
InputTag.h
MultiTokenT::getGoodTokenIndex
int getGoodTokenIndex() const
Definition:
MultiToken.h:147
MultiTokenT::MultiTokenT
MultiTokenT(const MultiTokenT< S > &master, edm::ConsumesCollector &&cc, Tags...tags)
Definition:
MultiToken.h:46
MultiTokenT::MultiTokenT
MultiTokenT(const MultiTokenT< S > &master, edm::ConsumesCollector &&cc, const edm::ParameterSet &pset, Tags &&...tags)
Definition:
MultiToken.h:62
edm::ParameterSet
Definition:
ParameterSet.h:36
edm::Event
Definition:
Event.h:71
T
long double T
Definition:
Basic3DVectorLD.h:66
ConsumesCollector.h
MultiTokenT
Definition:
MultiToken.h:31
edm::ConsumesCollector
Definition:
ConsumesCollector.h:39
Handle.h
MultiTokenT::MultiTokenT
MultiTokenT(edm::ConsumesCollector &&cc, Tags...tags)
Definition:
MultiToken.h:38
Generated for CMSSW Reference Manual by
1.8.11