Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:26

0001 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/LuminosityBlock.h"
0004 #include "FWCore/Framework/interface/Run.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 
0011 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0012 #include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
0013 #include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"
0014 #include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h"
0015 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
0016 
0017 #include <sstream>
0018 #include <iostream>
0019 
0020 namespace cgra {
0021   struct DummyCache {};
0022 };  // namespace cgra
0023 
0024 class CompareGeneratorResultsAnalyzer
0025     : public edm::global::EDAnalyzer<edm::RunCache<cgra::DummyCache>, edm::LuminosityBlockCache<cgra::DummyCache>> {
0026 public:
0027   CompareGeneratorResultsAnalyzer(edm::ParameterSet const&);
0028 
0029   std::shared_ptr<cgra::DummyCache> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
0030   void globalEndRun(edm::Run const&, edm::EventSetup const&) const override;
0031 
0032   std::shared_ptr<cgra::DummyCache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0033                                                                edm::EventSetup const&) const override;
0034   void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const override;
0035 
0036   void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0037 
0038 private:
0039   std::string mod1_;
0040   std::string mod2_;
0041 
0042   edm::EDGetTokenT<GenEventInfoProduct> evToken1_;
0043   edm::EDGetTokenT<GenEventInfoProduct> evToken2_;
0044 
0045   edm::EDGetTokenT<edm::HepMCProduct> hepMCToken1_;
0046   edm::EDGetTokenT<edm::HepMCProduct> hepMCToken2_;
0047 
0048   edm::EDGetTokenT<GenLumiInfoHeader> lumiHeaderToken1_;
0049   edm::EDGetTokenT<GenLumiInfoHeader> lumiHeaderToken2_;
0050 
0051   edm::EDGetTokenT<GenLumiInfoProduct> lumiProductToken1_;
0052   edm::EDGetTokenT<GenLumiInfoProduct> lumiProductToken2_;
0053 
0054   edm::EDGetTokenT<GenRunInfoProduct> runProductToken1_;
0055   edm::EDGetTokenT<GenRunInfoProduct> runProductToken2_;
0056 
0057   bool allowXSecDifferences_;
0058 };
0059 
0060 CompareGeneratorResultsAnalyzer::CompareGeneratorResultsAnalyzer(edm::ParameterSet const& iPSet)
0061     : mod1_{iPSet.getUntrackedParameter<std::string>("module1")},
0062       mod2_{iPSet.getUntrackedParameter<std::string>("module2")},
0063       evToken1_{consumes<GenEventInfoProduct>(mod1_)},
0064       evToken2_{consumes<GenEventInfoProduct>(mod2_)},
0065       hepMCToken1_{consumes<edm::HepMCProduct>(edm::InputTag(mod1_, "unsmeared"))},
0066       hepMCToken2_{consumes<edm::HepMCProduct>(edm::InputTag(mod2_, "unsmeared"))},
0067       lumiHeaderToken1_{consumes<GenLumiInfoHeader, edm::InLumi>(mod1_)},
0068       lumiHeaderToken2_{consumes<GenLumiInfoHeader, edm::InLumi>(mod2_)},
0069       lumiProductToken1_{consumes<GenLumiInfoProduct, edm::InLumi>(mod1_)},
0070       lumiProductToken2_{consumes<GenLumiInfoProduct, edm::InLumi>(mod2_)},
0071       runProductToken1_{consumes<GenRunInfoProduct, edm::InRun>(mod1_)},
0072       runProductToken2_{consumes<GenRunInfoProduct, edm::InRun>(mod2_)},
0073       allowXSecDifferences_{iPSet.getUntrackedParameter<bool>("allowXSecDifferences", false)} {}
0074 
0075 std::shared_ptr<cgra::DummyCache> CompareGeneratorResultsAnalyzer::globalBeginRun(edm::Run const&,
0076                                                                                   edm::EventSetup const&) const {
0077   return std::shared_ptr<cgra::DummyCache>();
0078 }
0079 
0080 void CompareGeneratorResultsAnalyzer::globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const {
0081   auto const& prod1 = iRun.get(runProductToken1_);
0082   auto const& prod2 = iRun.get(runProductToken2_);
0083 
0084   if (not prod1.isProductEqual(prod2)) {
0085     throw cms::Exception("ComparisonFailure") << "The GenRunInfoProducts are different";
0086   }
0087 }
0088 
0089 std::shared_ptr<cgra::DummyCache> CompareGeneratorResultsAnalyzer::globalBeginLuminosityBlock(
0090     edm::LuminosityBlock const& iLumi, edm::EventSetup const&) const {
0091   auto const& prod1 = iLumi.get(lumiHeaderToken1_);
0092   auto const& prod2 = iLumi.get(lumiHeaderToken2_);
0093 
0094   if (prod1.randomConfigIndex() != prod2.randomConfigIndex()) {
0095     throw cms::Exception("ComparisonFailure") << "The GenLumiInfoHeaders have different randomConfigIndex "
0096                                               << prod1.randomConfigIndex() << " " << prod2.randomConfigIndex();
0097   }
0098 
0099   if (prod1.configDescription() != prod2.configDescription()) {
0100     throw cms::Exception("ComparisonFailure") << "The GenLumiInfoHeaders have different configDescription "
0101                                               << prod1.configDescription() << " " << prod2.configDescription();
0102   }
0103 
0104   if (prod1.lheHeaders().size() != prod2.lheHeaders().size()) {
0105     throw cms::Exception("ComparisonFailure") << "The GenLumiInfoHeaders have different lheHeaders "
0106                                               << prod1.lheHeaders().size() << " " << prod2.lheHeaders().size();
0107   }
0108 
0109   if (prod1.weightNames().size() != prod2.weightNames().size()) {
0110     throw cms::Exception("ComparisonFailure") << "The GenLumiInfoHeaders have different weightNames "
0111                                               << prod1.weightNames().size() << " " << prod2.weightNames().size();
0112   }
0113 
0114   return std::shared_ptr<cgra::DummyCache>();
0115 }
0116 
0117 namespace {
0118   void compare(size_t iIndex,
0119                GenLumiInfoProduct::ProcessInfo const& p1,
0120                GenLumiInfoProduct::ProcessInfo const& p2,
0121                bool allowXSecDifferences) {
0122     if (p1.process() != p2.process()) {
0123       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0124                                                 << "] process " << p1.process() << " " << p2.process();
0125     }
0126 
0127     if (p1.nPassPos() != p2.nPassPos()) {
0128       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0129                                                 << "] nPassPos " << p1.nPassPos() << " " << p2.nPassPos();
0130     }
0131 
0132     if (p1.nPassNeg() != p2.nPassNeg()) {
0133       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0134                                                 << "] nPassNeg " << p1.nPassNeg() << " " << p2.nPassNeg();
0135     }
0136 
0137     if (p1.nTotalPos() != p2.nTotalPos()) {
0138       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0139                                                 << "] nTotalPos " << p1.nTotalPos() << " " << p2.nTotalPos();
0140     }
0141 
0142     if (p1.nTotalNeg() != p2.nTotalNeg()) {
0143       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0144                                                 << "] nTotalNeg " << p1.nTotalNeg() << " " << p2.nTotalNeg();
0145     }
0146 
0147     if (p1.lheXSec().error() != p2.lheXSec().error()) {
0148       if (allowXSecDifferences) {
0149         edm::LogWarning("ComparisonFailure")
0150             << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] lheXSec.error "
0151             << p1.lheXSec().error() << " " << p2.lheXSec().error();
0152       } else {
0153         throw cms::Exception("ComparisonFailure")
0154             << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] lheXSec.error "
0155             << p1.lheXSec().error() << " " << p2.lheXSec().error();
0156       }
0157     }
0158 
0159     if (p1.lheXSec().value() != p2.lheXSec().value()) {
0160       if (allowXSecDifferences) {
0161         //throw cms::Exception("ComparisonFailure")
0162         edm::LogWarning("ComparisonFailure")
0163             << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] lheXSec.value "
0164             << p1.lheXSec().value() << " " << p2.lheXSec().value();
0165       } else {
0166         throw cms::Exception("ComparisonFailure")
0167             << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] lheXSec.value "
0168             << p1.lheXSec().value() << " " << p2.lheXSec().value();
0169       }
0170     }
0171 
0172     if (p1.tried().n() != p2.tried().n()) {
0173       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0174                                                 << "] tried.n " << p1.tried().n() << " " << p2.tried().n();
0175     }
0176 
0177     if (p1.tried().sum() != p2.tried().sum()) {
0178       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0179                                                 << "] tried.sum " << p1.tried().sum() << " " << p2.tried().sum();
0180     }
0181 
0182     if (p1.tried().sum2() != p2.tried().sum2()) {
0183       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0184                                                 << "] tried.sum2 " << p1.tried().sum2() << " " << p2.tried().sum2();
0185     }
0186 
0187     if (p1.selected().n() != p2.selected().n()) {
0188       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0189                                                 << "] selected.n " << p1.selected().n() << " " << p2.selected().n();
0190     }
0191 
0192     if (p1.selected().sum() != p2.selected().sum()) {
0193       throw cms::Exception("ComparisonFailure")
0194           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] selected.sum "
0195           << p1.selected().sum() << " " << p2.selected().sum();
0196     }
0197 
0198     if (p1.selected().sum2() != p2.selected().sum2()) {
0199       throw cms::Exception("ComparisonFailure")
0200           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] selected.sum2 "
0201           << p1.selected().sum2() << " " << p2.selected().sum2();
0202     }
0203 
0204     if (p1.killed().n() != p2.killed().n()) {
0205       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0206                                                 << "] killed.n " << p1.killed().n() << " " << p2.killed().n();
0207     }
0208 
0209     if (p1.killed().sum() != p2.killed().sum()) {
0210       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0211                                                 << "] killed sum " << p1.killed().sum() << " " << p2.killed().sum();
0212     }
0213 
0214     if (p1.killed().sum2() != p2.killed().sum2()) {
0215       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0216                                                 << "] killed.sum2 " << p1.killed().sum2() << " " << p2.killed().sum2();
0217     }
0218 
0219     if (p1.accepted().n() != p2.accepted().n()) {
0220       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex
0221                                                 << "] accepted.n " << p1.accepted().n() << " " << p2.accepted().n();
0222     }
0223 
0224     if (p1.accepted().sum() != p2.accepted().sum()) {
0225       throw cms::Exception("ComparisonFailure")
0226           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] accepted.sum "
0227           << p1.accepted().sum() << " " << p2.accepted().sum();
0228     }
0229 
0230     if (p1.accepted().sum2() != p2.accepted().sum2()) {
0231       throw cms::Exception("ComparisonFailure")
0232           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] accepted.sum2 "
0233           << p1.accepted().sum2() << " " << p2.accepted().sum2();
0234     }
0235 
0236     if (p1.acceptedBr().n() != p2.acceptedBr().n()) {
0237       throw cms::Exception("ComparisonFailure")
0238           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] acceptedBr.n "
0239           << p1.acceptedBr().n() << " " << p2.acceptedBr().n();
0240     }
0241 
0242     if (p1.acceptedBr().sum() != p2.acceptedBr().sum()) {
0243       throw cms::Exception("ComparisonFailure")
0244           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] acceptedZBr.sum "
0245           << p1.acceptedBr().sum() << " " << p2.acceptedBr().sum();
0246     }
0247 
0248     if (p1.acceptedBr().sum2() != p2.acceptedBr().sum2()) {
0249       throw cms::Exception("ComparisonFailure")
0250           << "The GenLumiInfoProducts have different getProcessInfos()[" << iIndex << "] acceptedBr.sum2 "
0251           << p1.acceptedBr().sum2() << " " << p2.acceptedBr().sum2();
0252     }
0253   }
0254 }  // namespace
0255 
0256 void CompareGeneratorResultsAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi,
0257                                                                edm::EventSetup const&) const {
0258   auto const& prod1 = iLumi.get(lumiProductToken1_);
0259   auto const& prod2 = iLumi.get(lumiProductToken2_);
0260 
0261   if (not prod1.isProductEqual(prod2)) {
0262     if (prod1.getHEPIDWTUP() != prod1.getHEPIDWTUP()) {
0263       throw cms::Exception("ComparisonFailure") << "The GenLumiInfoProducts have different getHEPIDWTUP "
0264                                                 << prod1.getHEPIDWTUP() << " " << prod2.getHEPIDWTUP();
0265     }
0266 
0267     if (prod1.getProcessInfos().size() != prod2.getProcessInfos().size()) {
0268       throw cms::Exception("ComparisonFailure")
0269           << "The GenLumiInfoHeaders have different getProcessInfos " << prod1.getProcessInfos().size() << " "
0270           << prod2.getProcessInfos().size();
0271     }
0272 
0273     for (size_t i = 0; i < prod1.getProcessInfos().size(); ++i) {
0274       compare(i, prod1.getProcessInfos()[i], prod2.getProcessInfos()[i], allowXSecDifferences_);
0275     }
0276 
0277     if (not allowXSecDifferences_) {
0278       throw cms::Exception("ComparisionFailure") << "The GenLumiInfoProducts are different";
0279     }
0280   }
0281 }
0282 
0283 namespace {
0284   void compare(GenEventInfoProduct const& prod1, GenEventInfoProduct const& prod2) {
0285     if (prod1.weights().size() != prod2.weights().size()) {
0286       throw cms::Exception("ComparisonFailure") << "The GenEventInfoProducts have different weights "
0287                                                 << prod1.weights().size() << " " << prod2.weights().size();
0288     }
0289 
0290     if (prod1.binningValues().size() != prod2.binningValues().size()) {
0291       throw cms::Exception("ComparisonFailure") << "The GenEventInfoProducts have different binningValues "
0292                                                 << prod1.binningValues().size() << " " << prod2.binningValues().size();
0293     }
0294 
0295     if (prod1.DJRValues().size() != prod2.DJRValues().size()) {
0296       throw cms::Exception("ComparisonFailure") << "The GenEventInfoProducts have different DJRValues "
0297                                                 << prod1.DJRValues().size() << " " << prod2.DJRValues().size();
0298     }
0299 
0300     if (prod1.signalProcessID() != prod2.signalProcessID()) {
0301       throw cms::Exception("ComparisonFailure") << "The GenEventInfoProducts have different signalProcessID "
0302                                                 << prod1.signalProcessID() << " " << prod2.signalProcessID();
0303     }
0304 
0305     if (prod1.qScale() != prod2.qScale()) {
0306       throw cms::Exception("ComparisonFailure")
0307           << "The GenEventInfoProducts have different qScale " << prod1.qScale() << " " << prod2.qScale();
0308     }
0309 
0310     if (prod1.alphaQCD() != prod2.alphaQCD()) {
0311       throw cms::Exception("ComparisonFailure")
0312           << "The GenEventInfoProducts have different alphaQCD " << prod1.alphaQCD() << " " << prod2.alphaQCD();
0313     }
0314 
0315     if (prod1.alphaQED() != prod2.alphaQED()) {
0316       throw cms::Exception("ComparisonFailure")
0317           << "The GenEventInfoProducts have different alphaQED " << prod1.alphaQED() << " " << prod2.alphaQED();
0318     }
0319 
0320     if (prod1.nMEPartons() != prod2.nMEPartons()) {
0321       throw cms::Exception("ComparisonFailure")
0322           << "The GenEventInfoProducts have different nMEPartons " << prod1.nMEPartons() << " " << prod2.nMEPartons();
0323     }
0324 
0325     if (prod1.nMEPartonsFiltered() != prod2.nMEPartonsFiltered()) {
0326       throw cms::Exception("ComparisonFailure") << "The GenEventInfoProducts have different nMEPartonsFiltered "
0327                                                 << prod1.nMEPartonsFiltered() << " " << prod2.nMEPartonsFiltered();
0328     }
0329   }
0330 
0331   void compare(HepMC::GenEvent const& prod1, HepMC::GenEvent const& prod2) {
0332     if (prod1.signal_process_id() != prod2.signal_process_id()) {
0333       throw cms::Exception("ComparisonFailure") << "The HepMCProducts have different signal_process_id "
0334                                                 << prod1.signal_process_id() << " " << prod2.signal_process_id();
0335     }
0336 
0337     if (prod1.vertices_size() != prod2.vertices_size()) {
0338       throw cms::Exception("ComparisonFailure") << "The HepMCProducts have different vertices_size() "
0339                                                 << prod1.vertices_size() << " " << prod2.vertices_size();
0340     }
0341 
0342     if (prod1.particles_size() != prod2.particles_size()) {
0343       throw cms::Exception("ComparisonFailure") << "The HepMCProducts have different particles_size() "
0344                                                 << prod1.particles_size() << " " << prod2.particles_size();
0345     }
0346   }
0347 }  // namespace
0348 
0349 void CompareGeneratorResultsAnalyzer::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0350   auto const& prod1 = iEvent.get(evToken1_);
0351   auto const& prod2 = iEvent.get(evToken2_);
0352 
0353   compare(prod1, prod2);
0354 
0355   auto const& hepmc1 = iEvent.get(hepMCToken1_);
0356   auto const& hepmc2 = iEvent.get(hepMCToken2_);
0357 
0358   compare(hepmc1.getHepMCData(), hepmc2.getHepMCData());
0359 }
0360 
0361 DEFINE_FWK_MODULE(CompareGeneratorResultsAnalyzer);