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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
// -*- C++ -*-
//
// Package: Core
// Class : FWGUIEventDataAdder
//
// Implementation:
// <Notes on implementation>
//
// Original Author: Chris Jones
// Created: Fri Jun 13 09:58:53 EDT 2008
//
// system include files
#include <iostream>
#include <sigc++/signal.h>
#include <functional>
#include <algorithm>
#include <cctype>
#include <string>
#include "TGFrame.h"
#include "TGTextEntry.h"
#include "TGButton.h"
#include "TGMsgBox.h"
#include "TClass.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
// user include files
#include "Fireworks/Core/src/FWGUIEventDataAdder.h"
#include "Fireworks/Core/interface/FWPhysicsObjectDesc.h"
#include "Fireworks/Core/interface/FWEventItemsManager.h"
#include "Fireworks/Core/interface/FWEventItem.h"
#include "Fireworks/Core/interface/FWItemAccessorFactory.h"
#include "Fireworks/Core/interface/FWJobMetadataManager.h"
#include "Fireworks/TableWidget/interface/FWTableWidget.h"
#include "Fireworks/TableWidget/interface/FWTableManagerBase.h"
#include "Fireworks/TableWidget/interface/FWTextTableCellRenderer.h"
#include "Fireworks/Core/interface/fwLog.h"
#include "Fireworks/Core/interface/FWDialogBuilder.h"
//
// constants, enums and typedefs
//
static const std::string &dataForColumn(const FWJobMetadataManager::Data &iData, int iCol) {
switch (iCol) {
case 0:
return iData.purpose_;
break;
case 4:
return iData.type_;
break;
case 1:
return iData.moduleLabel_;
break;
case 2:
return iData.productInstanceLabel_;
break;
case 3:
return iData.processName_;
break;
default:
break;
}
static const std::string s_blank;
return s_blank;
}
static const unsigned int kNColumns = 5;
class DataAdderTableManager : public FWTableManagerBase {
public:
DataAdderTableManager(FWJobMetadataManager *manager) : m_manager(manager), m_selectedRow(-1), m_filter() { reset(); }
int numberOfRows() const override { return m_row_to_index.size(); }
int numberOfColumns() const override { return kNColumns; }
/** Updates the table using the passed @a filter.
Notice that in this case we reset the sorting and show results by those
best matching the filter.
*/
virtual void sortWithFilter(const char *filter) {
m_filter = filter;
sort(-1, sortOrder());
dataChanged();
}
int unsortedRowNumber(int iSortedRowNumber) const override { return m_row_to_index[iSortedRowNumber]; }
void implSort(int col, bool sortOrder) override;
std::vector<std::string> getTitles() const override {
std::vector<std::string> returnValue;
returnValue.reserve(kNColumns);
returnValue.push_back("Purpose");
returnValue.push_back("Module Label");
returnValue.push_back("Product Instance Label");
returnValue.push_back("Process Name");
returnValue.push_back("C++ Class");
return returnValue;
}
FWTableCellRendererBase *cellRenderer(int iSortedRowNumber, int iCol) const override {
if (static_cast<int>(m_row_to_index.size()) > iSortedRowNumber) {
int unsortedRow = m_row_to_index[iSortedRowNumber];
const FWJobMetadataManager::Data &data = (m_manager->usableData())[unsortedRow];
m_renderer.setData(dataForColumn(data, iCol), m_selectedRow == unsortedRow);
} else {
m_renderer.setData(std::string(), false);
}
return &m_renderer;
}
void setSelection(int row, int mask) {
if (mask == 4) {
if (row == m_selectedRow) {
row = -1;
}
}
changeSelection(row);
}
virtual const std::string title() const { return "Viewable Collections"; }
int selectedRow() const { return m_selectedRow; }
//virtual void sort (int col, bool reset = false);
virtual bool rowIsSelected(int row) const { return m_selectedRow == row; }
void reset() {
changeSelection(-1);
m_row_to_index.clear();
m_row_to_index.reserve(m_manager->usableData().size());
for (unsigned int i = 0; i < m_manager->usableData().size(); ++i) {
m_row_to_index.push_back(i);
}
dataChanged();
}
sigc::signal<void(int)> indexSelected_;
private:
void changeSelection(int iRow) {
if (iRow != m_selectedRow) {
m_selectedRow = iRow;
if (-1 == iRow) {
indexSelected_(-1);
} else {
indexSelected_(iRow);
}
visualPropertiesChanged();
}
}
FWJobMetadataManager *m_manager;
std::vector<int> m_row_to_index;
int m_selectedRow;
std::string m_filter;
mutable FWTextTableCellRenderer m_renderer;
};
namespace {
void strip(std::string &source, const char *str) {
std::string remove(str);
while (true) {
size_t found = source.find(remove);
if (found == std::string::npos)
break;
source.erase(found, remove.size());
}
}
/** Helper classes to handle sorting and filtering.
The idea is that we sort things so that:
- An item that matches is always less than one that does not.
- If two items both match they are sorted according to the sorting
criteria.
- If two items both do not match, the are always sorted (so that we
do not waste time in sorting non matching items).
Then we tell the table that the size of the available data is only
the size of the matching items.
Notice that the matching here does not work with regular expressions
but it matches in the case the filter string is found anywhere,
regardless of the case.
*/
class SortAndFilter {
public:
SortAndFilter(const char *filter, int column, bool order, const std::vector<FWJobMetadataManager::Data> &data)
: m_filter(filter), m_column(column), m_order(order), m_data(data) {
simplify(m_filter);
m_weights.resize(data.size());
// Calculate whether or not all the entries match the given filter.
// This is done only once, since it's invariant under permutations
// of the data.
for (size_t i = 0, e = m_weights.size(); i != e; ++i)
m_weights[i] = matchesFilter(m_data[i]);
}
/** Makes @a str lowercase and eliminates bits we dont want to take
into account in while searching.
*/
static void simplify(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), tolower);
strip(str, "std::");
strip(str, "edm::");
strip(str, "vector<");
strip(str, "clonepolicy");
strip(str, "ownvector");
strip(str, "rangemap<");
strip(str, "strictweakordering<");
strip(str, "sortedcollection<");
strip(str, "reco::");
strip(str, "edmnew::");
}
unsigned int matches(const std::string &str) const {
std::string up(str);
simplify(up);
const char *begin = up.c_str();
// If the filter is empty, we consider anything as matching
// (i.e. it will not loop).
// If the filter is not empty but the string to be matched is, we
// consider it as if it was not matching.
if ((!m_filter.empty()) && str.empty())
return 0;
// There are two level of matching. "Full string" and
// "All characters". "Full string" matches return an higher weight
// and therefore should appear on top.
if (strstr(begin, m_filter.c_str()))
return 2;
return 0;
}
/** If any of the columns (including "Purpose"!!) matches, we consider
the row valid.
@return the best score obtained when matching strings.
*/
unsigned int matchesFilter(const FWJobMetadataManager::Data &data) const {
std::vector<unsigned int> scores;
scores.reserve(10);
scores.push_back(matches(data.purpose_));
scores.push_back(matches(data.type_));
scores.push_back(matches(data.moduleLabel_));
scores.push_back(matches(data.productInstanceLabel_));
scores.push_back(matches(data.processName_));
std::sort(scores.begin(), scores.end());
return scores.back();
}
/** Have a look at the class description to see the rationale behind
this.
*/
bool operator()(const int &aIndex, const int &bIndex) {
// In case no column is selected, we sort by relevance of the
// filter.
if (m_column == -1)
return m_weights[aIndex] >= m_weights[bIndex];
const FWJobMetadataManager::Data &a = m_data[aIndex];
const FWJobMetadataManager::Data &b = m_data[bIndex];
if (m_order)
return dataForColumn(a, m_column) < dataForColumn(b, m_column);
else
return dataForColumn(a, m_column) > dataForColumn(b, m_column);
}
private:
std::string m_filter;
int m_column;
bool m_order;
const std::vector<FWJobMetadataManager::Data> &m_data;
std::vector<unsigned int> m_weights;
};
void doSort(int column,
const char *filter,
bool descentSort,
const std::vector<FWJobMetadataManager::Data> &iData,
std::vector<int> &oRowToIndex) {
std::vector<int> ordered;
ordered.reserve(iData.size());
for (size_t i = 0, e = iData.size(); i != e; ++i)
ordered.push_back(i);
SortAndFilter sorter(filter, column, descentSort, iData);
// GE: Using std::sort does not work for some reason... Bah...
std::stable_sort(ordered.begin(), ordered.end(), sorter);
oRowToIndex.clear();
oRowToIndex.reserve(ordered.size());
// Only keep track of the rows that match.
for (size_t i = 0, e = ordered.size(); i != e; ++i)
if (sorter.matchesFilter(iData[ordered[i]]) != 0)
oRowToIndex.push_back(ordered[i]);
}
} // namespace
void DataAdderTableManager::implSort(int column, bool sortOrder) {
doSort(column, m_filter.c_str(), sortOrder, m_manager->usableData(), m_row_to_index);
}
//
// static data member definitions
//
//
// constructors and destructor
//
FWGUIEventDataAdder::FWGUIEventDataAdder(UInt_t iWidth,
UInt_t iHeight,
FWEventItemsManager *iManager,
TGFrame *iParent,
FWJobMetadataManager *iMetadataManager)
: m_manager(iManager), m_metadataManager(iMetadataManager), m_parentFrame(iParent) {
m_metadataManager->metadataChanged_.connect(std::bind(&FWGUIEventDataAdder::metadataUpdatedSlot, this));
createWindow();
}
// FWGUIEventDataAdder::FWGUIEventDataAdder(const FWGUIEventDataAdder& rhs)
// {
// // do actual copying here;
// }
FWGUIEventDataAdder::~FWGUIEventDataAdder() {
/*
// m_frame->Cleanup();
// delete m_frame;
m_frame=0;
// delete m_tableWidget;
m_tableWidget=0;
delete m_tableManager;
m_tableManager=0;
*/
}
//
// assignment operators
//
// const FWGUIEventDataAdder& FWGUIEventDataAdder::operator=(const FWGUIEventDataAdder& rhs)
// {
// //An exception safe implementation is
// FWGUIEventDataAdder temp(rhs);
// swap(rhs);
//
// return *this;
// }
//
// member functions
//
void FWGUIEventDataAdder::addNewItem() {
TClass *theClass = TClass::GetClass(m_type.c_str());
if (nullptr == theClass) {
return;
}
const std::string moduleLabel = m_moduleLabel;
if (moduleLabel.empty()) {
return;
}
const std::string name = m_name->GetText();
if (name.empty()) {
return;
}
if (m_manager->find(name)) {
TString msg("Event item '");
msg += name;
msg += "' is already registered. Please use another name.";
fwLog(fwlog::kWarning) << msg.Data() << std::endl;
new TGMsgBox(gClient->GetDefaultRoot(), m_frame, "Error - Name conflict", msg, kMBIconExclamation, kMBOk);
return;
}
int largest = -1;
if (m_manager->begin() != m_manager->end()) {
if (*(m_manager->begin()))
largest = (*(m_manager->begin()))->layer();
}
for (FWEventItemsManager::const_iterator it = m_manager->begin(), itEnd = m_manager->end(); it != itEnd; ++it) {
if ((*it) && largest < (*it)->layer()) {
largest = (*it)->layer();
}
}
++largest;
std::string processName = m_processName;
if (m_doNotUseProcessName->IsOn() && m_doNotUseProcessName->IsEnabled()) {
processName = "";
}
FWPhysicsObjectDesc desc(name,
theClass,
m_purpose,
FWDisplayProperties::defaultProperties,
moduleLabel,
m_productInstanceLabel,
processName,
"",
largest);
m_manager->add(desc);
}
void FWGUIEventDataAdder::addNewItemAndClose() {
addNewItem();
windowIsClosing();
}
void FWGUIEventDataAdder::show() {
// Map main frame
if (nullptr == m_frame) {
createWindow();
}
m_frame->MapWindow();
}
void FWGUIEventDataAdder::windowIsClosing() {
m_name->SetText("");
m_search->SetText("");
m_purpose.clear();
m_type.clear();
m_moduleLabel.clear();
m_processName.clear();
m_productInstanceLabel.clear();
m_apply->SetEnabled(false);
m_applyAndClose->SetEnabled(false);
m_frame->UnmapWindow();
m_frame->DontCallClose();
}
void FWGUIEventDataAdder::updateFilterString(const char *str) {
m_tableManager->sortWithFilter(str);
m_tableManager->dataChanged();
}
void FWGUIEventDataAdder::createWindow() {
m_tableManager = new DataAdderTableManager(m_metadataManager);
m_tableManager->indexSelected_.connect(
std::bind(&FWGUIEventDataAdder::newIndexSelected, this, std::placeholders::_1));
m_frame = new TGTransientFrame(gClient->GetDefaultRoot(), m_parentFrame, 600, 400);
m_frame->Connect("CloseWindow()", "FWGUIEventDataAdder", this, "windowIsClosing()");
FWDialogBuilder builder(m_frame);
TGTextButton *cancelButton, *resetButton;
builder.indent(10)
.spaceDown(15)
.addLabel("Search:", 0)
.expand(false)
.spaceUp(4)
.floatLeft(4)
.addTextEntry("", &m_search)
.spaceUp(0)
.frameSpaceDown(10)
.addLabel("Viewable Collections", 8)
.frameSpaceDown(5)
.addTable(m_tableManager, &m_tableWidget)
.expand(true, true)
.addLabel("Name:", 0)
.expand(false)
.spaceUp(4)
.floatLeft(4)
.addTextEntry("", &m_name)
.spaceUp(0)
.floatLeft(4)
.addTextButton(" Reset ", &resetButton)
.expand(false, false)
.frameSpaceUpDown(5)
.addCheckbox(
"Do not use Process Name and "
"instead only get this data "
"from the most recent Process",
&m_doNotUseProcessName)
.frameSpaceDown(15)
.hSpacer()
.floatLeft(0)
.addTextButton(" Close ", &cancelButton)
.expand(false)
.floatLeft(4)
.addTextButton(" Add Data ", &m_apply)
.expand(false)
.floatLeft(4)
.addTextButton(" Add Data && Close ", &m_applyAndClose)
.expand(false)
.spaceRight(25)
.spaceDown(15);
m_search->Connect("TextChanged(const char *)", "FWGUIEventDataAdder", this, "updateFilterString(const char *)");
m_search->SetEnabled(true);
m_tableWidget->SetBackgroundColor(0xffffff);
m_tableWidget->SetLineSeparatorColor(0x000000);
m_tableWidget->SetHeaderBackgroundColor(0xececec);
m_tableWidget->Connect("rowClicked(Int_t,Int_t,Int_t,Int_t,Int_t)",
"FWGUIEventDataAdder",
this,
"rowClicked(Int_t,Int_t,Int_t,Int_t,Int_t)");
m_tableWidget->disableGrowInWidth();
m_name->SetState(true);
resetButton->SetEnabled(true);
resetButton->Connect("Clicked()", "FWGUIEventDataAdder", this, "resetNameEntry()");
m_doNotUseProcessName->SetState(kButtonDown);
cancelButton->Connect("Clicked()", "FWGUIEventDataAdder", this, "windowIsClosing()");
cancelButton->SetEnabled(true);
m_apply->Connect("Clicked()", "FWGUIEventDataAdder", this, "addNewItem()");
m_applyAndClose->Connect("Clicked()", "FWGUIEventDataAdder", this, "addNewItemAndClose()");
m_frame->SetWindowName("Add Collection");
m_frame->MapSubwindows();
m_frame->Layout();
}
/** Slot for the metadataChanged signal of the FWJobMetadataManager.
Whenever metadata changes, for whatever reason (e.g. a new file), we
need to update the table.
*/
void FWGUIEventDataAdder::metadataUpdatedSlot(void) {
m_tableManager->reset();
m_tableManager->sort(0, true);
}
void FWGUIEventDataAdder::resetNameEntry() {
m_name->SetText(m_apply->IsEnabled() ? m_moduleLabel.c_str() : "", kFALSE);
}
void FWGUIEventDataAdder::newIndexSelected(int iSelectedIndex) {
if (-1 != iSelectedIndex) {
std::vector<FWJobMetadataManager::Data> &metadata = m_metadataManager->usableData();
m_purpose = metadata[iSelectedIndex].purpose_;
m_type = metadata[iSelectedIndex].type_;
std::string oldModuleLabel = m_moduleLabel;
m_moduleLabel = metadata[iSelectedIndex].moduleLabel_;
m_productInstanceLabel = metadata[iSelectedIndex].productInstanceLabel_;
m_processName = metadata[iSelectedIndex].processName_;
if (strlen(m_name->GetText()) == 0 || oldModuleLabel == m_name->GetText()) {
m_name->SetText(m_moduleLabel.c_str());
}
m_apply->SetEnabled(true);
m_applyAndClose->SetEnabled(true);
//Check to see if this is the last process, if it is then we can let the user decide
// to not use the process name when doing the lookup. This makes a saved configuration
// more robust. However, if they choose a collection not from the last process then we need the
// process name in order to correctly get the data they want
bool isMostRecentProcess = true;
int index = 0;
for (std::vector<FWJobMetadataManager::Data>::iterator it = metadata.begin(), itEnd = metadata.end();
it != itEnd && isMostRecentProcess;
++it, ++index) {
if (index == iSelectedIndex) {
continue;
}
if (it->moduleLabel_ == m_moduleLabel && it->purpose_ == m_purpose && it->type_ == m_type &&
it->productInstanceLabel_ == m_productInstanceLabel) {
//see if this process is newer than the data requested
for (size_t pni = 0, pne = m_metadataManager->processNamesInJob().size(); pni != pne; ++pni) {
const std::string &processName = m_metadataManager->processNamesInJob()[pni];
if (m_processName == processName)
break;
if (it->processName_ == processName) {
isMostRecentProcess = false;
break;
}
}
}
}
if (isMostRecentProcess) {
if (!m_doNotUseProcessName->IsEnabled()) {
m_doNotUseProcessName->SetEnabled(true);
}
} else {
//NOTE: must remember state before we get here because 'enable' and 'on' are mutually
// exlcusive :(
m_doNotUseProcessName->SetEnabled(false);
}
}
}
void FWGUIEventDataAdder::rowClicked(Int_t iRow, Int_t iButton, Int_t iKeyMod, Int_t, Int_t) {
if (iButton == kButton1) {
m_tableManager->setSelection(iRow, iKeyMod);
}
}
//
// const member functions
//
//
// static member functions
//
|