Skip to content

Commit

Permalink
Merge remote-tracking branch 'cmg-central/CMG_MiniAOD_Lite_V6_0_from-…
Browse files Browse the repository at this point in the history
…CMSSW_7_0_6' into CMG_MiniAOD_Lite_V6_0_from-CMSSW_7_0_6
  • Loading branch information
MarkBaber committed Sep 19, 2014
2 parents 25da43c + 50604c9 commit 7af469c
Show file tree
Hide file tree
Showing 33 changed files with 734 additions and 362 deletions.
35 changes: 15 additions & 20 deletions CMGTools/Production/python/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,36 +408,31 @@ def getPrimaryDatasetEntries(self):
return self.findPrimaryDatasetEntries(self.name, self.dbsInstance, runmin, runmax)
### MM

def getDatasetFromCache( cachename ) :
cachedir = '/'.join( [os.environ['HOME'],'.cmgdataset'])
pckfile = open( cachedir + "/" + cachename )
dataset = pickle.load(pckfile)
return dataset

def writeDatasetToCache( cachename, dataset ):
cachedir = '/'.join( [os.environ['HOME'],'.cmgdataset'])
if not os.path.exists(cachedir):
os.mkdir(cachedir)
pckfile = open( cachedir + "/" + cachename, 'w')
pickle.dump(dataset, pckfile)

def createDataset( user, dataset, pattern, readcache=False,
basedir = None, run_range = None):

cachedir = '/'.join( [os.environ['HOME'],'.cmgdataset'])

def cacheFileName(data, user, pattern):
cf = data.replace('/','_')
name = '{dir}/{user}%{name}%{pattern}.pck'.format(
dir = cachedir,
user = user,
name = cf,
pattern = pattern)
return name
return '{user}%{name}%{pattern}.pck'.format( user = user, name = data.replace('/','_'), pattern = pattern)

def writeCache(dataset):
if not os.path.exists(cachedir):
os.mkdir(cachedir)
cachename = cacheFileName(dataset.name,
dataset.user,
dataset.pattern)
pckfile = open( cachename, 'w')
pickle.dump(dataset, pckfile)
writeDatasetToCache( cacheFileName(dataset.name, dataset.user, dataset.pattern), dataset )

def readCache(data, user, pattern):
cachename = cacheFileName(data, user, pattern)
pckfile = open( cachename)
dataset = pickle.load(pckfile)
#print 'reading cache'
return dataset
return getDatasetFromCache( cacheFileName(data, user, pattern) )

if readcache:
try:
Expand Down
23 changes: 23 additions & 0 deletions CMGTools/Production/scripts/cmgListChunksToResub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
if [[ "$1" == "" || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: $0 [ -q <queue> ] [ directory ] "
echo "Will look in the specified directory (or in the current one if none is specified), "
echo "list failed jobs and print the cmgResubChunk commands to resubmit them."
echo "The default queue is 8nh"
exit 1;
fi

Q=8nh
if [[ "$1" == "-q" ]]; then
Q=$2; shift; shift;
fi;

BASE="";
if [[ "$1" != "" && "$1" != "." ]]; then
test -d $1 && cd $1 && BASE="$PWD/";
fi

echo "Will print out the commands to resubmit the chunks that failed "
for D in *_Chunk[0-9]*; do
test -f $D/JSONAnalyzer/JSON.pck || echo cmgResubChunk -q 8nh ${BASE}${D};
done;
22 changes: 22 additions & 0 deletions CMGTools/Production/scripts/cmgResubChunk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
if [[ "$1" == "" || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: $0 [ -q <queue> ] <Component_ChunkXYZ> [ <JobName> ] "
echo " will resubmit the chunk <Component_ChunkXYZ> to the queue <queue> (default: 2nd) "
echo " if no jobname is specified, the job name in LSF will be the chunk name."
echo " you can also run this command from within the Component_ChunkXYZ directory, in that case you can omit the chunk name"
exit 1;
fi

Q=2nd
if [[ "$1" == "-q" ]]; then
Q=$2; shift; shift;
fi;
if [[ "$1" != "" ]] && test -d $1 && test -f $1/batchScript.sh; then
cd $1; shift;
elif test \! -f batchScript.sh; then
echo "You did not specify a valid chunk to resubmit and you're not in a chunk directory.";
exit 1;
fi
LAB="$(basename $PWD)"; if [[ "$1" != "" ]]; then LAB=$1; fi;
rename LSFJ OLD_LSFJ *;
bsub -q $Q -J $LAB < batchScript.sh
25 changes: 25 additions & 0 deletions CMGTools/TTHAnalysis/interface/SignedImpactParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h"

#if defined( __GXX_EXPERIMENTAL_CXX0X__)
#include "TrackingTools/TransientTrack/interface/TransientTrack.h"
#endif

struct MagneticField;


class SignedImpactParameter {
public:
SignedImpactParameter() ;
~SignedImpactParameter() ;

Measurement1D signedIP3D(const reco::Track &tk, const reco::Vertex &vtx, const reco::Track::Vector jetdir) const ;
std::pair<double,double> twoTrackChi2(const reco::Track &tk1, const reco::Track &tk2) const ;
//For the vertex related variables
//A = selectedLeptons[0], B = selectedLeptons[1], C = selectedLeptons[2], D = selectedLeptons[3]

#if defined( __GXX_EXPERIMENTAL_CXX0X__)
//Helping functions
std::vector<reco::TransientTrack> ttrksf(const reco::Track &trkA, const reco::Track &trkB, const reco::Track &trkC, const reco::Track &trkD, int nlep) const;
std::vector<reco::TransientTrack> ttrksbuthef(const reco::Track &trkA, const reco::Track &trkB, const reco::Track &trkC, const reco::Track &trkD, int nlep, int iptrk) const;
reco::TransientTrack thettrkf(const reco::Track &trkA, const reco::Track &trkB, const reco::Track &trkC, const reco::Track &trkD, int nlep, int iptrk) const;
#endif

//Variables related to IP
//Of one lepton w.r.t. the PV of the event
std::pair<double,double> absIP3D(const reco::Track &trk, const reco::Vertex &pv) const;
//Of one lepton w.r.t. the PV of the PV of the other leptons only
std::pair<double,double> absIP3Dtrkpvtrks(const reco::Track &trkA, const reco::Track &trkB, const reco::Track &trkC, const reco::Track &trkD, int nlep, int iptrk) const;
//Variables related to chi2
std::pair<double,double> chi2pvtrks(const reco::Track &trkA, const reco::Track &trkB, const reco::Track &trkC, const reco::Track &trkD, int nlep) const;


private:
//MagneticField *bfield_;
static MagneticField *paramField_;
Expand Down
36 changes: 16 additions & 20 deletions CMGTools/TTHAnalysis/macros/leptons/prepareLepMVAFriendTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,17 @@ def __call__(self,lep,ncorr):
_CommonSpect = [
]
_CommonVars = [
MVAVar("neuRelIso := relIso - chargedIso/pt",lambda x: x.relIso - x.chargedIso/x.pt),
MVAVar("chRelIso := chargedIso/pt",lambda x: x.chargedIso/x.pt),
MVAVar("jetDR_in := min(dr_in,0.5)", lambda x : min(x.jetDR,0.5), corrfunc=ROOT.correctJetDRMC),
MVAVar("jetPtRatio_in := min(ptf_in,1.5)", lambda x : min(x.jetPtRatio,1.5), corrfunc=ROOT.correctJetPtRatioMC),
MVAVar("jetBTagCSV_in := max(CSV_in,0)", lambda x : max(x.jetBTagCSV,0.)),
#MVAVar("jetDR_out := min(dr_out,5)", lambda x : min(x.dr_out,5.)),
#MVAVar("jetPtRatio_out := min(ptf_out,1.5)", lambda x : min(x.ptf_out,1.5)),
#MVAVar("jetBTagCSV_out := max(CSV_out,0)", lambda x : max(x.CSV_out,0.)),
MVAVar("neuRelIso03 := relIso03 - chargedHadRelIso03",lambda x: x.relIso03 - x.chargedHadRelIso03),
MVAVar("chRelIso03 := chargedHadRelIso03",lambda x: x.chargedHadRelIso03),
MVAVar("jetDR := min(jetDR,0.5)", lambda x : min(x.jetDR,0.5), corrfunc=ROOT.correctJetDRMC),
MVAVar("jetPtRatio := min(jetPtRatio,1.5)", lambda x : min(x.jetPtRatio,1.5), corrfunc=ROOT.correctJetPtRatioMC),
MVAVar("jetBTagCSV := max(jetBTagCSV,0)", lambda x : max(x.jetBTagCSV,0.)),
MVAVar("sip3d",lambda x: x.sip3d, corrfunc=ROOT.scaleSip3dMC),
MVAVar("dxy := log(abs(dxy))",lambda x: log(abs(x.dxy)), corrfunc=ROOT.scaleDxyMC),
MVAVar("dz := log(abs(dz))", lambda x: log(abs(x.dz)), corrfunc=ROOT.scaleDzMC),
]
_ElectronVars = [
MVAVar("mvaId",lambda x: x.mvaId),
MVAVar("innerHits",lambda x: x.innerHits),
MVAVar("mvaId",lambda x: x.mvaId)
]
class LeptonMVA:
def __init__(self,basepath):
Expand Down Expand Up @@ -95,24 +91,24 @@ def beginJob(self):
self.t = PyTree(self.book("TTree","t","t"))
for i in range(8):
self.t.branch("LepGood%d_mvaNew" % (i+1),"F")
if not self.data and not self.fast:
self.t.branch("LepGood%d_mvaNewUncorr" % (i+1),"F")
self.t.branch("LepGood%d_mvaNewDoubleCorr" % (i+1),"F")
#if not self.data and not self.fast:
# self.t.branch("LepGood%d_mvaNewUncorr" % (i+1),"F")
# self.t.branch("LepGood%d_mvaNewDoubleCorr" % (i+1),"F")
def analyze(self,event):
lep = Collection(event,"LepGood","nLepGood",8)
for i,l in enumerate(lep):
if self.data:
setattr(self.t, "LepGood%d_mvaNew" % (i+1), self.mva(l,ncorr=0))
else:
setattr(self.t, "LepGood%d_mvaNew" % (i+1), self.mva(l,ncorr=1))
if not self.fast:
setattr(self.t, "LepGood%d_mvaNewUncorr" % (i+1), self.mva(l,ncorr=0))
setattr(self.t, "LepGood%d_mvaNewDoubleCorr" % (i+1), self.mva(l,ncorr=2))
setattr(self.t, "LepGood%d_mvaNew" % (i+1), self.mva(l,ncorr=0))
#if not self.fast:
# setattr(self.t, "LepGood%d_mvaNewUncorr" % (i+1), self.mva(l,ncorr=0))
# setattr(self.t, "LepGood%d_mvaNewDoubleCorr" % (i+1), self.mva(l,ncorr=2))
for i in xrange(len(lep),8):
setattr(self.t, "LepGood%d_mvaNew" % (i+1), -99.)
if not self.data and not self.fast:
setattr(self.t, "LepGood%d_mvaNewUncorr" % (i+1), -99.)
setattr(self.t, "LepGood%d_mvaNewDoubleCorr" % (i+1), -99.)
#if not self.data and not self.fast:
# setattr(self.t, "LepGood%d_mvaNewUncorr" % (i+1), -99.)
# setattr(self.t, "LepGood%d_mvaNewDoubleCorr" % (i+1), -99.)
self.t.fill()

import os, itertools
Expand Down
69 changes: 20 additions & 49 deletions CMGTools/TTHAnalysis/macros/leptons/prepareLepTrain.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,65 @@
#!/usr/bin/env python
from CMGTools.TTHAnalysis.treeReAnalyzer import *

import os, ROOT
import os,itertools, ROOT
if "/smearer_cc.so" not in ROOT.gSystem.GetLibraries():
ROOT.gROOT.ProcessLine(".L %s/src/CMGTools/TTHAnalysis/python/plotter/smearer.cc+" % os.environ['CMSSW_BASE']);
if "/mcCorrections_cc.so" not in ROOT.gSystem.GetLibraries():
ROOT.gROOT.ProcessLine(".L %s/src/CMGTools/TTHAnalysis/python/plotter/mcCorrections.cc+" % os.environ['CMSSW_BASE']);

def plausible(rec,gen):
dr = deltaR(rec,gen)
if abs(rec.pdgId) == 11 and abs(gen.pdgId) != 11: return False
if abs(rec.pdgId) == 13 and abs(gen.pdgId) != 13: return False
if dr < 0.3: return True
if gen.pt < abs(rec.pdgId) == 13 and gen.pdgId != rec.pdgId: return False
if dr < 0.7: return True
if min(rec.pt,gen.pt)/max(rec.pt,gen.pt) < 0.3: return False
return True

from optparse import OptionParser
parser = OptionParser(usage="%prog [options] <PROCESS> <FILEOUT>")
parser.add_option("-t", "--tree", dest="tree", default='ttHLepTreeProducerTTH', help="Pattern for tree name");
parser.add_option("-V", "--vector", dest="vectorTree", action="store_true", default=True, help="Input tree is a vector")
(options, args) = parser.parse_args()

class LepTreeProducer(Module):
def beginJob(self):
self.t = PyTree(self.book("TTree","t","t"))
self.t.branch("dr_in","F")
self.t.branch("ptf_in","F")
self.t.branch("CSV_in","F")
#self.t.branch("dr_out","F")
#self.t.branch("ptf_out","F")
#self.t.branch("CSV_out","F")
self.t.branch("good","I")
self.t.branch("nJet25","I")
self.t.branch("nBJetLoose25","I")
self.t.branch("nBJetMedium25","I")
self.t.branch("puWeight","F")
#self.t.branch("nVert","F")
self.copyvars = [ 'relIso','chargedIso','mvaId','pt','eta','pdgId','innerHits','tightId',"mva"]
self.copyvars = ['relIso03','chargedHadRelIso03','relIso04','chargedHadRelIso04','mvaId','pt','eta','pdgId','lostHits','tightId','nStations','trkKink','caloCompatibility','globalTrackChi2','trackerLayers','pixelLayers','mcMatchId','mcMatchAny', 'mcMatchTau']
for C in self.copyvars: self.t.branch(C,"F")
# these I can't copy since I need
for C in [ 'sip3d','dxy','dz' ]: self.t.branch(C,"F")
# these I can't copy since I need to apply corrections
for C in [ 'sip3d','dxy','dz','jetPtRatio','jetBTagCSV','jetDR']: self.t.branch(C,"F")
self.first = True
def analyze(self,event):
#if event.nLepGood < 3: return False
#jet = Collection(event,"Jet")
lep = Collection(event,"LepGood","nLepGood",8)
glep = Collection(event,"GenLep")
gtau = Collection(event,"GenLepFromTau")
for l in lep:
self.t.sip3d = ROOT.scaleSip3dMC(l.sip3d, l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else l.sip3d
self.t.dz = ROOT.scaleDzMC( l.dz, l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else l.dz
self.t.dxy = ROOT.scaleDxyMC( l.dxy, l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else l.dxy
(dr,ptf) = (l.jetDR,l.jetPtRatio)
self.t.dr_in = ROOT.correctJetDRMC(dr,l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else dr
self.t.ptf_in = ROOT.correctJetPtRatioMC(ptf,l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else ptf
self.t.CSV_in = l.jetBTagCSV
#(j,dr) = closest(l, jet)
#ptot = j.p4() + l.p4()
#self.t.dr_out = dr
#self.t.ptf_out = l.pt/ptot.Pt()
#self.t.CSV_out = j.btagCSV
(gmatch,dr) = closest(l,glep,presel=plausible)
if dr < 1.5 and abs(gmatch.pdgId) == abs(l.pdgId):
self.t.good = 20 if dr < 0.5 else 2
else:
(gmatch,dr) = closest(l,gtau,presel=plausible)
if dr < 1.5 and abs(gmatch.pdgId) == abs(l.pdgId):
self.t.good = 10 if dr < 0.5 else 1
else:
self.t.good = -l.mcMatchAny
self.t.fill()
self.t.jetDR = ROOT.correctJetDRMC(dr,l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else dr
self.t.jetPtRatio = ROOT.correctJetPtRatioMC(ptf,l.pdgId,l.pt,l.eta,l.mcMatchId,l.mcMatchAny) if self.corr else ptf
self.t.jetBTagCSV = l.jetBTagCSV
for C in self.copyvars: setattr(self.t, C, getattr(l,C))
self.t.nJet25 = event.nJet25
self.t.nBJetLoose25 = event.nBJetLoose25
self.t.nBJetMedium25 = event.nBJetMedium25
self.t.puWeight = event.puWeight
#self.t.nVert = event.nVert
self.t.fill()

from sys import argv
f = ROOT.TFile.Open(argv[1])
t = f.Get("ttHLepTreeProducerBase")
print "Reading %s (%d entries)" % (argv[1], t.GetEntries())
f = ROOT.TFile.Open(args[0]+"/"+options.tree+"/"+options.tree+"_tree.root")
t = f.Get(options.tree)
t.vectorTree = options.vectorTree
print "Reading %s (%d entries)" % (args[0], t.GetEntries())

booker = Booker(argv[2] if len(argv) >= 3 else "lepTree.root")
booker = Booker(args[1] if len(argv) >= 3 else "lepTree.root")
prod = LepTreeProducer("rec",booker)
if len(argv) >= 4 and argv[3] == "NoCorr":
if len(args) >= 3 and args[2] == "NoCorr":
print "Will not apply corrections"
prod.corr = False
else:
print "Will apply corrections"
prod.corr = True
el = EventLoop([ prod, ])
maxEv = (int(argv[4]) if len(argv) >= 5 else -1)
maxEv = (int(args[3]) if len(args) >= 4 else -1)
print "max entries: ",maxEv
el.loop([t], maxEvents=maxEv)
booker.done()
Loading

0 comments on commit 7af469c

Please sign in to comment.