KaliVeda
Toolkit for HIC analysis
KVEventViewer.cpp
1 //Created by KVClassFactory on Fri Apr 12 15:48:03 2013
2 //Author: John Frankland,,,
3 
4 #include "KVEventViewer.h"
5 #include "TGeoMatrix.h"
6 #include "TGLViewer.h"
7 #include "TGLLightSet.h"
8 #include "TVirtualPad.h"
9 #include "TSystem.h"
10 #include "TRandom.h"
11 #include "TGLPerspectiveCamera.h"
12 #include <TBranch.h>
13 #include "KVNucleusEvent.h"
14 #include "TTree.h"
15 
17 
18 
19 
27 KVEventViewer::KVEventViewer(Int_t protoncolor, Int_t neutroncolor, Int_t highlightprotoncolor, Int_t highlightneutroncolor, Double_t freenucleonradius, Double_t nucleonradius, Double_t nuclearradiusparameter)
28  : fproton_color(protoncolor), fneutron_color(neutroncolor),
29  fProton_color(highlightprotoncolor), fNeutron_color(highlightneutroncolor),
30  free_nucleon_radius(freenucleonradius),
31  nucleon_radius(nucleonradius),
32  nuclear_radius_parameter(nuclearradiusparameter)
33 {
34  // protoncolor = color of protons
35  // neutroncolor = color of neutrons
36  // highlight...color = color of protons/neutrons in highlighted nuclei
37  // freenucleonradius = radius of free nucleons
38  // nucleonradius = radius of nucleons in nuclei
39  // nuclearradiusparameter = r0 in expression r=r0*A**1/3
40 
41  ivol = 0;
42 
43  geom = 0;
44 
45  fMaxVelocity = -1.;
46  fFixSeed = kFALSE;
47  fRefresh = 0;
48  fSeed = 863167;
49  fXYMode = kFALSE;
50  fMomentumSpace = kFALSE;
51  fScaleFactor = 1.;
52 
53  fSavePicture = kFALSE;
54  textInput = kFALSE;
55  theEvent = 0;
56 
57  fHighlightMode = kNoHighlight;
58  fAxesMode = kFALSE;
59 
60  fFixPerspective = kFALSE;
61 }
62 
63 
64 
67 
69 {
70  // Destructor
71 }
72 
73 
74 
77 
78 void KVEventViewer::DrawNucleus(KVNucleus& nucleus, const Char_t* frame)
79 {
80  // Draw nucleus
81 
82  Int_t N = nucleus.GetN();
83  Int_t Z = nucleus.GetZ();
84 
85  TVector3 V = (fMomentumSpace ? nucleus.GetFrame(frame, kFALSE)->GetMomentum() : nucleus.GetFrame(frame, kFALSE)->GetV());
86 
87  Bool_t Highlight = SetHighlight(&nucleus);
88 
89  if (nucleus.GetA() == 1) {
90  TGeoVolume* ball = 0;
91  if (Z == 0) ball = geom->MakeSphere("n", Nuc, 0., free_nucleon_radius);
92  else if (Z == 1) ball = geom->MakeSphere("p", Nuc, 0., free_nucleon_radius);
93  int color = 0;
94  if (Z == 0) color = (Highlight ? fNeutron_color : fneutron_color);
95  else if (Z == 1) color = (Highlight ? fProton_color : fproton_color);
96  ball->SetLineColor(color);
97  top->AddNode(ball, ivol++, new TGeoTranslation(fScaleFactor * V.X(), fScaleFactor * V.Y(), fScaleFactor * V.Z()));
98  return;
99  }
100 
101  double sph_rad = pow(nuclear_radius_parameter, 3.) * (N + Z);
102 
103  for (int i = 0; i < N; i++) {
104  TGeoVolume* ball = geom->MakeSphere("n", Nuc, 0., nucleon_radius);
105  ball->SetLineColor(Highlight ? fNeutron_color : fneutron_color);
106  double rvec = pow(gRandom->Uniform(0., sph_rad), .33333);
107  double x, y, z;
108  gRandom->Sphere(x, y, z, rvec);
109  top->AddNode(ball, ivol++, new TGeoTranslation(fScaleFactor * V.X() + x, fScaleFactor * V.Y() + y, fScaleFactor * V.Z() + z));
110  }
111  for (int i = 0; i < Z; i++) {
112  TGeoVolume* ball = geom->MakeSphere("p", Nuc, 0., nucleon_radius);
113  ball->SetLineColor(Highlight ? fProton_color : fproton_color);
114  double rvec = pow(gRandom->Uniform(0., sph_rad), .33333);
115  double x, y, z;
116  gRandom->Sphere(x, y, z, rvec);
117  top->AddNode(ball, ivol++, new TGeoTranslation(fScaleFactor * V.X() + x, fScaleFactor * V.Y() + y, fScaleFactor * V.Z() + z));
118  }
119 }
120 
121 
122 
125 
127 {
128  // Draw all particles in event which are "ok"
129 
130  if (geom) delete geom;
131  geom = new TGeoManager(Form("Event%d", event->GetNumber()), "Event view");
132  geom->SetNsegments(500);
133  matEmptySpace = new TGeoMaterial("EmptySpace", 0, 0, 0);
134  matNuc = new TGeoMaterial("Nuc", .938, 1., 10000.);
135  matBox = new TGeoMaterial("Box", .938, 1., 10000.);
136 
137  EmptySpace = new TGeoMedium("Empty", 1, matEmptySpace);
138  Nuc = new TGeoMedium("Nuc", 1, matNuc);
139  Box = new TGeoMedium("Box", 1, matBox);
140 
141  top = geom->MakeBox("WORLD", EmptySpace, 30, 30, 30);
143 
144  // Find biggest velocity/momentum & biggest fragment
145  maxV = 0;
146  maxZ = 0;
147  if (fMomentumSpace) fScaleFactor = 1.e-2;
148  else fScaleFactor = 1.;
149  for (auto& nuc : EventOKIterator(event)) {
150  Double_t v = fScaleFactor * (fMomentumSpace ? nuc.GetFrame(frame, kFALSE)->GetMomentum().Mag() :
151  nuc.GetFrame(frame, kFALSE)->GetV().Mag());
152  if (v > maxV) maxV = v;
153  if (nuc.GetZ() > maxZ) maxZ = nuc.GetZ();
154  }
155  // scale down
156  maxV *= 0.5;
157  if (fFixPerspective) {
158  // fixed perspective mode
159  // calculate maxV once (first event), store it in fMaxVelocity, then use
160  // for all other events
161  if (fMaxVelocity > 0) maxV = fMaxVelocity;
162  else fMaxVelocity = maxV;
163  } else if (fMaxVelocity > 0) maxV = fMaxVelocity;
164 
165  TGeoVolume* box = geom->MakeSphere("box", Box, 0.99 * maxV, maxV);
166  box->SetLineColor(kBlack);
167  box->SetTransparency(100);
168  top->AddNode(box, 1000);
169 
170  // set volume (nucleon) counter to 0
171  ivol = 0;
172 
173  if (fFixSeed) {
174  if (!(event->GetNumber() % fRefresh)) fSeed += 7;
176  }
177 
178  for (auto& nuc : EventOKIterator(event)) DrawNucleus(nuc, frame);
179 
180  geom->CloseGeometry();
181 
183  top->Draw("ogl");
184  TGLViewer* view = (TGLViewer*)gPad->GetViewer3D();
185  if (fAxesMode) view->SetGuideState(2, 1, 0, 0);
186  view->SetClearColor(kWhite);
187  view->SetSmoothLines(1);
188  view->SetSmoothPoints(1);
196  ((TGLOrthoCamera&)view->CurrentCamera()).SetEnableRotate(kTRUE);
197  if (fXYMode) view->SetOrthoCamera(TGLViewer::kCameraOrthoZOY, 0.9, 0, 0, 0, TMath::Pi() / 2.); //TMath::Pi()/8.,TMath::Pi()/8.);
198  else view->SetOrthoCamera(TGLViewer::kCameraOrthoZOY, 0.9, 0, 0, TMath::Pi() / 8., TMath::Pi() / 8.);
199 // if(fSavePicture) view->SavePicture(Form("Event%d.gif",event->GetNumber()));
200  if (fSavePicture) view->SavePicture(Form("Event-%d.png", event->GetNumber()));
201 }
202 
203 
204 
206 
207 void KVEventViewer::DrawEvent(KVEvent* event, const Char_t* frame) const
208 {
209  const_cast<KVEventViewer*>(this)->DrawEvent(event, frame);
210 }
211 
212 
213 
216 
218 {
219  // Read events from branch of a TTree
220 
222  eventbranch->SetAddress(&theEvent);
223  textInput = kFALSE;
224  theTree = eventbranch->GetTree();
226  eventCounter = 0;
227 }
228 
229 
230 
233 
234 void KVEventViewer::SetInput(const char* filename)
235 {
236  // Read events from file
237 
238  eventFile.open(filename);
239  if (!eventFile.good()) {
240  Error("SetInput", "Problem opening %s", filename);
241  return;
242  }
243  eventCounter = 0;
244  textInput = kTRUE;
245 }
246 
247 
248 
251 
253 {
254  // Read an event from input source
255 
256  if (textInput) {
257  if (!theEvent) theEvent = new KVNucleusEvent;
258  ReadTextEvent();
259  } else {
260  ReadTreeEvent();
261  }
262 
263 }
264 
265 
266 
273 
275 {
276  // Read event from text input
277  // We assume a file containing, for each event,
278  // the multiplicity, N,
279  // followed by N lines containing Z Vx Vy Vz
280  // the atomic number and velocity components (in cm/ns)
281 
282  theEvent->Clear();
283 
284  Double_t V[3];
285  Int_t Z;
286 
287  Int_t nballs;
288  eventFile >> nballs;
289  eventCounter++;
290 
292 
293  for (int i = 0; i < nballs; i++) {
294 
295  eventFile >> Z >> V[0] >> V[1] >> V[2];
296 
297  auto nuc = theEvent->AddNucleus();
298  assert(nuc);
299  nuc->SetZ(Z);
300  TVector3 v(V);
301  nuc->SetVelocity(v);
302 
303  }
304 }
305 
306 
307 
309 
311 {
312  if (eventCounter == (Int_t)NtreeEntries) {
313  Warning("ReadTreeEvent", "All events have been read");
314  return;
315  }
317 }
318 
319 
320 
323 
325 {
326  // Draw next event read from input source
327 
328  ReadEvent();
330 }
331 
332 
333 
336 
338 {
339  // Decide whether or not to highlight this nucleus in the event display
340 
341  if (fHighlightMode == kHighlightZmax && n->GetZ() == maxZ) return kTRUE;
342  else if (fHighlightMode == kHighlightParameter && n->GetParameters()->IsValue("EventViewer.Highlight", 1)) return kTRUE;
343  return kFALSE;
344 }
345 
346 
int Int_t
#define SafeDelete(p)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
kBlack
kWhite
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
R__EXTERN TRandom * gRandom
char * Form(const char *fmt,...)
#define gPad
Class for iterating over "OK" nuclei in events accessed through base pointer/reference.
virtual void SetNumber(UInt_t num)
Definition: KVBase.h:216
Draw events in 3D using OpenGL.
Definition: KVEventViewer.h:52
Long64_t NtreeEntries
Definition: KVEventViewer.h:90
virtual Bool_t SetHighlight(KVNucleus *n)
Decide whether or not to highlight this nucleus in the event display.
Double_t nucleon_radius
radius of nucleons in nuclei
Definition: KVEventViewer.h:59
void ReadEvent()
Read an event from input source.
void DrawNextEvent()
Draw next event read from input source.
Double_t fMaxVelocity
Definition: KVEventViewer.h:62
Int_t fproton_color
proton colour
Definition: KVEventViewer.h:54
Double_t fScaleFactor
Definition: KVEventViewer.h:68
Int_t fProton_color
proton colour for highlighted nuclei
Definition: KVEventViewer.h:56
Int_t eventCounter
Definition: KVEventViewer.h:86
TGeoVolume * top
Definition: KVEventViewer.h:80
virtual ~KVEventViewer()
Destructor.
Int_t fHighlightMode
Definition: KVEventViewer.h:95
TGeoMaterial * matNuc
Definition: KVEventViewer.h:74
Double_t nuclear_radius_parameter
to calculate radii of nuclei
Definition: KVEventViewer.h:60
void DrawEvent(KVEvent *, const Char_t *frame="")
Draw all particles in event which are "ok".
TGeoMedium * Nuc
Definition: KVEventViewer.h:78
void SetInput(TBranch *eventbranch)
Read events from branch of a TTree.
TGeoManager * geom
Definition: KVEventViewer.h:81
Int_t fNeutron_color
neutron colour for highlighted nuclei
Definition: KVEventViewer.h:57
ifstream eventFile
Definition: KVEventViewer.h:85
TTree * theTree
Definition: KVEventViewer.h:89
Double_t maxV
largest velocity of event
Definition: KVEventViewer.h:92
KVEvent * theEvent
Definition: KVEventViewer.h:83
TGeoMedium * Box
Definition: KVEventViewer.h:79
Double_t free_nucleon_radius
radius of free nucleons
Definition: KVEventViewer.h:58
TGeoMedium * EmptySpace
Definition: KVEventViewer.h:77
Int_t ivol
geovolume counter
Definition: KVEventViewer.h:70
void DrawNucleus(KVNucleus &, const Char_t *frame="")
Draw nucleus.
Bool_t fSavePicture
kTRUE to save in GIF file
Definition: KVEventViewer.h:71
Int_t fneutron_color
neutron colour
Definition: KVEventViewer.h:55
Int_t maxZ
largest Z of event
Definition: KVEventViewer.h:93
Bool_t fAxesMode
Definition: KVEventViewer.h:96
Bool_t fMomentumSpace
kTRUE to show event in momentum space
Definition: KVEventViewer.h:67
Bool_t fFixPerspective
Definition: KVEventViewer.h:98
Bool_t textInput
Definition: KVEventViewer.h:87
TGeoMaterial * matBox
Definition: KVEventViewer.h:75
TGeoMaterial * matEmptySpace
Definition: KVEventViewer.h:73
Abstract base class container for multi-particle events.
Definition: KVEvent.h:67
void Clear(Option_t *opt="")
Definition: KVEvent.h:238
KVNucleus * AddNucleus()
Definition: KVEvent.cpp:109
An event container for KVNucleus objects.
Description of properties and kinematics of atomic nuclei.
Definition: KVNucleus.h:126
Int_t GetA() const
Definition: KVNucleus.cpp:802
Int_t GetN() const
Return the number of neutron.
Definition: KVNucleus.cpp:784
Int_t GetZ() const
Return the number of proton / atomic number.
Definition: KVNucleus.cpp:773
TVector3 GetMomentum() const
Definition: KVParticle.h:604
KVParticle const * GetFrame(const Char_t *frame, Bool_t warn_and_return_null_if_unknown=kTRUE) const
Definition: KVParticle.cpp:865
TVector3 GetV() const
Definition: KVParticle.h:671
TTree * GetTree() const
virtual void SetAddress(void *add)
void SetLight(ELight light, Bool_t on)
void SetGuideState(Int_t axesType, Bool_t axesDepthTest, Bool_t referenceOn, const Double_t *referencePos)
TGLLightSet * GetLightSet() const
TGLCamera & CurrentCamera() const
void SetCurrentCamera(ECameraType camera)
Bool_t SavePicture()
void SetSmoothPoints(Bool_t s)
void SetOrthoCamera(ECameraType camera, Double_t zoom, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate)
void SetSmoothLines(Bool_t s)
void SetClearColor(Color_t col)
void CloseGeometry(Option_t *option="d")
TGeoVolume * MakeBox(const char *name, TGeoMedium *medium, Double_t dx, Double_t dy, Double_t dz)
TGeoVolume * MakeSphere(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t themin=0, Double_t themax=180, Double_t phimin=0, Double_t phimax=360)
void SetTopVolume(TGeoVolume *vol)
void SetNsegments(Int_t nseg)
void SetVisContainers(Bool_t flag=kTRUE) override
virtual TGeoNode * AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=nullptr, Option_t *option="")
void Draw(Option_t *option="") override
void SetLineColor(Color_t lcolor) override
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual void Error(const char *method, const char *msgfmt,...) const
virtual void SetSeed(ULong_t seed=0)
virtual Double_t Uniform(Double_t x1, Double_t x2)
virtual void Sphere(Double_t &x, Double_t &y, Double_t &z, Double_t r)
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
virtual Long64_t GetEntries() const
Double_t Z() const
Double_t Y() const
Double_t X() const
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Double_t y[n]
Double_t x[n]
const Int_t n
constexpr Double_t Pi()
v
ClassImp(TPyArg)