ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/prepare_environment_with_config.py
Revision: 1.10
Committed: Wed Oct 3 10:12:27 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.9: +5 -2 lines
Log Message:
trainfalg option

File Contents

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