ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/prepare_environment_with_config.py
Revision: 1.14
Committed: Wed Dec 5 12:39:03 2012 UTC (12 years, 5 months ago) by bortigno
Content type: text/x-python
Branch: MAIN
Changes since 1.13: +15 -3 lines
Log Message:
added lheWeight for DYJets bins

File Contents

# User Rev Content
1 bortigno 1.12 #! /usr/bin/env python
2     import os, pickle, sys, ROOT
3 nmohr 1.8 from optparse import OptionParser
4 nmohr 1.4 from BetterConfigParser import BetterConfigParser
5 bortigno 1.1
6 peller 1.11 argv = sys.argv
7 bortigno 1.1
8     #get files info from config
9 nmohr 1.8 parser = OptionParser()
10     parser.add_option("-I", "--inPath", dest="pathIn", default="",
11     help="path to the input files")
12     parser.add_option("-O", "--outPath", dest="pathOut", default="",
13     help="path to the output files")
14     parser.add_option("-C", "--config", dest="config", default="",
15 peller 1.9 help="configuration of samples")
16     parser.add_option("-U", "--update", dest="update", default=False, action='store_true',
17     help="append sample to existing samples.info")
18     parser.add_option("-D", "--dry", dest="dry", default=False, action='store_true',
19     help="dry drun - just write samples. info withou copying and skimming trees")
20 peller 1.10 parser.add_option("-S", "--samples", dest="samples", default="",
21     help="List of samples")
22 bortigno 1.14 parser.add_option("-L","--lheList", dest="fileLHEList",default="[]",
23     help="List of lhe weights. Use it if you need to rerun on one DYJets sample alone.")
24 peller 1.9
25 peller 1.10 (opts, args) = parser.parse_args(argv)
26 peller 1.9
27 bortigno 1.12 from copytree import copytree
28     from printcolor import printc
29     from samplesclass import sample
30 bortigno 1.13 from addingSamples import writeLheWeights
31 bortigno 1.12
32 bortigno 1.13 SamplesList=opts.samples.split(',')
33 nmohr 1.8
34     pathIN=opts.pathIn
35     pathOUT=opts.pathOut
36    
37     print "Config is: %s" %(opts.config)
38 nmohr 1.4 config = BetterConfigParser()
39 nmohr 1.8 config.read(opts.config)
40 nmohr 1.3
41     prefix=config.get('General','prefix')
42 nmohr 1.5 newprefix=config.get('General','newprefix')
43 nmohr 1.3 lumi=float(config.get('General','lumi'))
44 nmohr 1.8 weightexpression=config.get('General','weightexpression')
45 bortigno 1.14 fileList=eval(opts.fileLHEList)
46 nmohr 1.8 #this is only to speed it up, remove for final trees!
47     Precut=''
48     info = []
49 nmohr 1.3
50 bortigno 1.1 for Sample in config.sections():
51 nmohr 1.3 if not config.has_option(Sample,'infile'): continue
52 peller 1.10 if not SamplesList == [''] and not config.get(Sample,'sampleName') in SamplesList: continue
53 bortigno 1.1 infile = config.get(Sample,'infile')
54     if not ROOT.TFile.Open(pathIN+prefix+infile+'.root',"READ"):
55     print 'WARNING: No file ' + pathIN+prefix+infile+ ' found! '
56     continue
57 peller 1.6 #this need exception handle
58 nmohr 1.3 #if type(eval(config.get(Sample,'sampleName'))) != list:
59 peller 1.6
60    
61     #Initialize samplecalss element
62     sampleName = config.get(Sample,'sampleName')
63     sampleType = config.get(Sample,'sampleType')
64     cut = config.get(Sample, 'cut')
65     info.append(sample(sampleName,sampleType))
66    
67     info[-1].addtreecut(cut)
68     info[-1].path=pathOUT
69     info[-1].identifier=infile
70     info[-1].weightexpression=weightexpression
71     info[-1].lumi=lumi
72     info[-1].prefix=newprefix
73    
74     if eval(config.get(Sample,'subsamples')):
75     info[-1].subsamples=True
76     info[-1].group = eval((config.get(Sample,'sampleGroup')))
77     info[-1].subcuts = eval((config.get(Sample, 'subcuts')))
78     info[-1].subnames = eval((config.get(Sample, 'subnames')))
79     if sampleType != 'DATA':
80     info[-1].sf = eval((config.get(Sample, 'SF')))
81     info[-1].xsec = eval((config.get(Sample,'xSec')))
82 bortigno 1.1 else:
83 peller 1.6 info[-1].group = config.get(Sample,'sampleGroup')
84     if sampleType != 'DATA':
85     info[-1].sf = config.get(Sample, 'SF')
86     info[-1].xsec = config.get(Sample,'xSec')
87    
88 bortigno 1.13 if not opts.dry:
89     if not 'DY' in sampleName: # for the DY sample we use writeLheConfig
90     copytree(pathIN,pathOUT,prefix,newprefix,infile,'',cut+Precut)
91    
92 bortigno 1.14 #this part take care of DYJets
93 bortigno 1.13 if not opts.dry:
94 bortigno 1.14 for Sample in config.sections():
95     if not config.has_option(Sample,'infile'): continue
96     sampleName = config.get(Sample,'sampleName')
97     infile = config.get(Sample,'infile')
98     print infile
99     if not 'DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball' in infile: continue
100     sampleType = config.get(Sample,'sampleType')
101     cut = config.get(Sample, 'cut')
102    
103     #this copy the trees for the DYJets samples
104     writeLheWeights( config, pathIN, pathOUT, prefix, newprefix, '', cut+Precut, fileList, SamplesList )
105 peller 1.9
106     #dump info
107 bortigno 1.13 pathConfig = 'pathConfig8TeV'
108     print "Path Config is: " + str(pathConfig)
109     pathconfig = BetterConfigParser()
110     pathconfig.read(pathConfig)
111    
112 peller 1.9 if opts.update:
113 bortigno 1.13 infofile = open(pathconfig.get('Directories','samplesinfo'),'r')
114 peller 1.9 info_old = pickle.load(infofile)
115     infofile.close()
116    
117     info += info_old
118    
119 bortigno 1.13 infofile = open(pathconfig.get('Directories','samplesinfo'),'w')
120 bortigno 1.1 pickle.dump(info,infofile)
121     infofile.close()