-
Notifications
You must be signed in to change notification settings - Fork 14
/
MakeISpyAnalyzer.py
executable file
·161 lines (133 loc) · 7 KB
/
MakeISpyAnalyzer.py
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
#! /usr/bin/env python
import sys
import re
import string
source_skeleton = ['#include \"ISpy/Analyzers/interface/ISpyExample.h\"',
'#include \"ISpy/Analyzers/interface/ISpyService.h\"\n',
'#include \"FWCore/Framework/interface/Event.h\"',
'#include \"FWCore/Framework/interface/EventSetup.h\"',
'#include \"FWCore/Framework/interface/ESHandle.h\"',
'#include \"FWCore/Framework/interface/MakerMacros.h\"',
'#include \"FWCore/ParameterSet/interface/ParameterSet.h\"',
'#include \"FWCore/ServiceRegistry/interface/Service.h\"',
'#include \"FWCore/Utilities/interface/Exception.h\"',
'#include \"ISpy/Services/interface/IgCollection.h\"\n',
'using namespace edm::service;',
'using namespace edm;\n',
'ISpyExample::ISpyExample(const edm::ParameterSet& iConfig)',
': inputTag_(iConfig.getParameter<edm::InputTag>(\"iSpyExampleTag\")){}\n',
'void ISpyExample::analyze(const edm::Event& event, const edm::EventSetup& eventSetup)',
'{',
' edm::Service<ISpyService> config;\n',
' if ( ! config.isAvailable() )',
' {',
' throw cms::Exception (\"Configuration\")',
' << \"ISpyExample requires the ISpyService\\n\"',
' \"which is not present in the configuration file.\\n\"',
' \"You must add the service in the configuration file\\n\"',
' \"or remove the module that requires it\";',
' }\n',
' IgDataStorage *storage = config->storage();\n',
'#ifdef EXAMPLE',
' edm::Handle<ExampleCollection> collection;',
' event.getByLabel(inputTag_, collection);\n',
' if ( collection.isValid() )',
' {',
' std::string product = \"Examples \"',
' + edm::TypeID (typeid (ExampleCollection)).friendlyClassName() + \":\"',
' + inputTag_.label() + \":\"',
' + inputTag_.instance() + \":\"',
' + inputTag_.process();\n',
' IgCollection& products = storage->getCollection(\"Products_V1\");',
' IgProperty PROD = products.addProperty(\"Product\", std::string ());',
' IgCollectionItem item = products.create();',
' item[PROD] = product;\n',
' IgCollection& templates = storage->getCollection(\"Examples_V1\");\n',
' IgProperty POS = templates.addProperty(\"position\", IgV3d());',
' IgProperty E = templates.addProperty(\"energy\", 0.0);\n',
' for ( ExampleCollection::const_iterator t = collection->begin(); t != collection->end(); ++t )',
' {',
' IgCollectionItem item = templates.create();\n',
' item[POS] = IgV3d((*t).position().x(),',
' (*t).position().y(),',
' (*t).position().z());\n',
' item[E] = (*t).energy();',
' }',
' }\n',
' else',
' {',
' std::string error = \"### Error: Examples \"',
' + edm::TypeID (typeid (ExampleCollection)).friendlyClassName() + \":\"',
' + inputTag_.label() + \":\"',
' + inputTag_.instance() + \" are not found\";',
' config->error(error);',
' }',
'#endif\n',
'}\n',
'DEFINE_FWK_MODULE(ISpyExample);']
interface_skeleton = ['#ifndef ANALYZER_ISPY_EXAMPLE_H',
'#define ANALYZER_ISPY_EXAMPLE_H\n',
'#include \"FWCore/Framework/interface/EDAnalyzer.h\"\n',
'#include \"FWCore/Utilities/interface/InputTag.h\"\n',
'class ISpyExample : public edm::EDAnalyzer',
'{',
'public:',
' explicit ISpyExample(const edm::ParameterSet&);',
' virtual ~ISpyExample(void){}',
' virtual void analyze(const edm::Event&, const edm::EventSetup&);',
'private:',
' edm::InputTag inputTag_;',
'};',
'#endif // ANALYZER_ISPY_EXAMPLE_H']
cfg_skeleton = ['import FWCore.ParameterSet.Config as cms\n',
'ISpyExample = cms.EDAnalyzer(\'ISpyExample\',',
' iSpyExampleTag = cms.InputTag(\"examples\")',
' )']
def generateSource(class_name):
source_file_name = './src/ISpy'+class_name+'.cc'
try:
source_file = open(source_file_name, 'w')
for line in source_skeleton:
if re.search('Example', line):
line = string.replace(line, 'Example', class_name)
source_file.write(line+'\n')
print 'Wrote', source_file_name
except IOError:
print source_file_name, 'not found'
print 'Check that you have a ./src directory'
sys.exit()
def generateInterface(class_name):
interface_file_name = './interface/ISpy'+class_name+'.h'
try:
interface_file = open(interface_file_name, 'w')
for line in interface_skeleton:
if re.search('_H', line) and re.search('EXAMPLE', line):
line = string.replace(line, 'EXAMPLE', class_name.upper())
if re.search('Example', line):
line = string.replace(line, 'Example', class_name)
interface_file.write(line+'\n')
print 'Wrote', interface_file_name
except IOError:
print interface_file_name, 'not found'
print 'Check that you have a ./interface directory'
sys.exit()
def generateCfg(class_name):
cfg_file_name = './python/ISpy'+class_name+'_cfi.py'
try:
cfg_file = open(cfg_file_name, 'w')
for line in cfg_skeleton:
if re.search('Example', line):
line = string.replace(line, 'Example', class_name)
cfg_file.write(line+'\n')
print 'Wrote', cfg_file_name
except IOError:
print cfg_file_name, 'not found'
print 'Check that you have a ./python directory'
sys.exit()
if len(sys.argv) != 2:
print 'Usage: python MakeISpyAnalyzer.py [\"target\" of analyzer, e.g. EBRecHit]'
sys.exit()
aname = sys.argv[1]
generateSource(aname)
generateInterface(aname)
generateCfg(aname)