add function to extract the number of time bins
[u/mrichter/AliRoot.git] / EVE / alice-macros / kine_tracks.C
CommitLineData
d810d0de 1// $Id$
2// Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4/**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
51346b82 7 * full copyright notice. *
d810d0de 8 **************************************************************************/
16718cdc 9
1b337638 10// Import tracks from kinematics-tree / particle-stack.
11// Preliminary/minimal solution.
12
15c12442 13#if !defined(__CINT__) || defined(__MAKECINT__)
14
15#include <TParticlePDG.h>
16
3c2901eb 17#include <TEveManager.h>
18#include <TEveTrackPropagator.h>
19
20#include <EveBase/AliEveEventManager.h>
21#include <EveBase/AliEveMagField.h>
22#include <EveBase/AliEveTrack.h>
23#include <EveBase/AliEveKineTools.h>
24
25#include <AliRunLoader.h>
26#include <AliStack.h>
27#include <AliMagF.h>
28
15c12442 29#endif
a7867742 30
6336e5d7 31// Use magnetic-field as retrieved from GRP.
32Bool_t g_kine_tracks_true_field = kTRUE;
33
34// Use Runge-Kutta track stepper.
35Bool_t g_kine_tracks_rk_stepper = kFALSE;
36
3c2901eb 37//==============================================================================
38
39void kine_track_propagator_setup(TEveTrackPropagator* trkProp);
40
41TEveTrackList*
42kine_tracks(Double_t min_pt = 0, Double_t min_p = 0,
43 Bool_t pdg_col = kTRUE, Bool_t recurse = kTRUE,
44 Bool_t use_track_refs = kTRUE);
45
46void kine_daughters(AliEveTrack* parent, AliStack* stack,
47 Double_t min_pt, Double_t min_p,
48 Bool_t pdg_col, Bool_t recurse);
49
50void set_track_color(AliEveTrack* t, Bool_t pdg_col);
51Color_t get_pdg_color(Int_t pdg);
52
53TEveElement*
54kine_track(Int_t label,
55 Bool_t import_mother = kTRUE, Bool_t import_daughters = kTRUE,
56 Bool_t pdg_col = kTRUE, Bool_t recurse = kTRUE,
57 TEveElement* cont = 0);
58
59void kine_hide_neutrals(TEveElement* el=0, Int_t level=0);
6336e5d7 60
61//==============================================================================
62
63void kine_track_propagator_setup(TEveTrackPropagator* trkProp)
64{
65 AliMagF* fld = AliEveEventManager::AssertMagField();
66
67 if (g_kine_tracks_true_field)
68 {
69 trkProp->SetMagFieldObj(new AliEveMagField(fld));
70 }
71 else
72 {
73 trkProp->SetMagField(-0.1*fld->SolenoidField());
74 }
75 if (g_kine_tracks_rk_stepper)
76 {
77 trkProp->SetStepper(TEveTrackPropagator::kRungeKutta);
78 }
79}
80
81//==============================================================================
82
84aff7a4 83TEveTrackList*
3c2901eb 84kine_tracks(Double_t min_pt, Double_t min_p,
85 Bool_t pdg_col, Bool_t recurse,
86 Bool_t use_track_refs)
1b337638 87{
d810d0de 88 AliRunLoader* rl = AliEveEventManager::AssertRunLoader();
1b337638 89 rl->LoadKinematics();
90 AliStack* stack = rl->Stack();
8661a211 91 if (!stack)
92 {
1b337638 93 Error("kine_tracks.C", "can not get kinematics.");
94 return 0;
95 }
96
84aff7a4 97 gEve->DisableRedraw();
51346b82 98
99 TEveTrackList* cont = new TEveTrackList("Kine Tracks");
fbc350a3 100 cont->SetMainColor(3);
8661a211 101 TEveTrackPropagator* trkProp = cont->GetPropagator();
102
6336e5d7 103 kine_track_propagator_setup(trkProp);
1b337638 104
84aff7a4 105 gEve->AddElement(cont);
1b337638 106 Int_t count = 0;
92bd3b8a 107 Int_t Np = stack->GetNprimary();
108 for (Int_t i = 0; i < Np; ++i)
43d68fc4 109 {
92bd3b8a 110 TParticle* p = stack->Particle(i);
111 if (p->GetStatusCode() <= 1)
43d68fc4 112 {
ef9d2241 113 if (p->Pt() < min_pt && p->P() < min_p) continue;
43d68fc4 114
115 ++count;
0e33c639 116 AliEveTrack* track = new AliEveTrack(p, i, trkProp);
51346b82 117
43d68fc4 118 //PH The line below is replaced waiting for a fix in Root
119 //PH which permits to use variable siza arguments in CINT
120 //PH on some platforms (alphalinuxgcc, solariscc5, etc.)
121 //PH track->SetName(Form("%s [%d]", p->GetName(), i));
122 char form[1000];
123 sprintf(form,"%s [%d]", p->GetName(), i);
124 track->SetName(form);
fc9514f5 125 track->SetStdTitle();
a77bb3bc 126 Int_t ml = p->GetMother(0);
127 if (ml != -1)
128 {
129 track->SetTitle(Form("%s\nMother label=%d\nMother Pdg=%d",
130 track->GetElementTitle(),
131 ml, stack->Particle(ml)->GetPdgCode()));
132 }
fc9514f5 133 set_track_color(track, pdg_col);
134
84aff7a4 135 gEve->AddElement(track, cont);
ef9d2241 136
137 if (recurse)
138 kine_daughters(track, stack, min_pt, min_p, pdg_col, recurse);
43d68fc4 139 }
1b337638 140 }
ef9d2241 141
43d68fc4 142 // set path marks
51346b82 143 AliEveKineTools kt;
ef9d2241 144 kt.SetDaughterPathMarks(cont, stack, recurse);
32e219c2 145 if (use_track_refs && rl->LoadTrackRefs() == 0)
9a040a65 146 {
147 kt.SetTrackReferences(cont, rl->TreeTR(), recurse);
8661a211 148 trkProp->SetEditPathMarks(kTRUE);
9a040a65 149 }
150 kt.SortPathMarks(cont, recurse);
43d68fc4 151
ef9d2241 152 //PH const Text_t* tooltip = Form("min pT=%.2lf, min P=%.2lf), N=%d", min_pt, min_p, count);
7be1e8cd 153 char tooltip[1000];
ef9d2241 154 sprintf(tooltip,"min pT=%.2lf, min P=%.2lf), N=%d", min_pt, min_p, count);
1b337638 155 cont->SetTitle(tooltip); // Not broadcasted automatically ...
1b337638 156
ef9d2241 157 cont->MakeTracks(recurse);
84aff7a4 158 gEve->EnableRedraw();
159 gEve->Redraw3D();
1b337638 160
161 return cont;
162}
a7867742 163
0e33c639 164void kine_daughters(AliEveTrack* parent, AliStack* stack,
ef9d2241 165 Double_t min_pt, Double_t min_p,
166 Bool_t pdg_col, Bool_t recurse)
167{
168 TParticle *p = stack->Particle(parent->GetLabel());
51346b82 169 if (p->GetNDaughters() > 0)
ef9d2241 170 {
84aff7a4 171 TEveTrackPropagator* rs = parent->GetPropagator();
51346b82 172 for (int d=p->GetFirstDaughter(); d>0 && d<=p->GetLastDaughter(); ++d)
173 {
ef9d2241 174 TParticle* dp = stack->Particle(d);
175 if (dp->Pt() < min_pt && dp->P() < min_p) continue;
176
0e33c639 177 AliEveTrack* dtrack = new AliEveTrack(dp, d, rs);
ef9d2241 178 char form[1000];
179 sprintf(form,"%s [%d]", dp->GetName(), d);
180 dtrack->SetName(form);
fc9514f5 181 dtrack->SetStdTitle();
182 set_track_color(dtrack, pdg_col);
183
84aff7a4 184 gEve->AddElement(dtrack, parent);
ef9d2241 185
186 if (recurse)
187 kine_daughters(dtrack, stack, min_pt, min_p, pdg_col, recurse);
188 }
189 }
190}
a7867742 191
3c2901eb 192//==============================================================================
193
194void set_track_color(AliEveTrack* t, Bool_t pdg_col)
fc9514f5 195{
196 if (pdg_col)
197 t->SetMainColor(get_pdg_color(t->GetPdg()));
198 else
fbc350a3 199 t->SetMainColor(30);
fc9514f5 200}
201
539d6798 202Color_t get_pdg_color(Int_t pdg)
203{
fc9514f5 204 // PDG color indices
205 static const Color_t DefCol = 30;
206 static const Color_t ECol = 5;
207 static const Color_t MuCol = 6;
51346b82 208 static const Color_t GammaCol = 7;
fc9514f5 209 static const Color_t MesCol1 = 3;
210 static const Color_t MesCol2 = 38;
211 static const Color_t BarCol = 10;
212
213 Int_t pdga = TMath::Abs(pdg);
214 Color_t col = DefCol;
a7867742 215
216 // elementary particles
217 if (pdga < 100) {
218 switch (pdga) {
51346b82 219 case 11:
220 col = ECol; break;
a7867742 221 case 12:
222 col = MuCol; break;
223 case 22:
224 col = GammaCol; break;
225 }
226 }
fc9514f5 227 // mesons and barions
228 else if (pdga < 100000) {
a7867742 229 Int_t i = pdga;
3c2901eb 230 // Int_t i0 = i%10; // Not used at the moment.
231 i /= 10;
51346b82 232 Int_t i1 = i%10; i /= 10;
233 Int_t i2 = i%10; i /= 10;
234 Int_t i3 = i%10; i /= 10;
a7867742 235 Int_t i4 = i%10;
236 //printf("pdg(%d) quark indices (%d,%d,%d,%d,%d) \n",pdg, i4,i3,i2, i1, i0);
237 // meson
238 if ((i3 == 0) && ( i4 < 2)){
239 col = MesCol1; // quarks: i1,i2 (spin = i0)
240 if(i1 == 3 || i2 == 3)
241 col = MesCol2;
242 } // barion
243 else if ( i2 >= i1 && i3 >= i2 ) {
244 col = BarCol; // quarks: i1,i2, i3 (spin = i0))
245 }
246 }
fc9514f5 247
a7867742 248 return col;
249}
c7b57c0b 250
3c2901eb 251//==============================================================================
c7b57c0b 252
84aff7a4 253TEveElement*
b65b3044 254kine_track(Int_t label,
3c2901eb 255 Bool_t import_mother, Bool_t import_daughters,
256 Bool_t pdg_col, Bool_t recurse,
257 TEveElement* cont)
c7b57c0b 258{
fc9514f5 259 // Create mother and daughters tracks with given label.
260 // mother -> particle with label
261 // daughters -> daughters of label
262
c7b57c0b 263 if (label < 0) {
264 Warning("kine_track", "label not set.");
265 return 0;
266 }
51346b82 267
d810d0de 268 AliRunLoader* rl = AliEveEventManager::AssertRunLoader();
c7b57c0b 269 rl->LoadKinematics();
270 AliStack* stack = rl->Stack();
f1b2cd0b 271 if (label >= stack->GetNtrack())
272 {
273 Warning("kine_track", "label out of range.");
274 return 0;
275 }
276
c7b57c0b 277 TParticle* p = stack->Particle(label);
278
b65b3044 279 if (import_mother || (import_daughters && p->GetNDaughters()))
c7b57c0b 280 {
84aff7a4 281 TEveTrackPropagator* rs = 0;
2caed564 282
b65b3044 283 if (cont == 0)
284 {
84aff7a4 285 TEveTrackList* tlist = new TEveTrackList
ef9d2241 286 (Form("Kinematics of %d", label, p->GetNDaughters()));
287 cont = tlist;
c7b57c0b 288
8661a211 289 TEveTrackPropagator* trkProp = tlist->GetPropagator();
290
6336e5d7 291 kine_track_propagator_setup(trkProp);
8661a211 292
c7b57c0b 293 char tooltip[1000];
b65b3044 294 sprintf(tooltip,"Ndaughters=%d", p->GetNDaughters());
ef9d2241 295 tlist->SetTitle(tooltip);
8661a211 296 trkProp->fMaxOrbs = 2;
297 trkProp->SetEditPathMarks(kTRUE);
fc9514f5 298
84aff7a4 299 gEve->AddElement(cont);
300 rs = tlist->GetPropagator();
2caed564 301 }
ef9d2241 302 else
303 {
0e33c639 304 // check if container is TEveTrackList or AliEveTrack (has rnr-style)
305 AliEveTrack* t = dynamic_cast<AliEveTrack*>(cont);
fc9514f5 306 if (t) {
84aff7a4 307 rs = t->GetPropagator();
fc9514f5 308 } else {
84aff7a4 309 TEveTrackList* l = dynamic_cast<TEveTrackList*>(cont);
fc9514f5 310 if (l)
84aff7a4 311 rs = l->GetPropagator();
ef9d2241 312 else
2caed564 313 Error("kine_tracks.C", "TrackRenderStyle not set.");
2caed564 314 }
c7b57c0b 315 }
2caed564 316
b65b3044 317 if (import_mother)
318 {
0e33c639 319 AliEveTrack* track = new AliEveTrack(p, label, rs);
c7b57c0b 320 char form[1000];
321 sprintf(form,"%s [%d]", p->GetName(), label);
322 track->SetName(form);
fc9514f5 323 track->SetStdTitle();
324 set_track_color(track, pdg_col);
2caed564 325
fc9514f5 326 track->MakeTrack();
84aff7a4 327 gEve->AddElement(track, cont);
fc9514f5 328 cont = track;
c7b57c0b 329 }
330
51346b82 331 if (import_daughters && p->GetNDaughters())
c7b57c0b 332 {
51346b82 333 for (int d=p->GetFirstDaughter(); d>0 && d<=p->GetLastDaughter(); ++d)
334 {
c7b57c0b 335 TParticle* dp = stack->Particle(d);
0e33c639 336 AliEveTrack* track = new AliEveTrack(dp, d, rs);
c7b57c0b 337 char form[1000];
338 sprintf(form,"%s [%d]", dp->GetName(), d);
339 track->SetName(form);
fc9514f5 340 track->SetStdTitle();
341 set_track_color(track, pdg_col);
342
2caed564 343 track->MakeTrack();
84aff7a4 344 gEve->AddElement(track, cont);
fc9514f5 345
346 if (recurse)
347 kine_daughters(track, stack, 0, 0, pdg_col, recurse);
c7b57c0b 348 }
349 }
350 }
351
84aff7a4 352 gEve->Redraw3D();
c7b57c0b 353 return cont;
354}
2caed564 355
3c2901eb 356//==============================================================================
357
358void kine_hide_neutrals(TEveElement* el, Int_t level)
359{
360 if (el == 0)
361 {
362 el = gEve->GetCurrentEvent()->FindChild("Kine Tracks");
363 if (!el)
364 return;
365 }
366
367 TEveTrack* t = dynamic_cast<TEveTrack*>(el);
368 if (t && t->GetCharge() == 0)
369 t->SetRnrSelf(kFALSE);
370
371 for (TEveElement::List_i i = el->BeginChildren(); i != el->EndChildren(); ++i)
372 {
373 kine_hide_neutrals(*i, level + 1);
374 }
375
376 if (level == 0)
377 gEve->Redraw3D();
378}