2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
4 /**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
7 * full copyright notice. *
8 **************************************************************************/
11 #include "AliEveVSDCreator.h"
13 #include "AliEveEventManager.h"
16 #include <AliITSLoader.h>
17 #include <AliTPCTrackHitsV2.h>
20 #include <AliESDEvent.h>
22 #include <AliTPCclusterMI.h>
23 #include <AliTPCClustersRow.h>
24 #include <AliITSclusterV2.h>
25 #include <AliESDkink.h>
26 #include <AliESDtrack.h>
29 #include <AliRunLoader.h>
30 #include <AliTPCParam.h>
32 #include <TDatabasePDG.h>
36 //______________________________________________________________________________
38 // Create VSD file from ALICE data.
40 ClassImp(AliEveVSDCreator)
42 AliEveVSDCreator::AliEveVSDCreator(const Text_t* name, const Text_t* title) :
54 // Particles not in ROOT's PDG database occuring in ALICE
55 AliPDG::AddParticlesToPdgDataBase();
57 TDatabasePDG *pdgDB = TDatabasePDG::Instance();
58 // const Int_t kspe=50000000;
59 const Int_t kion=10000000;
61 const Double_t kAu2Gev=0.9314943228;
62 const Double_t khSlash = 1.0545726663e-27;
63 const Double_t kErg2Gev = 1/1.6021773349e-3;
64 const Double_t khShGev = khSlash*kErg2Gev;
65 const Double_t kYear2Sec = 3600*24*365.25;
67 pdgDB->AddParticle("Deuteron","Deuteron",2*kAu2Gev+8.071e-3,kTRUE,
68 0,1,"Ion",kion+10020);
69 pdgDB->AddParticle("Triton","Triton",3*kAu2Gev+14.931e-3,kFALSE,
70 khShGev/(12.33*kYear2Sec),1,"Ion",kion+10030);
71 pdgDB->AddParticle("Alpha","Alpha",4*kAu2Gev+2.424e-3,kTRUE,
72 khShGev/(12.33*kYear2Sec),2,"Ion",kion+20040);
73 pdgDB->AddParticle("HE3","HE3",3*kAu2Gev+14.931e-3,kFALSE,
74 0,2,"Ion",kion+20030);
77 // AliKalmanTrack::SetConvConst(1);
80 /******************************************************************************/
82 void AliEveVSDCreator::CreateVSD(const Text_t* vsdFile)
84 // Create the VSD for current event in AliEveEventManager.
85 // Result is stored in vsdFile.
87 // Needs to be extended to support conversion of multiple events.
89 static const TEveException kEH("AliEveVSDCreator::CreateVSD ");
91 fRunLoader = AliEveEventManager::AssertRunLoader();
94 printf("%s open seems ok. Now loading sim data.\n", kEH.Data());
96 fRunLoader->LoadHeader();
97 fRunLoader->LoadKinematics();
98 fRunLoader->LoadTrackRefs();
99 fRunLoader->LoadHits();
104 printf("%s opening output TEveVSD.\n", kEH.Data());
106 TFile* file = TFile::Open(vsdFile, "RECREATE", "ALICE Visualization Summary Data");
107 fDirectory = new TDirectoryFile("Event0", "");
110 printf("%s creating trees now ...\n", kEH.Data());
115 printf("%s trees created, closing files.\n", kEH.Data());
124 // clean after the TEveVSD data was sucessfuly written
137 printf("%s all done.\n", kEH.Data());
140 void AliEveVSDCreator::CreateTrees()
142 // Create and fill the output trees by calling all the
143 // ConvertXyzz() functions.
144 // Exceptions from individual functions are displayed as warnings.
146 static const TEveException kEH("AliEveVSDCreator::CreateTrees ");
149 throw(kEH + "output directory not set.");
153 printf("%sConvertKinematics.\n", kEH.Data());
155 } catch(TEveException& exc) { Warning(kEH, exc); }
157 Warning(kEH, "Explicitly abandoning further conversion.");
162 printf("%sConvertHits.\n", kEH.Data());
164 } catch(TEveException& exc) { Warning(kEH, exc); }
168 printf("%sConvertClusters.\n", kEH.Data());
170 } catch(TEveException& exc) { Warning(kEH, exc); }
174 printf("%sConvertRecTracks.\n", kEH.Data());
176 } catch(TEveException& exc) {
177 Warning(exc, "skipping AliEveV0 extraction.");
178 goto end_esd_processing;
183 printf("%sConvertV0.\n", kEH.Data());
185 } catch(TEveException& exc) { Warning(kEH, exc); }
189 printf("%sConvertKinks.\n", kEH.Data());
191 } catch(TEveException& exc) { Warning(kEH, exc); }
197 printf("%sConvertGenInfo.\n", kEH.Data());
199 } catch(TEveException& exc) { Warning(kEH, exc); }
204 /******************************************************************************/
206 /******************************************************************************/
208 void AliEveVSDCreator::ConvertKinematics()
210 // Convert kinematics.
211 // Track references are not stored, they should be.
213 static const TEveException kEH("AliEveVSDCreator::ConvertKinematics ");
216 throw (kEH + "kinematics already converted");
218 AliStack* stack = fRunLoader->Stack();
220 throw(kEH + "stack is null.");
223 fTreeK = new TTree("Kinematics", "TParticles sorted by Label");
225 Int_t nentries = stack->GetNtrack();
226 std::vector<TEveMCTrack> vmc(nentries);
227 for (Int_t idx=0; idx<nentries; idx++) {
228 TParticle* tp = stack->Particle(idx);
230 vmc[idx].fLabel = idx;
233 // read track refrences
234 // functionality now in AliEveKineTools.
236 TTree* fTreeTR = fRunLoader->TreeTR();
239 Warning(kEH, "no TrackRefs; some data will not be available.");
241 TClonesArray* RunArrayTR = 0;
242 fTreeTR->SetBranchAddress("AliRun", &RunArrayTR);
244 Int_t nPrimaries = (Int_t) fTreeTR->GetEntries();
245 for (Int_t iPrimPart = 0; iPrimPart<nPrimaries; iPrimPart++) {
246 // printf("T0 fTreeTR->GetEntry(%d) \n",iPrimPart);
247 fTreeTR->GetEntry(iPrimPart);
248 // printf("END fTreeTR->GetEntry(%d) \n",iPrimPart);
250 for (Int_t iTrackRef = 0; iTrackRef < RunArrayTR->GetEntriesFast(); iTrackRef++) {
251 AliTrackReference *trackRef = (AliTrackReference*)RunArrayTR->At(iTrackRef);
252 Int_t track = trackRef->GetTrack();
253 if(track < nentries && track > 0){
254 TEveMCTrack& mct = vmc[track];
255 if(trackRef->TestBit(kNotDeleted)) {
257 mct.t_decay = trackRef->GetTime();
258 mct.V_decay.x = trackRef->X();
259 mct.V_decay.y = trackRef->Y();
260 mct.V_decay.z = trackRef->Z();
261 mct.P_decay.x = trackRef->Px();
262 mct.P_decay.y = trackRef->Py();
263 mct.P_decay.z = trackRef->Pz();
264 if(TMath::Abs(mct.GetPdgCode()) == 11)
265 mct.decayed = false; // a bug in TreeTR
273 fTreeK->Branch("K", "TEveMCTrack", &fpK, fBuffSize);
275 printf("sizeofvmc = %d\n", (Int_t) vmc.size());
276 for(std::vector<TEveMCTrack>::iterator k=vmc.begin(); k!=vmc.end(); ++k) {
277 TEveMCTrack& mct = *k;
281 Int_t mi = mct.fLabel;
283 while(m->GetMother(0) != -1) {
285 printf("cnt %d mi=%d, mo=%d\n", cnt, mi, m->GetMother(0));
287 mi = m->GetMother(0);
296 fTreeK->BuildIndex("fLabel");
299 /******************************************************************************/
301 /******************************************************************************/
307 const char* fName; // Detector name.
308 const char* fHitbranch; // Name of branch containing hits.
309 unsigned char fDetidx; // Index identifying the detector internally.
312 Detector_t fgDetectors[] = {
313 { "ITS", "AliITShit", 0 },
314 { "TPC", "AliTPCTrackHitsV2", 1 },
315 { "TRD", "AliTRDhit", 2 },
316 { "TOF", "AliTOFhit", 3 }
317 // { "HMPID", "AliHMPIDhit", 4 },
322 /******************************************************************************/
324 void AliEveVSDCreator::ConvertHits()
327 // TPC hits are handled specially as they are compressed - only mayor
330 static const TEveException kEH("AliEveVSDCreator::ConvertHits ");
333 throw(kEH + "hits already converted.");
336 fTreeH = new TTree("Hits", "Combined detector hits.");
337 fTreeH->Branch("H", "TEveHit", &fpH, fBuffSize);
339 std::map<Int_t, Int_t> hmap;
340 // parameters for ITS, TPC hits filtering
341 Float_t x,y,z, x1,y1,z1;
342 Float_t tpcSqrRes = fTPCHitRes*fTPCHitRes;
343 Float_t trdSqrRes = fTRDHitRes*fTRDHitRes;
346 // load hits from the rest of detectors
347 while (fgDetectors[l].fName != 0)
349 Detector_t& det = fgDetectors[l++];
356 TTree* treeh = fRunLoader->GetTreeH(det.fName, false);
358 Warning(kEH, Form("no hits for %s.", det.fName));
361 AliTPCTrackHitsV2 hv2, *hv2p = &hv2;
362 treeh->SetBranchAddress("TPC2", &hv2p);
363 Int_t np = treeh->GetEntries();
364 for (Int_t i = 0; i < np; ++i)
367 Int_t evaIdx = np -i -1;
368 if (hv2.First() == 0) continue;
371 AliHit* ah = hv2.GetHit();
372 x1 = ah->X(); y1 = ah->Y(); z1 = ah->Z();
373 if ((x-x1)*(x-x1) + (y-y1)*(y-y1) + (z-z1)*(z-z1) > tpcSqrRes)
375 fH.fDetId = det.fDetidx;
377 fH.fLabel = ah->Track();
378 fH.fEvaLabel = evaIdx;
379 fH.fV.fX = x1; fH.fV.fY = y1; fH.fV.fZ = z1;
382 x = x1; y = y1; z = z1;
385 } while (hv2.Next());
387 // printf("%d entries in TPChits \n",count);
392 TTree* treeh = fRunLoader->GetTreeH(det.fName, false);
394 Warning(kEH, Form("no hits for %s.", det.fName));
397 TClonesArray *arr = new TClonesArray(det.fHitbranch);
398 treeh->SetBranchAddress(det.fName, &arr);
399 Int_t np = treeh->GetEntries();
400 // in TreeH files hits are grouped in clones arrays
401 // each eva particle has its own clone array
402 for (Int_t i = 0; i < np; ++i)
405 Int_t evaIdx = np -i -1;
406 Int_t nh=arr->GetEntriesFast();
408 // printf("%d entry %d hits for primary %d \n", i, nh, evaIdx);
409 for (Int_t j = 0; j < nh; ++j)
411 AliHit* aliHit = (AliHit*)arr->UncheckedAt(j);
412 fH.fDetId = det.fDetidx;
414 fH.fLabel = aliHit->GetTrack();
415 fH.fEvaLabel = evaIdx;
416 fH.fV.Set(aliHit->X(), aliHit->Y(), aliHit->Z());
417 if (det.fDetidx == 2)
419 x1=aliHit->X();y1=aliHit->Y();z1=aliHit->Z();
420 if ((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1) < trdSqrRes) continue;
435 for(std::map<Int_t, Int_t>::iterator j=hmap.begin(); j!=hmap.end(); ++j)
437 GetGeninfo(j->first)->fNHits += j->second;
441 /******************************************************************************/
443 /******************************************************************************/
445 void AliEveVSDCreator::ConvertClusters()
449 // Only supported for ITS and TPC at the moment, see dedicated
450 // functions ConvertITSClusters() and ConvertTPCClusters().
452 // It should be possible now to do this in a general manner (with
453 // the alignment framework).
455 static const TEveException kEH("AliEveVSDCreator::ConvertClusters ");
458 throw(kEH + "clusters already converted.");
461 fTreeC = new TTree("Clusters", "rec clusters");
462 fTreeC->Branch("C", "TEveCluster", &fpC, fBuffSize);
465 ConvertITSClusters();
466 } catch(TEveException& exc) { Warning(kEH, exc); }
469 ConvertTPCClusters();
470 } catch(TEveException& exc) { Warning(kEH, exc); }
473 /******************************************************************************/
475 void AliEveVSDCreator::ConvertTPCClusters()
477 // Convert TPC clusters and transform them to global coordinates.
479 static const TEveException kEH("AliEveVSDCreator::ConvertTPCClusters ");
481 fRunLoader->LoadRecPoints("TPC");
482 TTree* tree = fRunLoader->GetTreeR("TPC", false);
484 throw(kEH + "'TreeR' not found.");
486 AliTPCClustersRow clrow, *clrowp = &clrow;
488 clrow.SetClass("AliTPCclusterMI");
489 tree->SetBranchAddress("Segment", &clrowp);
493 Int_t nEnt = tree->GetEntries();
494 for (Int_t n = 0; n < nEnt; ++n)
497 nClusters += clrow.GetArray()->GetEntriesFast();
500 std::map<Int_t, Int_t> cmap;
502 for (Int_t n = 0; n < tree->GetEntries(); ++n)
505 Int_t ncl = clrow.GetArray()->GetEntriesFast();
510 if (clrow.GetArray())
512 // cl = new AliTPCclusterMI(*(AliTPCclusterMI*)clrow.GetArray()->UncheckedAt(ncl));
513 cl = (AliTPCclusterMI*)clrow.GetArray()->UncheckedAt(ncl);
514 if (cl->GetLabel(0) >= 0)
516 Float_t g[3]; //global coordinates
520 fC.fSubdetId = clrow.GetID();
521 fC.fLabel[0] = cl->GetLabel(0);
522 fC.fLabel[1] = cl->GetLabel(1);
523 fC.fLabel[2] = cl->GetLabel(2);
529 while (i < 3 && fC.fLabel[i])
530 cmap[fC.fLabel[i++]]++;
538 for (std::map<Int_t, Int_t>::iterator j=cmap.begin(); j!=cmap.end(); ++j)
540 GetGeninfo(j->first)->fNClus += j->second;
544 /******************************************************************************/
546 void AliEveVSDCreator::ConvertITSClusters()
548 // Convert ITS clusters and transform them to global coordinates.
550 static const TEveException kEH("AliEveVSDCreator::ConvertITSClusters ");
552 fRunLoader->LoadRecPoints("ITS");
553 TTree* tree = fRunLoader->GetTreeR("ITS", false);
555 throw(kEH + "'TreeR' not found.");
558 AliITSLoader *itsLd = (AliITSLoader*) fRunLoader->GetLoader("ITSLoader");
559 AliITSgeom *geom = itsLd->GetITSgeom();
561 //printf("alice ITS geom %p \n",geom );
564 throw(kEH + "can not find ITS geometry");
566 TClonesArray *arr = new TClonesArray("AliITSclusterV2");
567 tree->SetBranchAddress("Clusters", &arr);
568 Int_t nmods = tree->GetEntries();
570 std::map<Int_t, Int_t> cmap;
572 for (Int_t mod = 0; mod < nmods; ++mod)
575 Int_t nc=arr->GetEntriesFast();
576 for (Int_t j = 0; j < nc; ++j)
578 AliITSclusterV2* recp = (AliITSclusterV2*) arr->UncheckedAt(j);
581 geom->GetRotMatrix(mod,rot);
583 geom->GetModuleId(mod,lay,lad,det);
585 geom->GetTrans(lay,lad,det,tx,ty,tz);
587 Double_t alpha=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
588 Double_t phi1=TMath::Pi()/2+alpha;
589 if (lay == 1) phi1+=TMath::Pi();
591 Float_t cp=TMath::Cos(phi1), sp=TMath::Sin(phi1);
592 Float_t r=tx*cp+ty*sp;
593 gc[0] = r*cp - recp->GetY()*sp;
594 gc[1] = r*sp + recp->GetY()*cp;
595 gc[2] = recp->GetZ();
599 fC.fLabel[0] = recp->GetLabel(0);
600 fC.fLabel[1] = recp->GetLabel(1);
601 fC.fLabel[2] = recp->GetLabel(2);
602 fC.fV.fX = r*cp - recp->GetY()*sp;
603 fC.fV.fY = r*sp + recp->GetY()*cp;
604 fC.fV.fZ = recp->GetZ();
607 while(i < 3 && fC.fLabel[i])
608 cmap[fC.fLabel[i++]]++;
612 for (std::map<Int_t, Int_t>::iterator j=cmap.begin(); j!=cmap.end(); ++j)
614 GetGeninfo(j->first)->fNClus += j->second;
620 /******************************************************************************/
622 /******************************************************************************/
624 void AliEveVSDCreator::ConvertRecTracks()
626 // Convert reconstructed tracks.
628 static const TEveException kEH("AliEveVSDCreator::ConvertRecTracks ");
631 throw(kEH + "tracks already converted.");
633 AliESDEvent* esdEvent = AliEveEventManager::AssertESD();
636 fTreeR = new TTree("RecTracks", "rec tracks");
638 fTreeR->Branch("R", "TEveRecTrack", &fpR, 512*1024,1);
640 // reconstructed tracks
641 AliESDtrack* esdTrack;
643 for (Int_t n = 0; n < esdEvent->GetNumberOfTracks(); ++n)
645 esdTrack = esdEvent->GetTrack(n);
647 fR.fLabel = esdTrack->GetLabel();
648 fR.fStatus = (Int_t) esdTrack->GetStatus();
649 fR.fSign = (Int_t) esdTrack->GetSign();
650 esdTrack->GetXYZ(dbuf); fR.fV.Set(dbuf);
651 esdTrack->GetPxPyPz(dbuf); fR.fP.Set(dbuf);
652 Double_t ep = esdTrack->GetP();
653 fR.fBeta = ep/TMath::Sqrt(ep*ep + TMath::C()*TMath::C()*esdTrack->GetMass()*esdTrack->GetMass());
656 fTreeR->BuildIndex("label");
660 /******************************************************************************/
662 void AliEveVSDCreator::ConvertV0()
664 // Convert reconstructed V0s.
666 static const TEveException kEH("AliEveVSDCreator::ConvertV0 ");
669 throw(kEH + "AliEveV0 already converted.");
671 AliESDEvent* esdEvent = AliEveEventManager::AssertESD();
674 fTreeV0 = new TTree("AliEveV0", "AliEveV0 points");
676 fTreeV0->Branch("AliEveV0", "TEveRecV0", &fpV0, 512*1024,1);
678 for (Int_t n = 0; n < esdEvent->GetNumberOfV0s(); ++n)
680 AliESDv0 *av = esdEvent->GetV0(n);
681 AliESDtrack *trackN = esdEvent->GetTrack(av->GetNindex()); // negative daughter
682 AliESDtrack *trackP = esdEvent->GetTrack(av->GetPindex()); // positive daughter
686 fV0.fStatus = av->GetStatus();
687 // Point of closest approach
688 av->GetXYZ(pos[0],pos[1],pos[2]);
689 fV0.fVCa.fX = pos[0];
690 fV0.fVCa.fY = pos[1];
691 fV0.fVCa.fZ = pos[2];
692 // set birth vertex of neutral particle
693 av->GetXYZ(pos[0], pos[1], pos[2]);
694 fV0.fV0Birth.Set(pos);
696 // momentum and position of negative particle
697 av->GetParamN()->GetPxPyPz(pos);
699 av->GetParamN()->GetXYZ(pos);
702 // momentum and position of positive particle
703 av->GetParamP()->GetPxPyPz(pos);
705 av->GetParamP()->GetXYZ(pos);
708 fV0.fLabel = 0; // !!!! mother label unknown
709 fV0.fPdg = av->GetPdgCode();
712 fV0.fDLabel[0] = TMath::Abs(trackN->GetLabel());
713 fV0.fDLabel[1] = TMath::Abs(trackP->GetLabel());
715 // printf("AliEveV0 convert labels(%d,%d) index(%d,%d)\n",
716 // fV0.d_label[0], fV0.d_label[1],
717 // av->GetNIndex(), av->GetPIndex());
721 // if (esdEvent->GetNumberOfV0s()) fTreeV0->BuildIndex("label");
725 /******************************************************************************/
727 void AliEveVSDCreator::ConvertKinks()
729 // Convert reconstructed kinks.
731 static const TEveException kEH("AliEveVSDCreator::ConvertKinks ");
734 throw(kEH + "Kinks already converted.");
736 throw kEH + "Currently non-supported - TEveRecKink being updated.";
739 AliESDEvent* esdEvent = AliEveEventManager::AssertESD();
742 fTreeKK = new TTree("Kinks", "ESD Kinks");
744 fTreeKK->Branch("KK", "TEveRecKink", &fpKK, fBuffSize);
746 // printf("CONVERT KINK Read %d entries in tree kinks \n", esdEvent->GetNumberOfKinks());
747 for (Int_t n = 0; n < esdEvent->GetNumberOfKinks(); ++n)
749 AliESDkink* kk = esdEvent->GetKink(n);
753 fKK.fLabel = kk->GetLabel(0);
754 fKK.fStatus = 0; // status is Char_t[12] ... have no idea how/what to extract.
756 // reconstructed kink position
757 fKK.fLabelSec = kk->GetLabel(1);
758 fKK.fVKink.Set(kk->GetPosition());
760 const AliExternalTrackParam& tpMother = kk->RefParamMother();
761 // momentum and position of mother
762 tpMother.GetPxPyPz(pos);
764 tpMother.GetXYZ(pos);
766 const Double_t* par = tpMother.GetParameter();
767 // printf("KINK Pt %f, %f \n",1/tpMother.Pt(),par[4] );
768 fKK.fSign = (par[4] < 0) ? -1 : 1;
770 const AliExternalTrackParam& tpDaughter = kk->RefParamDaughter();
771 // momentum and position of daughter
772 tpDaughter.GetPxPyPz(pos);
774 tpDaughter.GetXYZ(pos);
779 if (esdEvent->GetNumberOfKinks()) fTreeKK->BuildIndex("label");
783 /******************************************************************************/
785 /******************************************************************************/
787 void AliEveVSDCreator::ConvertGenInfo()
789 // Build simulation-reconstruction cross-reference table.
790 // In a rather poor state at the moment.
792 static const TEveException kEH("AliEveVSDCreator::ConvertGenInfo ");
795 throw(kEH + "GI already converted.");
798 fTreeGI = new TTree("TEveMCRecCrossRef", "Objects prepared for cross querry");
800 TEveMCRecCrossRef::Class()->IgnoreTObjectStreamer(true);
801 fTreeGI->Branch("GI", "TEveMCRecCrossRef", &fpGI, fBuffSize);
802 fTreeGI->Branch("K.", "TEveMCTrack", &fpK);
803 fTreeGI->Branch("R.", "TEveRecTrack", &fpR);
805 for (std::map<Int_t, TEveMCRecCrossRef*>::iterator j=fGenInfoMap.begin(); j!=fGenInfoMap.end(); ++j) {
807 fGI.fLabel = j->first;
808 fTreeK->GetEntry(j->first);
811 Int_t re = fTreeR->GetEntryNumberWithIndex(j->first);
815 // Int_t hasV0 = fTreeV0->GetEntryNumberWithIndex(j->first);
817 // fGI.has_AliEveV0 = true;
819 Int_t hasKk = fTreeKK->GetEntryNumberWithIndex(j->first);
828 /******************************************************************************/
829 /******************************************************************************/
831 /******************************************************************************/
832 /******************************************************************************/
834 TEveMCRecCrossRef* AliEveVSDCreator::GetGeninfo(Int_t label)
836 // Return the cross-reference structure for given label.
837 // If the object does not exist it is created.
839 // printf("get_geninfo %d\n", label);
840 TEveMCRecCrossRef* gi;
841 std::map<Int_t, TEveMCRecCrossRef*>::iterator i = fGenInfoMap.find(label);
842 if (i == fGenInfoMap.end()) {
843 gi = new TEveMCRecCrossRef();
844 fGenInfoMap[label] = gi;