-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventInfoPrinter.cc
402 lines (356 loc) · 14.6 KB
/
EventInfoPrinter.cc
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// $Id: EventInfoPrinter.cc,v 1.11 2013/05/09 20:14:55 mschrode Exp $
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "EventInfoPrinter.h"
#include "Output.h"
#include "Selection.h"
#include "Variable.h"
TString EventInfoPrinter::runSortVar_ = "";
EventInfoPrinter::EventInfoPrinter(const Config &cfg)
: cfg_(cfg) {
// Init and run
if( init("print event info") ) {
// Print setup
std::cout << " - Writing event-provenance information to " << outFileName_ << std::endl;
std::cout << " - Printing " << std::flush;
if( printAllEvents() ) {
std::cout << "all events " << std::endl;
} else {
for(std::map<TString,unsigned int>::const_iterator itSV = selectionVariables_.begin();
itSV != selectionVariables_.end(); ++itSV) {
std::cout << "\n the " << itSV->second << " events with highest " << itSV->first << std::flush;
}
std::cout << std::endl;
}
if( printedSelections_.size() > 0 ) {
std::cout << " for selections" << std::flush;
for(std::set<TString>::const_iterator it = printedSelections_.begin(); it != printedSelections_.end(); ++it) {
std::cout << "\n " << *it << std::flush;
}
std::cout << std::endl;
}
std::cout << " - Creating LaTeX slides for event displays in " << latexSlidesName_ << std::endl;
selectEvents();
print();
}
}
bool EventInfoPrinter::init(const TString &key) {
// Get parameters
std::vector<Config::Attributes> attrList = cfg_(key);
// Required event-provenance information
varNameRunNum = "";
varNameLumiBlockNum = "";
varNameEvtNum = "";
bool provVarsDefined = false;
for(std::vector<Config::Attributes>::const_iterator it = attrList.begin();
it != attrList.end(); ++it) {
if( it->hasName("provenance variables") ) {
std::vector<TString> vars;
if( Config::split(it->value("provenance variables"),"+",vars) ) {
if( vars.size() == 3 ) {
varNameRunNum = vars.at(0);
varNameLumiBlockNum = vars.at(1);
varNameEvtNum = vars.at(2);
if( Variable::exists(varNameRunNum) &&
Variable::exists(varNameLumiBlockNum) &&
Variable::exists(varNameEvtNum) ) {
provVarsDefined = true;
EventInfoPrinter::runSortVar_ = varNameRunNum;
break;
}
} else {
std::cerr << "\n\nERROR in EventInfoPrinter::init(): wrong definition of event-provenance variables" << std::endl;
std::cerr << " Please use the following syntax in the config file:" << std::endl;
std::cerr << " 'print event info :: provenance variables: <VarNameRunNum> + <VarNameLumiBlockNum> + <VarNameEvtNum>'" << std::endl;
exit(-1);
}
}
}
}
// Optionally, read selections to be printed
for(std::vector<Config::Attributes>::const_iterator it = attrList.begin();
it != attrList.end(); ++it) {
if( it->hasName("selections") ) {
std::vector<TString> sels;
if( Config::split(it->value("selections"),",",sels) ) {
for(std::vector<TString>::const_iterator its = sels.begin();
its != sels.end(); ++its) {
printedSelections_.insert(*its);
}
}
} else {
std::cerr << "\n\nERROR in EventInfoPrinter::init(): wrong syntax" << std::endl;
std::cerr << " in line with key '" << key << "'" << std::endl;
std::cerr << " in config file '" << cfg_.fileName() << "'" << std::endl;
exit(-1);
}
}
// Optionally, print num events with highest value of a variable
for(std::vector<Config::Attributes>::const_iterator it = attrList.begin();
it != attrList.end(); ++it) {
if( it->hasName("highest") ) {
std::vector<TString> varNumPair;
if( Config::split(it->value("highest"),",",varNumPair) ) {
bool correctSyntax = false;
if( varNumPair.size() == 2 ) {
TString variable = varNumPair.at(0);
int number = varNumPair.at(1).Atoi();
if( Variable::exists(variable) ) {
selectionVariables_[variable] = number;
correctSyntax = true;
}
}
if( !correctSyntax ) {
std::cerr << "\n\nERROR in EventInfoPrinter::init(): wrong syntax" << std::endl;
std::cerr << " in line with key '" << key << "'" << std::endl;
std::cerr << " in config file '" << cfg_.fileName() << "'" << std::endl;
exit(-1);
}
}
}
}
// Define names of special variables
// This should be configurable
varNameNJets = ( Variable::exists("NJets") ? "NJets" : "" );
varNameHT = ( Variable::exists("HT") ? "HT" : "" );
varNameMHT = ( Variable::exists("MHT") ? "MHT" : "" );
// Output file
outFileName_ = Output::resultDir()+"/"+Output::id()+"_EventInfo.txt";
latexSlidesName_ = Output::resultDir()+"/"+Output::id()+"_EventDisplays.tex";
return provVarsDefined && attrList.size() > 1;
}
void EventInfoPrinter::selectEvents() {
// Loop over all global selections
// !!!!!!!!!!!!!This should be treated more carefully: one set of selectionVariables_
// per global selection
for(SelectionIt its = Selection::begin(); its != Selection::end(); ++its) {
if( printSelection((*its)->uid()) ) {
// Loop over all datasets with this global selection
DataSets selectedDataSets = DataSet::findAllWithSelection((*its)->uid());
for(DataSetIt itsd = selectedDataSets.begin(); itsd != selectedDataSets.end(); ++itsd) {
// Select events accoridng to specification
std::vector<const Event*> selectedEvts;
if( printAllEvents() ) { // select all events to print info
for(EventIt itEvt = (*itsd)->evtsBegin(); itEvt != (*itsd)->evtsEnd(); ++itEvt) {
selectedEvts.push_back(*itEvt);
}
} else { // select n (as specified) events with highest value of selection variable
for(std::map<TString,unsigned int>::const_iterator itSV = selectionVariables_.begin();
itSV != selectionVariables_.end(); ++itSV) {
std::vector<EvtValPair*> evtValPairs;
for(EventIt itEvt = (*itsd)->evtsBegin(); itEvt != (*itsd)->evtsEnd(); ++itEvt) {
evtValPairs.push_back(new EvtValPair(*itEvt,(*itEvt)->get(itSV->first)));
}
// sort by size of variable's values
std::sort(evtValPairs.begin(),evtValPairs.end(),EvtValPair::valueGreaterThan);
// store n (as specified) events with largest value
for(unsigned int n = 0;
n < std::min(itSV->second,static_cast<unsigned int>(evtValPairs.size())); ++n) {
bool isNewEvt = true;
for(std::vector<const Event*>::const_iterator it = selectedEvts.begin();
it != selectedEvts.end(); ++it) {
if( (*it) == evtValPairs.at(n)->event() ) {
isNewEvt = false;
break;
}
}
if( isNewEvt ) selectedEvts.push_back(evtValPairs.at(n)->event());
}
for(std::vector<EvtValPair*>::iterator it = evtValPairs.begin();
it != evtValPairs.end(); ++it) {
delete *it;
}
}
}
// Sort by run
std::sort(selectedEvts.begin(),selectedEvts.end(),EventInfoPrinter::greaterByRun);
// Store list of events
printedEvts_[(*itsd)->uid()] = selectedEvts;
} // End of loop over datasets
}
} // End of loop over selections
}
void EventInfoPrinter::print() const {
const unsigned int width = 18;
std::list<TString> vars = listOfPrintedVariables();
TString separator1 = "";
TString separator2 = "";
for(unsigned int i = 0; i < vars.size(); ++i) {
for(unsigned int j = 0; j < width+3; ++j) {
separator1 += "=";
separator2 += "-";
}
}
// Print file with detailed event info and some formatting
ofstream file(outFileName_);
// txt-style table
for(std::map< TString, std::vector<const Event*> >::const_iterator it = printedEvts_.begin(); it != printedEvts_.end(); ++it) {
file << separator1 << std::endl;
file << "Dataset: '" << it->first << "'" << std::endl;
file << separator2 << std::endl;
for(std::list<TString>::const_iterator itVar = vars.begin(); itVar != vars.end(); ++itVar) {
file << std::setw(width) << std::fixed << *itVar;
if( itVar != --vars.end() ) file << " : ";
else file << std::endl;
}
file << separator2 << std::endl;
for(std::vector<const Event*>::const_iterator itEvt = it->second.begin();
itEvt != it->second.end(); ++itEvt) {
for(std::list<TString>::const_iterator itVar = vars.begin(); itVar != vars.end(); ++itVar) {
if( Variable::type(*itVar) == "UShort_t" ||
Variable::type(*itVar) == "Int_t" ||
Variable::type(*itVar) == "UInt_t" ||
Variable::type(*itVar) == "UChar_t" ) {
file << std::setprecision(0);
} else {
file << std::setprecision(3);
}
file << std::setw(width) << (*itEvt)->get(*itVar);
if( itVar != --vars.end() ) file << " : ";
else file << std::endl;
}
}
file << separator1 << std::endl;
file << "\n\n\n";
}
// LaTeX-style table
for(std::map< TString, std::vector<const Event*> >::const_iterator it = printedEvts_.begin(); it != printedEvts_.end(); ++it) {
file << "\n\n\n%" << separator1 << std::endl;
file << "% Dataset: '" << it->first << "'" << std::endl;
file << "%" << separator2 << std::endl;
file << "\n\\begin{tabular}{";
for(unsigned int i = 0; i < vars.size(); ++i) {
file << "r";
}
file << "}\n";
file << "\\toprule\n";
for(std::list<TString>::const_iterator itVar = vars.begin(); itVar != vars.end(); ++itVar) {
file << std::setw(width) << std::fixed << "\\multicolumn{1}{c}{" << *itVar << "}";
if( itVar != --vars.end() ) file << " & ";
else file << " \\\\ \n";
}
file << "\\midrule\n";
for(std::vector<const Event*>::const_iterator itEvt = it->second.begin();
itEvt != it->second.end(); ++itEvt) {
for(std::list<TString>::const_iterator itVar = vars.begin(); itVar != vars.end(); ++itVar) {
if( Variable::type(*itVar) == "UShort_t" ||
Variable::type(*itVar) == "Int_t" ||
Variable::type(*itVar) == "UInt_t" ||
Variable::type(*itVar) == "UChar_t" ) {
file << std::setprecision(0);
} else {
file << std::setprecision(2);
}
if( (*itEvt)->get(*itVar) == 9999 ) {
file << std::setw(width) << "---";
} else {
file << std::setw(width) << (*itEvt)->get(*itVar);
}
if( itVar != --vars.end() ) file << " & ";
else file << " \\\\ \n";
}
}
file << "\\bottomrule \n\\end{tabular}\n\n\n";
}
file.close();
// Print CMSSW-like run lists
for(std::map< TString, std::vector<const Event*> >::const_iterator it = printedEvts_.begin(); it != printedEvts_.end(); ++it) {
ofstream file(Output::resultDir()+"/"+Output::id()+"_EventInfo__"+Output::cleanName(it->first)+"__Runlist.txt");
for(std::vector<const Event*>::const_iterator itEvt = it->second.begin();
itEvt != it->second.end(); ++itEvt) {
file << std::setw(width) << (*itEvt)->get(varNameRunNum);
file << ":";
file << std::setw(width) << (*itEvt)->get(varNameLumiBlockNum);
file << ":";
file << std::setw(width) << std::setprecision(15) << (*itEvt)->get(varNameEvtNum);
file << std::endl;
}
file.close();
}
// LaTeX slides for event displays
ofstream texFile(latexSlidesName_);
for(std::map< TString, std::vector<const Event*> >::const_iterator it = printedEvts_.begin();
it != printedEvts_.end(); ++it) {
for(std::vector<const Event*>::const_iterator itEvt = it->second.begin();
itEvt != it->second.end(); ++itEvt) {
texFile << "% --------------------------------------------------" << std::endl;
texFile << "\\begin{frame}" << std::endl;
texFile << "\\frametitle{Event " << (*itEvt)->get(varNameRunNum) << ":" << (*itEvt)->get(varNameLumiBlockNum) << ":" << std::setprecision(15) << (*itEvt)->get(varNameEvtNum) << " (" << it->first << ")}" << std::endl; // (Name of dataset and selection)
texFile << " \\begin{columns}" << std::endl;
texFile << " \\begin{column}{0.55\\textwidth}" << std::endl;
texFile << " \\centering" << std::endl;
texFile << " \\includegraphics[width=\\textwidth]{figures/ID-" << (*itEvt)->get(varNameRunNum) << "_" << std::setprecision(15) << (*itEvt)->get(varNameEvtNum) << "_" << (*itEvt)->get(varNameLumiBlockNum) << "_RhoPhi.png}" << std::endl;
texFile << " \\end{column}" << std::endl;
texFile << " \\begin{column}{0.45\\textwidth}" << std::endl;
texFile << " \\includegraphics[width=\\textwidth]{figures/ID-" << (*itEvt)->get(varNameRunNum) << "_" << std::setprecision(15) << (*itEvt)->get(varNameEvtNum) << "_" << (*itEvt)->get(varNameLumiBlockNum) << "_Lego.png}\\\\" << std::endl;
texFile << " \\includegraphics[width=\\textwidth]{figures/ID-" << (*itEvt)->get(varNameRunNum) << "_" << std::setprecision(15) << (*itEvt)->get(varNameEvtNum) << "_" << (*itEvt)->get(varNameLumiBlockNum) << "_RhoZ.png}" << std::endl;
texFile << " \\end{column} " << std::endl;
texFile << " \\end{columns}" << std::endl;
texFile << "\\end{frame}\n\n\n" << std::endl;
}
}
texFile.close();
}
std::list<TString> EventInfoPrinter::listOfPrintedVariables() const {
// List of variables
std::list<TString> list;
for(std::vector<TString>::const_iterator itV = Variable::begin();
itV != Variable::end(); ++itV) {
list.push_back(*itV);
}
// Order list such that the following variabels
// are at front
std::vector<TString> varsInOrder;
varsInOrder.push_back(varNameRunNum);
varsInOrder.push_back(varNameLumiBlockNum);
varsInOrder.push_back(varNameEvtNum);
if( varNameHT != "" ) varsInOrder.push_back(varNameHT);
if( varNameMHT != "" ) varsInOrder.push_back(varNameMHT);
if( varNameNJets != "" ) varsInOrder.push_back(varNameNJets);
// The resulting list
std::list<TString>::iterator endOfOrderedPart = list.begin();
++endOfOrderedPart;
for(std::vector<TString>::const_iterator itOV = varsInOrder.begin();
itOV != varsInOrder.end(); ++itOV) {
for(std::list<TString>::iterator it = list.begin(); it != list.end(); ++it) {
if( *it == *itOV ) {
TString tmp = *it;
list.erase(it);
list.insert(endOfOrderedPart,tmp);
++endOfOrderedPart;
break;
}
}
}
return list;
}
bool EventInfoPrinter::printAllEvents() const {
bool printAll = false;
if( selectionVariables_.size() == 0 ) {
printAll = true;
} else {
for(std::map<TString,unsigned int>::const_iterator itSV = selectionVariables_.begin(); itSV != selectionVariables_.end(); ++itSV) {
if( itSV->second == -1 ) {
printAll = true;
break;
}
}
}
return printAll;
}
// Check if selection 'uid' is to be printed
// If no selections are specified in the config, print all
bool EventInfoPrinter::printSelection(const TString &uid) const {
return printedSelections_.size() == 0 ? true : printedSelections_.find(uid) != printedSelections_.end();
}
bool EventInfoPrinter::EvtValPair::valueGreaterThan(const EvtValPair *idx1, const EvtValPair *idx2) {
if(idx1 == 0) {
return idx2 != 0;
} else if(idx2 == 0) {
return false;
} else {
return idx1->value() > idx2->value();
}
}