forked from alja/cms-nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evd.C
661 lines (560 loc) · 20 KB
/
evd.C
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#include "ROOT/REveDataClasses.hxx"
#include "ROOT/REveDataSimpleProxyBuilderTemplate.hxx"
#include "ROOT/REveManager.hxx"
#include "ROOT/REveScalableStraightLineSet.hxx"
#include "ROOT/REveViewContext.hxx"
#include <ROOT/REveGeoShape.hxx>
#include <ROOT/REveJetCone.hxx>
#include <ROOT/REvePointSet.hxx>
#include <ROOT/REveProjectionBases.hxx>
#include <ROOT/REveProjectionManager.hxx>
#include <ROOT/REveScene.hxx>
#include <ROOT/REveTableProxyBuilder.hxx>
#include <ROOT/REveTableInfo.hxx>
#include <ROOT/REveTrack.hxx>
#include <ROOT/REveTrackPropagator.hxx>
#include <ROOT/REveViewer.hxx>
#include <ROOT/REveViewContext.hxx>
#include "TTree.h"
#include "TGeoTube.h"
#include "TList.h"
#include "TParticle.h"
#include "TRandom.h"
#include "TApplication.h"
#include "TMathBase.h"
// globals
ROOT::Experimental::REveManager* eveMng;
ROOT::Experimental::REveProjectionManager* mngRhoZ;
ROOT::Experimental::REveViewContext* viewContext;
ROOT::Experimental::REveTrackPropagator* muonPropagator_g;
using namespace ROOT::Experimental;
float EtaToTheta(float eta)
{
using namespace TMath;
if (eta < 0)
return Pi() - 2*ATan(Exp(- Abs(eta)));
else
return 2*ATan(Exp(- Abs(eta)));
}
class ElectronProxyBuilder : public REveDataSimpleProxyBuilderTemplate<nanoaod::Electron>
{
using REveDataSimpleProxyBuilderTemplate<nanoaod::Electron>::Build;
void Build(const nanoaod::Electron& c_electron, int /*idx*/, REveElement* iItemHolder, const REveViewContext* context) override
{
nanoaod::Electron& electron = (nanoaod::Electron&)(c_electron);
int pdg = 11 * electron.charge();
float theta = EtaToTheta(electron.eta());
float phi = electron.phi();
float p = electron.pt()/TMath::Sin(theta);
float px = p * TMath::Cos(theta) * TMath::Cos(phi);
float py = p * TMath::Cos(theta) * TMath::Sin(phi);
float pz = p * TMath::Sin(theta);
float etot = p; // ???
auto x = new TParticle(pdg, 0, 0, 0, 0, 0,
px, py, pz, p,
0, 0, 0, 0 );
// printf("============== BUILD track %s (pt=%f, eta=%f) \n", iItemHolder->GetCName(), p.Pt(), p.Eta());
auto track = new REveTrack((TParticle*)(x), 1, context->GetPropagator());
track->MakeTrack();
SetupAddElement(track, iItemHolder, true);
// iItemHolder->AddElement(track);
//track->SetName(Form("element %s id=%d", iItemHolder->GetCName(), track->GetElementId()));
}
};
class MuonProxyBuilder : public REveDataSimpleProxyBuilderTemplate<nanoaod::Muon>
{
using REveDataSimpleProxyBuilderTemplate<nanoaod::Muon>::Build;
void Build(const nanoaod::Muon& c_electron, int /*idx*/, REveElement* iItemHolder, const REveViewContext* context) override
{
nanoaod::Muon& electron = (nanoaod::Muon&)(c_electron);
int pdg = 11 * electron.charge();
float theta = EtaToTheta(electron.eta());
float phi = electron.phi();
float p = electron.pt()/TMath::Sin(theta);
float px = p * TMath::Cos(theta) * TMath::Cos(phi);
float py = p * TMath::Cos(theta) * TMath::Sin(phi);
float pz = p * TMath::Sin(theta);
float etot = p; // ???
auto x = new TParticle(pdg, 0, 0, 0, 0, 0,
px, py, pz, p,
0, 0, 0, 0 );
// printf("============== BUILD track %s (pt=%f, eta=%f) \n", iItemHolder->GetCName(), p.Pt(), p.Eta());
auto track = new REveTrack((TParticle*)(x), 1, muonPropagator_g);
track->MakeTrack();
SetupAddElement(track, iItemHolder, true);
// iItemHolder->AddElement(track);
// track->SetName(Form("element %s id=%d", iItemHolder->GetCName(), track->GetElementId()));
}
};
class JetProxyBuilder: public REveDataSimpleProxyBuilderTemplate<nanoaod::Jet>
{
struct Cell {
float thetaMin;
float thetaMax;
float phiMin;
float phiMax;
};
bool HaveSingleProduct() const override { return true; }
void makeBarrelCell( Cell &cellData, float &offset , float towerH, float *pnts)
{
using namespace TMath;
float r1 = offset;
float r2 = r1 + towerH*Sin(cellData.thetaMin);
float z1In, z1Out, z2In, z2Out;
z1In = r1/Tan(cellData.thetaMax);
z1Out = r2/Tan(cellData.thetaMax);
z2In = r1/Tan(cellData.thetaMin);
z2Out = r2/Tan(cellData.thetaMin);
float cos1 = Cos(cellData.phiMin);
float sin1 = Sin(cellData.phiMin);
float cos2 = Cos(cellData.phiMax);
float sin2 = Sin(cellData.phiMax);
// 0
pnts[0] = r1*cos2;
pnts[1] = r1*sin2;
pnts[2] = z1In;
pnts += 3;
// 1
pnts[0] = r1*cos1;
pnts[1] = r1*sin1;
pnts[2] = z1In;
pnts += 3;
// 2
pnts[0] = r1*cos1;
pnts[1] = r1*sin1;
pnts[2] = z2In;
pnts += 3;
// 3
pnts[0] = r1*cos2;
pnts[1] = r1*sin2;
pnts[2] = z2In;
pnts += 3;
//---------------------------------------------------
// 4
pnts[0] = r2*cos2;
pnts[1] = r2*sin2;
pnts[2] = z1Out;
pnts += 3;
// 5
pnts[0] = r2*cos1;
pnts[1] = r2*sin1;
pnts[2] = z1Out;
pnts += 3;
// 6
pnts[0] = r2*cos1;
pnts[1] = r2*sin1;
pnts[2] = z2Out;
pnts += 3;
// 7
pnts[0] = r2*cos2;
pnts[1] = r2*sin2;
pnts[2] = z2Out;
offset += towerH*Sin(cellData.thetaMin);
}// end RenderBarrelCell
void makeEndCapCell(Cell &cellData, float &offset, float towerH , float *pnts)
{
using namespace TMath;
float z1, r1In, r1Out, z2, r2In, r2Out;
z1 = offset;
z2 = z1 + towerH;
r1In = z1*Tan(cellData.thetaMin);
r2In = z2*Tan(cellData.thetaMin);
r1Out = z1*Tan(cellData.thetaMax);
r2Out = z2*Tan(cellData.thetaMax);
float cos2 = Cos(cellData.phiMin);
float sin2 = Sin(cellData.phiMin);
float cos1 = Cos(cellData.phiMax);
float sin1 = Sin(cellData.phiMax);
// 0
pnts[0] = r1In*cos1;
pnts[1] = r1In*sin1;
pnts[2] = z1;
pnts += 3;
// 1
pnts[0] = r1In*cos2;
pnts[1] = r1In*sin2;
pnts[2] = z1;
pnts += 3;
// 2
pnts[0] = r2In*cos2;
pnts[1] = r2In*sin2;
pnts[2] = z2;
pnts += 3;
// 3
pnts[0] = r2In*cos1;
pnts[1] = r2In*sin1;
pnts[2] = z2;
pnts += 3;
//---------------------------------------------------
// 4
pnts[0] = r1Out*cos1;
pnts[1] = r1Out*sin1;
pnts[2] = z1;
pnts += 3;
// 5
pnts[0] = r1Out*cos2;
pnts[1] = r1Out*sin2;
pnts[2] = z1;
pnts += 3;
// 6
pnts[0] = r2Out*cos2;
pnts[1] = r2Out*sin2;
pnts[2] = z2;
pnts += 3;
// 7
pnts[0] = r2Out*cos1;
pnts[1] = r2Out*sin1;
pnts[2] = z2;
if (z1 > 0)
offset += towerH * Cos(cellData.thetaMin);
else
offset -= towerH * Cos(cellData.thetaMin);
} // end makeEndCapCell
using REveDataSimpleProxyBuilderTemplate<nanoaod::Jet>::BuildViewType;
void BuildViewType(const nanoaod::Jet& cdj, int idx, REveElement* iItemHolder,
std::string viewType, const REveViewContext* context) override
{
printf("REveDataSimpleProxyBuilderTemplate BuildViewType [%s](%d) >>>\n\n\n\n\n", viewType.c_str(), idx);
}
void Build(const nanoaod::Jet& cdj, int idx, REveElement* iItemHolder,
const REveViewContext* context) override
{
nanoaod::Jet& dj = (nanoaod::Jet&)(cdj);
auto jet = new REveJetCone();
jet->SetCylinder(context->GetMaxR() -5, context->GetMaxZ());
jet->AddEllipticCone(dj.eta(), dj.phi(), 0.2, 0.2);
SetupAddElement(jet, iItemHolder, true);
jet->SetTitle(Form("jet %d", idx));
static const float_t offr = 5;
float r_ecal = context->GetMaxR() + offr;
float z_ecal = context->GetMaxZ() + offr;
float energyScale = 5.f;
float transAngle = abs(atan(r_ecal/z_ecal));
double theta = EtaToTheta(dj.eta());
double phi = dj.phi();
Cell cell;
// hardcoded cell size
float ad = 0.02;
// thetaMin => etaMax, thetaMax => thetaMin
cell.thetaMax = EtaToTheta(dj.eta() - ad);
cell.thetaMin = EtaToTheta(dj.eta() + ad);
cell.phiMin = phi - ad;
cell.phiMax = phi + ad;
float pnts[24];
// an example of slices
std::vector<float> sliceVals;
sliceVals.push_back( dj.pt() * (1 - dj.chHEF()));
sliceVals.push_back( dj.pt() * dj.chHEF());
if (theta < transAngle || (3.14-theta) < transAngle)
{
// printf("Set points for ENDCAP REveBox [%d] ======================= \n", idx);
float offset = TMath::Sign(context->GetMaxZ(), dj.eta());
for (auto &val : sliceVals) {
offset += TMath::Sign(offr, dj.eta());
makeEndCapCell(cell, offset, TMath::Sign(val*energyScale, dj.eta()), &pnts[0]);
REveBox* reveBox = new REveBox();
reveBox->SetVertices(pnts);
SetupAddElement(reveBox, iItemHolder, true);
}
}
else
{
// printf("Set points for BARREL REveBox [%d] ======================= \n", idx);
float offset = context->GetMaxR();
for (auto &val : sliceVals) {
offset += offr;
makeBarrelCell(cell, offset, val*energyScale, &pnts[0]);
REveBox* reveBox = new REveBox();
reveBox->SetVertices(pnts);
SetupAddElement(reveBox, iItemHolder, true);
reveBox->SetTitle(Form("jet %d", idx)); // amt this is workaround and should be unnecessary
}
}
}
using REveDataProxyBuilderBase::LocalModelChanges;
void LocalModelChanges(int idx, REveElement* el, const REveViewContext* ctx) override
{
printf("LocalModelChanges jet %s ( %s )\n", el->GetCName(), el->FirstChild()->GetCName());
REveJetCone* cone = dynamic_cast<REveJetCone*>(el->FirstChild());
cone->SetLineColor(cone->GetMainColor());
}
};
//==============================================================================
//==============================================================================
//==============================================================================
class CollectionManager
{
private:
nanoaod::Event *m_event{nullptr};
REveElement *m_collections{nullptr};
std::vector<REveDataProxyBuilderBase *> m_builders;
public:
CollectionManager(nanoaod::Event* event) : m_event(event)
{
m_collections = eveMng->GetScenes()->FindChild("Collections");
// m_collections = eveMng->SpawnNewScene("Collections", "Collections");
}
void LoadCurrentEventInCollection(REveDataCollection* rdc)
{
rdc->ClearItems();
rdc->DestroyElements();
nanoaod::MamaCollection& mc = m_event->RefColl(rdc->GetCName());
std::string cname = rdc->GetName();
printf("-------- LoadCurrentEventInCollection size %d\n",mc.get_n_entries() );
for (int i = 0; i < mc.get_n_entries(); ++i)
{
TString pname(Form("%s %2d", cname.c_str(), i));
printf("access item %d \n", i);
rdc->AddItem(mc.get_item(i), pname.Data(), "");
}
}
void RenewEvent()
{
for (auto &el: m_collections->RefChildren())
{
auto c = dynamic_cast<REveDataCollection *>(el);
LoadCurrentEventInCollection(c);
}
for (auto proxy : m_builders)
{
proxy->Build();
}
}
REveDataProxyBuilderBase* makeGLBuilderForType(TClass* c)
{
// AMT can we do this with gRoot->ProcessLine() instead
std::string cname = c->GetName();
if (cname == "nanoaod::Jet")
{
return new JetProxyBuilder();
}
else if (cname == "nanoaod::Electron")
{
return new ElectronProxyBuilder();
}
else if (cname == "nanoaod::Muon")
{
return new MuonProxyBuilder();
}
else
{
return nullptr;
}
}
REveDataCollection* addCollection(std::string name, Color_t ccol = kBlue, bool showInTable=false)
{
nanoaod::MamaCollection& mc = m_event->RefColl(name);
auto rdc = new REveDataCollection (mc.get_class_name());
rdc->SetItemClass(mc.get_item_class());
rdc->SetMainColor(ccol);
m_collections->AddElement(rdc);
LoadCurrentEventInCollection(rdc);
printf("add collection instant proxy builders\n");
if (1) {
// GL view types
auto glBuilder = makeGLBuilderForType(rdc->GetItemClass());
if (glBuilder)
{
glBuilder->SetCollection(rdc);
glBuilder->SetHaveAWindow(true);
for (auto &scene: eveMng->GetScenes()->RefChildren())
{
if (strncmp(scene->GetCTitle(), "Table", 5) == 0) continue;
if (!strncmp(scene->GetCTitle(), "RhoZProjected", 8))
{
REveElement *product = glBuilder->CreateProduct("RhoZViewType", viewContext);
mngRhoZ->ImportElements(product, scene);
continue;
}
else if ((!strncmp(scene->GetCName(), "Event scene", 8)))
{
REveElement *product = glBuilder->CreateProduct(scene->GetTitle(), viewContext);
scene->AddElement(product);
}
}
m_builders.push_back(glBuilder);
glBuilder->Build();
}
}
if (1){
// Table view types
auto tableBuilder = new REveTableProxyBuilder();
tableBuilder->SetHaveAWindow(true);
tableBuilder->SetCollection(rdc);
REveElement* tablep = tableBuilder->CreateProduct("table-type", viewContext);
auto tableMng = viewContext->GetTableViewInfo();
if (showInTable)
{
tableMng->SetDisplayedCollection(rdc->GetElementId());
}
tableMng->AddDelegate([=]() { tableBuilder->ConfigChanged(); });
for (auto &scene: eveMng->GetScenes()->RefChildren())
{
printf("COMPARE %s %s\n", scene->GetCTitle(), scene->GetCName());
if (strncmp(scene->GetCTitle(), "Table", 5) == 0)
{
scene->AddElement(tablep);
tableBuilder->Build(rdc, tablep, viewContext );
printf("build table view");
break;
}
}
m_builders.push_back(tableBuilder);
}
rdc->SetHandlerFunc([&] (REveDataCollection* rdc)
{
this->CollectionChanged( rdc );
});
rdc->SetHandlerFuncIds([&] (REveDataCollection* rdc, const REveDataCollection::Ids_t& ids)
{
this->ModelChanged( rdc, ids );
});
return rdc;
}
void CollectionChanged(REveDataCollection* collection)
{
printf("collection changes not implemented %s!\n", collection->GetCName());
}
void ModelChanged(REveDataCollection* collection, const REveDataCollection::Ids_t& ids)
{
for (auto proxy : m_builders)
{
if (proxy->Collection() == collection)
{
// printf("Model changes check proxy %s: \n", proxy->Type().c_str());
proxy->ModelChanges(ids);
}
}
}
};
//==============================================================================
//== Event Manager =============================================================
//==============================================================================
class EventManager : public REveElement
{
private:
CollectionManager *m_collectionMng{nullptr};
nanoaod::Event *m_event{nullptr};
public:
EventManager(CollectionManager* m, nanoaod::Event* e):REveElement("EventManager") {m_collectionMng = m; m_event = e; }
virtual ~EventManager() {}
void UpdateTitle()
{
SetTitle(Form("%lld/%lld",m_event->GetEvent(), m_event->GetNumEvents() ));
StampObjProps();
}
virtual void NextEvent()
{
int id = m_event->GetEvent() +1;
if (id == m_event->GetNumEvents()) {
printf("NextEvent: reached last %lld\n", m_event->GetNumEvents());
id = 0;
}
m_event->GotoEvent(id);
UpdateTitle();
m_collectionMng->RenewEvent();
}
virtual void PreviousEvent()
{
int id;
if (m_event->GetEvent() == 0) {
id = m_event->GetNumEvents()-1;
}
else {
id = m_event->GetEvent() -1;
}
printf("going to previous %d \n", id);
m_event->GotoEvent(id);
UpdateTitle();
m_collectionMng->RenewEvent();
}
void SetNanoEvent(nanoaod::Event* e) {m_event = e;}
nanoaod::Event* GetNanoEvent() {return m_event;}
};
//==============================================================================
//== init scenes and views =============================================================
//==============================================================================
void createScenesAndViews()
{
//view context
float r = 139.5;
float z = 290;
auto prop = new REveTrackPropagator();
prop->SetMagFieldObj(new REveMagFieldDuo(350, -3.5, 2.0));
prop->SetMaxR(r);
prop->SetMaxZ(z);
prop->SetMaxOrbs(6);
prop->IncRefCount();
// AMT this is ugly ... introduce a global contenxt
muonPropagator_g = new REveTrackPropagator();
muonPropagator_g->SetMagFieldObj(new REveMagFieldDuo(350, -3.5, 2.0));
muonPropagator_g->SetMaxR(850);
muonPropagator_g->SetMaxZ(1100);
muonPropagator_g->SetMaxOrbs(6);
muonPropagator_g->IncRefCount();
viewContext = new REveViewContext();
viewContext->SetBarrel(r, z);
viewContext->SetTrackPropagator(prop);
// table specs
auto tableInfo = new REveTableViewInfo();
tableInfo->table("nanoaod::Jet").
column("pt", 1, "i.pt()").
column("eta", 3, "i.eta()").
column("phi", 3, "i.phi()");
tableInfo->table("nanoaod::Electron").
column("pt", 1, "i.pt()").
column("eta", 3, "i.eta()").
column("phi", 3, "i.phi()");
viewContext->SetTableViewInfo(tableInfo);
// Geometry
auto b1 = new REveGeoShape("Barrel 1");
float dr = 3;
b1->SetShape(new TGeoTube(r -2 , r+2, z));
b1->SetMainColor(kCyan);
eveMng->GetGlobalScene()->AddElement(b1);
// Projected RhoZ
if (1){
auto rhoZEventScene = eveMng->SpawnNewScene("RhoZ Scene","RhoZProjected");
mngRhoZ = new REveProjectionManager(REveProjection::kPT_RhoZ);
mngRhoZ->SetImportEmpty(true);
auto rhoZView = eveMng->SpawnNewViewer("RhoZ View");
rhoZView->AddScene(rhoZEventScene);
auto pgeoScene = eveMng->SpawnNewScene("Projection Geometry");
mngRhoZ->ImportElements(b1,pgeoScene );
rhoZView->AddScene(pgeoScene);
}
// collections
auto collections = eveMng->SpawnNewScene("Collections", "Collections");
// Table
if (1) {
auto tableScene = eveMng->SpawnNewScene ("Tables", "Tables");
auto tableView = eveMng->SpawnNewViewer("Table", "Table View");
tableView->AddScene(tableScene);
tableScene->AddElement(viewContext->GetTableViewInfo());
}
}
void evd(int portNum=9092)
{
auto event = g_event;
event->GotoEvent(0);
// init REve stuff
eveMng = REveManager::Create();
createScenesAndViews();
auto collectionMng = new CollectionManager(event);
auto eventMng = new EventManager(collectionMng, event);
eventMng->UpdateTitle();
eventMng->SetName(event->GetFile()->GetName());
eveMng->GetWorld()->AddElement(eventMng);
// test collections
auto rdc = collectionMng->addCollection("Jet", kBlue, true);
// rdc->SetFilterExpr("i.pt() > 25");
collectionMng->addCollection("Electron", kOrange + 7, false);
collectionMng->addCollection("MET", kGreen, false);
collectionMng->addCollection("Muon", kRed, false);
std::string locPath = "ui5";
eveMng->AddLocation("mydir/", locPath);
eveMng->SetDefaultHtmlPage("file:mydir/eventDisplay.html");
gEnv->SetValue("WebEve.DisableShow", 1);
gEnv->SetValue("WebGui.HttpPort", portNum);
eveMng->Show();
}