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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#include <string>
#include <fstream>
#include <map>
#include <iostream>
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/EventPrincipal.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "TFile.h"
#include "TString.h"
#include "TChain.h"
#include "TStopwatch.h"
class TkAlCaSkimTreeMerger : public edm::one::EDAnalyzer<> {
public:
TkAlCaSkimTreeMerger(const edm::ParameterSet &iConfig);
~TkAlCaSkimTreeMerger() override;
void beginJob() override;
void endJob() override;
void analyze(const edm::Event &, const edm::EventSetup &) override;
static void fillDescriptions(edm::ConfigurationDescriptions &);
private:
TTree *out_; //TTree containing the merged result
TTree *firsttree_; //first tree of the list; this gives the structure to all the others
TChain *ch_; //chain containing all the tree you want to merge
std::string filelist_; //text file containing the list of input files
std::string firstfilename_;
std::string treename_; //name of the tree you want to merge (contained in the file)
std::string outfilename_; //name of the file where you want to save the output
//Hit Population
typedef std::map<uint32_t, uint32_t> DetHitMap;
DetHitMap hitmap_;
DetHitMap overlapmap_;
int maxhits_; //above this number, the hit population is prescaled. Configurable for each subdet
edm::ParameterSet maxhitsSet_;
int maxPXBhits_, maxPXFhits_, maxTIBhits_, maxTIDhits_, maxTOBhits_, maxTEChits_;
TStopwatch myclock;
};
TkAlCaSkimTreeMerger::TkAlCaSkimTreeMerger(const edm::ParameterSet &iConfig)
: filelist_(iConfig.getParameter<std::string>("FileList")),
treename_(iConfig.getParameter<std::string>("TreeName")),
outfilename_(iConfig.getParameter<std::string>("OutputFile")),
maxhits_(iConfig.getParameter<int32_t>("NhitsMaxLimit")),
maxhitsSet_(iConfig.getParameter<edm::ParameterSet>("NhitsMaxSet")) {
maxPXBhits_ = maxhitsSet_.getParameter<int32_t>("PXBmaxhits");
maxPXFhits_ = maxhitsSet_.getParameter<int32_t>("PXFmaxhits");
maxTIBhits_ = maxhitsSet_.getParameter<int32_t>("TIBmaxhits");
maxTIDhits_ = maxhitsSet_.getParameter<int32_t>("TIDmaxhits");
maxTOBhits_ = maxhitsSet_.getParameter<int32_t>("TOBmaxhits");
maxTEChits_ = maxhitsSet_.getParameter<int32_t>("TECmaxhits");
//anything you want to do for initializing
LogDebug("TkAlCaSkimTreeMerger") << "\n\n*** MAX N HITS = " << maxhits_ << std::endl << std::endl;
out_ = nullptr;
firsttree_ = nullptr;
ch_ = nullptr;
}
TkAlCaSkimTreeMerger::~TkAlCaSkimTreeMerger() {
delete ch_;
LogDebug("TkAlCaSkimTreeMerger") << "finished." << std::endl;
}
// ------------ method called before analyzing the first event ------------
void TkAlCaSkimTreeMerger::beginJob() {
myclock.Start();
//prepare the chain
ch_ = new TChain(treename_.c_str());
LogDebug("TkAlCaSkimTreeMerger") << "The chain contains " << ch_->GetNtrees() << " trees" << std::endl;
//load the trees into the chain
std::ifstream flist(filelist_.c_str(), std::ios::in);
std::string filename;
std::string firstfilename;
bool first = true;
while (!flist.eof()) {
filename = "";
flist >> filename;
if (filename.empty())
continue;
LogDebug("TkAlCaSkimTreeMerger") << "Adding " << filename << std::endl;
ch_->Add(filename.c_str());
if (first) {
firstfilename_ = filename;
first = false;
}
}
LogDebug("TkAlCaSkimTreeMerger") << "Now the chain contains " << ch_->GetNtrees() << " trees (" << ch_->GetEntries()
<< " entries)" << std::endl;
unsigned int id_ch = 0;
uint32_t nhits_ch = 0, noverlaps_ch = 0;
ch_->SetBranchAddress("DetId", &id_ch);
ch_->SetBranchAddress("Nhits", &nhits_ch);
ch_->SetBranchAddress("Noverlaps", &noverlaps_ch);
ch_->SetBranchStatus("SubDet", false);
ch_->SetBranchStatus("Layer", false);
ch_->SetBranchStatus("is2D", false);
ch_->SetBranchStatus("isStereo", false);
ch_->SetBranchStatus("posX", false);
ch_->SetBranchStatus("posY", false);
ch_->SetBranchStatus("posZ", false);
ch_->SetBranchStatus("posR", false);
ch_->SetBranchStatus("posEta", false);
ch_->SetBranchStatus("posPhi", false); //now only id, nhits and noverlaps are on...
int totnhits(0), totnoverlaps(0);
//look if you find this detid in the map
DetHitMap::iterator mapiter;
DetHitMap::iterator overlapiter;
for (int ent = 0; ent < ch_->GetEntries(); ++ent) {
// for(int ent=0;ent<100;++ent){
ch_->GetEntry(ent);
totnhits += nhits_ch;
totnoverlaps += noverlaps_ch;
mapiter = hitmap_.find(id_ch);
if (mapiter != hitmap_.end()) { //present, increase its value
hitmap_[id_ch] = hitmap_[id_ch] + nhits_ch;
} else { //not present, let's add this key to the map with value=1
hitmap_.insert(std::pair<uint32_t, uint32_t>(id_ch, nhits_ch));
}
//do the same for overlaps
overlapiter = overlapmap_.find(id_ch);
if (overlapiter != overlapmap_.end()) {
overlapmap_[id_ch] = overlapmap_[id_ch] + noverlaps_ch;
} else {
overlapmap_.insert(std::pair<uint32_t, uint32_t>(id_ch, noverlaps_ch));
}
} //end loop on ent - entries in the chain
LogDebug("TkAlCaSkimTreeMerger") << "Nhits in the chain: " << totnhits << std::endl;
LogDebug("TkAlCaSkimTreeMerger") << "NOverlaps in the chain: " << totnoverlaps << std::endl;
myclock.Stop();
LogDebug("TkAlCaSkimTreeMerger") << "Finished beginJob after " << myclock.RealTime() << " s (real time) / "
<< myclock.CpuTime() << " s (cpu time)" << std::endl;
myclock.Continue();
} //end beginJob
// ------------ method called to for each event ------------
void TkAlCaSkimTreeMerger::analyze(const edm::Event &, const edm::EventSetup &) {
LogDebug("TkAlCaSkimTreeMerger") << firsttree_->GetEntries() << std::endl;
} //end analyze
// ------------ method called after having analyzed all the events ------------
void TkAlCaSkimTreeMerger::endJob() {
//address variables in the first tree and in the chain
TFile *firstfile = new TFile(firstfilename_.c_str(), "READ");
firsttree_ = (TTree *)firstfile->Get(treename_.c_str());
LogDebug("TkAlCaSkimTreeMerger") << "the first tree has " << firsttree_->GetEntries() << " entries" << std::endl;
unsigned int id = 0;
uint32_t nhits = 0, noverlaps = 0;
float posX(-99999.0), posY(-77777.0), posZ(-88888.0);
float posEta(-6666.0), posPhi(-5555.0), posR(-4444.0);
int subdet = 0;
unsigned int layer = 0;
// bool is2D=false,isStereo=false;
firsttree_->SetBranchAddress("DetId", &id);
firsttree_->SetBranchAddress("Nhits", &nhits);
firsttree_->SetBranchAddress("Noverlaps", &noverlaps);
firsttree_->SetBranchAddress("SubDet", &subdet);
firsttree_->SetBranchAddress("Layer", &layer);
// firsttree_->SetBranchAddress("is2D" , &is2D);
// firsttree_->SetBranchAddress("isStereo", &isStereo);
firsttree_->SetBranchAddress("posX", &posX);
firsttree_->SetBranchAddress("posY", &posY);
firsttree_->SetBranchAddress("posZ", &posZ);
firsttree_->SetBranchAddress("posR", &posR);
firsttree_->SetBranchAddress("posEta", &posEta);
firsttree_->SetBranchAddress("posPhi", &posPhi);
//create and book the output
TFile *outfile = new TFile(outfilename_.c_str(), "RECREATE");
out_ = new TTree(treename_.c_str(), "AlignmentHitMapsTOTAL");
unsigned int id_out = 0;
uint32_t nhits_out = 0, noverlaps_out = 0;
float posX_out(-99999.0), posY_out(-77777.0), posZ_out(-88888.0);
float posEta_out(-6666.0), posPhi_out(-5555.0), posR_out(-4444.0);
int subdet_out = 0;
unsigned int layer_out = 0;
bool is2D_out = false, isStereo_out = false;
float prescfact_out = 1.0;
float prescfact_overlap_out = 1.0;
out_->Branch("DetId", &id_out, "DetId/i");
out_->Branch("Nhits", &nhits_out, "Nhits/i");
out_->Branch("Noverlaps", &noverlaps_out, "Noverlaps/i");
out_->Branch("SubDet", &subdet_out, "SubDet/I");
out_->Branch("Layer", &layer_out, "Layer/i");
out_->Branch("is2D", &is2D_out, "is2D/B");
out_->Branch("isStereo", &isStereo_out, "isStereo/B");
out_->Branch("posX", &posX_out, "posX/F");
out_->Branch("posY", &posY_out, "posY/F");
out_->Branch("posZ", &posZ_out, "posZ/F");
out_->Branch("posR", &posR_out, "posR/F");
out_->Branch("posEta", &posEta_out, "posEta/F");
out_->Branch("posPhi", &posPhi_out, "posPhi/F");
out_->Branch("PrescaleFactor", &prescfact_out, "PrescaleFact/F");
out_->Branch("PrescaleFactorOverlap", &prescfact_overlap_out, "PrescaleFactOverlap/F");
//look if you find this detid in the map
DetHitMap::iterator mapiter;
for (int mod = 0; mod < firsttree_->GetEntries(); mod++) {
//for(int mod=0;mod<100;++mod){
// nhits_out=0;
// noverlaps_out=0;
firsttree_->GetEntry(mod);
nhits_out = hitmap_[id];
noverlaps_out = overlapmap_[id];
// if(mod<25)LogDebug("TkAlCaSkimTreeMerger")<<"Nhits 1st tree: "<<nhits<<"\tTotal nhits chain: "<<nhits_out<<std::endl;
id_out = id;
subdet_out = subdet;
layer_out = layer;
posX_out = posX;
posY_out = posY;
posZ_out = posZ;
posR_out = posR;
posEta_out = posEta;
posPhi_out = posPhi;
//calculate prescaling factor
int subdetmax = -1;
if (subdet_out == 1)
subdetmax = maxPXBhits_;
else if (subdet_out == 2)
subdetmax = maxPXFhits_;
else if (subdet_out == 3)
subdetmax = maxTIBhits_;
else if (subdet_out == 4)
subdetmax = maxTIDhits_;
else if (subdet_out == 5)
subdetmax = maxTOBhits_;
else if (subdet_out == 6)
subdetmax = maxTEChits_;
else
subdetmax = -9999;
if (maxhits_ > -1) {
if (int(nhits_out) > maxhits_) {
prescfact_out = float(maxhits_) / float(nhits_out);
}
if (int(noverlaps_out) > maxhits_) {
prescfact_overlap_out = float(maxhits_) / float(noverlaps_out);
}
} else if (subdetmax > 0) { //calculate different prescaling factors for each subdet
if (int(nhits_out) > subdetmax) {
prescfact_out = float(subdetmax / nhits_out);
}
if (int(noverlaps_out) > subdetmax) {
prescfact_overlap_out = float(subdetmax) / float(noverlaps_out);
}
} else {
prescfact_out = 1.0;
prescfact_overlap_out = 1.0;
}
out_->Fill();
} //end loop on mod - first tree modules
myclock.Stop();
LogDebug("TkAlCaSkimTreeMerger") << "Finished endJob after " << myclock.RealTime() << " s (real time) / "
<< myclock.CpuTime() << " s (cpu time)" << std::endl;
LogDebug("TkAlCaSkimTreeMerger") << "Ending the tree merging." << std::endl;
out_->Write();
LogDebug("TkAlCaSkimTreeMerger") << "Deleting..." << std::flush;
delete firstfile;
delete outfile;
} //end endJob
void TkAlCaSkimTreeMerger::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
edm::ParameterSetDescription desc;
desc.setComment("Merger of TkAlCaSkim Trees");
desc.add<std::string>("FileList", "DQMHitMapsList.txt");
desc.add<std::string>("TreeName", "AlignmentHitMaps");
desc.add<std::string>("OutputFile", "AlignmentHitMapsMerged.root");
desc.add<int>("NhitsMaxLimit", 0);
edm::ParameterSetDescription tkAlCaSkimTreeParamsDesc;
std::vector<edm::ParameterSet> tkAlCaSkimDefaults(1);
tkAlCaSkimDefaults[0].addParameter("PXBmaxhits", -1);
tkAlCaSkimDefaults[0].addParameter("PXFmaxhits", -1);
tkAlCaSkimDefaults[0].addParameter("TIBmaxhits", -1);
tkAlCaSkimDefaults[0].addParameter("TIDmaxhits", -1);
tkAlCaSkimDefaults[0].addParameter("TOBmaxhits", -1);
tkAlCaSkimDefaults[0].addParameter("TECmaxhits", -1);
desc.addVPSet("NhitsMaxSet", tkAlCaSkimTreeParamsDesc, tkAlCaSkimDefaults);
descriptions.addWithDefaultLabel(desc);
}
// ========= MODULE DEF ==============
#include "FWCore/PluginManager/interface/ModuleDef.h"
#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(TkAlCaSkimTreeMerger);
|