CMS 3D CMS Logo

MVASteering_cfi.py
Go to the documentation of this file.
1 """
2  MVASteering.py
3  Author: Evan K. Friis, UC Davis (friis@physics.ucdavis.edu)
4 
5  Define the MVA configurations (ie TaNC) to be used in training/testing
6  - which neural net
7  - which algorithms (shrinkingConePFTauDecayModeProducer, etc)
8  Define locations of train/test ROOT files
9 """
10 from __future__ import print_function
11 
12 import sys
13 import six
14 import os
15 # Get CMSSW base
16 try:
17  Project_Area = os.environ["CMSSW_BASE"]
18 except KeyError:
19  print("$CMSSW_BASE enviroment variable not set! Please run eval `scramv1 ru -[c]sh`")
20  sys.exit(1)
21 
22 import FWCore.ParameterSet.Config as cms
23 import glob
24 
25 # Get defintions (w/ decay mode mapping) from the python defintion file
27 
28 ####### USER PARAMETERS #######################################
29 #################################################################
30 
31 # Define lists of neural nets corresponding to a total configuration
32 # The Neural net objects used here (SingleNet, OneProngNoPiZero, etc) must be defined in
33 # the TauMVAConfigurations_cfi.py found in ../python
34 MVACollections = {}
35 
36 
37 # Use the Tau neural classifier configuration
38 MVACollections['TaNC'] = TaNC.value() # <--- defined in TauMVAConfigurations_cfi.py
39 
40 # non isolated, single net only
41 # MVACollections['SingleNet'] = SingleNetBasedTauID.value()
42 
43 # isolation applied, neural net for each decay mode
44 #MVACollections['MultiNetIso'] = MultiNetIso.value()
45 
46 # isolation applied, single neural net
47 #MVACollections['SingleNetIso'] = [SingleNetIso]
48 
49 # For training/evaluating on an isolated sample, define the isolated criteria here
50 IsolationCutForTraining = "Alt$(ChargedOutlierPt[0], 0) < 1.0 && Alt$(NeutralOutlierPt[0], 0) < 1.5" #no tracks above 1 GeV, no gammas above 1.5 GeV
51 
52 #Define the PFRecoTauDecayMode source to use (in the case of more than one separate directories will be created for each training sample)
53 myTauAlgorithms = ["shrinkingConePFTauDecayModeProducer"]
54 
55 # If true, output will be weighted such that the signal and background distributions have the same
56 # Pt-Eta distribution. To generate the weights file, run python BuildWeights.py
57 #UseWeights = True
58 UseWeights = True
59 
60 # If this is true, the weighted pt-eta distributions for each decay mode will be the same.
61 # otherwise, the pt-eta distribution of the entire training sample (post DM preselection)
62 # will be used.
63 WeightByIndividualDecayMode = False
64 
65 # If true, single isolatated charged pions and three prongs
66 # with charge +-3 will not be included in the training sample
67 ExcludePrepassAndPrefail = False
68 
69 # If greater than zero, require that either the lead track or lead pion have pt greater
70 # than the value supplied
71 RequireLeadPionPt = 5.0
72 LeadPionRequirementString = "( (MainTrackPt > %f && MainTrackAngle < 0.1) || (Alt$(TrackPt[0],0) > %f && Alt$(TrackAngle[0], 20) < 0.1) || (Alt$(PiZeroPt[0], 0) > %f && Alt$(PiZeroAngle[0], 20) < 0.1) )" % (RequireLeadPionPt,RequireLeadPionPt,RequireLeadPionPt)
73 
74 """
75 Example of multiple algorithms
76 myTauAlgorithms = ["pfTauDecayModeHighEfficiency",
77  "pfTauDecayModeInsideOut"]
78 """
79 
80 # define locations of signal/background root files
81 TauTagToolsWorkingDirectory = os.path.join(Project_Area, "src/RecoTauTag/TauTagTools")
82 SignalRootDir = os.path.join(TauTagToolsWorkingDirectory, "test", "ztt")
83 BackgroundRootDir = os.path.join(TauTagToolsWorkingDirectory, "test", "qcd")
84 
85 #Globs to get files for training and evaluation. If you want to ensure different sets, you can do something like
86 # add a requirement such as *[0123].root for training and *[4].root. (files not ending in four used for trianing, ending in four used for testing)
87 SignalFileTrainingGlob = "%s/*[012356789].root" % SignalRootDir
88 BackgroundFileTrainingGlob = "%s/*[012356789].root" % BackgroundRootDir
89 #SignalFileTrainingGlob = "%s/*[0].root" % SignalRootDir
90 #BackgroundFileTrainingGlob = "%s/*[0].root" % BackgroundRootDir
91 
92 SignalFileTestingGlob = "%s/*4.root" % SignalRootDir
93 BackgroundFileTestingGlob = "%s/*4.root" % BackgroundRootDir
94 
95 #################################################################
96 ##### DO NOT MODIFY BELOW THIS LINE (experts only) #############
97 #################################################################
98 
99 def GetTrainingFile(computerName, anAlgo):
100  return os.path.join(TauTagToolsWorkingDirectory, "test", "TrainDir_%s_%s" % (computerName, anAlgo), "%s.mva" % computerName)
101 
102 #Find the unique mva types to train
103 listOfMVANames = {}
104 for name, mvaCollection in six.iteritems(MVACollections):
105  for _mva in mvaCollection:
106  name = _mva.computerName.value()
107  if not name in listOfMVANames:
108  listOfMVANames[name] = _mva
109 
110 myModules = []
111 for name, _mva in six.iteritems(listOfMVANames):
112  myModules.append(_mva)
113 
114 SignalTrainFiles = glob.glob(SignalFileTrainingGlob)
115 BackgroundTrainFiles = glob.glob(BackgroundFileTrainingGlob)
116 
117 SignalTestingFiles = glob.glob(SignalFileTestingGlob)
118 BackgroundTestingFiles = glob.glob(BackgroundFileTestingGlob)
119 
120 # Catch dumb errors before we begin
122  if not len(SignalTrainFiles) or not len(BackgroundTrainFiles) or not len(SignalTestingFiles) or not len(BackgroundTestingFiles):
123  raise IOError("The signal/background root file training/testing file list is empty! Check the SignalFileTrainingGlob etc. in MVASteering.py")
124 
125  # Ensure that we have all the necessary XML files
126  for aModule in myModules:
127  computerName = aModule.computerName.value() #conver to python string
128  xmlFileLoc = os.path.join(TauTagToolsWorkingDirectory, "xml", "%s.xml" % computerName)
129  if not os.path.exists(xmlFileLoc):
130  raise IOError("Can't find xml configuration file for %s - please check that %s exists!" % (computerName, xmlFileLoc))
131 
132  if not os.path.exists(SignalRootDir):
133  raise IOError("Signal root file directory (%s) does not exist! Have you created the MVA raw training data?" % SignalRootDir)
134  if not os.path.exists(BackgroundRootDir):
135  raise IOError("Background root file directory (%s) does not exist! Have you created the MVA raw training data?" % BackgroundRootDir)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def GetTrainingFile(computerName, anAlgo)
DO NOT MODIFY BELOW THIS LINE (experts only) #############.
def EverythingInItsRightPlace()