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
#include "GeneratorInterface/GenFilters/plugins/PythiaDauVFilterMatchID.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include <iostream>
#include <memory>

#include <vector>

using namespace edm;
using namespace std;
using namespace Pythia8;

PythiaDauVFilterMatchID::PythiaDauVFilterMatchID(const edm::ParameterSet& iConfig)
    : fVerbose(iConfig.getUntrackedParameter("verbose", 0)),
      token_(consumes<edm::HepMCProduct>(
          edm::InputTag(iConfig.getUntrackedParameter("moduleLabel", std::string("generator")), "unsmeared"))),
      particleID(iConfig.getUntrackedParameter("ParticleID", 0)),
      motherID(iConfig.getUntrackedParameter("MotherID", 0)),
      chargeconju(iConfig.getUntrackedParameter("ChargeConjugation", true)),
      ndaughters(iConfig.getUntrackedParameter("NumberDaughters", 0)),
      maxptcut(iConfig.getUntrackedParameter("MaxPt", 14000.)) {
  //now do what ever initialization is needed
  vector<int> defdauID;
  defdauID.push_back(0);
  dauIDs = iConfig.getUntrackedParameter<vector<int> >("DaughterIDs", defdauID);
  vector<double> defminptcut;
  defminptcut.push_back(0.);
  minptcut = iConfig.getUntrackedParameter<vector<double> >("MinPt", defminptcut);
  vector<double> defminetacut;
  defminetacut.push_back(-10.);
  minetacut = iConfig.getUntrackedParameter<vector<double> >("MinEta", defminetacut);
  vector<double> defmaxetacut;
  defmaxetacut.push_back(10.);
  maxetacut = iConfig.getUntrackedParameter<vector<double> >("MaxEta", defmaxetacut);

  edm::LogInfo("PythiaDauVFilterMatchID")
      << "----------------------------------------------------------------------" << endl;
  edm::LogInfo("PythiaDauVFilterMatchID") << "--- PythiaDauVFilterMatchID" << endl;
  for (unsigned int i = 0; i < dauIDs.size(); ++i) {
    edm::LogInfo("PythiaDauVFilterMatchID")
        << "ID: " << dauIDs[i] << " pT > " << minptcut[i] << " " << minetacut[i] << " eta < " << maxetacut[i] << endl;
  }
  edm::LogInfo("PythiaDauVFilterMatchID") << "maxptcut   = " << maxptcut << endl;
  edm::LogInfo("PythiaDauVFilterMatchID") << "particleID = " << particleID << endl;
  edm::LogInfo("PythiaDauVFilterMatchID") << "motherID   = " << motherID << endl;

  // create pythia8 instance to access particle data
  edm::LogInfo("PythiaDauVFilterMatchID") << "Creating pythia8 instance for particle properties" << endl;
  if (!fLookupGen.get())
    fLookupGen = std::make_unique<Pythia>();
}

PythiaDauVFilterMatchID::~PythiaDauVFilterMatchID() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called to produce the data  ------------
bool PythiaDauVFilterMatchID::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
  using namespace edm;
  bool accepted = false;
  Handle<HepMCProduct> evt;
  iEvent.getByToken(token_, evt);

  int OK(1);
  vector<int> vparticles;

  HepMC::GenEvent* myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));

  if (fVerbose > 5) {
    edm::LogInfo("PythiaDauVFilterMatchID") << "looking for " << particleID << endl;
  }

  for (HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
    if ((*p)->pdg_id() != particleID)
      continue;

    // -- Check for mother of this particle
    if (0 != motherID) {
      OK = 0;
      for (HepMC::GenVertex::particles_in_const_iterator des = (*p)->production_vertex()->particles_in_const_begin();
           des != (*p)->production_vertex()->particles_in_const_end();
           ++des) {
        if (fVerbose > 10) {
          edm::LogInfo("PythiaDauVFilterMatchID")
              << "mother: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp()
              << " eta: " << (*des)->momentum().eta() << endl;
        }
        if (abs(motherID) == abs((*des)->pdg_id())) {
          OK = 1;
          break;
        }
      }
    }
    if (0 == OK)
      continue;

    //generate targets
    std::vector<decayTarget> targets;
    for (unsigned int i = 0; i < dauIDs.size(); i++) {
      decayTarget target;
      target.pdgID = dauIDs[i];
      target.minPt = minptcut[i];
      target.maxPt = maxptcut;
      target.minEta = minetacut[i];
      target.maxEta = maxetacut[i];
      targets.push_back(target);
    }
    if (fVerbose > 10) {
      edm::LogInfo("PythiaDauVFilterMatchID") << "created target: ";
      for (unsigned int i = 0; i < targets.size(); i++) {
        edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";
      }
      edm::LogInfo("PythiaDauVFilterMatchID") << endl;
    }

    // -- check for daugthers
    int ndau = 0;
    if (fVerbose > 5) {
      edm::LogInfo("PythiaDauVFilterMatchID") << "found ID: " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp()
                                              << " eta: " << (*p)->momentum().eta() << endl;
    }
    vparticles.push_back((*p)->pdg_id());
    if ((*p)->end_vertex()) {
      for (HepMC::GenVertex::particle_iterator des = (*p)->end_vertex()->particles_begin(HepMC::children);
           des != (*p)->end_vertex()->particles_end(HepMC::children);
           ++des) {
        if (TMath::Abs((*des)->pdg_id()) == 22) {
          continue;
        }
        ++ndau;
        if (fVerbose > 5) {
          edm::LogInfo("PythiaDauVFilterMatchID") << "ID: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp()
                                                  << " eta: " << (*des)->momentum().eta() << endl;
        }
        {  // protect matchedIdx
          int matchedIdx = -1;
          for (unsigned int i = 0; i < targets.size(); i++) {
            if ((*des)->pdg_id() != targets[i].pdgID) {
              continue;
            }
            if (fVerbose > 5) {
              edm::LogInfo("PythiaDauVFilterMatchID") << "i = " << i << " pT = " << (*des)->momentum().perp()
                                                      << " eta = " << (*des)->momentum().eta() << endl;
            }

            if ((*des)->momentum().perp() > targets[i].minPt && (*des)->momentum().perp() < targets[i].maxPt &&
                (*des)->momentum().eta() > targets[i].minEta && (*des)->momentum().eta() < targets[i].maxEta) {
              vparticles.push_back((*des)->pdg_id());
              if (fVerbose > 2) {
                edm::LogInfo("PythiaDauVFilterMatchID")
                    << "  accepted this particle " << (*des)->pdg_id() << " pT = " << (*des)->momentum().perp()
                    << " eta = " << (*des)->momentum().eta() << endl;
              }
              matchedIdx = i;
              break;
            }
          }
          if (matchedIdx > -1) {
            targets.erase(targets.begin() + matchedIdx);
          }
          if (fVerbose > 10) {
            edm::LogInfo("PythiaDauVFilterMatchID") << "remaining targets: ";
            for (unsigned int i = 0; i < targets.size(); i++) {
              edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";
            }
            edm::LogInfo("PythiaDauVFilterMatchID") << endl;
          }
        }
      }
    }

    if (ndau == ndaughters && targets.empty()) {
      accepted = true;
      if (fVerbose > 0) {
        edm::LogInfo("PythiaDauVFilterMatchID") << "  accepted this decay: ";
        for (unsigned int iv = 0; iv < vparticles.size(); ++iv)
          edm::LogInfo("PythiaDauVFilterMatchID") << vparticles[iv] << " ";
        edm::LogInfo("PythiaDauVFilterMatchID") << " from mother = " << motherID << endl;
      }
      break;
    }
  }

  int anti_particleID = -particleID;
  if (!(fLookupGen->particleData.isParticle(anti_particleID))) {
    anti_particleID = 0;
    if (fVerbose > 5)
      edm::LogInfo("PythiaDauVFilterMatchID")
          << "Particle " << particleID << " is its own anti-particle, skipping further testing " << endl;
  }
  if (!accepted && chargeconju && anti_particleID) {
    OK = 1;

    for (HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
      if ((*p)->pdg_id() != anti_particleID)
        continue;

      // -- Check for mother of this particle
      if (0 != motherID) {
        OK = 0;
        for (HepMC::GenVertex::particles_in_const_iterator des = (*p)->production_vertex()->particles_in_const_begin();
             des != (*p)->production_vertex()->particles_in_const_end();
             ++des) {
          if (fVerbose > 10) {
            edm::LogInfo("PythiaDauVFilterMatchID")
                << "mother: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp()
                << " eta: " << (*des)->momentum().eta() << endl;
          }
          if (abs(motherID) == abs((*des)->pdg_id())) {
            OK = 1;
            break;
          }
        }
      }
      if (0 == OK)
        continue;

      //generate anti targets
      std::vector<decayTarget> targets;
      for (unsigned int i = 0; i < dauIDs.size(); i++) {
        decayTarget target;
        int IDanti = -dauIDs[i];
        if (!(fLookupGen->particleData.isParticle(IDanti)))
          IDanti = dauIDs[i];
        target.pdgID = IDanti;
        target.minPt = minptcut[i];
        target.maxPt = maxptcut;
        target.minEta = minetacut[i];
        target.maxEta = maxetacut[i];
        targets.push_back(target);
      }
      if (fVerbose > 10) {
        edm::LogInfo("PythiaDauVFilterMatchID") << "created anti target: ";
        for (unsigned int i = 0; i < targets.size(); i++) {
          edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";
        }
        edm::LogInfo("PythiaDauVFilterMatchID") << endl;
      }

      if (fVerbose > 5) {
        edm::LogInfo("PythiaDauVFilterMatchID") << "found ID: " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp()
                                                << " eta: " << (*p)->momentum().eta() << endl;
      }
      vparticles.push_back((*p)->pdg_id());
      int ndau = 0;
      if ((*p)->end_vertex()) {
        for (HepMC::GenVertex::particle_iterator des = (*p)->end_vertex()->particles_begin(HepMC::children);
             des != (*p)->end_vertex()->particles_end(HepMC::children);
             ++des) {
          if (TMath::Abs((*des)->pdg_id()) == 22) {
            continue;
          }
          ++ndau;
          if (fVerbose > 5) {
            edm::LogInfo("PythiaDauVFilterMatchID")
                << "ID: " << (*des)->pdg_id() << " pT: " << (*des)->momentum().perp()
                << " eta: " << (*des)->momentum().eta() << endl;
          }
          {  // protect matchedIdx
            int matchedIdx = -1;
            for (unsigned int i = 0; i < targets.size(); i++) {
              if ((*des)->pdg_id() != targets[i].pdgID) {
                continue;
              }
              if (fVerbose > 5) {
                edm::LogInfo("PythiaDauVFilterMatchID") << "i = " << i << " pT = " << (*des)->momentum().perp()
                                                        << " eta = " << (*des)->momentum().eta() << endl;
              }

              if ((*des)->momentum().perp() > targets[i].minPt && (*des)->momentum().perp() < targets[i].maxPt &&
                  (*des)->momentum().eta() > targets[i].minEta && (*des)->momentum().eta() < targets[i].maxEta) {
                vparticles.push_back((*des)->pdg_id());
                if (fVerbose > 2) {
                  edm::LogInfo("PythiaDauVFilterMatchID")
                      << "  accepted this particle " << (*des)->pdg_id() << " pT = " << (*des)->momentum().perp()
                      << " eta = " << (*des)->momentum().eta() << endl;
                }
                matchedIdx = i;
                break;
              }
            }
            if (matchedIdx > -1) {
              targets.erase(targets.begin() + matchedIdx);
            }
            if (fVerbose > 10) {
              edm::LogInfo("PythiaDauVFilterMatchID") << "remaining targets: ";
              for (unsigned int i = 0; i < targets.size(); i++) {
                edm::LogInfo("PythiaDauVFilterMatchID") << targets[i].pdgID << " ";
              }
              edm::LogInfo("PythiaDauVFilterMatchID") << endl;
            }
          }
        }
      }
      if (ndau == ndaughters && targets.empty()) {
        accepted = true;
        if (fVerbose > 0) {
          edm::LogInfo("PythiaDauVFilterMatchID") << "  accepted this decay: ";
          for (unsigned int iv = 0; iv < vparticles.size(); ++iv)
            edm::LogInfo("PythiaDauVFilterMatchID") << vparticles[iv] << " ";
          edm::LogInfo("PythiaDauVFilterMatchID") << " from mother = " << motherID << endl;
        }
        break;
      }
    }
  }

  delete myGenEvent;
  return accepted;
}