-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateNewAnalysis.py
More file actions
executable file
·191 lines (138 loc) · 7.72 KB
/
createNewAnalysis.py
File metadata and controls
executable file
·191 lines (138 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#! /usr/bin/env python
import os, sys
import subprocess
import time
import argparse
from utils import *
def main():
parser = argparse.ArgumentParser(description='Create all necessary files for a brand-new analysis.')
parser.add_argument('name', metavar='name', type=str, nargs=1,
help='The name of the new analysis, choose wisely, you might have to stick with it for a longer time!')
args = parser.parse_args()
name = args.name[0]
print green('--> Creating new analysis setup for analysis: %s' % (name))
leafpath = os.environ['LEAFPATH']
# create main folder and Analyzer/Plotter/Generator/Tuplizer subfolders
# ensureDirectory(os.path.join(leafpath, name, 'Analyzer'))
ensureDirectory(os.path.join(leafpath, name, 'Plotter'))
ensureDirectory(os.path.join(leafpath, name, 'Generator'))
ensureDirectory(os.path.join(leafpath, name, 'Tuplizer'))
# create necessary subfolders
subfolders = ['include', 'src', 'lib', 'obj', 'config']
for folder in subfolders:
ensureDirectory('%s' % (os.path.join(leafpath, name, folder)))
# define placeholders and desired replacements
placeholder_dict = {
'$MYANALYSISNAME': name,
'$MYEVENTCLASS' : '%sEvent' % (name),
'$MYTOOLNAME' : '%sTool' % (name),
'$MYHISTNAME' : '%sHists' % (name),
'$MYUSERNAME' : os.environ['USER'],
'$MYANALYZERPATH': os.environ['ANALYZERPATH'],
'$MYLEAFPATH': os.environ['LEAFPATH']
}
# create makefile
createNewMakefileAndBuildfile(leafpath, name, placeholder_dict)
# create all new header files
createNewIncludes(leafpath, name, placeholder_dict)
# create all new source files
createNewSources(leafpath, name, placeholder_dict)
# create all new source files
createNewConfigs(leafpath, name, placeholder_dict)
# create all new source files
createTestSampleXMLs(leafpath, name, placeholder_dict)
# create all new source files
createNewPostAnalyzer(leafpath, name, placeholder_dict)
# create all new source files
createNewPlotterXMLs(leafpath, name, placeholder_dict)
# update Makefile.local
updateMakefileLocal(leafpath, name)
def createNewMakefileAndBuildfile(basefolder, name, placeholders):
command = 'cp templates/Makefile_template %s/Makefile' % (os.path.join(basefolder, name))
os.system(command)
command = 'cp templates/BuildFile_template.xml %s/BuildFile.xml' % (os.path.join(basefolder, name))
os.system(command)
replace_placeholders('%s/Makefile' % (os.path.join(basefolder, name)), placeholders)
replace_placeholders('%s/BuildFile.xml' % (os.path.join(basefolder, name)), placeholders)
def createNewIncludes(basefolder, name, placeholders):
command = 'cp templates/Linkdef_template.hpp %s/include/%sClasses_Linkdef.hpp' % (os.path.join(basefolder, name), name)
os.system(command)
command = 'cp templates/Event_template.h %s/include/%s.h' % (os.path.join(basefolder, name), placeholders['$MYEVENTCLASS'])
os.system(command)
command = 'cp templates/Hists_template.h %s/include/%s.h' % (os.path.join(basefolder, name), placeholders['$MYHISTNAME'])
os.system(command)
replace_placeholders('%s/include/%sClasses_Linkdef.hpp' % (os.path.join(basefolder, name), name), placeholders)
replace_placeholders('%s/include/%s.h' % (os.path.join(basefolder, name), placeholders['$MYEVENTCLASS']), placeholders)
replace_placeholders('%s/include/%s.h' % (os.path.join(basefolder, name), placeholders['$MYHISTNAME']), placeholders)
def createNewSources(basefolder, name, placeholders):
command = 'cp templates/Event_template.cc %s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYEVENTCLASS'])
os.system(command)
command = 'cp templates/Tool_template.cc %s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYTOOLNAME'])
os.system(command)
command = 'cp templates/Hists_template.cc %s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYHISTNAME'])
os.system(command)
replace_placeholders('%s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYEVENTCLASS']), placeholders)
replace_placeholders('%s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYTOOLNAME']), placeholders)
replace_placeholders('%s/src/%s.cc' % (os.path.join(basefolder, name), placeholders['$MYHISTNAME']), placeholders)
def createNewConfigs(basefolder, name, placeholders):
command = 'cp templates/config_template.xml %s/config/%s.xml' % (os.path.join(basefolder, name), placeholders['$MYANALYSISNAME'])
os.system(command)
command = 'cp templates/Configuration.dtd %s/config/' % (os.path.join(basefolder, name))
os.system(command)
replace_placeholders('%s/config/%s.xml' % (os.path.join(basefolder, name), placeholders['$MYANALYSISNAME']), placeholders)
def createTestSampleXMLs(basefolder, name, placeholders):
command = 'cp templates/TestData.xml %s/.testsamples/' % (os.environ['LEAFPATH'])
os.system(command)
command = 'cp templates/TestBackground.xml %s/.testsamples/' % (os.environ['LEAFPATH'])
os.system(command)
command = 'cp templates/TestSignal_M1000.xml %s/.testsamples/' % (os.environ['LEAFPATH'])
os.system(command)
command = 'cp templates/TestSignal_M2000.xml %s/.testsamples/' % (os.environ['LEAFPATH'])
os.system(command)
replace_placeholders('%s/.testsamples/TestData.xml' % (os.environ['LEAFPATH']), placeholders)
replace_placeholders('%s/.testsamples/TestBackground.xml' % (os.environ['LEAFPATH']), placeholders)
replace_placeholders('%s/.testsamples/TestSignal_M1000.xml' % (os.environ['LEAFPATH']), placeholders)
replace_placeholders('%s/.testsamples/TestSignal_M2000.xml' % (os.environ['LEAFPATH']), placeholders)
def createNewPostAnalyzer(basefolder, name, placeholders):
command = 'cp -r templates/PostAnalyzerTemplate %s/PostAnalyzer' % (os.path.join(basefolder, name))
os.system(command)
replace_placeholders('%s/PostAnalyzer/steer.py' % (os.path.join(basefolder, name)), placeholders)
def createNewPlotterXMLs(basefolder, name, placeholders):
ensureDirectory(os.path.join(basefolder, name, 'Plotter'))
command = 'cp templates/PlotterXML/* %s/' % (os.path.join(basefolder, name, 'Plotter'))
os.system(command)
command = 'mv %s/Template.xml %s/Default.xml' % (os.path.join(basefolder, name, 'Plotter'), os.path.join(basefolder, name, 'Plotter'))
os.system(command)
replace_placeholders('%s/Default.xml' % (os.path.join(basefolder, name, 'Plotter')), placeholders)
def updateMakefileLocal(basefolder, name):
need_to_update = True
newlines = []
fullmakefilepath = os.path.join(basefolder, 'Analyzer', 'Makefile.local')
if os.path.isfile(fullmakefilepath):
with open(fullmakefilepath, 'r') as f:
newlines = f.readlines()
if any(('subdirs := %s' % (name) in l or 'subdirs += %s' % (name) in l) for l in newlines):
need_to_update = False
if need_to_update:
symbol = ':=' if len(newlines) == 0 else '+='
newlines.append('subdirs '+symbol+' %s\n' % (name))
with open(fullmakefilepath, 'w') as f:
for l in newlines:
f.write(l)
def replace_placeholders(filename, placeholder_dict):
newlines = []
with open(filename, 'r') as f:
lines = f.readlines()
newlines = []
for line in lines:
newline = line
for key in placeholder_dict.keys():
newline = newline.replace(key, placeholder_dict[key])
newlines.append(newline)
create_new_file_with_lines(filename, newlines)
def create_new_file_with_lines(filename, lines):
with open(filename, 'w') as f:
for l in lines:
f.write(l)
if __name__ == '__main__':
main()