forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGenerator.cxx
574 lines (524 loc) · 24 KB
/
TGenerator.cxx
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
// @(#)root/eg:$Id$
// Author: Ola Nordmann 21/09/95
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// TGenerator //
// //
// Is an base class, that defines the interface of ROOT to various //
// event generators. Every event generator should inherit from //
// TGenerator or its subclasses. //
// //
// Derived class can overload the member function GenerateEvent //
// to do the actual event generation (e.g., call PYEVNT or similar). //
// //
// The derived class should overload the member function //
// ImportParticles (both types) to read the internal storage of the //
// generated event into either the internal TObjArray or the passed //
// TClonesArray of TParticles. //
// //
// If the generator code stores event data in the /HEPEVT/ common block //
// Then the default implementation of ImportParticles should suffice. //
// The common block /HEPEVT/ is structed like //
// //
// /* C */ //
// typedef struct { //
// Int_t nevhep; // Event number //
// Int_t nhep; // # of particles //
// Int_t isthep[4000]; // Status flag of i'th particle //
// Int_t idhep[4000]; // PDG # of particle //
// Int_t jmohep[4000][2]; // 1st & 2nd mother particle # //
// Int_t jdahep[4000][2]; // 1st & 2nd daughter particle # //
// Double_t phep[4000][5]; // 4-momentum and 1 word //
// Double_t vhep[4000][4]; // 4-position of production //
// } HEPEVT_DEF; //
// //
// //
// C Fortran //
// COMMON/HEPEVT/NEVHEP,NHEP,ISTHEP(4000),IDHEP(4000), //
// + JMOHEP(2,4000),JDAHEP(2,4000),PHEP(5,4000),VHEP(4,4000) //
// INTEGER NEVHEP,NHEP,ISTHEP,IDHEP,JMOHEP,JDAHEP //
// DOUBLE PRECISION PHEP,VHEP //
// //
// The generic member functions SetParameter and GetParameter can be //
// overloaded to set and get parameters of the event generator. //
// //
// Note, if the derived class interfaces a (set of) Fortran common //
// blocks (like TPythia, TVenus does), one better make the derived //
// class a singleton. That is, something like //
// //
// class MyGenerator : public TGenerator //
// { //
// public: //
// static MyGenerator* Instance() //
// { //
// if (!fgInstance) fgInstance = new MyGenerator; //
// return fgInstance; //
// } //
// void GenerateEvent() { ... } //
// void ImportParticles(TClonesArray* a, Option_t opt="") {...} //
// Int_t ImportParticles(Option_t opt="") { ... } //
// Int_t SetParameter(const char* name, Double_t val) { ... } //
// Double_t GetParameter(const char* name) { ... } //
// virtual ~MyGenerator() { ... } //
// protected: //
// MyGenerator() { ... } //
// MyGenerator(const MyGenerator& o) { ... } //
// MyGenerator& operator=(const MyGenerator& o) { ... } //
// static MyGenerator* fgInstance; //
// ClassDef(MyGenerator,0); //
// }; //
// //
// Having multiple objects accessing the same common blocks is not //
// safe. //
// //
// concrete TGenerator classes can be loaded in scripts and subseqent- //
// ly used in compiled code: //
// //
// // MyRun.h //
// class MyRun : public TObject //
// { //
// public: //
// static MyRun* Instance() { ... } //
// void SetGenerator(TGenerator* g) { fGenerator = g; } //
// void Run(Int_t n, Option_t* option="") //
// { //
// TFile* file = TFile::Open("file.root","RECREATE"); //
// TTree* tree = new TTree("T","T"); //
// TClonesArray* p = new TClonesArray("TParticles"); //
// tree->Branch("particles", &p); //
// for (Int_t event = 0; event < n; event++) { //
// fGenerator->GenerateEvent(); //
// fGenerator->ImportParticles(p,option); //
// tree->Fill(); //
// } //
// file->Write(); //
// file->Close(); //
// } //
// ... //
// protected: //
// TGenerator* fGenerator; //
// ClassDef(MyRun,0); //
// }; //
// //
// // Config.C //
// void Config() //
// { //
// MyRun* run = MyRun::Instance(); //
// run->SetGenerator(MyGenerator::Instance()); //
// } //
// //
// // main.cxx //
// int //
// main(int argc, char** argv) //
// { //
// TApplication app("", 0, 0); //
// gSystem->ProcessLine(".x Config.C"); //
// MyRun::Instance()->Run(10); //
// return 0; //
// } //
// //
// This is especially useful for example with TVirtualMC or similar. //
// //
//////////////////////////////////////////////////////////////////////////
#include "TROOT.h"
#include "TGenerator.h"
#include "TDatabasePDG.h"
#include "TParticlePDG.h"
#include "TParticle.h"
#include "TObjArray.h"
#include "Hepevt.h"
#include "TVirtualPad.h"
#include "TView.h"
#include "TText.h"
#include "TPaveText.h"
#include "TClonesArray.h"
#include "Riostream.h"
ClassImp(TGenerator)
////////////////////////////////////////////////////////////////////////////////
/// Event generator default constructor
///
TGenerator::TGenerator(const char *name,const char *title): TNamed(name,title)
{
// Initialize particles table
TDatabasePDG::Instance();
//TDatabasePDG *pdg = TDatabasePDG::Instance();
//if (!pdg->ParticleList()) pdg->Init();
fPtCut = 0;
fShowNeutrons = kTRUE;
fParticles = new TObjArray(10000);
}
////////////////////////////////////////////////////////////////////////////////
/// Event generator default destructor
///
TGenerator::~TGenerator()
{
//do nothing
if (fParticles) {
fParticles->Delete();
delete fParticles;
fParticles = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
/// must be implemented in concrete class (see eg TPythia6)
void TGenerator::GenerateEvent()
{
}
////////////////////////////////////////////////////////////////////////////////
///
/// It reads the /HEPEVT/ common block which has been filled by the
/// GenerateEvent method. If the event generator does not use the
/// HEPEVT common block, This routine has to be overloaded by the
/// subclasses.
///
/// The default action is to store only the stable particles (ISTHEP =
/// 1) This can be demanded explicitly by setting the option = "Final"
/// If the option = "All", all the particles are stored.
///
TObjArray* TGenerator::ImportParticles(Option_t *option)
{
fParticles->Clear();
Int_t numpart = HEPEVT.nhep;
if (!strcmp(option,"") || !strcmp(option,"Final")) {
for (Int_t i = 0; i<numpart; i++) {
if (HEPEVT.isthep[i] == 1) {
//
// Use the common block values for the TParticle constructor
//
TParticle *p = new TParticle(
HEPEVT.idhep[i],
HEPEVT.isthep[i],
HEPEVT.jmohep[i][0]-1,
HEPEVT.jmohep[i][1]-1,
HEPEVT.jdahep[i][0]-1,
HEPEVT.jdahep[i][1]-1,
HEPEVT.phep[i][0],
HEPEVT.phep[i][1],
HEPEVT.phep[i][2],
HEPEVT.phep[i][3],
HEPEVT.vhep[i][0],
HEPEVT.vhep[i][1],
HEPEVT.vhep[i][2],
HEPEVT.vhep[i][3]);
fParticles->Add(p);
}
}
} else if (!strcmp(option,"All")) {
for (Int_t i = 0; i<numpart; i++) {
TParticle *p = new TParticle(
HEPEVT.idhep[i],
HEPEVT.isthep[i],
HEPEVT.jmohep[i][0]-1,
HEPEVT.jmohep[i][1]-1,
HEPEVT.jdahep[i][0]-1,
HEPEVT.jdahep[i][1]-1,
HEPEVT.phep[i][0],
HEPEVT.phep[i][1],
HEPEVT.phep[i][2],
HEPEVT.phep[i][3],
HEPEVT.vhep[i][0],
HEPEVT.vhep[i][1],
HEPEVT.vhep[i][2],
HEPEVT.vhep[i][3]);
fParticles->Add(p);
}
}
return fParticles;
}
////////////////////////////////////////////////////////////////////////////////
///
/// It reads the /HEPEVT/ common block which has been filled by the
/// GenerateEvent method. If the event generator does not use the
/// HEPEVT common block, This routine has to be overloaded by the
/// subclasses.
///
/// The function loops on the generated particles and store them in
/// the TClonesArray pointed by the argument particles. The default
/// action is to store only the stable particles (ISTHEP = 1) This can
/// be demanded explicitly by setting the option = "Final" If the
/// option = "All", all the particles are stored.
///
Int_t TGenerator::ImportParticles(TClonesArray *particles, Option_t *option)
{
if (particles == 0) return 0;
TClonesArray &clonesParticles = *particles;
clonesParticles.Clear();
Int_t numpart = HEPEVT.nhep;
if (!strcmp(option,"") || !strcmp(option,"Final")) {
for (Int_t i = 0; i<numpart; i++) {
if (HEPEVT.isthep[i] == 1) {
//
// Use the common block values for the TParticle constructor
//
new(clonesParticles[i]) TParticle(
HEPEVT.idhep[i],
HEPEVT.isthep[i],
HEPEVT.jmohep[i][0]-1,
HEPEVT.jmohep[i][1]-1,
HEPEVT.jdahep[i][0]-1,
HEPEVT.jdahep[i][1]-1,
HEPEVT.phep[i][0],
HEPEVT.phep[i][1],
HEPEVT.phep[i][2],
HEPEVT.phep[i][3],
HEPEVT.vhep[i][0],
HEPEVT.vhep[i][1],
HEPEVT.vhep[i][2],
HEPEVT.vhep[i][3]);
}
}
} else if (!strcmp(option,"All")) {
for (Int_t i = 0; i<numpart; i++) {
new(clonesParticles[i]) TParticle(
HEPEVT.idhep[i],
HEPEVT.isthep[i],
HEPEVT.jmohep[i][0]-1,
HEPEVT.jmohep[i][1]-1,
HEPEVT.jdahep[i][0]-1,
HEPEVT.jdahep[i][1]-1,
HEPEVT.phep[i][0],
HEPEVT.phep[i][1],
HEPEVT.phep[i][2],
HEPEVT.phep[i][3],
HEPEVT.vhep[i][0],
HEPEVT.vhep[i][1],
HEPEVT.vhep[i][2],
HEPEVT.vhep[i][3]);
}
}
return numpart;
}
////////////////////////////////////////////////////////////////////////////////
///browse generator
void TGenerator::Browse(TBrowser *)
{
Draw();
gPad->Update();
}
////////////////////////////////////////////////////////////////////////////////
///*-*-*-*-*-*-*-*Compute distance from point px,py to objects in event*-*-*-*
///*-* =====================================================
///*-*
Int_t TGenerator::DistancetoPrimitive(Int_t px, Int_t py)
{
const Int_t big = 9999;
const Int_t inview = 0;
Int_t dist = big;
if (px > 50 && py > 50) dist = inview;
return dist;
}
////////////////////////////////////////////////////////////////////////////////
///
/// Insert one event in the pad list
///
void TGenerator::Draw(Option_t *option)
{
// Create a default canvas if a canvas does not exist
if (!gPad) {
gROOT->MakeDefCanvas();
if (gPad->GetVirtCanvas())
gPad->GetVirtCanvas()->SetFillColor(13);
}
static Float_t rbox = 1000;
Float_t rmin[3],rmax[3];
TView *view = gPad->GetView();
if (!strstr(option,"same")) {
if (view) { view->GetRange(rmin,rmax); rbox = rmax[2];}
gPad->Clear();
}
AppendPad(option);
view = gPad->GetView();
// compute 3D view
if (view) {
view->GetRange(rmin,rmax);
rbox = rmax[2];
} else {
view = TView::CreateView(1,0,0);
if (view) view->SetRange(-rbox,-rbox,-rbox, rbox,rbox,rbox );
}
const Int_t kColorProton = 4;
const Int_t kColorNeutron = 5;
const Int_t kColorAntiProton= 3;
const Int_t kColorPionPlus = 6;
const Int_t kColorPionMinus = 2;
const Int_t kColorKaons = 7;
const Int_t kColorElectrons = 0;
const Int_t kColorGamma = 18;
Int_t nProtons = 0;
Int_t nNeutrons = 0;
Int_t nAntiProtons= 0;
Int_t nPionPlus = 0;
Int_t nPionMinus = 0;
Int_t nKaons = 0;
Int_t nElectrons = 0;
Int_t nGammas = 0;
Int_t ntracks = fParticles->GetEntriesFast();
Int_t i,lwidth,color,lstyle;
TParticlePDG *ap;
TParticle *p;
const char *name;
Double_t etot,vx,vy,vz;
Int_t ninvol = 0;
for (i=0;i<ntracks;i++) {
p = (TParticle*)fParticles->UncheckedAt(i);
if(!p) continue;
ap = (TParticlePDG*)p->GetPDG();
vx = p->Vx();
vy = p->Vy();
vz = p->Vz();
if (vx*vx+vy*vy+vz*vz > rbox*rbox) continue;
Float_t pt = p->Pt();
if (pt < fPtCut) continue;
etot = p->Energy();
if (etot > 0.1) lwidth = Int_t(6*TMath::Log10(etot));
else lwidth = 1;
if (lwidth < 1) lwidth = 1;
lstyle = 1;
color = 0;
name = ap->GetName();
if (!strcmp(name,"n")) { if (!fShowNeutrons) continue;
color = kColorNeutron; nNeutrons++;}
if (!strcmp(name,"p")) { color = kColorProton; nProtons++;}
if (!strcmp(name,"p bar")) { color = kColorAntiProton; nAntiProtons++;}
if (!strcmp(name,"pi+")) { color = kColorPionPlus; nPionPlus++;}
if (!strcmp(name,"pi-")) { color = kColorPionMinus; nPionMinus++;}
if (!strcmp(name,"e+")) { color = kColorElectrons; nElectrons++;}
if (!strcmp(name,"e-")) { color = kColorElectrons; nElectrons++;}
if (!strcmp(name,"gamma")) { color = kColorGamma; nGammas++; lstyle = 3; }
if ( strstr(name,"K")) { color = kColorKaons; nKaons++;}
p->SetLineColor(color);
p->SetLineStyle(lstyle);
p->SetLineWidth(lwidth);
p->AppendPad();
ninvol++;
}
// event title
TPaveText *pt = new TPaveText(-0.94,0.85,-0.25,0.98,"br");
pt->AddText((char*)GetName());
pt->AddText((char*)GetTitle());
pt->SetFillColor(42);
pt->Draw();
// Annotate color codes
Int_t tcolor = 5;
if (gPad->GetFillColor() == 10) tcolor = 4;
TText *text = new TText(-0.95,-0.47,"Particles");
text->SetTextAlign(12);
text->SetTextSize(0.025);
text->SetTextColor(tcolor);
text->Draw();
text->SetTextColor(kColorGamma); text->DrawText(-0.95,-0.52,"(on screen)");
text->SetTextColor(kColorGamma); text->DrawText(-0.95,-0.57,"Gamma");
text->SetTextColor(kColorProton); text->DrawText(-0.95,-0.62,"Proton");
text->SetTextColor(kColorNeutron); text->DrawText(-0.95,-0.67,"Neutron");
text->SetTextColor(kColorAntiProton); text->DrawText(-0.95,-0.72,"AntiProton");
text->SetTextColor(kColorPionPlus); text->DrawText(-0.95,-0.77,"Pion +");
text->SetTextColor(kColorPionMinus); text->DrawText(-0.95,-0.82,"Pion -");
text->SetTextColor(kColorKaons); text->DrawText(-0.95,-0.87,"Kaons");
text->SetTextColor(kColorElectrons); text->DrawText(-0.95,-0.92,"Electrons,etc.");
text->SetTextColor(tcolor);
text->SetTextAlign(32);
char tcount[32];
snprintf(tcount,12,"%d",ntracks); text->DrawText(-0.55,-0.47,tcount);
snprintf(tcount,12,"%d",ninvol); text->DrawText(-0.55,-0.52,tcount);
snprintf(tcount,12,"%d",nGammas); text->DrawText(-0.55,-0.57,tcount);
snprintf(tcount,12,"%d",nProtons); text->DrawText(-0.55,-0.62,tcount);
snprintf(tcount,12,"%d",nNeutrons); text->DrawText(-0.55,-0.67,tcount);
snprintf(tcount,12,"%d",nAntiProtons); text->DrawText(-0.55,-0.72,tcount);
snprintf(tcount,12,"%d",nPionPlus); text->DrawText(-0.55,-0.77,tcount);
snprintf(tcount,12,"%d",nPionMinus); text->DrawText(-0.55,-0.82,tcount);
snprintf(tcount,12,"%d",nKaons); text->DrawText(-0.55,-0.87,tcount);
snprintf(tcount,12,"%d",nElectrons); text->DrawText(-0.55,-0.92,tcount);
text->SetTextAlign(12);
if (nPionPlus+nPionMinus) {
snprintf(tcount,31,"Protons/Pions= %4f",Float_t(nProtons)/Float_t(nPionPlus+nPionMinus));
} else {
strlcpy(tcount,"Protons/Pions= inf",31);
}
text->DrawText(-0.45,-0.92,tcount);
if (nPionPlus+nPionMinus) {
snprintf(tcount,12,"Kaons/Pions= %4f",Float_t(nKaons)/Float_t(nPionPlus+nPionMinus));
} else {
strlcpy(tcount,"Kaons/Pions= inf",31);
}
text->DrawText(0.30,-0.92,tcount);
}
////////////////////////////////////////////////////////////////////////////////
///*-*-*-*-*-*-*-*-*-*-*Execute action corresponding to one event*-*-*-*
///*-* =========================================
void TGenerator::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
if (gPad->GetView()) {
gPad->GetView()->ExecuteRotateView(event, px, py);
return;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Return the number of particles in the stack
Int_t TGenerator::GetNumberOfParticles() const
{
return fParticles->GetLast()+1;
}
////////////////////////////////////////////////////////////////////////////////
/// Returns pointer to primary number i;
///
TParticle *TGenerator::GetParticle(Int_t i) const
{
if (!fParticles) return 0;
Int_t n = fParticles->GetLast();
if (i < 0 || i > n) return 0;
return (TParticle*)fParticles->UncheckedAt(i);
}
////////////////////////////////////////////////////////////////////////////////
///
/// Paint one event
///
void TGenerator::Paint(Option_t *)
{
}
////////////////////////////////////////////////////////////////////////////////
///
/// Set Pt threshold below which primaries are not drawn
///
void TGenerator::SetPtCut(Float_t ptcut)
{
fPtCut = ptcut;
Draw();
gPad->Update();
}
////////////////////////////////////////////////////////////////////////////////
///
/// Set lower and upper values of the view range
///
void TGenerator::SetViewRadius(Float_t rbox)
{
SetViewRange(-rbox,-rbox,-rbox,rbox,rbox,rbox);
}
////////////////////////////////////////////////////////////////////////////////
///
/// Set lower and upper values of the view range
///
void TGenerator::SetViewRange(Float_t xmin, Float_t ymin, Float_t zmin, Float_t xmax, Float_t ymax, Float_t zmax)
{
TView *view = gPad->GetView();
if (!view) return;
view->SetRange(xmin,ymin,zmin,xmax,ymax,zmax);
Draw();
gPad->Update();
}
////////////////////////////////////////////////////////////////////////////////
///
/// Set flag to display or not neutrons
///
void TGenerator::ShowNeutrons(Bool_t show)
{
fShowNeutrons = show;
Draw();
gPad->Update();
}