CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/PhysicsTools/PatAlgos/python/tools/trackTools.py

Go to the documentation of this file.
00001 from FWCore.GuiBrowsers.ConfigToolBase import *
00002 
00003 
00004 class MakeAODTrackCandidates(ConfigToolBase):
00005 
00006     """ Create selected tracks and a candidate hypothesis on AOD:
00007     """
00008     _label='makeAODTrackCandidates'
00009     _defaultParameters=dicttypes.SortedKeysDict()
00010     
00011     def __init__(self):
00012         ConfigToolBase.__init__(self)
00013         self.addParameter(self._defaultParameters,'label','TrackCands', "output collection will be <'patAOD'+label>")
00014         self.addParameter(self._defaultParameters,'tracks',cms.InputTag('generalTracks'), 'input tracks')
00015         self.addParameter(self._defaultParameters,'particleType','pi+', 'particle type (for mass)')
00016         self.addParameter(self._defaultParameters,'candSelection','pt > 10', 'preselection cut on the candidates')
00017         
00018         self._parameters=copy.deepcopy(self._defaultParameters)
00019         self._comment = ""
00020 
00021     def getDefaultParameters(self):
00022         return self._defaultParameters
00023 
00024     def __call__(self,process,
00025                  label         = None,
00026                  tracks        = None,
00027                  particleType  = None,
00028                  candSelection = None) :
00029         if label  is None:
00030             label=self._defaultParameters['label'].value
00031         if  tracks is None:
00032             tracks=self._defaultParameters['tracks'].value
00033         if  particleType is None:
00034             particleType=self._defaultParameters['particleType'].value
00035         if  candSelection is None:
00036             candSelection=self._defaultParameters['candSelection'].value
00037         self.setParameter('label',label)
00038         self.setParameter('tracks',tracks)
00039         self.setParameter('particleType',particleType)
00040         self.setParameter('candSelection',candSelection)
00041         self.apply(process) 
00042         
00043     def toolCode(self, process):             
00044         label=self._parameters['label'].value
00045         tracks=self._parameters['tracks'].value
00046         particleType=self._parameters['particleType'].value
00047         candSelection=self._parameters['candSelection'].value        
00048 
00049         process.load("SimGeneral.HepPDTESSource.pythiapdt_cfi");
00050         ## add ChargedCandidateProducer from track
00051         setattr(process, 'patAOD' + label + 'Unfiltered', cms.EDProducer("ConcreteChargedCandidateProducer",
00052                                                                          src  = tracks,
00053                                                                          particleType = cms.string(particleType)
00054                                                                          )
00055                 )
00056         ## add CandViewSelector with preselection string
00057         setattr(process, 'patAOD' + label, cms.EDFilter("CandViewSelector",
00058                                                         src = cms.InputTag('patAOD' + label + 'Unfiltered'),
00059                                                         cut = cms.string(candSelection)
00060                                                         )
00061                 )
00062         ## run production of TrackCandidates at the very beginning of the sequence
00063         process.patDefaultSequence.replace(process.patCandidates, getattr(process, 'patAOD' + label + 'Unfiltered') * getattr(process, 'patAOD' + label) * process.patCandidates)
00064         
00065 makeAODTrackCandidates=MakeAODTrackCandidates()
00066 
00067 
00068 class MakePATTrackCandidates(ConfigToolBase):
00069 
00070     """ Create pat track candidates from AOD track collections:
00071     """
00072     _label='makePATTrackCandidates'
00073     _defaultParameters=dicttypes.SortedKeysDict()
00074     
00075     def __init__(self):
00076         ConfigToolBase.__init__(self)
00077         self.addParameter(self._defaultParameters,'label','TrackCands', "output will be 'all/selectedLayer1'+label")
00078         self.addParameter(self._defaultParameters,'input',cms.InputTag('patAODTrackCands'), 'name of the input collection')
00079         self.addParameter(self._defaultParameters,'selection','pt > 10', 'selection on PAT Layer 1 objects')
00080         self.addParameter(self._defaultParameters,'isolation',{'tracker':0.3, 'ecalTowers':0.3, 'hcalTowers':0.3}, "solation to use (as 'source': value of dR)\ntracker     : as muon iso from tracks\necalTowers  : as muon iso from calo tower\nhcalTowers  : as muon iso from calo towers",allowedValues=['tracker','ecalTowers','hcalTowers'])
00081         self.addParameter(self._defaultParameters,'isoDeposits',['tracker','ecalTowers','hcalTowers'], 'iso deposits')
00082         self.addParameter(self._defaultParameters,'mcAs',None, "eplicate mc match as the one used by PAT on this AOD collection (None=no mc match); choose 'photon', 'electron', 'muon', 'tau','jet', 'met' as input string",Type=str, allowedValues=['photon', 'electron', 'muon', 'tau','jet', 'met', None], acceptNoneValue = True) 
00083         
00084         self._parameters=copy.deepcopy(self._defaultParameters)
00085         self._comment = ""
00086 
00087     def getDefaultParameters(self):
00088         return self._defaultParameters
00089 
00090     def __call__(self,process,
00091                  label       = None,
00092                  input       = None,
00093                  selection   = None,
00094                  isolation   = None,
00095                  isoDeposits = None,
00096                  mcAs        = None) :
00097         if label  is None:
00098             label=self._defaultParameters['label'].value
00099         if input is None:
00100             input=self._defaultParameters['input'].value
00101         if selection is None:
00102             selection=self._defaultParameters['selection'].value
00103         if isolation is None:
00104             isolation=self._defaultParameters['isolation'].value
00105         if isoDeposits is None:
00106             isoDeposits=self._defaultParameters['isoDeposits'].value
00107         if mcAs is None:
00108             mcAs=self._defaultParameters['mcAs'].value    
00109         self.setParameter('label',label)
00110         self.setParameter('input',input)
00111         self.setParameter('selection',selection)
00112         self.setParameter('isolation',isolation)
00113         self.setParameter('isoDeposits',isoDeposits)
00114         self.setParameter('mcAs',mcAs,True)                                                                                              
00115         self.apply(process) 
00116         
00117     def toolCode(self, process):                
00118         label=self._parameters['label'].value
00119         input=self._parameters['input'].value
00120         selection=self._parameters['selection'].value
00121         isolation=self._parameters['isolation'].value
00122         isoDeposits=self._parameters['isoDeposits'].value
00123         mcAs=self._parameters['mcAs'].value
00124                                                 
00125         ## add patTracks to the process
00126         from PhysicsTools.PatAlgos.producersLayer1.genericParticleProducer_cfi import patGenericParticles
00127         setattr(process, 'pat' + label, patGenericParticles.clone(src = input))
00128         ## add selectedPatTracks to the process
00129         setattr(process, 'selectedPat' + label, cms.EDFilter("PATGenericParticleSelector",
00130                                                              src = cms.InputTag("pat"+label),
00131                                                              cut = cms.string(selection) 
00132                                                              ) 
00133                 )
00134         ## add cleanPatTracks to the process
00135         from PhysicsTools.PatAlgos.cleaningLayer1.genericTrackCleaner_cfi import cleanPatTracks
00136         setattr(process, 'cleanPat' + label, cleanPatTracks.clone(src = cms.InputTag('selectedPat' + label)))
00137         
00138         ## get them as variables, so we can put them in the sequences and/or configure them
00139         l1cands         = getattr(process, 'pat' + label)
00140         selectedL1cands = getattr(process, 'selectedPat' + label)
00141         cleanL1cands    = getattr(process, 'cleanPat' + label)
00142         
00143         ## insert them in sequence, after the electrons
00144         process.patCandidates.replace(process.patElectrons, l1cands + process.patElectrons)
00145         process.selectedPatCandidates.replace(process.selectedPatElectrons, process.selectedPatElectrons + selectedL1cands)
00146         process.cleanPatCandidates.replace(process.cleanPatElectrons, process.cleanPatElectrons + cleanL1cands)
00147     
00148         ## add them to the Summary Tables
00149         process.patCandidateSummary.candidates += [ cms.InputTag("allPat"+label) ]
00150         process.selectedPatCandidateSummary.candidates += [ cms.InputTag("selectedPat"+label) ]
00151         process.cleanPatCandidateSummary.candidates += [ cms.InputTag("cleanPat"+label) ]
00152     
00153         ## isolation: start with empty config
00154         if(isolation or isoDeposits):
00155             process.load("TrackPropagation.SteppingHelixPropagator.SteppingHelixPropagatorAlong_cfi")
00156             process.load("TrackPropagation.SteppingHelixPropagator.SteppingHelixPropagatorOpposite_cfi")
00157             process.load("TrackPropagation.SteppingHelixPropagator.SteppingHelixPropagatorAny_cfi")
00158         isoModules = []
00159         runIsoDeps = {'tracker':False, 'caloTowers':False}
00160     
00161         for source,deltaR in isolation.items():
00162             ## loop items in isolation
00163             if(source == 'tracker'):
00164                 runIsoDeps['tracker'] = True
00165                 l1cands.userIsolation.tracker = cms.PSet(
00166                     src    = cms.InputTag('pat'+label+'IsoDepositTracks'),
00167                     deltaR = cms.double(deltaR),
00168                     )
00169             elif(source == 'ecalTowers'):
00170                 runIsoDeps['caloTowers'] = True
00171                 l1cands.userIsolation.ecal = cms.PSet(
00172                     src    = cms.InputTag('pat'+label+'IsoDepositCaloTowers', 'ecal'),
00173                     deltaR = cms.double(deltaR),
00174                     )
00175             elif(source == 'hcalTowers'):
00176                 runIsoDeps['caloTowers'] = True
00177                 l1cands.userIsolation.hcal = cms.PSet(
00178                     src    = cms.InputTag('pat'+label+'IsoDepositCaloTowers', 'hcal'),
00179                     deltaR = cms.double(deltaR),
00180                     )
00181             
00182         for source in isoDeposits:
00183             ## loop items in isoDeposits
00184             if(source == 'tracker'):
00185                 runIsoDeps['tracker'] = True
00186                 l1cands.isoDeposits.tracker = cms.InputTag('pat'+label+'IsoDepositTracks') 
00187             elif(source == 'ecalTowers'):
00188                 runIsoDeps['caloTowers'] = True
00189                 l1cands.isoDeposits.ecal = cms.InputTag('pat'+label+'IsoDepositCaloTowers', 'ecal') 
00190             elif(source == 'hcalTowers'):
00191                 runIsoDeps['caloTowers'] = True
00192                 l1cands.isoDeposits.hcal = cms.InputTag('pat'+label+'IsoDepositCaloTowers', 'hcal')
00193             
00194         for dep in [ dep for dep,runme in runIsoDeps.items() if runme == True ]:
00195             if(dep == 'tracker'):
00196                 from RecoMuon.MuonIsolationProducers.trackExtractorBlocks_cff import MIsoTrackExtractorCtfBlock
00197                 setattr(process, 'pat'+label+'IsoDepositTracks',
00198                         cms.EDProducer("CandIsoDepositProducer",
00199                                        src                  = input,
00200                                        trackType            = cms.string('best'),
00201                                        MultipleDepositsFlag = cms.bool(False),
00202                                        ExtractorPSet        = cms.PSet( MIsoTrackExtractorCtfBlock )
00203                                        )
00204                         )
00205                 isoModules.append( getattr(process, 'pat'+label+'IsoDepositTracks') )
00206             elif(dep == 'caloTowers'):
00207                 from RecoMuon.MuonIsolationProducers.caloExtractorByAssociatorBlocks_cff import MIsoCaloExtractorByAssociatorTowersBlock
00208                 setattr(process, 'pat'+label+'IsoDepositCaloTowers',
00209                         cms.EDProducer("CandIsoDepositProducer",
00210                                        src                  = input,
00211                                        trackType            = cms.string('best'),
00212                                        MultipleDepositsFlag = cms.bool(True),
00213                                        ExtractorPSet        = cms.PSet( MIsoCaloExtractorByAssociatorTowersBlock )
00214                                        )
00215                         )
00216                 isoModules.append( getattr(process, 'pat'+label+'IsoDepositCaloTowers') )
00217         for m in isoModules:
00218             process.patDefaultSequence.replace(l1cands, m * l1cands)
00219         # ES
00220         process.load( 'TrackingTools.TrackAssociator.DetIdAssociatorESProducer_cff' ) 
00221         # MC
00222         from PhysicsTools.PatAlgos.tools.helpers import MassSearchParamVisitor
00223         if(type(mcAs) != type(None)):
00224             findMatch= []
00225             findMatch.append(getattr(process, mcAs+'Match'))
00226             
00227             ## clone mc matchiong module of object mcAs and add it to the path
00228             setattr(process, 'pat'+label+'MCMatch', findMatch[0].clone(src = input))
00229             process.patDefaultSequence.replace( l1cands, getattr(process, 'pat'+label+'MCMatch') * l1cands)
00230             l1cands.addGenMatch = True
00231             l1cands.genParticleMatch = cms.InputTag('pat'+label+'MCMatch')
00232 
00233       
00234 makePATTrackCandidates=MakePATTrackCandidates()
00235 
00236 
00237 class MakeTrackCandidates(ConfigToolBase):
00238     """ Create selected tracks and a candidate hypothesis on AOD:
00239     """
00240     _label='makeTrackCandidates'
00241     _defaultParameters=dicttypes.SortedKeysDict()
00242     
00243     def __init__(self):
00244         ConfigToolBase.__init__(self)
00245         self.addParameter(self._defaultParameters,'label','TrackCands', "output collection will be <'patAOD'+label>")
00246         self.addParameter(self._defaultParameters,'tracks',cms.InputTag('generalTracks'), 'input tracks')
00247         self.addParameter(self._defaultParameters,'particleType','pi+', 'particle type (for mass)')
00248         self.addParameter(self._defaultParameters,'preselection','pt > 10', 'preselection cut on the AOD candidates')
00249         self.addParameter(self._defaultParameters,'selection','pt > 10', 'selection cut on the PAT candidates (for the selectedLayer1Candidate collection)')
00250         self.addParameter(self._defaultParameters,'isolation',{'tracker':0.3, 'ecalTowers':0.3, 'hcalTowers':0.3}, "isolation to use (as 'source': value of dR)\ntracker     : as muon iso from tracks\necalTowers  : as muon iso from calo tower\nhcalTowers  : as muon iso from calo towers",allowedValues=['tracker','ecalTowers','hcalTowers'])
00251         self.addParameter(self._defaultParameters,'isoDeposits',['tracker','ecalTowers','hcalTowers'], 'iso deposits')
00252         self.addParameter(self._defaultParameters,'mcAs',None, "eplicate mc match as the one used by PAT on this AOD collection (None=no mc match); choose 'photon', 'electron', 'muon', 'tau','jet', 'met' as input string",Type=str,allowedValues=['photon', 'electron', 'muon', 'tau','jet', 'met', None], acceptNoneValue = True)
00253         
00254         self._parameters=copy.deepcopy(self._defaultParameters)
00255         self._comment = ""
00256 
00257     def getDefaultParameters(self):
00258         return self._defaultParameters
00259 
00260     def __call__(self,process,
00261                  label        = None,
00262                  tracks       = None,
00263                  particleType = None,
00264                  preselection = None,
00265                  selection    = None,
00266                  isolation    = None,
00267                  isoDeposits  = None,
00268                  mcAs         = None) :
00269         if label  is None:
00270             label=self._defaultParameters['label'].value
00271         if tracks is None:
00272             tracks=self._defaultParameters['tracks'].value
00273         if particleType is None:
00274             particleType=self._defaultParameters['particleType'].value
00275         if preselection is None:
00276             preselection=self._defaultParameters['preselection'].value
00277         if selection is None:
00278             selection=self._defaultParameters['selection'].value
00279         if isolation is None:
00280             isolation=self._defaultParameters['isolation'].value
00281         if isoDeposits is None:
00282             isoDeposits=self._defaultParameters['isoDeposits'].value
00283         if mcAs is None:
00284             mcAs=self._defaultParameters['mcAs'].value  
00285         self.setParameter('label',label)
00286         self.setParameter('tracks',tracks)
00287         self.setParameter('particleType',particleType)
00288         self.setParameter('preselection',preselection)
00289         self.setParameter('selection',selection)
00290         self.setParameter('isolation',isolation)
00291         self.setParameter('isoDeposits',isoDeposits)
00292         self.setParameter('mcAs',mcAs,True)                                                                                              
00293         self.apply(process) 
00294         
00295     def toolCode(self, process):                
00296         label=self._parameters['label'].value
00297         tracks=self._parameters['tracks'].value
00298         particleType=self._parameters['particleType'].value
00299         preselection=self._parameters['preselection'].value
00300         selection=self._parameters['selection'].value
00301         isolation=self._parameters['isolation'].value
00302         isoDeposits=self._parameters['isoDeposits'].value
00303         mcAs=self._parameters['mcAs'].value
00304         
00305         makeAODTrackCandidates(process,
00306                                tracks        = tracks,
00307                                particleType  = particleType,
00308                                candSelection = preselection,
00309                                label         = label
00310                                ) 
00311         makePATTrackCandidates(process,
00312                                label         = label,
00313                                input         = cms.InputTag('patAOD' + label), 
00314                                isolation     = isolation,
00315                                isoDeposits   = isoDeposits,
00316                                mcAs          = mcAs,
00317                                selection     = selection
00318                                )
00319     
00320 makeTrackCandidates=MakeTrackCandidates()