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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
#include "DQMOffline/Trigger/interface/EgHLTOffHelper.h"

#include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/JetReco/interface/CaloJetCollection.h"
#include "FWCore/Common/interface/TriggerNames.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"

#include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h"

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/GsfTrackReco/interface/GsfTrack.h"

#include "RecoEgamma/EgammaIsolationAlgos/interface/EgammaHLTTrackIsolation.h"
#include "RecoEgamma/EgammaIsolationAlgos/interface/EgammaTowerIsolation.h"
#include "RecoEgamma/EgammaIsolationAlgos/interface/EgammaRecHitIsolation.h"
#include "RecoLocalCalo/EcalRecAlgos/interface/EcalSeverityLevelAlgo.h"
#include "RecoLocalCalo/EcalRecAlgos/interface/EcalSeverityLevelAlgoRcd.h"

#include "DQMOffline/Trigger/interface/EgHLTTrigCodes.h"
#include "DQMOffline/Trigger/interface/EgHLTTrigTools.h"
#include "DQMOffline/Trigger/interface/EgHLTErrCodes.h"

#include <iostream>

using namespace egHLT;

OffHelper::~OffHelper() {
  if (hltEleTrkIsolAlgo_)
    delete hltEleTrkIsolAlgo_;
  if (hltPhoTrkIsolAlgo_)
    delete hltPhoTrkIsolAlgo_;
}

void OffHelper::setup(const edm::ParameterSet& conf, edm::ConsumesCollector&& iC) {
  ecalRecHitsEBToken = iC.consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("BarrelRecHitCollection"));
  ecalRecHitsEEToken = iC.consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("EndcapRecHitCollection"));
  caloJetsToken = iC.consumes<reco::CaloJetCollection>(conf.getParameter<edm::InputTag>("CaloJetCollection"));
  isolTrkToken = iC.consumes<reco::TrackCollection>(conf.getParameter<edm::InputTag>("IsolTrackCollection"));
  hbheHitsToken = iC.consumes<HBHERecHitCollection>(conf.getParameter<edm::InputTag>("HBHERecHitCollection"));
  hfHitsToken = iC.consumes<HFRecHitCollection>(conf.getParameter<edm::InputTag>("HFRecHitCollection"));
  electronsToken = iC.consumes<reco::GsfElectronCollection>(conf.getParameter<edm::InputTag>("ElectronCollection"));
  photonsToken = iC.consumes<reco::PhotonCollection>(conf.getParameter<edm::InputTag>("PhotonCollection"));
  triggerSummaryToken = iC.consumes<trigger::TriggerEvent>(conf.getParameter<edm::InputTag>("triggerSummaryLabel"));
  hltTag_ = conf.getParameter<std::string>("hltTag");
  beamSpotToken = iC.consumes<reco::BeamSpot>(conf.getParameter<edm::InputTag>("BeamSpotProducer"));
  caloTowersToken = iC.consumes<CaloTowerCollection>(conf.getParameter<edm::InputTag>("CaloTowers"));
  trigResultsToken = iC.consumes<edm::TriggerResults>(conf.getParameter<edm::InputTag>("TrigResults"));
  vertexToken = iC.consumes<reco::VertexCollection>(conf.getParameter<edm::InputTag>("VertexCollection"));

  caloGeomToken_ = iC.esConsumes();
  caloTopoToken_ = iC.esConsumes();
  magFieldToken_ = iC.esConsumes();
  ecalSeverityToken_ = iC.esConsumes();

  eleCuts_.setup(conf.getParameter<edm::ParameterSet>("eleCuts"));
  eleLooseCuts_.setup(conf.getParameter<edm::ParameterSet>("eleLooseCuts"));
  phoCuts_.setup(conf.getParameter<edm::ParameterSet>("phoCuts"));
  phoLooseCuts_.setup(conf.getParameter<edm::ParameterSet>("phoLooseCuts"));

  //now we have the isolations completely configurable via python
  hltEMIsolOuterCone_ = conf.getParameter<double>("hltEMIsolOuterCone");
  hltEMIsolInnerConeEB_ = conf.getParameter<double>("hltEMIsolInnerConeEB");
  hltEMIsolEtaSliceEB_ = conf.getParameter<double>("hltEMIsolEtaSliceEB");
  hltEMIsolEtMinEB_ = conf.getParameter<double>("hltEMIsolEtMinEB");
  hltEMIsolEMinEB_ = conf.getParameter<double>("hltEMIsolEMinEB");
  hltEMIsolInnerConeEE_ = conf.getParameter<double>("hltEMIsolInnerConeEE");
  hltEMIsolEtaSliceEE_ = conf.getParameter<double>("hltEMIsolEtaSliceEE");
  hltEMIsolEtMinEE_ = conf.getParameter<double>("hltEMIsolEtMinEE");
  hltEMIsolEMinEE_ = conf.getParameter<double>("hltEMIsolEMinEE");

  hltPhoTrkIsolPtMin_ = conf.getParameter<double>("hltPhoTrkIsolPtMin");
  hltPhoTrkIsolOuterCone_ = conf.getParameter<double>("hltPhoTrkIsolOuterCone");
  hltPhoTrkIsolInnerCone_ = conf.getParameter<double>("hltPhoTrkIsolInnerCone");
  hltPhoTrkIsolZSpan_ = conf.getParameter<double>("hltPhoTrkIsolZSpan");
  hltPhoTrkIsolRSpan_ = conf.getParameter<double>("hltPhoTrkIsolZSpan");
  hltPhoTrkIsolCountTrks_ = conf.getParameter<bool>("hltPhoTrkIsolCountTrks");

  hltEleTrkIsolPtMin_ = conf.getParameter<double>("hltEleTrkIsolPtMin");
  hltEleTrkIsolOuterCone_ = conf.getParameter<double>("hltEleTrkIsolOuterCone");
  hltEleTrkIsolInnerCone_ = conf.getParameter<double>("hltEleTrkIsolInnerCone");
  hltEleTrkIsolZSpan_ = conf.getParameter<double>("hltEleTrkIsolZSpan");
  hltEleTrkIsolRSpan_ = conf.getParameter<double>("hltEleTrkIsolZSpan");

  hltHadIsolOuterCone_ = conf.getParameter<double>("hltHadIsolOuterCone");
  hltHadIsolInnerCone_ = conf.getParameter<double>("hltHadIsolInnerCone");
  hltHadIsolEtMin_ = conf.getParameter<double>("hltHadIsolEtMin");
  hltHadIsolDepth_ = conf.getParameter<int>("hltHadIsolDepth");

  calHLTHcalIsol_ = conf.getParameter<bool>("calHLTHcalIsol");
  calHLTEmIsol_ = conf.getParameter<bool>("calHLTEmIsol");
  calHLTEleTrkIsol_ = conf.getParameter<bool>("calHLTEleTrkIsol");
  calHLTPhoTrkIsol_ = conf.getParameter<bool>("calHLTPhoTrkIsol");

  trigCutParams_ = conf.getParameter<std::vector<edm::ParameterSet> >(
      "triggerCuts");  //setupTriggers used to be in this function but had to be moved due to HLTConfigChanges (has to be called beginRun) so we have to save this for later.

  hltEleTrkIsolAlgo_ = new EgammaHLTTrackIsolation(
      hltEleTrkIsolPtMin_, hltEleTrkIsolOuterCone_, hltEleTrkIsolZSpan_, hltEleTrkIsolRSpan_, hltEleTrkIsolInnerCone_);
  hltPhoTrkIsolAlgo_ = new EgammaHLTTrackIsolation(
      hltPhoTrkIsolPtMin_, hltPhoTrkIsolOuterCone_, hltPhoTrkIsolZSpan_, hltPhoTrkIsolRSpan_, hltPhoTrkIsolInnerCone_);
}

//this code was taken out of OffHelper::setup due to HLTConfigProvider changes
//it still assumes that this is called only once
void OffHelper::setupTriggers(const HLTConfigProvider& hltConfig,
                              const std::vector<std::string>& hltFiltersUsed,
                              const TrigCodes& trigCodes) {
  hltFiltersUsed_ = hltFiltersUsed;  //expensive but only do this once and faster ways could make things less clear
  //now work out how many objects are requires to pass filter for it to accept
  hltFiltersUsedWithNrCandsCut_.clear();
  std::vector<int> getMRObjs = egHLT::trigTools::getMinNrObjsRequiredByFilter(hltFiltersUsed_);
  for (size_t filterNr = 0; filterNr < hltFiltersUsed_.size(); filterNr++) {
    hltFiltersUsedWithNrCandsCut_.push_back(std::make_pair(hltFiltersUsed_[filterNr], getMRObjs[filterNr]));
  }

  //now loading the cuts for every trigger into our vector which stores them
  //only load cuts for triggers that are in hltFiltersUsed

  for (auto& trigCutParam : trigCutParams_) {
    std::string trigName = trigCutParam.getParameter<std::string>("trigName");
    if (std::find(hltFiltersUsed_.begin(), hltFiltersUsed_.end(), trigName) !=
        hltFiltersUsed_.end()) {  //perhaps I should sort hltFiltersUsed_....
      trigCuts_.push_back(std::make_pair(trigCodes.getCode(trigName), OffEgSel(trigCutParam)));
      //   std::cout<<trigName<<std::endl<<"between"<<std::endl<<trigCutParams_[trigNr]<<std::endl<<"after"<<std::endl;
    }
  }
  trigCutParams_.clear();  //dont need it any more, get rid of it

  //to make my life difficult, the scaled l1 paths are special
  //and arent stored in trigger event
  //to I have to figure out the path, see if it passes
  //and then hunt down the l1 seed filter and use that to match to the pho/ele
  //matching on l1 seed filter is not enough as that will be passed for normal
  //electron triggers even if pre-scale hasnt fired
  l1PreScaledFilters_.clear();
  l1PreScaledPaths_.clear();
  l1PreAndSeedFilters_.clear();
  for (auto& filterNr : hltFiltersUsed_) {
    if (filterNr.find("hltPreL1") == 0) {  //l1 prescaled path
      l1PreScaledFilters_.push_back(filterNr);
    }
  }

  egHLT::trigTools::translateFiltersToPathNames(hltConfig, l1PreScaledFilters_, l1PreScaledPaths_);
  if (l1PreScaledPaths_.size() == l1PreScaledFilters_.size()) {
    for (size_t pathNr = 0; pathNr < l1PreScaledPaths_.size(); pathNr++) {
      std::string l1SeedFilter = egHLT::trigTools::getL1SeedFilterOfPath(hltConfig, l1PreScaledPaths_[pathNr]);
      //---Morse====
      //std::cout<<l1PreScaledFilters_[pathNr]<<"  "<<l1PreScaledPaths_[pathNr]<<"  "<<l1SeedFilter<<std::endl;
      //------------
      l1PreAndSeedFilters_.push_back(std::make_pair(l1PreScaledFilters_[pathNr], l1SeedFilter));
    }
  }
}

int OffHelper::makeOffEvt(const edm::Event& edmEvent,
                          const edm::EventSetup& setup,
                          egHLT::OffEvt& offEvent,
                          const TrigCodes& c) {
  offEvent.clear();
  int errCode = 0;  //excution stops as soon as an error is flagged
  if (errCode == 0)
    errCode = getHandles(edmEvent, setup);
  if (errCode == 0)
    errCode = fillOffEleVec(offEvent.eles());
  if (errCode == 0)
    errCode = fillOffPhoVec(offEvent.phos());
  if (errCode == 0)
    errCode = setTrigInfo(edmEvent, offEvent, c);
  if (errCode == 0)
    offEvent.setJets(recoJets_);
  return errCode;
}

int OffHelper::getHandles(const edm::Event& event, const edm::EventSetup& setup) {
  try {
    caloGeom_ = setup.getHandle(caloGeomToken_);
    caloTopology_ = setup.getHandle(caloTopoToken_);
    //ecalSeverityLevel_ = setup.getHandle(ecalSeverityToken_);
  } catch (cms::Exception& iException) {
    return errCodes::Geom;
  }
  try {
    magField_ = setup.getHandle(magFieldToken_);
  } catch (cms::Exception& iException) {
    return errCodes::MagField;
  }

  //get objects
  if (!getHandle(event, triggerSummaryToken, trigEvt_))
    return errCodes::TrigEvent;  //must have this, otherwise skip event
  if (!getHandle(event, trigResultsToken, trigResults_))
    return errCodes::TrigEvent;  //re using bit to minimise bug fix code changes
  if (!getHandle(event, electronsToken, recoEles_))
    return errCodes::OffEle;  //need for electrons
  if (!getHandle(event, photonsToken, recoPhos_))
    return errCodes::OffPho;  //need for photons
  if (!getHandle(event, caloJetsToken, recoJets_))
    return errCodes::OffJet;  //need for electrons and photons
  if (!getHandle(event, vertexToken, recoVertices_))
    return errCodes::OffVertex;  //need for eff vs nVertex

  //need for HLT isolations (rec hits also need for sigmaIPhiIPhi (ele/pho) and r9 pho)
  if (!getHandle(event, ecalRecHitsEBToken, ebRecHits_))
    return errCodes::EBRecHits;
  if (!getHandle(event, ecalRecHitsEEToken, eeRecHits_))
    return errCodes::EERecHits;
  if (!getHandle(event, isolTrkToken, isolTrks_))
    return errCodes::IsolTrks;
  if (!getHandle(event, hbheHitsToken, hbheHits_))
    return errCodes::HBHERecHits;  //I dont think we need hbhe rec-hits any more
  if (!getHandle(event, hfHitsToken, hfHits_))
    return errCodes::HFRecHits;  //I dont think we need hf rec-hits any more
  if (!getHandle(event, beamSpotToken, beamSpot_))
    return errCodes::BeamSpot;
  if (!getHandle(event, caloTowersToken, caloTowers_))
    return errCodes::CaloTowers;

  return 0;
}

//this function coverts GsfElectrons to a format which is actually useful to me
int OffHelper::fillOffEleVec(std::vector<OffEle>& egHLTOffEles) {
  egHLTOffEles.clear();
  egHLTOffEles.reserve(recoEles_->size());
  for (auto const& gsfIter : *recoEles_) {
    if (!gsfIter.ecalDrivenSeed())
      continue;  //avoid PF electrons (this is Eg HLT validation and HLT is ecal driven)

    int nVertex = 0;
    for (auto const& nVit : *recoVertices_) {
      if (!nVit.isFake() && nVit.ndof() > 4 && std::fabs(nVit.z() < 24.0) &&
          sqrt(nVit.x() * nVit.x() + nVit.y() * nVit.y()) < 2.0) {
        nVertex++;
      }
    }
    //if(nVertex>20)std::cout<<"nVertex: "<<nVertex<<std::endl;
    OffEle::EventData eventData;
    eventData.NVertex = nVertex;

    OffEle::IsolData isolData;
    fillIsolData(gsfIter, isolData);

    OffEle::ClusShapeData clusShapeData;
    fillClusShapeData(gsfIter, clusShapeData);

    OffEle::HLTData hltData;
    fillHLTData(gsfIter, hltData);

    egHLTOffEles.emplace_back(gsfIter, clusShapeData, isolData, hltData, eventData);

    //now we would like to set the cut results
    OffEle& ele = egHLTOffEles.back();
    ele.setCutCode(eleCuts_.getCutCode(ele));
    ele.setLooseCutCode(eleLooseCuts_.getCutCode(ele));

    std::vector<std::pair<TrigCodes::TrigBitSet, int> > trigCutsCutCodes;
    trigCutsCutCodes.reserve(trigCuts_.size());
    for (auto& trigCut : trigCuts_)
      trigCutsCutCodes.push_back(std::make_pair(trigCut.first, trigCut.second.getCutCode(ele)));
    ele.setTrigCutsCutCodes(trigCutsCutCodes);
  }  //end loop over gsf electron collection
  return 0;
}

void OffHelper::fillIsolData(const reco::GsfElectron& ele, OffEle::IsolData& isolData) {
  EgammaTowerIsolation hcalIsolAlgo(
      hltHadIsolOuterCone_, hltHadIsolInnerCone_, hltHadIsolEtMin_, hltHadIsolDepth_, caloTowers_.product());
  EgammaRecHitIsolation ecalIsolAlgoEB(hltEMIsolOuterCone_,
                                       hltEMIsolInnerConeEB_,
                                       hltEMIsolEtaSliceEB_,
                                       hltEMIsolEtMinEB_,
                                       hltEMIsolEMinEB_,
                                       caloGeom_,
                                       *ebRecHits_,
                                       ecalSeverityLevel_.product(),
                                       DetId::Ecal);
  EgammaRecHitIsolation ecalIsolAlgoEE(hltEMIsolOuterCone_,
                                       hltEMIsolInnerConeEE_,
                                       hltEMIsolEtaSliceEE_,
                                       hltEMIsolEtMinEE_,
                                       hltEMIsolEMinEE_,
                                       caloGeom_,
                                       *eeRecHits_,
                                       ecalSeverityLevel_.product(),
                                       DetId::Ecal);

  isolData.ptTrks = ele.dr03TkSumPt();
  isolData.nrTrks = 999;  //no longer supported
  isolData.em = ele.dr03EcalRecHitSumEt();
  isolData.hadDepth1 = ele.dr03HcalTowerSumEt(1);
  isolData.hadDepth2 = ele.dr03HcalTowerSumEt(2);

  //now time to do the HLT algos
  if (calHLTHcalIsol_)
    isolData.hltHad = hcalIsolAlgo.getTowerESum(&ele);
  else
    isolData.hltHad = 0.;
  if (calHLTEleTrkIsol_)
    isolData.hltTrksEle = hltEleTrkIsolAlgo_->electronPtSum(&(*(ele.gsfTrack())), isolTrks_.product());
  else
    isolData.hltTrksEle = 0.;
  if (calHLTPhoTrkIsol_) {
    if (hltPhoTrkIsolCountTrks_)
      isolData.hltTrksPho = hltPhoTrkIsolAlgo_->photonTrackCount(&ele, isolTrks_.product(), false);
    else
      isolData.hltTrksPho = hltPhoTrkIsolAlgo_->photonPtSum(&ele, isolTrks_.product(), false);
  } else
    isolData.hltTrksPho = 0.;
  if (calHLTEmIsol_)
    isolData.hltEm = ecalIsolAlgoEB.getEtSum(&ele, thresholds) + ecalIsolAlgoEE.getEtSum(&ele, thresholds);
  else
    isolData.hltEm = 0.;
}

void OffHelper::fillClusShapeData(const reco::GsfElectron& ele, OffEle::ClusShapeData& clusShapeData) {
  clusShapeData.sigmaEtaEta = ele.sigmaEtaEta();
  clusShapeData.sigmaIEtaIEta = ele.sigmaIetaIeta();
  double e5x5 = ele.e5x5();
  if (e5x5 != 0.) {
    clusShapeData.e1x5Over5x5 = ele.e1x5() / e5x5;
    clusShapeData.e2x5MaxOver5x5 = ele.e2x5Max() / e5x5;
  } else {
    clusShapeData.e1x5Over5x5 = -1;
    clusShapeData.e2x5MaxOver5x5 = -1;
  }

  //want to calculate r9, sigmaPhiPhi and sigmaIPhiIPhi, have to do old fashioned way
  const reco::BasicCluster& seedClus = *(ele.superCluster()->seed());
  const DetId seedDetId =
      seedClus.hitsAndFractions()[0]
          .first;  //note this may not actually be the seed hit but it doesnt matter because all hits will be in the barrel OR endcap
  if (seedDetId.subdetId() == EcalBarrel) {
    const auto& stdCov =
        EcalClusterTools::covariances(seedClus, ebRecHits_.product(), caloTopology_.product(), caloGeom_.product());
    const auto& crysCov = EcalClusterTools::localCovariances(seedClus, ebRecHits_.product(), caloTopology_.product());
    clusShapeData.sigmaPhiPhi = sqrt(stdCov[2]);
    clusShapeData.sigmaIPhiIPhi = sqrt(crysCov[2]);
    if (ele.superCluster()->rawEnergy() != 0.) {
      clusShapeData.r9 = EcalClusterTools::e3x3(seedClus, ebRecHits_.product(), caloTopology_.product()) /
                         ele.superCluster()->rawEnergy();
    } else
      clusShapeData.r9 = -1.;

  } else {
    const auto& stdCov =
        EcalClusterTools::covariances(seedClus, eeRecHits_.product(), caloTopology_.product(), caloGeom_.product());
    const auto& crysCov = EcalClusterTools::localCovariances(seedClus, eeRecHits_.product(), caloTopology_.product());
    clusShapeData.sigmaPhiPhi = sqrt(stdCov[2]);
    clusShapeData.sigmaIPhiIPhi = sqrt(crysCov[2]);
    if (ele.superCluster()->rawEnergy() != 0.) {
      clusShapeData.r9 = EcalClusterTools::e3x3(seedClus, eeRecHits_.product(), caloTopology_.product()) /
                         ele.superCluster()->rawEnergy();
    } else
      clusShapeData.r9 = -1.;
  }
}

//reco approximations of hlt quantities
void OffHelper::fillHLTData(const reco::GsfElectron& ele, OffEle::HLTData& hltData) {
  if (ele.closestCtfTrackRef().isNonnull() && ele.closestCtfTrackRef()->extra().isNonnull()) {
    reco::TrackRef ctfTrack = ele.closestCtfTrackRef();
    reco::SuperClusterRef scClus = ele.superCluster();

    //dEta
    const reco::BeamSpot::Point& bsPos = beamSpot_->position();
    math::XYZPoint scPosWRTVtx(scClus->x() - bsPos.x(), scClus->y() - bsPos.y(), scClus->z() - ctfTrack->vz());
    hltData.dEtaIn = fabs(scPosWRTVtx.eta() - ctfTrack->eta());

    //dPhi: lifted straight from hlt code
    float deltaPhi = fabs(ctfTrack->outerPosition().phi() - scClus->phi());
    if (deltaPhi > 6.283185308)
      deltaPhi -= 6.283185308;
    if (deltaPhi > 3.141592654)
      deltaPhi = 6.283185308 - deltaPhi;
    hltData.dPhiIn = deltaPhi;

    //invEInvP
    if (ele.ecalEnergy() != 0 && ctfTrack->p() != 0)
      hltData.invEInvP = 1 / ele.ecalEnergy() - 1 / ctfTrack->p();
    else
      hltData.invEInvP = 0;
  } else {
    hltData.dEtaIn = 999;
    hltData.dPhiIn = 999;
    hltData.invEInvP = 999;
  }

  //Now get HLT p4 from triggerobject
  //reset the position first
  hltData.HLTeta = 999;
  hltData.HLTphi = 999;
  hltData.HLTenergy = -999;
  trigTools::fillHLTposition(ele, hltData, hltFiltersUsed_, trigEvt_.product(), hltTag_);
  //trigTools::fillHLTposition(phos(),hltFiltersUsed_,l1PreAndSeedFilters_,evtTrigBits,trigEvt_.product(),hltTag_);
}

void OffHelper::fillHLTDataPho(const reco::Photon& pho, OffPho::HLTData& hltData) {
  //Now get HLT p4 from triggerobject
  //reset the position first
  hltData.HLTeta = 999;
  hltData.HLTphi = 999;
  hltData.HLTenergy = -999;
  trigTools::fillHLTposition(pho, hltData, hltFiltersUsed_, trigEvt_.product(), hltTag_);
  //trigTools::fillHLTposition(phos(),hltFiltersUsed_,l1PreAndSeedFilters_,evtTrigBits,trigEvt_.product(),hltTag_);
}

//this function coverts Photons to a format which more useful to me
int OffHelper::fillOffPhoVec(std::vector<OffPho>& egHLTOffPhos) {
  egHLTOffPhos.clear();
  egHLTOffPhos.reserve(recoPhos_->size());
  for (auto const& phoIter : *recoPhos_) {
    OffPho::IsolData isolData;
    OffPho::ClusShapeData clusShapeData;

    fillIsolData(phoIter, isolData);
    fillClusShapeData(phoIter, clusShapeData);

    OffPho::HLTData hltData;
    fillHLTDataPho(phoIter, hltData);

    egHLTOffPhos.emplace_back(phoIter, clusShapeData, isolData, hltData);
    OffPho& pho = egHLTOffPhos.back();
    pho.setCutCode(phoCuts_.getCutCode(pho));
    pho.setLooseCutCode(phoLooseCuts_.getCutCode(pho));

    std::vector<std::pair<TrigCodes::TrigBitSet, int> > trigCutsCutCodes;
    trigCutsCutCodes.reserve(trigCuts_.size());
    for (auto& trigCut : trigCuts_)
      trigCutsCutCodes.push_back(std::make_pair(trigCut.first, trigCut.second.getCutCode(pho)));
    pho.setTrigCutsCutCodes(trigCutsCutCodes);

  }  //end loop over photon collection
  return 0;
}

void OffHelper::fillIsolData(const reco::Photon& pho, OffPho::IsolData& isolData) {
  EgammaTowerIsolation hcalIsolAlgo(
      hltHadIsolOuterCone_, hltHadIsolInnerCone_, hltHadIsolEtMin_, hltHadIsolDepth_, caloTowers_.product());
  EgammaRecHitIsolation ecalIsolAlgoEB(hltEMIsolOuterCone_,
                                       hltEMIsolInnerConeEB_,
                                       hltEMIsolEtaSliceEB_,
                                       hltEMIsolEtMinEB_,
                                       hltEMIsolEMinEB_,
                                       caloGeom_,
                                       *ebRecHits_,
                                       ecalSeverityLevel_.product(),
                                       DetId::Ecal);
  EgammaRecHitIsolation ecalIsolAlgoEE(hltEMIsolOuterCone_,
                                       hltEMIsolInnerConeEE_,
                                       hltEMIsolEtaSliceEE_,
                                       hltEMIsolEtMinEE_,
                                       hltEMIsolEMinEE_,
                                       caloGeom_,
                                       *eeRecHits_,
                                       ecalSeverityLevel_.product(),
                                       DetId::Ecal);

  isolData.nrTrks = pho.nTrkHollowConeDR03();
  isolData.ptTrks = pho.trkSumPtHollowConeDR03();
  isolData.em = pho.ecalRecHitSumEtConeDR03();
  isolData.had = pho.hcalTowerSumEtConeDR03();

  //now calculate hlt algos
  if (calHLTHcalIsol_)
    isolData.hltHad = hcalIsolAlgo.getTowerESum(&pho);
  else
    isolData.hltHad = 0.;
  if (calHLTPhoTrkIsol_) {
    if (hltPhoTrkIsolCountTrks_)
      isolData.hltTrks = hltPhoTrkIsolAlgo_->photonTrackCount(&pho, isolTrks_.product(), false);
    else
      isolData.hltTrks = hltPhoTrkIsolAlgo_->photonPtSum(&pho, isolTrks_.product(), false);
  } else
    isolData.hltTrks = 0.;
  if (calHLTEmIsol_)
    isolData.hltEm = ecalIsolAlgoEB.getEtSum(&pho, thresholds) + ecalIsolAlgoEE.getEtSum(&pho, thresholds);
  else
    isolData.hltEm = 0.;
}

void OffHelper::fillClusShapeData(const reco::Photon& pho, OffPho::ClusShapeData& clusShapeData) {
  clusShapeData.sigmaEtaEta = pho.sigmaEtaEta();
  clusShapeData.sigmaIEtaIEta = pho.sigmaIetaIeta();
  double e5x5 = pho.e5x5();
  if (e5x5 !=
      0.) {  //even though it is almost impossible for this to be 0., this code can never ever crash under any situation
    clusShapeData.e1x5Over5x5 = pho.e1x5() / e5x5;
    clusShapeData.e2x5MaxOver5x5 = pho.e2x5() / e5x5;
  } else {
    clusShapeData.e1x5Over5x5 = -1;
    clusShapeData.e2x5MaxOver5x5 = -1;
  }
  clusShapeData.r9 = pho.r9();

  //sigmaPhiPhi and sigmaIPhiIPhi are not in object (and nor should they be) so have to get them old fashioned way
  //need to figure out if its in the barrel or endcap
  //get the first hit of the cluster and figure out if its barrel or endcap
  const reco::BasicCluster& seedClus = *(pho.superCluster()->seed());
  const DetId seedDetId =
      seedClus.hitsAndFractions()[0]
          .first;  //note this may not actually be the seed hit but it doesnt matter because all hits will be in the barrel OR endcap (it is also incredably inefficient as it getHitsByDetId passes the vector by value not reference
  if (seedDetId.subdetId() == EcalBarrel) {
    const auto& stdCov =
        EcalClusterTools::covariances(seedClus, ebRecHits_.product(), caloTopology_.product(), caloGeom_.product());
    const auto& crysCov = EcalClusterTools::localCovariances(seedClus, ebRecHits_.product(), caloTopology_.product());
    clusShapeData.sigmaPhiPhi = sqrt(stdCov[2]);
    clusShapeData.sigmaIPhiIPhi = sqrt(crysCov[2]);
  } else {
    const auto& stdCov =
        EcalClusterTools::covariances(seedClus, eeRecHits_.product(), caloTopology_.product(), caloGeom_.product());
    const auto& crysCov = EcalClusterTools::localCovariances(seedClus, eeRecHits_.product(), caloTopology_.product());
    clusShapeData.sigmaPhiPhi = sqrt(stdCov[2]);
    clusShapeData.sigmaIPhiIPhi = sqrt(crysCov[2]);
  }
}

int OffHelper::setTrigInfo(const edm::Event& edmEvent, egHLT::OffEvt& offEvent, const TrigCodes& trigCodes) {
  TrigCodes::TrigBitSet evtTrigBits =
      trigTools::getFiltersPassed(hltFiltersUsedWithNrCandsCut_, trigEvt_.product(), hltTag_, trigCodes);
  //the l1 prescale paths dont have a filter with I can figure out if it passed or failed with so have to use TriggerResults
  if (l1PreScaledPaths_.size() ==
      l1PreScaledFilters_.size()) {  //check to ensure both vectors have same number of events incase of screw ups
    const edm::TriggerNames& triggerNames = edmEvent.triggerNames(*trigResults_);
    for (size_t pathNr = 0; pathNr < l1PreScaledPaths_.size();
         pathNr++) {  //now we have to check the prescaled l1 trigger paths
      unsigned int pathIndex = triggerNames.triggerIndex(l1PreScaledPaths_[pathNr]);
      if (pathIndex < trigResults_->size() && trigResults_->accept(pathIndex)) {
        evtTrigBits |= trigCodes.getCode(l1PreScaledFilters_[pathNr]);
      }
    }
  }

  offEvent.setEvtTrigBits(evtTrigBits);

  trigTools::setFiltersObjPasses(
      offEvent.eles(), hltFiltersUsed_, l1PreAndSeedFilters_, evtTrigBits, trigCodes, trigEvt_.product(), hltTag_);
  trigTools::setFiltersObjPasses(
      offEvent.phos(), hltFiltersUsed_, l1PreAndSeedFilters_, evtTrigBits, trigCodes, trigEvt_.product(), hltTag_);
  return 0;
}