CMS 3D CMS Logo

CheckTransitions.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Services
4 // Class : CheckTransitions
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Thu Sep 8 14:17:58 EDT 2005
11 //
13 
16 
19 
24 
31 
32 #include <vector>
33 #include <string>
34 #include "tbb/concurrent_vector.h"
35 #include <iostream>
36 
37 namespace edm {
38 
39  namespace service {
41  public:
42 
43  enum class Phase {
44  kBeginRun,
45  kBeginLumi,
46  kEvent,
47  kEndLumi,
48  kEndRun
49  };
50 
51  enum class Transition {
52  IsInvalid,
53  IsStop,
54  IsFile,
55  IsRun,
56  IsLumi,
57  IsEvent
58  };
59 
61  ~CheckTransitions() noexcept(false);
62 
63  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
64 
66 
68  void postEndJob();
69 
70  void preOpenFile(std::string const&, bool);
71 
72  void preCloseFile(std::string const& lfn, bool primary);
73 
74  void preGlobalBeginRun(GlobalContext const&);
75 
76  void preGlobalEndRun(GlobalContext const&);
77 
78  void preStreamBeginRun(StreamContext const&);
79 
80  void preStreamEndRun(StreamContext const&);
81 
82  void preGlobalBeginLumi(GlobalContext const&);
83 
84  void preGlobalEndLumi(GlobalContext const&);
85 
86  void preStreamBeginLumi(StreamContext const&);
87 
88  void preStreamEndLumi(StreamContext const&);
89 
90  void preEvent(StreamContext const&);
91 
92  private:
93  tbb::concurrent_vector<std::tuple<Phase,edm::EventID,int>> m_seenTransitions;
94  std::vector<std::pair<Transition,edm::EventID>> m_expectedTransitions;
95  int m_nstreams=0;
96  bool m_failed = false;
97 
98  };
99  }
100 }
101 using namespace edm::service;
102 
103 namespace {
106 
107  Transition stringToType( const std::string& iTrans ) {
108  if (iTrans == "IsStop") { return Transition::IsStop;}
109  if (iTrans == "IsFile") { return Transition::IsFile;}
110  if (iTrans == "IsRun") { return Transition::IsRun;}
111  if (iTrans == "IsLumi") {return Transition::IsLumi;}
112  if (iTrans == "IsEvent") {return Transition::IsEvent;}
113 
114  throw edm::Exception(edm::errors::Configuration)<<"Unknown transition type \'"<<iTrans<<"\'";
115 
116  return Transition::IsInvalid;
117  }
118 
119 
120  std::vector<std::tuple<Phase,edm::EventID,int>> expectedValues(std::vector<std::pair<Transition,edm::EventID>> const& iTrans, int iNStreams ) {
121  std::vector<std::tuple<Phase,edm::EventID,int>> returnValue;
122  returnValue.reserve(iTrans.size());
123 
124  const edm::RunNumber_t maxIDValue = edm::EventID::maxRunNumber();
125  edm::EventID lastRun = {maxIDValue,0,0};
126  edm::EventID lastLumi = {maxIDValue,maxIDValue,0};
127  for(auto const& tran: iTrans) {
128  switch(tran.first) {
129  case Transition::IsFile:
130  {
131  break;
132  }
133  case Transition::IsRun:
134  {
135  if(tran.second != lastRun) {
136  if(lastRun.run() != maxIDValue) {
137  //end transitions
138  for(int i = 0; i<iNStreams;++i) {
139  returnValue.emplace_back(Phase::kEndRun,lastRun,i);
140  }
141  returnValue.emplace_back(Phase::kEndRun,lastRun,1000);
142  }
143  //begin transitions
144  returnValue.emplace_back(Phase::kBeginRun,tran.second,-1);
145  for(int i = 0; i<iNStreams;++i) {
146  returnValue.emplace_back(Phase::kBeginRun,tran.second,i);
147  }
148  lastRun = tran.second;
149  }
150  break;
151  }
152  case Transition::IsLumi:
153  {
154  if(tran.second != lastLumi) {
155  if(lastLumi.run() != maxIDValue) {
156  //end transitions
157  for(int i = 0; i<iNStreams;++i) {
158  returnValue.emplace_back(Phase::kEndLumi,lastLumi,i);
159  }
160  returnValue.emplace_back(Phase::kEndLumi,lastLumi,1000);
161  }
162  //begin transitions
163  returnValue.emplace_back(Phase::kBeginLumi,tran.second,-1);
164  for(int i = 0; i<iNStreams;++i) {
165  returnValue.emplace_back(Phase::kBeginLumi,tran.second,i);
166  }
167  lastLumi = tran.second;
168  }
169  break;
170 
171  }
172  case Transition::IsEvent:
173  {
174  returnValue.emplace_back(Phase::kEvent,tran.second,-2);
175  }
176  case Transition::IsStop:
178  {
179  break;
180  }
181  }
182  }
183  if(lastLumi.run() != maxIDValue) {
184  //end transitions
185  for(int i = 0; i<iNStreams;++i) {
186  returnValue.emplace_back(Phase::kEndLumi,lastLumi,i);
187  }
188  returnValue.emplace_back(Phase::kEndLumi,lastLumi,1000);
189  }
190  if(lastRun.run() != maxIDValue) {
191  //end transitions
192  for(int i = 0; i<iNStreams;++i) {
193  returnValue.emplace_back(Phase::kEndRun,lastRun,i);
194  }
195  returnValue.emplace_back(Phase::kEndRun,lastRun,1000);
196  }
197  return returnValue;
198  }
199 
200 }
201 
203 {
204  for( auto const& p: iPS.getUntrackedParameter<std::vector<edm::ParameterSet>>("transitions")) {
205  m_expectedTransitions.emplace_back( stringToType(p.getUntrackedParameter<std::string>("type")),
206  p.getUntrackedParameter<EventID>("id"));
207  }
208 
210 
212 
214 
216 
218 
220 
222 
224 
226 
228 
230 
232 
233  iRegistry.watchPreEvent(this, &CheckTransitions::preEvent);
234 }
235 
237  if (m_failed) {
238  throw edm::Exception(errors::EventProcessorFailure)<<"incorrect transtions";
239  }
240 }
241 
242 void
245  desc.setComment("Checks that the transitions specified occur during the job.");
246 
248  trans.addUntracked<std::string>("type");
249  trans.addUntracked<edm::EventID>("id");
250  desc.addVPSetUntracked("transitions",trans, {{}});
251  descriptions.add("CheckTransitions", desc);
252 }
253 
254 void
256  m_nstreams = bounds.maxNumberOfStreams();
257 }
258 
259 void
261 
262  auto expectedV = expectedValues(m_expectedTransitions,m_nstreams);
263 
264  std::vector<std::tuple<Phase,edm::EventID,int>> orderedSeen;
265  orderedSeen.reserve(m_seenTransitions.size());
266  for(auto const& i: m_seenTransitions) {
267  // std::cout <<i.first.m_run<<" "<<i.first.m_lumi<<" "<<i.first.m_event<<" "<<i.second<<std::endl;
268  auto s = std::get<2>(i);
269  if(std::get<1>(i).event() > 0) {
270  s=-2;
271  }
272  orderedSeen.emplace_back(std::get<0>(i),std::get<1>(i),s);
273  }
274  std::sort(orderedSeen.begin(),orderedSeen.end());
275 
276  auto orderedExpected = expectedV;
277  std::sort(orderedExpected.begin(),orderedExpected.end());
278  /* for(auto const& i: expectedV) {
279  std::cout <<i.first.m_run<<" "<<i.first.m_lumi<<" "<<i.first.m_event<<" "<<i.second<<std::endl;
280  } */
281 
282  auto itOS = orderedSeen.begin();
283  for(auto itOE = orderedExpected.begin(); itOE != orderedExpected.end(); ++itOE) {
284  if(itOS == orderedSeen.end()) {
285  break;
286  }
287  if ( *itOE != *itOS) {
288  auto syncOE = std::get<1>(*itOE);
289  auto syncOS = std::get<1>(*itOS);
290  std::cout <<"Different ordering "<<syncOE<<" "<<std::get<2>(*itOE)<<"\n"
291  <<" "<<syncOS<<" "<<std::get<2>(*itOS)<<"\n";
292  m_failed=true;
293  }
294  ++itOS;
295  }
296 
297  if(orderedSeen.size() != orderedExpected.size()) {
298  std::cout <<"Wrong number of transition "<<orderedSeen.size() <<" "<<orderedExpected.size()<<std::endl;
299  m_failed = true;
300  return;
301  }
302 
303 }
304 
305 void
307 }
308 
309 void
311 }
312 
313 void
315  auto id =gc.luminosityBlockID();
316  m_seenTransitions.emplace_back(Phase::kBeginRun,edm::EventID{id.run(),0,0},-1);
317 }
318 
319 void
321  auto id =gc.luminosityBlockID();
322  m_seenTransitions.emplace_back(Phase::kEndRun,edm::EventID{id.run(),0,0},1000);
323 }
324 
325 void
327  m_seenTransitions.emplace_back(Phase::kBeginRun,sc.eventID(),sc.streamID());
328 }
329 
330 void
332  m_seenTransitions.emplace_back(Phase::kEndRun,sc.eventID(),sc.streamID());
333 }
334 
335 void
337  auto id =gc.luminosityBlockID();
338  m_seenTransitions.emplace_back(Phase::kBeginLumi,edm::EventID{id.run(),id.luminosityBlock(),0},-1);
339 }
340 
341 void
343  auto id =gc.luminosityBlockID();
344  m_seenTransitions.emplace_back(Phase::kEndLumi,edm::EventID{id.run(),id.luminosityBlock(),0},1000);
345 }
346 
347 void
349  m_seenTransitions.emplace_back(Phase::kBeginLumi,sc.eventID(),sc.streamID());
350 }
351 
352 void
354  m_seenTransitions.emplace_back(Phase::kEndLumi,sc.eventID(),sc.streamID());
355 }
356 
357 void
359  m_seenTransitions.emplace_back(Phase::kEvent,sc.eventID(),sc.streamID());
360 }
361 
364 
365 
RunNumber_t run() const
Definition: EventID.h:39
T getUntrackedParameter(std::string const &, T const &) const
void watchPreEvent(PreEvent::slot_type const &iSlot)
CheckTransitions(const ParameterSet &, ActivityRegistry &)
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
void watchPreallocate(Preallocate::slot_type const &iSlot)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
static RunNumber_t maxRunNumber()
Definition: EventID.h:103
void watchPreGlobalEndLumi(PreGlobalEndLumi::slot_type const &iSlot)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:57
void watchPreStreamEndRun(PreStreamEndRun::slot_type const &iSlot)
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
#define noexcept
void preStreamEndLumi(StreamContext const &)
void watchPreStreamEndLumi(PreStreamEndLumi::slot_type const &iSlot)
std::vector< std::pair< Transition, edm::EventID > > m_expectedTransitions
void preEvent(StreamContext const &)
void preStreamEndRun(StreamContext const &)
void preStreamBeginLumi(StreamContext const &)
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:43
void watchPreOpenFile(PreOpenFile::slot_type const &iSlot)
void setComment(std::string const &value)
void watchPreGlobalEndRun(PreGlobalEndRun::slot_type const &iSlot)
void preGlobalEndRun(GlobalContext const &)
void preGlobalBeginRun(GlobalContext const &)
StreamID const & streamID() const
Definition: StreamContext.h:57
void watchPreGlobalBeginRun(PreGlobalBeginRun::slot_type const &iSlot)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void watchPreCloseFile(PreCloseFile::slot_type const &iSlot)
double b
Definition: hdecay.h:120
void watchPreStreamBeginLumi(PreStreamBeginLumi::slot_type const &iSlot)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void preGlobalBeginLumi(GlobalContext const &)
void preOpenFile(std::string const &, bool)
HLT enums.
void watchPreStreamBeginRun(PreStreamBeginRun::slot_type const &iSlot)
void preStreamBeginRun(StreamContext const &)
void preGlobalEndLumi(GlobalContext const &)
tbb::concurrent_vector< std::tuple< Phase, edm::EventID, int > > m_seenTransitions
void preallocate(service::SystemBounds const &)
unsigned int RunNumber_t
EventID const & eventID() const
Definition: StreamContext.h:63
ParameterDescriptionBase * addVPSetUntracked(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
void preCloseFile(std::string const &lfn, bool primary)