Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-11 23:32:56

0001 // system include files
0002 #include "Validation/Mixing/interface/MixCollectionValidation.h"
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 
0012 #include "DQMServices/Core/interface/DQMStore.h"
0013 #include "TFile.h"
0014 
0015 #include <memory>
0016 #include <utility>
0017 
0018 using namespace edm;
0019 
0020 MixCollectionValidation::MixCollectionValidation(const edm::ParameterSet &iConfig)
0021     : minbunch_(iConfig.getParameter<int>("minBunch")),
0022       maxbunch_(iConfig.getParameter<int>("maxBunch")),
0023       verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
0024       nbin_(maxbunch_ - minbunch_ + 1) {
0025   // Histograms will be defined according to the configuration
0026   ParameterSet mixObjextsSet_ = iConfig.getParameter<ParameterSet>("mixObjects");
0027 }
0028 
0029 MixCollectionValidation::~MixCollectionValidation() {
0030   // do anything here that needs to be done at desctruction time
0031   // (e.g. close files, deallocate resources etc.)
0032 }
0033 
0034 void MixCollectionValidation::bookHistograms(DQMStore::IBooker &iBooker,
0035                                              edm::Run const &iRun,
0036                                              edm::EventSetup const & /* iSetup */) {
0037   iBooker.setCurrentFolder("MixingV/Mixing");
0038 
0039   std::vector<std::string> names = mixObjextsSet_.getParameterNames();
0040 
0041   for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
0042     ParameterSet pset = mixObjextsSet_.getParameter<ParameterSet>((*it));
0043     if (!pset.exists("type"))
0044       continue;  // to allow replacement by empty pset
0045     std::string object = pset.getParameter<std::string>("type");
0046     std::vector<InputTag> tags = pset.getParameter<std::vector<InputTag>>("input");
0047 
0048     if (object == "HepMCProduct") {
0049       std::string title = "Log10 Number of GenParticle in " + object;
0050       std::string name = "NumberOf" + object;
0051       nrHepMCProductH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
0052 
0053       HepMCProductTags_ = tags;
0054       if (!HepMCProductTags_.empty()) {
0055         crossingFrame_Hep_Token_ =
0056             consumes<CrossingFrame<HepMCProduct>>(edm::InputTag("mix", HepMCProductTags_[0].label()));
0057       }
0058     } else if (object == "SimTrack") {
0059       std::string title = "Log10 Number of " + object;
0060       std::string name = "NumberOf" + object;
0061       nrSimTrackH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
0062 
0063       SimTrackTags_ = tags;
0064       if (!SimTrackTags_.empty()) {
0065         crossingFrame_SimTr_Token_ = consumes<CrossingFrame<SimTrack>>(edm::InputTag("mix", SimTrackTags_[0].label()));
0066       }
0067     } else if (object == "SimVertex") {
0068       std::string title = "Log10 Number of " + object;
0069       std::string name = "NumberOf" + object;
0070       nrSimVertexH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
0071 
0072       SimVertexTags_ = tags;
0073       if (!SimVertexTags_.empty()) {
0074         crossingFrame_SimVtx_Token_ =
0075             consumes<CrossingFrame<SimVertex>>(edm::InputTag("mix", SimVertexTags_[0].label()));
0076       }
0077     } else if (object == "PSimHit") {
0078       std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
0079       for (unsigned int ii = 0; ii < subdets.size(); ii++) {
0080         std::string title = "Log10 Number of " + subdets[ii];
0081         std::string name = "NumberOf" + subdets[ii];
0082         SimHitNrmap_[subdets[ii]] = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
0083 
0084         title = "Time of " + subdets[ii];
0085         name = "TimeOf" + subdets[ii];
0086         SimHitTimemap_[subdets[ii]] =
0087             iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, -125., 375.);
0088       }
0089 
0090       PSimHitTags_ = tags;
0091       for (auto const &it : PSimHitTags_)
0092         crossingFrame_PSimHit_Tokens_.push_back(
0093             consumes<CrossingFrame<PSimHit>>(edm::InputTag("mix", it.label() + it.instance())));
0094     } else if (object == "PCaloHit") {
0095       std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
0096       for (unsigned int ii = 0; ii < subdets.size(); ii++) {
0097         std::string title = "Log10 Number of " + subdets[ii];
0098         std::string name = "NumberOf" + subdets[ii];
0099         CaloHitNrmap_[subdets[ii]] = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
0100 
0101         title = "Time of " + subdets[ii];
0102         name = "TimeOf" + subdets[ii];
0103         CaloHitTimemap_[subdets[ii]] =
0104             iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, -125., 375.);
0105       }
0106 
0107       PCaloHitTags_ = tags;
0108       for (auto const &it : PCaloHitTags_)
0109         crossingFrame_PCaloHit_Tokens_.push_back(
0110             consumes<CrossingFrame<PCaloHit>>(edm::InputTag("mix", it.label() + it.instance())));
0111     }
0112   }
0113 }
0114 
0115 void MixCollectionValidation::analyze(const edm::Event &iEvent, const edm::EventSetup &iConfig) {
0116   using namespace edm;
0117 
0118   if (!HepMCProductTags_.empty()) {
0119     bool gotHepMCProduct;
0120     edm::Handle<CrossingFrame<HepMCProduct>> crossingFrame;
0121     gotHepMCProduct = iEvent.getByToken(crossingFrame_Hep_Token_, crossingFrame);
0122 
0123     if (gotHepMCProduct) {
0124       std::unique_ptr<MixCollection<HepMCProduct>> hepMCProduct(
0125           new MixCollection<HepMCProduct>(crossingFrame.product()));
0126       MixCollection<HepMCProduct>::MixItr hitItr;
0127 
0128       fillGenParticleMulti(hitItr, hepMCProduct, nrHepMCProductH_);
0129     }
0130   }
0131 
0132   if (!SimTrackTags_.empty()) {
0133     bool gotSimTrack;
0134     edm::Handle<CrossingFrame<SimTrack>> crossingFrame;
0135     gotSimTrack = iEvent.getByToken(crossingFrame_SimTr_Token_, crossingFrame);
0136 
0137     if (gotSimTrack) {
0138       std::unique_ptr<MixCollection<SimTrack>> simTracks(new MixCollection<SimTrack>(crossingFrame.product()));
0139       MixCollection<SimTrack>::MixItr hitItr;
0140 
0141       fillMultiplicity(hitItr, simTracks, nrSimTrackH_);
0142     }
0143   }
0144 
0145   if (!SimVertexTags_.empty()) {
0146     bool gotSimVertex;
0147     edm::Handle<CrossingFrame<SimVertex>> crossingFrame;
0148     std::string SimVertexLabel = SimVertexTags_[0].label();
0149     gotSimVertex = iEvent.getByToken(crossingFrame_SimVtx_Token_, crossingFrame);
0150 
0151     if (gotSimVertex) {
0152       std::unique_ptr<MixCollection<SimVertex>> simVerteces(new MixCollection<SimVertex>(crossingFrame.product()));
0153       MixCollection<SimVertex>::MixItr hitItr;
0154 
0155       fillMultiplicity(hitItr, simVerteces, nrSimVertexH_);
0156     }
0157   }
0158 
0159   if (!PSimHitTags_.empty()) {
0160     edm::Handle<CrossingFrame<PSimHit>> crossingFrame;
0161 
0162     for (int i = 0; i < (int)PSimHitTags_.size(); i++) {
0163       bool gotPSimHit;
0164       gotPSimHit = iEvent.getByToken(crossingFrame_PSimHit_Tokens_[i], crossingFrame);
0165 
0166       if (gotPSimHit) {
0167         std::unique_ptr<MixCollection<PSimHit>> simHits(new MixCollection<PSimHit>(crossingFrame.product()));
0168 
0169         MixCollection<PSimHit>::MixItr hitItr;
0170 
0171         fillMultiplicity(hitItr, simHits, SimHitNrmap_[PSimHitTags_[i].instance()]);
0172 
0173         fillSimHitTime(hitItr, simHits, SimHitTimemap_[PSimHitTags_[i].instance()]);
0174       }
0175     }
0176   }
0177 
0178   if (!PCaloHitTags_.empty()) {
0179     edm::Handle<CrossingFrame<PCaloHit>> crossingFrame;
0180 
0181     for (int i = 0; i < (int)PCaloHitTags_.size(); i++) {
0182       bool gotPCaloHit;
0183       std::string PCaloHitLabel = PCaloHitTags_[i].label() + PCaloHitTags_[i].instance();
0184       gotPCaloHit = iEvent.getByToken(crossingFrame_PCaloHit_Tokens_[i], crossingFrame);
0185 
0186       if (gotPCaloHit) {
0187         std::unique_ptr<MixCollection<PCaloHit>> caloHits(new MixCollection<PCaloHit>(crossingFrame.product()));
0188 
0189         MixCollection<PCaloHit>::MixItr hitItr;
0190 
0191         fillMultiplicity(hitItr, caloHits, CaloHitNrmap_[PCaloHitTags_[i].instance()]);
0192 
0193         fillCaloHitTime(hitItr, caloHits, CaloHitTimemap_[PCaloHitTags_[i].instance()]);
0194       }
0195     }
0196   }
0197 }
0198 
0199 template <class T1, class T2>
0200 void MixCollectionValidation::fillMultiplicity(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
0201   std::vector<int> theMult(nbin_);
0202 
0203   for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
0204     int bunch = (*theItr_).eventId().bunchCrossing();
0205     int index = bunch - minbunch_;
0206     if (index >= 0 && index < nbin_) {
0207       theMult[index] += 1;
0208     } else {
0209       edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range";
0210     }
0211   }
0212 
0213   for (int i = 0; i < nbin_; i++) {
0214     theProfile_->Fill(float(i + minbunch_ + 0.5), std::log10(std::max(float(0.1), float(theMult[i]))));
0215   }
0216 }
0217 
0218 template <class T1, class T2>
0219 void MixCollectionValidation::fillGenParticleMulti(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
0220   std::vector<int> theMult(nbin_);
0221 
0222   for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
0223     int bunch = theItr_.bunch();
0224     int index = bunch - minbunch_;
0225     if (index >= 0 && index < nbin_) {
0226       theMult[index] += (*theItr_).GetEvent()->particles_size();
0227     } else {
0228       edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range";
0229     }
0230   }
0231 
0232   for (int i = 0; i < nbin_; i++) {
0233     theProfile_->Fill(float(i + minbunch_ + 0.5), std::log10(std::max(float(0.1), float(theMult[i]))));
0234   }
0235 }
0236 
0237 template <class T1, class T2>
0238 void MixCollectionValidation::fillSimHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
0239   for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
0240     int bunch = (*theItr_).eventId().bunchCrossing();
0241     float time = (*theItr_).timeOfFlight();
0242     int index = bunch - minbunch_;
0243     if (index >= 0 && index < nbin_) {
0244       theProfile_->Fill(float(bunch + 0.5), time);
0245     } else {
0246       edm::LogWarning("MixCollectionValidation") << "fillSimHitTime: bunch number " << bunch << " out of range";
0247     }
0248   }
0249 }
0250 
0251 template <class T1, class T2>
0252 void MixCollectionValidation::fillCaloHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
0253   for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
0254     int bunch = (*theItr_).eventId().bunchCrossing();
0255     float time = (*theItr_).time();
0256     int index = bunch - minbunch_;
0257     if (index >= 0 && index < nbin_) {
0258       theProfile_->Fill(float(bunch + 0.5), time);
0259     } else {
0260       edm::LogWarning("MixCollectionValidation") << "fillCaloHitTime: bunch number " << bunch << " out of range";
0261     }
0262   }
0263 }