CheckTransitions

Phase

Transition

Line Code
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
// -*- C++ -*-
//
// Package:     Services
// Class  :     CheckTransitions
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Thu Sep  8 14:17:58 EDT 2005
//
#include "FWCore/ServiceRegistry/interface/ServiceMaker.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"

#include "FWCore/ServiceRegistry/interface/SystemBounds.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "DataFormats/Provenance/interface/EventID.h"
#include "DataFormats/Provenance/interface/LuminosityBlockID.h"
#include "DataFormats/Provenance/interface/RunID.h"
#include "DataFormats/Provenance/interface/Timestamp.h"

#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/GlobalContext.h"
#include "FWCore/ServiceRegistry/interface/PathContext.h"
#include "FWCore/ServiceRegistry/interface/ProcessContext.h"
#include "FWCore/ServiceRegistry/interface/StreamContext.h"

#include <vector>
#include <string>
#include "oneapi/tbb/concurrent_vector.h"
#include <iostream>

namespace edm {

  namespace service {
    class CheckTransitions {
    public:
      enum class Phase { kBeginRun, kBeginLumi, kEvent, kEndLumi, kEndRun };

      enum class Transition { IsInvalid, IsStop, IsFile, IsRun, IsLumi, IsEvent };

      CheckTransitions(const ParameterSet&, ActivityRegistry&);
      ~CheckTransitions() noexcept(false);

      static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

      void preallocate(service::SystemBounds const&);

      void postEndJob();

      void preOpenFile(std::string const&);

      void preCloseFile(std::string const& lfn);

      void preGlobalBeginRun(GlobalContext const&);

      void preGlobalEndRun(GlobalContext const&);

      void preStreamBeginRun(StreamContext const&);

      void preStreamEndRun(StreamContext const&);

      void preGlobalBeginLumi(GlobalContext const&);

      void preGlobalEndLumi(GlobalContext const&);

      void preStreamBeginLumi(StreamContext const&);

      void preStreamEndLumi(StreamContext const&);

      void preEvent(StreamContext const&);

    private:
      oneapi::tbb::concurrent_vector<std::tuple<Phase, edm::EventID, int>> m_seenTransitions;
      std::vector<std::pair<Transition, edm::EventID>> m_expectedTransitions;
      int m_nstreams = 0;
      bool m_failed = false;
    };
  }  // namespace service
}  // namespace edm
using namespace edm::service;

namespace {
  using Phase = CheckTransitions::Phase;
  using Transition = CheckTransitions::Transition;

  Transition stringToType(const std::string& iTrans) {
    if (iTrans == "IsStop") {
      return Transition::IsStop;
    }
    if (iTrans == "IsFile") {
      return Transition::IsFile;
    }
    if (iTrans == "IsRun") {
      return Transition::IsRun;
    }
    if (iTrans == "IsLumi") {
      return Transition::IsLumi;
    }
    if (iTrans == "IsEvent") {
      return Transition::IsEvent;
    }

    throw edm::Exception(edm::errors::Configuration) << "Unknown transition type \'" << iTrans << "\'";

    return Transition::IsInvalid;
  }

  std::vector<std::tuple<Phase, edm::EventID, int>> expectedValues(
      std::vector<std::pair<Transition, edm::EventID>> const& iTrans, int iNStreams) {
    std::vector<std::tuple<Phase, edm::EventID, int>> returnValue;
    returnValue.reserve(iTrans.size());

    const edm::RunNumber_t maxIDValue = edm::EventID::maxRunNumber();
    edm::EventID lastRun = {maxIDValue, 0, 0};
    edm::EventID lastLumi = {maxIDValue, maxIDValue, 0};
    for (auto const& tran : iTrans) {
      switch (tran.first) {
        case Transition::IsFile: {
          break;
        }
        case Transition::IsRun: {
          if (tran.second != lastRun) {
            if (lastRun.run() != maxIDValue) {
              //end transitions
              for (int i = 0; i < iNStreams; ++i) {
                returnValue.emplace_back(Phase::kEndRun, lastRun, i);
              }
              returnValue.emplace_back(Phase::kEndRun, lastRun, 1000);
            }
            //begin transitions
            returnValue.emplace_back(Phase::kBeginRun, tran.second, -1);
            for (int i = 0; i < iNStreams; ++i) {
              returnValue.emplace_back(Phase::kBeginRun, tran.second, i);
            }
            lastRun = tran.second;
          }
          break;
        }
        case Transition::IsLumi: {
          if (tran.second != lastLumi) {
            if (lastLumi.run() != maxIDValue) {
              //end transitions
              for (int i = 0; i < iNStreams; ++i) {
                returnValue.emplace_back(Phase::kEndLumi, lastLumi, i);
              }
              returnValue.emplace_back(Phase::kEndLumi, lastLumi, 1000);
            }
            //begin transitions
            returnValue.emplace_back(Phase::kBeginLumi, tran.second, -1);
            for (int i = 0; i < iNStreams; ++i) {
              returnValue.emplace_back(Phase::kBeginLumi, tran.second, i);
            }
            lastLumi = tran.second;
          }
          break;
        }
        case Transition::IsEvent: {
          returnValue.emplace_back(Phase::kEvent, tran.second, -2);
        }
        case Transition::IsStop:
        case Transition::IsInvalid: {
          break;
        }
      }
    }
    if (lastLumi.run() != maxIDValue) {
      //end transitions
      for (int i = 0; i < iNStreams; ++i) {
        returnValue.emplace_back(Phase::kEndLumi, lastLumi, i);
      }
      returnValue.emplace_back(Phase::kEndLumi, lastLumi, 1000);
    }
    if (lastRun.run() != maxIDValue) {
      //end transitions
      for (int i = 0; i < iNStreams; ++i) {
        returnValue.emplace_back(Phase::kEndRun, lastRun, i);
      }
      returnValue.emplace_back(Phase::kEndRun, lastRun, 1000);
    }
    return returnValue;
  }

}  // namespace

CheckTransitions::CheckTransitions(ParameterSet const& iPS, ActivityRegistry& iRegistry) {
  for (auto const& p : iPS.getUntrackedParameter<std::vector<edm::ParameterSet>>("transitions")) {
    m_expectedTransitions.emplace_back(stringToType(p.getUntrackedParameter<std::string>("type")),
                                       p.getUntrackedParameter<EventID>("id"));
  }

  iRegistry.watchPreallocate(this, &CheckTransitions::preallocate);

  iRegistry.watchPostEndJob(this, &CheckTransitions::postEndJob);

  iRegistry.watchPreOpenFile(this, &CheckTransitions::preOpenFile);

  iRegistry.watchPreCloseFile(this, &CheckTransitions::preCloseFile);

  iRegistry.watchPreGlobalBeginRun(this, &CheckTransitions::preGlobalBeginRun);

  iRegistry.watchPreGlobalEndRun(this, &CheckTransitions::preGlobalEndRun);

  iRegistry.watchPreStreamBeginRun(this, &CheckTransitions::preStreamBeginRun);

  iRegistry.watchPreStreamEndRun(this, &CheckTransitions::preStreamEndRun);

  iRegistry.watchPreGlobalBeginLumi(this, &CheckTransitions::preGlobalBeginLumi);

  iRegistry.watchPreGlobalEndLumi(this, &CheckTransitions::preGlobalEndLumi);

  iRegistry.watchPreStreamBeginLumi(this, &CheckTransitions::preStreamBeginLumi);

  iRegistry.watchPreStreamEndLumi(this, &CheckTransitions::preStreamEndLumi);

  iRegistry.watchPreEvent(this, &CheckTransitions::preEvent);
}

CheckTransitions::~CheckTransitions() noexcept(false) {
  if (m_failed) {
    throw edm::Exception(errors::EventProcessorFailure) << "incorrect transtions";
  }
}

void CheckTransitions::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  ParameterSetDescription desc;
  desc.setComment("Checks that the transitions specified occur during the job.");

  ParameterSetDescription trans;
  trans.addUntracked<std::string>("type");
  trans.addUntracked<edm::EventID>("id");
  desc.addVPSetUntracked("transitions", trans, {{}});
  descriptions.add("CheckTransitions", desc);
}

void CheckTransitions::preallocate(service::SystemBounds const& bounds) { m_nstreams = bounds.maxNumberOfStreams(); }

void CheckTransitions::postEndJob() {
  auto expectedV = expectedValues(m_expectedTransitions, m_nstreams);

  std::vector<std::tuple<Phase, edm::EventID, int>> orderedSeen;
  orderedSeen.reserve(m_seenTransitions.size());
  for (auto const& i : m_seenTransitions) {
    //      std::cout <<i.first.m_run<<" "<<i.first.m_lumi<<" "<<i.first.m_event<<" "<<i.second<<std::endl;
    auto s = std::get<2>(i);
    if (std::get<1>(i).event() > 0) {
      s = -2;
    }
    orderedSeen.emplace_back(std::get<0>(i), std::get<1>(i), s);
  }
  std::sort(orderedSeen.begin(), orderedSeen.end());

  auto orderedExpected = expectedV;
  std::sort(orderedExpected.begin(), orderedExpected.end());
  /*   for(auto const& i: expectedV) {
   std::cout <<i.first.m_run<<" "<<i.first.m_lumi<<" "<<i.first.m_event<<" "<<i.second<<std::endl;
   } */

  unsigned int nSkippedStreamLumiTransitions = 0;

  // Use the next two vectors to test that skipped stream lumi transitions
  // come in matched begin and end pairs.
  std::vector<std::tuple<Phase, edm::EventID, int>> expectedSkippedStreamEndLumi;
  std::vector<std::tuple<Phase, edm::EventID, int>> seenSkippedStreamEndLumi;

  // Use the next two sets to test that for every global begin lumi there
  // is at least one stream begin lumi, even when some stream begin lumi
  // transitions are skipped.
  std::set<std::tuple<Phase, edm::EventID, int>> seenGlobalBeginLumi;
  std::set<std::tuple<Phase, edm::EventID, int>> seenStreamBeginLumi;

  auto itOS = orderedSeen.begin();
  for (auto itOE = orderedExpected.begin(); itOE != orderedExpected.end(); ++itOE) {
    if (itOS == orderedSeen.end()) {
      break;
    }

    if (std::get<0>(*itOS) == Phase::kBeginLumi && std::get<2>(*itOS) == -1) {
      seenGlobalBeginLumi.emplace(*itOS);
    }
    if (std::get<0>(*itOS) == Phase::kBeginLumi && (std::get<2>(*itOS) > -1 && std::get<2>(*itOS) < 1000)) {
      // Note that the third field is falsely filled with the value for a global begin lumi to
      // make a comparison with seenGlobalBeginLumi easier.
      seenStreamBeginLumi.emplace(std::get<0>(*itOS), std::get<1>(*itOS), -1);
    }

    while (*itOE != *itOS && (std::get<0>(*itOE) == Phase::kBeginLumi || std::get<0>(*itOE) == Phase::kEndLumi) &&
           (std::get<2>(*itOE) > -1 && std::get<2>(*itOE) < 1000)) {
      ++nSkippedStreamLumiTransitions;
      if (std::get<0>(*itOE) == Phase::kBeginLumi) {
        expectedSkippedStreamEndLumi.emplace_back(Phase::kEndLumi, std::get<1>(*itOE), std::get<2>(*itOE));
      } else {
        seenSkippedStreamEndLumi.emplace_back(*itOE);
      }

      ++itOE;
    }
    if (*itOE != *itOS) {
      auto syncOE = std::get<1>(*itOE);
      auto syncOS = std::get<1>(*itOS);
      std::cout << "Different ordering " << syncOE << " " << std::get<2>(*itOE) << "\n"
                << "                   " << syncOS << " " << std::get<2>(*itOS) << "\n";
      m_failed = true;
    }
    ++itOS;
  }

  if (seenGlobalBeginLumi != seenStreamBeginLumi) {
    std::cout << "We didn't see at least one stream begin lumi for every global begin lumi" << std::endl;
    m_failed = true;
  }

  if (expectedSkippedStreamEndLumi != seenSkippedStreamEndLumi) {
    std::cout << "Skipped stream begin and end lumi transitions do not match" << std::endl;
    m_failed = true;
  }

  if (orderedSeen.size() + nSkippedStreamLumiTransitions != orderedExpected.size()) {
    std::cout << "Wrong number of transition " << orderedSeen.size() << " " << orderedExpected.size() << std::endl;
    m_failed = true;
    return;
  }
}

void CheckTransitions::preOpenFile(std::string const& lfn) {}

void CheckTransitions::preCloseFile(std::string const& lfn) {}

void CheckTransitions::preGlobalBeginRun(GlobalContext const& gc) {
  auto id = gc.luminosityBlockID();
  m_seenTransitions.emplace_back(Phase::kBeginRun, edm::EventID{id.run(), 0, 0}, -1);
}

void CheckTransitions::preGlobalEndRun(GlobalContext const& gc) {
  auto id = gc.luminosityBlockID();
  m_seenTransitions.emplace_back(Phase::kEndRun, edm::EventID{id.run(), 0, 0}, 1000);
}

void CheckTransitions::preStreamBeginRun(StreamContext const& sc) {
  m_seenTransitions.emplace_back(Phase::kBeginRun, sc.eventID(), sc.streamID());
}

void CheckTransitions::preStreamEndRun(StreamContext const& sc) {
  m_seenTransitions.emplace_back(Phase::kEndRun, sc.eventID(), sc.streamID());
}

void CheckTransitions::preGlobalBeginLumi(GlobalContext const& gc) {
  auto id = gc.luminosityBlockID();
  m_seenTransitions.emplace_back(Phase::kBeginLumi, edm::EventID{id.run(), id.luminosityBlock(), 0}, -1);
}

void CheckTransitions::preGlobalEndLumi(GlobalContext const& gc) {
  auto id = gc.luminosityBlockID();
  m_seenTransitions.emplace_back(Phase::kEndLumi, edm::EventID{id.run(), id.luminosityBlock(), 0}, 1000);
}

void CheckTransitions::preStreamBeginLumi(StreamContext const& sc) {
  m_seenTransitions.emplace_back(Phase::kBeginLumi, sc.eventID(), sc.streamID());
}

void CheckTransitions::preStreamEndLumi(StreamContext const& sc) {
  m_seenTransitions.emplace_back(Phase::kEndLumi, sc.eventID(), sc.streamID());
}

void CheckTransitions::preEvent(StreamContext const& sc) {
  m_seenTransitions.emplace_back(Phase::kEvent, sc.eventID(), sc.streamID());
}

using edm::service::CheckTransitions;
DEFINE_FWK_SERVICE(CheckTransitions);