Skip to content

Commit

Permalink
Merge pull request cms-sw#238 from gpetruc/heppy_colin_20150206
Browse files Browse the repository at this point in the history
Upstream Heppy updates: logger verbosity and minor fixes
  • Loading branch information
gpetruc committed Feb 10, 2015
2 parents 4eaa19e + 3e0c575 commit 71d0757
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 146 deletions.
2 changes: 2 additions & 0 deletions PhysicsTools/Heppy/interface/Hemisphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Hemisphere {
// RejectISRDRmax() max DeltaR below which objects can be included
// (default = 10.)

Hemisphere(){};

Hemisphere(std::vector<float> Px_vector, std::vector<float> Py_vector, std::vector<float> Pz_vector, std::vector<float> E_vector, int seed_method, int hemisphere_association_method);

Hemisphere(std::vector<float> Px_vector, std::vector<float> Py_vector, std::vector<float> Pz_vector, std::vector<float> E_vector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def __init__(self, cfg_ana, cfg_comp, looperName):

def beginLoop(self, setup) :
super(AutoFillTreeProducer, self).beginLoop(setup)
## Declare how we store floats by default
self.tree.setDefaultFloatType("F"); # otherwise it's "D"

def declareHandles(self):
super(AutoFillTreeProducer, self).declareHandles()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def beginLoop(self, setup) :
print 'Compression', isCompressed
self.file = TFile( fileName, 'recreate', '', isCompressed )
self.tree = Tree('tree', self.name)
self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D')); # or 'F'
self.declareVariables(setup)

def declareVariables(self,setup):
Expand Down
8 changes: 7 additions & 1 deletion PhysicsTools/Heppy/src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "PhysicsTools/Heppy/interface/Hemisphere.h"
#include "PhysicsTools/Heppy/interface/AlphaT.h"
#include "PhysicsTools/Heppy/interface/ReclusterJets.h"

#include "EgammaAnalysis/ElectronTools/interface/SimpleElectron.h"
#include "EgammaAnalysis/ElectronTools/interface/ElectronEPcombinator.h"
//#include "EgammaAnalysis/ElectronTools/interface/ElectronEnergyCalibrator.h"
Expand All @@ -24,7 +25,12 @@ namespace {
heppy::TriggerBitChecker checker;
heppy::CMGMuonCleanerBySegmentsAlgo cmgMuonCleanerBySegmentsAlgo;
heppy::EGammaMvaEleEstimatorFWLite egMVA;
heppy::Hemisphere hemisphere(std::vector<float> px, std::vector<float> py, std::vector<float> pz, std::vector<float> E, int hemi_seed, int hemi_association);
heppy::Hemisphere hemisphere(std::vector<float> px,
std::vector<float> py,
std::vector<float> pz,
std::vector<float> E, int hemi_seed,
int hemi_association);
heppy::Hemisphere hemisphere_;
heppy::Davismt2 mt2;
heppy::mt2w_bisect::mt2w mt2wlept;
heppy::AlphaT alphaT;
Expand Down
6 changes: 3 additions & 3 deletions PhysicsTools/Heppy/test/example_autofill.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#dump of gen objects
"gentopquarks" : NTupleCollection("GenTop", genParticleType, 2, help="Generated top quarks from hard scattering"),
"genbquarks" : NTupleCollection("GenBQuark", genParticleType, 2, help="Generated bottom quarks from top quark decays"),
"genwzquarks" : NTupleCollection("GenQuark", genParticleWithSourceType, 6, help="Generated quarks from W/Z decays"),
"genleps" : NTupleCollection("GenLep", genParticleWithSourceType, 6, help="Generated leptons from W/Z decays"),
"gentauleps" : NTupleCollection("GenLepFromTau", genParticleWithSourceType, 6, help="Generated leptons from decays of taus from W/Z/h decays"),
"genwzquarks" : NTupleCollection("GenQuark", genParticleType, 6, help="Generated quarks from W/Z decays"),
"genleps" : NTupleCollection("GenLep", genParticleType, 6, help="Generated leptons from W/Z decays"),
"gentauleps" : NTupleCollection("GenLepFromTau", genParticleType, 6, help="Generated leptons from decays of taus from W/Z/h decays"),

}
)
Expand Down
9 changes: 5 additions & 4 deletions PhysicsTools/HeppyCore/python/framework/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ def __init__(self, cfg_ana, cfg_comp, looperName ):
# this is the main logger corresponding to the looper.
# each analyzer could also declare its own logger
self.mainLogger = logging.getLogger( looperName )
# print self.mainLogger.handlers
self.beginLoopCalled = False

def beginLoop(self, setup):
"""Automatically called by Looper, for all analyzers."""
self.counters = Counters()
self.averages = Averages()
self.mainLogger.warning( 'beginLoop ' + self.cfg_ana.name )
self.mainLogger.info( 'beginLoop ' + self.cfg_ana.name )
self.beginLoopCalled = True

def endLoop(self, setup):
"""Automatically called by Looper, for all analyzers."""
#print self.cfg_ana
self.mainLogger.warning( '' )
self.mainLogger.warning( str(self) )
self.mainLogger.warning( '' )
self.mainLogger.info( '' )
self.mainLogger.info( str(self) )
self.mainLogger.info( '' )

def process(self, event ):
"""Automatically called by Looper, for all analyzers.
Expand Down
25 changes: 0 additions & 25 deletions PhysicsTools/HeppyCore/python/framework/anapath.py

This file was deleted.

28 changes: 20 additions & 8 deletions PhysicsTools/HeppyCore/python/framework/chain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,51 @@
import shutil

from PhysicsTools.HeppyCore.framework.chain import Chain
from PhysicsTools.HeppyCore.utils.testtree import create_tree

testfname = 'test_tree.root'

class ChainTestCase(unittest.TestCase):

def setUp(self):
self.file = '../../test/test_tree.root'
self.chain = Chain(self.file, 'test_tree')
self.chain = Chain(testfname, 'test_tree')

def test_file(self):
self.assertTrue(os.path.isfile(self.file))
'''Test that the test file exists'''
self.assertTrue(os.path.isfile(testfname))

def test_guess_name(self):
def test_wrong_filename(self):
self.assertRaises(ValueError,
Chain, 'self.file')
Chain, 'non_existing_file.root')

def test_guess_treename(self):
chain = Chain(testfname)
self.assertEqual(len(self.chain), 100)

def test_load_1(self):
'''Test that the chain has the correct number of entries'''
self.assertEqual(len(self.chain), 100)

def test_load_2(self):
tmpfile = self.file.replace('test_tree', 'test_tree_2_tmp')
shutil.copyfile(self.file, tmpfile)
chain = Chain(self.file.replace('.root', '*.root'), 'test_tree')
'''Test chaining of two files.'''
tmpfile = testfname.replace('test_tree', 'test_tree_2_tmp')
shutil.copyfile(testfname, tmpfile)
chain = Chain(testfname.replace('.root', '*.root'), 'test_tree')
self.assertEqual(len(chain), 200)
os.remove(tmpfile)

def test_iterate(self):
'''Test iteration'''
for ev in self.chain:
pass
self.assertTrue(True)

def test_get(self):
'''Test direct event access'''
event = self.chain[2]
self.assertEqual(event.var1, 2.)


if __name__ == '__main__':
create_tree(testfname)
unittest.main()
32 changes: 0 additions & 32 deletions PhysicsTools/HeppyCore/python/framework/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,3 @@ def __str__(self):
return '\n'.join([comp, sequence, services])


if __name__ == '__main__':

from PhysicsTools.HeppyCore.framework.chain import Chain as Events
from PhysicsTools.HeppyCore.analyzers.Printer import Printer

class Ana1(object):
pass
ana1 = Analyzer(
Ana1,
toto = '1',
tata = 'a'
)
ana2 = Analyzer(
Printer,
'instance1'
)
sequence = Sequence( [ana1, ana2] )

DYJets = MCComponent(
name = 'DYJets',
files ='blah_mc.root',
xSection = 3048.,
nGenEvents = 34915945,
triggers = ['HLT_MC'],
vertexWeight = 1.,
effCorrFactor = 1 )
selectedComponents = [DYJets]
sequence = [ana1, ana2]
config = Config( components = selectedComponents,
sequence = sequence,
events_class = Events )
print config
1 change: 1 addition & 0 deletions PhysicsTools/HeppyCore/python/framework/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Ana1(object):
from PhysicsTools.HeppyCore.framework.chain import Chain as Events
config = Config( components = [comp1],
sequence = [ana1],
services = [],
events_class = Events )


Expand Down
4 changes: 0 additions & 4 deletions PhysicsTools/HeppyCore/python/framework/eventsalbers.py

This file was deleted.

49 changes: 42 additions & 7 deletions PhysicsTools/HeppyCore/python/framework/heppy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def runLoop( comp, outDir, config, options):
config,
options.nevents, 0,
nPrint = options.nprint,
timeReport = options.timeReport)
print loop
timeReport = options.timeReport,
quiet = options.quiet)
# print loop
if options.iEvent is None:
loop.loop()
loop.write()
print loop
# print loop
else:
# loop.InitOutput()
iEvent = int(options.iEvent)
Expand Down Expand Up @@ -121,6 +122,11 @@ def split(comps):
return splitComps


_heppyGlobalOptions = {}

def getHeppyOption(name,default=None):
global _heppyGlobalOptions
return _heppyGlobalOptions[name] if name in _heppyGlobalOptions else default

def main( options, args ):

Expand All @@ -140,13 +146,28 @@ def main( options, args ):
print 'ERROR: second argument must be an existing file (your input cfg).'
sys.exit(3)

if options.verbose:
import logging
logging.basicConfig(level=logging.INFO)

# Propagate global options to _heppyGlobalOptions within this module
# I have to import it explicitly, 'global' does not work since the
# module is not set when executing the main
from PhysicsTools.HeppyCore.framework.heppy import _heppyGlobalOptions
for opt in options.extraOptions:
if "=" in opt:
(key,val) = opt.split("=",1)
_heppyGlobalOptions[key] = val
else:
_heppyGlobalOptions[opt] = True

file = open( cfgFileName, 'r' )
cfg = imp.load_source( 'PhysicsTools.HeppyCore.__cfg_to_run__', cfgFileName, file)

selComps = [comp for comp in cfg.config.components if len(comp.files)>0]
selComps = split(selComps)
for comp in selComps:
print comp
# for comp in selComps:
# print comp
if len(selComps)>10:
print "WARNING: too many threads {tnum}, will just use a maximum of 10.".format(tnum=len(selComps))
if not createOutputDir(outDir, selComps, options.force):
Expand Down Expand Up @@ -208,8 +229,22 @@ def main( options, args ):
action='store_true',
help="Make a report of the time used by each analyzer",
default=False)


parser.add_option("-v", "--verbose",
dest="verbose",
action='store_true',
help="increase the verbosity of the output (from 'warning' to 'info' level)",
default=False)
parser.add_option("-q", "--quiet",
dest="quiet",
action='store_true',
help="do not print log messages to screen.",
default=False)
parser.add_option("-o", "--option",
dest="extraOptions",
type="string",
action="append",
default=[],
help="Save one extra option (either a flag, or a key=value pair) that can be then accessed from the job config file")

(options,args) = parser.parse_args()

Expand Down
Loading

0 comments on commit 71d0757

Please sign in to comment.