-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
EDConsumerBase.cc
542 lines (488 loc) · 19.8 KB
/
EDConsumerBase.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// -*- C++ -*-
//
// Package: FWCore/Framework
// Class : EDConsumerBase
//
// Implementation:
// [Notes on implementation]
//
// Original Author: Chris Jones
// Created: Tue, 02 Apr 2013 21:36:06 GMT
//
// system include files
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#include <utility>
// user include files
#include "FWCore/Framework/interface/EDConsumerBase.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Utilities/interface/BranchType.h"
#include "FWCore/Utilities/interface/Likely.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "DataFormats/Provenance/interface/ProductHolderIndexHelper.h"
#include "DataFormats/Provenance/interface/ProductRegistry.h"
using namespace edm;
//
// constants, enums and typedefs
//
//
// static data member definitions
//
//
// constructors and destructor
//
//EDConsumerBase::EDConsumerBase()
//{
//}
// EDConsumerBase::EDConsumerBase(const EDConsumerBase& rhs)
// {
// // do actual copying here;
// }
EDConsumerBase::~EDConsumerBase()
{
}
//
// assignment operators
//
// const EDConsumerBase& EDConsumerBase::operator=(const EDConsumerBase& rhs)
// {
// //An exception safe implementation is
// EDConsumerBase temp(rhs);
// swap(rhs);
//
// return *this;
// }
//
// member functions
//
ConsumesCollector
EDConsumerBase::consumesCollector() {
ConsumesCollector c{this};
return c;
}
static const edm::InputTag kWasEmpty("@EmptyLabel@");
edm::InputTag const&
EDConsumerBase::checkIfEmpty(edm::InputTag const& iTag) {
if (iTag.label().empty()) {
return kWasEmpty;
}
return iTag;
}
unsigned int
EDConsumerBase::recordConsumes(BranchType iBranch, TypeToGet const& iType, edm::InputTag const& iTag, bool iAlwaysGets) {
if(frozen_) {
throwConsumesCallAfterFrozen(iType, iTag);
}
unsigned int index =m_tokenInfo.size();
bool skipCurrentProcess = iTag.willSkipCurrentProcess();
const size_t labelSize = iTag.label().size();
const size_t productInstanceSize = iTag.instance().size();
unsigned int labelStart = m_tokenLabels.size();
unsigned short delta1 = labelSize+1;
unsigned short delta2 = labelSize+2+productInstanceSize;
m_tokenInfo.emplace_back(TokenLookupInfo{iType.type(), ProductHolderIndexInvalid, skipCurrentProcess, iBranch},
iAlwaysGets,
LabelPlacement{labelStart,delta1,delta2},
iType.kind());
const size_t additionalSize =
skipCurrentProcess ?
labelSize+productInstanceSize+3 :
labelSize+productInstanceSize+iTag.process().size()+3;
m_tokenLabels.reserve(m_tokenLabels.size()+additionalSize);
{
const std::string& m =iTag.label();
m_tokenLabels.insert(m_tokenLabels.end(),m.begin(),m.end());
m_tokenLabels.push_back('\0');
}
{
const std::string& m =iTag.instance();
m_tokenLabels.insert(m_tokenLabels.end(),m.begin(),m.end());
m_tokenLabels.push_back('\0');
}
{
if (!skipCurrentProcess) {
const std::string& m =iTag.process();
m_tokenLabels.insert(m_tokenLabels.end(),m.begin(),m.end());
m_tokenLabels.push_back('\0');
} else {
m_tokenLabels.push_back('\0');
}
}
return index;
}
void
EDConsumerBase::updateLookup(BranchType iBranchType,
ProductHolderIndexHelper const& iHelper)
{
frozen_ = true;
{
auto itKind = m_tokenInfo.begin<kKind>();
auto itLabels = m_tokenInfo.begin<kLabels>();
for(auto itInfo = m_tokenInfo.begin<kLookupInfo>(),itEnd = m_tokenInfo.end<kLookupInfo>();
itInfo != itEnd; ++itInfo,++itKind,++itLabels) {
if(itInfo->m_branchType == iBranchType) {
const unsigned int labelStart = itLabels->m_startOfModuleLabel;
const char* moduleLabel = &(m_tokenLabels[labelStart]);
itInfo->m_index = ProductHolderIndexAndSkipBit(iHelper.index(*itKind,
itInfo->m_type,
moduleLabel,
moduleLabel+itLabels->m_deltaToProductInstance,
moduleLabel+itLabels->m_deltaToProcessName),
itInfo->m_index.skipCurrentProcess());
}
}
}
//now add resolved requests to get many to the end of our list
// a get many will have an empty module label
for(size_t i=0, iEnd = m_tokenInfo.size(); i!=iEnd;++i) {
//need to copy since pointer could be invalidated by emplace_back
auto const info = m_tokenInfo.get<kLookupInfo>(i);
if(info.m_branchType == iBranchType &&
info.m_index.productHolderIndex() == ProductHolderIndexInvalid &&
m_tokenLabels[m_tokenInfo.get<kLabels>(i).m_startOfModuleLabel]=='\0') {
//find all matching types
const auto kind=m_tokenInfo.get<kKind>(i);
auto matches = iHelper.relatedIndexes(kind,info.m_type);
//NOTE: This could be changed to contain the true labels for what is being
// requested but for now I want to remember these are part of a get many
const LabelPlacement labels= m_tokenInfo.get<kLabels>(i);
bool alwaysGet = m_tokenInfo.get<kAlwaysGets>(i);
for(unsigned int j=0;j!=matches.numberOfMatches();++j) {
//only keep the ones that are for a specific data item and not a collection
if(matches.isFullyResolved(j)) {
auto index =matches.index(j);
m_tokenInfo.emplace_back(TokenLookupInfo{info.m_type, index, info.m_index.skipCurrentProcess(), info.m_branchType},
alwaysGet,
labels,
kind);
}
}
}
}
m_tokenInfo.shrink_to_fit();
if(iBranchType == InEvent) {
itemsToGet(iBranchType, itemsToGetFromEvent_);
//Temporary: until we release resources on Event::getBy* calls
// we need to prefetch mayConsumes as well
itemsMayGet(iBranchType, itemsToGetFromEvent_);
}
}
//
// const member functions
//
ProductHolderIndexAndSkipBit
EDConsumerBase::indexFrom(EDGetToken iToken, BranchType iBranch, TypeID const& iType) const
{
if(unlikely(iToken.index()>=m_tokenInfo.size())) {
throwBadToken(iType,iToken);
}
const auto& info = m_tokenInfo.get<kLookupInfo>(iToken.index());
if (likely(iBranch == info.m_branchType)) {
if (likely(iType == info.m_type)) {
return info.m_index;
} else {
throwTypeMismatch(iType, iToken);
}
} else {
throwBranchMismatch(iBranch,iToken);
}
return ProductHolderIndexAndSkipBit(edm::ProductHolderIndexInvalid, false);
}
void
EDConsumerBase::itemsToGet(BranchType iBranch, std::vector<ProductHolderIndexAndSkipBit>& oIndices) const
{
//how many are we adding?
unsigned int count=0;
{
auto itAlwaysGet = m_tokenInfo.begin<kAlwaysGets>();
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it,++itAlwaysGet) {
if(iBranch==it->m_branchType) {
if (it->m_index.productHolderIndex() != ProductHolderIndexInvalid) {
if(*itAlwaysGet) {
++count;
}
}
}
}
}
oIndices.reserve(oIndices.size()+count);
{
auto itAlwaysGet = m_tokenInfo.begin<kAlwaysGets>();
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it,++itAlwaysGet) {
if(iBranch==it->m_branchType) {
if (it->m_index.productHolderIndex() != ProductHolderIndexInvalid) {
if(*itAlwaysGet) {
oIndices.push_back(it->m_index);
}
}
}
}
}
}
void
EDConsumerBase::itemsMayGet(BranchType iBranch, std::vector<ProductHolderIndexAndSkipBit>& oIndices) const
{
//how many are we adding?
unsigned int count=0;
{
auto itAlwaysGet = m_tokenInfo.begin<kAlwaysGets>();
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it,++itAlwaysGet) {
if(iBranch==it->m_branchType) {
if (it->m_index.productHolderIndex() != ProductHolderIndexInvalid) {
if(not *itAlwaysGet) {
++count;
}
}
}
}
}
oIndices.reserve(oIndices.size()+count);
{
auto itAlwaysGet = m_tokenInfo.begin<kAlwaysGets>();
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it,++itAlwaysGet) {
if(iBranch==it->m_branchType) {
if (it->m_index.productHolderIndex() != ProductHolderIndexInvalid) {
if(not *itAlwaysGet) {
oIndices.push_back(it->m_index);
}
}
}
}
}
}
void
EDConsumerBase::labelsForToken(EDGetToken iToken, Labels& oLabels) const
{
unsigned int index = iToken.index();
auto labels = m_tokenInfo.get<kLabels>(index);
unsigned int start = labels.m_startOfModuleLabel;
oLabels.module = &(m_tokenLabels[start]);
oLabels.productInstance = oLabels.module+labels.m_deltaToProductInstance;
oLabels.process = oLabels.module+labels.m_deltaToProcessName;
}
bool
EDConsumerBase::registeredToConsume(ProductHolderIndex iIndex, bool skipCurrentProcess, BranchType iBranch) const
{
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it) {
if(it->m_index.productHolderIndex() == iIndex and
it->m_index.skipCurrentProcess() == skipCurrentProcess and
it->m_branchType == iBranch) {
return true;
}
}
//TEMPORARY: Remember so we do not have to do this again
//non thread-safe
EDConsumerBase* nonConstThis = const_cast<EDConsumerBase*>(this);
nonConstThis->m_tokenInfo.emplace_back(TokenLookupInfo{TypeID{}, iIndex, skipCurrentProcess, iBranch},
true,
LabelPlacement{0,0,0},
PRODUCT_TYPE);
return false;
}
bool
EDConsumerBase::registeredToConsumeMany(TypeID const& iType, BranchType iBranch) const
{
for(auto it = m_tokenInfo.begin<kLookupInfo>(),
itEnd = m_tokenInfo.end<kLookupInfo>();
it != itEnd; ++it) {
//consumesMany entries do not have their index resolved
if(it->m_index.productHolderIndex() == ProductHolderIndexInvalid and
it->m_type == iType and
it->m_branchType == iBranch) {
return true;
}
}
//TEMPORARY: Remember so we do not have to do this again
//non thread-safe
EDConsumerBase* nonConstThis = const_cast<EDConsumerBase*>(this);
nonConstThis->m_tokenInfo.emplace_back(TokenLookupInfo{iType,ProductHolderIndexInvalid, false, iBranch},
true,
LabelPlacement{0,0,0},
PRODUCT_TYPE);
return false;
}
void
EDConsumerBase::throwTypeMismatch(edm::TypeID const& iType, EDGetToken iToken) const
{
throw cms::Exception("TypeMismatch")<<"A get using a EDGetToken used the C++ type '"<<iType.className()<<"' but the consumes call was for type '"<<m_tokenInfo.get<kLookupInfo>(iToken.index()).m_type.className()<<"'.\n Please modify either the consumes or get call so the types match.";
}
void
EDConsumerBase::throwBranchMismatch(BranchType iBranch, EDGetToken iToken) const {
throw cms::Exception("BranchTypeMismatch")<<"A get using a EDGetToken was done in "<<BranchTypeToString(iBranch)<<" but the consumes call was for "<<BranchTypeToString(m_tokenInfo.get<kLookupInfo>(iToken.index()).m_branchType)<<".\n Please modify the consumes call to use the correct branch type.";
}
void
EDConsumerBase::throwBadToken(edm::TypeID const& iType, EDGetToken iToken) const
{
if(iToken.isUninitialized()) {
throw cms::Exception("BadToken")<<"A get using a EDGetToken with the C++ type '"<<iType.className()<<"' was made using an uninitialized token.\n Please check that the variable is being initialized from a 'consumes' call.";
}
throw cms::Exception("BadToken")<<"A get using a EDGetToken with the C++ type '"<<iType.className()<<"' was made using a token with a value "<<iToken.index()<<" which is beyond the range used by this module.\n Please check that the variable is being initialized from a 'consumes' call from this module.\n You can not share EDGetToken values between modules.";
}
void
EDConsumerBase::throwConsumesCallAfterFrozen(TypeToGet const& typeToGet, InputTag const& inputTag) const {
throw cms::Exception("LogicError") << "A module declared it consumes a product after its constructor.\n"
<< "This must be done in the contructor\n"
<< "The product type was: " << typeToGet.type() << "\n"
<< "and " << inputTag << "\n";
}
namespace {
struct CharStarComp {
bool operator()(const char* iLHS, const char* iRHS) const {
return strcmp(iLHS,iRHS) < 0;
}
};
}
void
EDConsumerBase::modulesDependentUpon(const std::string& iProcessName,
std::vector<const char*>& oModuleLabels) const {
std::set<const char*, CharStarComp> uniqueModules;
for(unsigned int index=0, iEnd=m_tokenInfo.size();index <iEnd; ++index) {
auto const& info = m_tokenInfo.get<kLookupInfo>(index);
if( not info.m_index.skipCurrentProcess() ) {
auto labels = m_tokenInfo.get<kLabels>(index);
unsigned int start = labels.m_startOfModuleLabel;
const char* processName = &(m_tokenLabels[start+labels.m_deltaToProcessName]);
if( (not processName) or processName[0]==0 or
iProcessName == processName) {
uniqueModules.insert(&(m_tokenLabels[start]));
}
}
}
oModuleLabels = std::vector<const char*>(uniqueModules.begin(),uniqueModules.end());
}
namespace {
void
insertFoundModuleLabel(const char* consumedModuleLabel,
std::vector<ModuleDescription const*>& modules,
std::set<std::string>& alreadyFound,
std::map<std::string, ModuleDescription const*> const& labelsToDesc,
ProductRegistry const& preg) {
// Convert from label string to module description, eliminate duplicates,
// then insert into the vector of modules
auto it = labelsToDesc.find(consumedModuleLabel);
if(it != labelsToDesc.end()) {
if(alreadyFound.insert(consumedModuleLabel).second) {
modules.push_back(it->second);
}
return;
}
// Deal with EDAlias's by converting to the original module label first
std::vector<std::pair<std::string, std::string> > const& aliasToOriginal = preg.aliasToOriginal();
std::pair<std::string, std::string> target(consumedModuleLabel, std::string());
auto iter = std::lower_bound(aliasToOriginal.begin(), aliasToOriginal.end(), target);
if(iter != aliasToOriginal.end() && iter->first == consumedModuleLabel) {
std::string const& originalModuleLabel = iter->second;
auto iter2 = labelsToDesc.find(originalModuleLabel);
if(iter2 != labelsToDesc.end()) {
if(alreadyFound.insert(originalModuleLabel).second) {
modules.push_back(iter2->second);
}
return;
}
}
// Ignore the source products, we are only interested in module products.
// As far as I know, it should never be anything else so throw if something
// unknown gets passed in.
if(std::string(consumedModuleLabel) != "source") {
throw cms::Exception("EDConsumerBase", "insertFoundModuleLabel")
<< "Couldn't find ModuleDescription for the consumed module label: "
<< std::string(consumedModuleLabel) << "\n";
}
}
}
void
EDConsumerBase::modulesWhoseProductsAreConsumed(std::vector<ModuleDescription const*>& modules,
ProductRegistry const& preg,
std::map<std::string, ModuleDescription const*> const& labelsToDesc,
std::string const& processName) const {
ProductHolderIndexHelper const& iHelper = *preg.productLookup(InEvent);
std::set<std::string> alreadyFound;
auto itKind = m_tokenInfo.begin<kKind>();
auto itLabels = m_tokenInfo.begin<kLabels>();
for(auto itInfo = m_tokenInfo.begin<kLookupInfo>(),itEnd = m_tokenInfo.end<kLookupInfo>();
itInfo != itEnd; ++itInfo,++itKind,++itLabels) {
if(itInfo->m_branchType == InEvent) {
const unsigned int labelStart = itLabels->m_startOfModuleLabel;
const char* consumedModuleLabel = &(m_tokenLabels[labelStart]);
const char* consumedProcessName = consumedModuleLabel+itLabels->m_deltaToProcessName;
if(*consumedModuleLabel != '\0') { // not a consumesMany
if(*consumedProcessName != '\0') { // process name is specified in consumes call
if (processName == consumedProcessName &&
iHelper.index(*itKind,
itInfo->m_type,
consumedModuleLabel,
consumedModuleLabel+itLabels->m_deltaToProductInstance,
consumedModuleLabel+itLabels->m_deltaToProcessName) != ProductHolderIndexInvalid) {
insertFoundModuleLabel(consumedModuleLabel, modules, alreadyFound, labelsToDesc, preg);
}
} else { // process name was empty
auto matches = iHelper.relatedIndexes(*itKind,
itInfo->m_type,
consumedModuleLabel,
consumedModuleLabel+itLabels->m_deltaToProductInstance);
for(unsigned int j = 0; j < matches.numberOfMatches(); ++j) {
if(processName == matches.processName(j)) {
insertFoundModuleLabel(consumedModuleLabel, modules, alreadyFound, labelsToDesc, preg);
}
}
}
// consumesMany case
} else if(itInfo->m_index.productHolderIndex() == ProductHolderIndexInvalid) {
auto matches = iHelper.relatedIndexes(*itKind,
itInfo->m_type);
for(unsigned int j = 0; j < matches.numberOfMatches(); ++j) {
if(processName == matches.processName(j)) {
insertFoundModuleLabel(matches.moduleLabel(j), modules, alreadyFound, labelsToDesc, preg);
}
}
}
}
}
}
std::vector<ConsumesInfo>
EDConsumerBase::consumesInfo() const {
// Use this to eliminate duplicate entries related
// to consumesMany items where only the type was specified
// and the there are multiple matches. In these cases the
// label, instance, and process will be empty.
std::set<edm::TypeID> alreadySeenTypes;
std::vector<ConsumesInfo> result;
auto itAlways = m_tokenInfo.begin<kAlwaysGets>();
auto itKind = m_tokenInfo.begin<kKind>();
auto itLabels = m_tokenInfo.begin<kLabels>();
for(auto itInfo = m_tokenInfo.begin<kLookupInfo>(),itEnd = m_tokenInfo.end<kLookupInfo>();
itInfo != itEnd; ++itInfo,++itKind,++itLabels, ++itAlways) {
const unsigned int labelStart = itLabels->m_startOfModuleLabel;
const char* consumedModuleLabel = &(m_tokenLabels[labelStart]);
const char* consumedInstance = consumedModuleLabel+itLabels->m_deltaToProductInstance;
const char* consumedProcessName = consumedModuleLabel+itLabels->m_deltaToProcessName;
// consumesMany case
if(*consumedModuleLabel == '\0') {
if(!alreadySeenTypes.insert(itInfo->m_type).second) {
continue;
}
}
// Just copy the information into the ConsumesInfo data structure
result.emplace_back(itInfo->m_type,
consumedModuleLabel,
consumedInstance,
consumedProcessName,
itInfo->m_branchType,
*itKind,
*itAlways,
itInfo->m_index.skipCurrentProcess());
}
return result;
}