1 //-----------------------------------------------------//
3 // Source File : PMDDigitizer.cxx, Version 00 //
5 // Date : September 20 2002 //
7 //-----------------------------------------------------//
12 #include <TGeometry.h>
13 #include <TObjArray.h>
14 #include <TClonesArray.h>
17 #include <TParticle.h>
22 #include "AliDetector.h"
23 #include "AliRunLoader.h"
24 #include "AliLoader.h"
25 #include "AliConfig.h"
27 #include "AliRunDigitizer.h"
28 #include "AliHeader.h"
30 #include "AliPMDcell.h"
31 #include "AliPMDsdigit.h"
32 #include "AliPMDdigit.h"
33 #include "AliPMDDigitizer.h"
34 #include "AliPMDClustering.h"
36 ClassImp(AliPMDDigitizer)
40 AliPMDDigitizer::AliPMDDigitizer()
42 if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
44 if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
47 for (Int_t i = 0; i < fTotUM; i++)
49 for (Int_t j = 0; j < fRow; j++)
51 for (Int_t k = 0; k < fCol; k++)
59 if (!fCell) fCell = new TObjArray();
61 fZPos = 361.5; // in units of cm, This is the default position of PMD
63 AliPMDDigitizer::~AliPMDDigitizer()
72 void AliPMDDigitizer::OpengAliceFile(Char_t *file, Option_t *option)
75 fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
80 Error("Open","Can not open session for file %s.",file);
83 fRunLoader->LoadgAlice();
84 fRunLoader->LoadHeader();
85 fRunLoader->LoadKinematics();
87 gAlice = fRunLoader->GetAliRun();
91 printf("<AliPMDdigitizer::Open> ");
92 printf("AliRun object found on file.\n");
96 printf("<AliPMDdigitizer::Open> ");
97 printf("Could not find AliRun object.\n");
100 PMD = (AliPMD*)gAlice->GetDetector("PMD");
101 pmdloader = fRunLoader->GetLoader("PMDLoader");
102 if (pmdloader == 0x0)
104 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
107 const char *cHS = strstr(option,"HS");
108 const char *cHD = strstr(option,"HD");
109 const char *cSD = strstr(option,"SD");
113 pmdloader->LoadHits("READ");
114 pmdloader->LoadSDigits("recreate");
118 pmdloader->LoadHits("READ");
119 pmdloader->LoadDigits("recreate");
123 pmdloader->LoadSDigits("READ");
124 pmdloader->LoadDigits("recreate");
128 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
130 cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
141 Float_t xPos, yPos, zPos;
142 Int_t xpad = -1, ypad = -1;
144 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
149 printf("Event Number = %d \n",ievt);
150 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
151 printf("Number of Particles = %d \n", nparticles);
152 fRunLoader->GetEvent(ievt);
153 Particles = gAlice->Particles();
154 // ------------------------------------------------------- //
155 // Pointer to specific detector hits.
156 // Get pointers to Alice detectors and Hits containers
158 treeH = pmdloader->TreeH();
160 Int_t ntracks = (Int_t) treeH->GetEntries();
161 printf("Number of Tracks in the TreeH = %d \n", ntracks);
163 treeS = pmdloader->TreeS();
166 pmdloader->MakeTree("S");
167 treeS = pmdloader->TreeS();
169 Int_t bufsize = 16000;
170 treeS->Branch("PMDSDigit", &fSDigits, bufsize);
172 if (PMD) PMDhits = PMD->Hits();
174 // Start loop on tracks in the hits containers
177 for (Int_t track=0; track<ntracks;track++)
180 treeH->GetEvent(track);
184 npmd = PMDhits->GetEntriesFast();
185 for (int ipmd = 0; ipmd < npmd; ipmd++)
187 pmdHit = (AliPMDhit*) PMDhits->UncheckedAt(ipmd);
188 trackno = pmdHit->GetTrack();
190 // get kinematics of the particles
192 particle = gAlice->Particle(trackno);
193 trackpid = particle->GetPdgCode();
202 TParticle* mparticle = particle;
203 Int_t trackno_old=0, trackpid_old=0, status_old = 0;
204 if (mparticle->GetFirstMother() == -1)
206 trackno_old = trackno;
207 trackpid_old = trackpid;
212 while((imo = mparticle->GetFirstMother()) >= 0)
215 mparticle = gAlice->Particle(imo);
216 id_mo = mparticle->GetPdgCode();
218 vx = mparticle->Vx();
219 vy = mparticle->Vy();
220 vz = mparticle->Vz();
222 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, id_mo, vx, vy, vz);
223 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, id_mo, vx, vy, vz);
224 if ((id_mo == kGamma || id_mo == -11 || id_mo == 11) && vx == 0. && vy == 0. && vz == 0.)
232 if (id_mo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
241 if (id_mo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
251 if (status_old == -1)
253 mtrackno = trackno_old;
254 mtrackpid = trackpid_old;
260 edep = pmdHit->fEnergy;
261 Int_t Vol1 = pmdHit->fVolume[1]; // Column
262 Int_t Vol2 = pmdHit->fVolume[2]; // Row
263 Int_t Vol3 = pmdHit->fVolume[3]; // UnitModule
264 Int_t Vol6 = pmdHit->fVolume[6]; // SuperModule
265 // -----------------------------------------//
266 // For Super Module 1 & 2 //
267 // nrow = 96, ncol = 48 //
268 // For Super Module 3 & 4 //
269 // nrow = 48, ncol = 96 //
270 // -----------------------------------------//
272 smnumber = (Vol6-1)*6 + Vol3;
274 if (Vol6 == 1 || Vol6 == 2)
279 else if (Vol6 == 3 || Vol6 == 4)
285 //cout << "zpos = " << zPos << " edep = " << edep << endl;
287 Float_t zposition = TMath::Abs(zPos);
288 if (zposition < fZPos)
293 else if (zposition > fZPos)
298 Int_t smn = smnumber - 1;
299 Int_t ixx = xpad - 1;
300 Int_t iyy = ypad - 1;
303 fPMD[smn][ixx][iyy] += edep;
304 fPMDCounter[smn][ixx][iyy]++;
306 pmdcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
312 fCPV[smn][ixx][iyy] += edep;
313 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
317 } // Track Loop ended
319 TrackAssignment2Cell();
327 for (Int_t idet = 0; idet < 2; idet++)
329 for (Int_t ism = 0; ism < fTotUM; ism++)
331 for (Int_t jrow = 0; jrow < fRow; jrow++)
333 for (Int_t kcol = 0; kcol < fCol; kcol++)
335 cellno = jrow*fCol + kcol;
338 deltaE = fPMD[ism][jrow][kcol];
339 trno = fPMDTrackNo[ism][jrow][kcol];
344 deltaE = fCPV[ism][jrow][kcol];
345 trno = fCPVTrackNo[ism][jrow][kcol];
350 AddSDigit(trno,detno,ism,cellno,deltaE);
358 pmdloader->WriteSDigits("OVERWRITE");
361 // cout << " -------- End of Hits2SDigit ----------- " << endl;
364 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
375 Float_t xPos, yPos, zPos;
376 Int_t xpad = -1, ypad = -1;
378 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
383 printf("Event Number = %d \n",ievt);
385 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
386 printf("Number of Particles = %d \n", nparticles);
387 fRunLoader->GetEvent(ievt);
388 Particles = gAlice->Particles();
389 // ------------------------------------------------------- //
390 // Pointer to specific detector hits.
391 // Get pointers to Alice detectors and Hits containers
393 PMD = (AliPMD*)gAlice->GetDetector("PMD");
394 pmdloader = fRunLoader->GetLoader("PMDLoader");
396 if (pmdloader == 0x0)
398 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
400 treeH = pmdloader->TreeH();
401 Int_t ntracks = (Int_t) treeH->GetEntries();
402 printf("Number of Tracks in the TreeH = %d \n", ntracks);
403 pmdloader->LoadDigits("recreate");
404 treeD = pmdloader->TreeD();
407 pmdloader->MakeTree("D");
408 treeD = pmdloader->TreeD();
410 Int_t bufsize = 16000;
411 treeD->Branch("PMDDigit", &fDigits, bufsize);
413 if (PMD) PMDhits = PMD->Hits();
415 // Start loop on tracks in the hits containers
417 for (Int_t track=0; track<ntracks;track++)
420 treeH->GetEvent(track);
424 npmd = PMDhits->GetEntriesFast();
425 for (int ipmd = 0; ipmd < npmd; ipmd++)
427 pmdHit = (AliPMDhit*) PMDhits->UncheckedAt(ipmd);
428 trackno = pmdHit->GetTrack();
430 // get kinematics of the particles
432 particle = gAlice->Particle(trackno);
433 trackpid = particle->GetPdgCode();
442 TParticle* mparticle = particle;
443 Int_t trackno_old=0, trackpid_old=0, status_old = 0;
444 if (mparticle->GetFirstMother() == -1)
446 trackno_old = trackno;
447 trackpid_old = trackpid;
452 while((imo = mparticle->GetFirstMother()) >= 0)
455 mparticle = gAlice->Particle(imo);
456 id_mo = mparticle->GetPdgCode();
458 vx = mparticle->Vx();
459 vy = mparticle->Vy();
460 vz = mparticle->Vz();
462 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, id_mo, vx, vy, vz);
463 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, id_mo, vx, vy, vz);
464 if ((id_mo == kGamma || id_mo == -11 || id_mo == 11) && vx == 0. && vy == 0. && vz == 0.)
472 if (id_mo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
481 if (id_mo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
491 if (status_old == -1)
493 mtrackno = trackno_old;
494 mtrackpid = trackpid_old;
500 edep = pmdHit->fEnergy;
501 Int_t Vol1 = pmdHit->fVolume[1]; // Column
502 Int_t Vol2 = pmdHit->fVolume[2]; // Row
503 Int_t Vol3 = pmdHit->fVolume[3]; // UnitModule
504 Int_t Vol6 = pmdHit->fVolume[6]; // SuperModule
506 // -----------------------------------------//
507 // For Super Module 1 & 2 //
508 // nrow = 96, ncol = 48 //
509 // For Super Module 3 & 4 //
510 // nrow = 48, ncol = 96 //
511 // -----------------------------------------//
513 smnumber = (Vol6-1)*6 + Vol3;
515 if (Vol6 == 1 || Vol6 == 2)
520 else if (Vol6 == 3 || Vol6 == 4)
526 //cout << "-zpos = " << -zPos << endl;
528 Float_t zposition = TMath::Abs(zPos);
530 if (zposition < fZPos)
535 else if (zposition > fZPos)
541 Int_t smn = smnumber - 1;
542 Int_t ixx = xpad - 1;
543 Int_t iyy = ypad - 1;
546 fPMD[smn][ixx][iyy] += edep;
547 fPMDCounter[smn][ixx][iyy]++;
549 pmdcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
555 fCPV[smn][ixx][iyy] += edep;
556 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
560 } // Track Loop ended
562 TrackAssignment2Cell();
570 for (Int_t idet = 0; idet < 2; idet++)
572 for (Int_t ism = 0; ism < fTotUM; ism++)
574 for (Int_t jrow = 0; jrow < fRow; jrow++)
576 for (Int_t kcol = 0; kcol < fCol; kcol++)
578 cellno = jrow*fCol + kcol;
581 deltaE = fPMD[ism][jrow][kcol];
582 trno = fPMDTrackNo[ism][jrow][kcol];
587 deltaE = fCPV[ism][jrow][kcol];
588 trno = fCPVTrackNo[ism][jrow][kcol];
593 AddDigit(trno,detno,ism,cellno,deltaE);
597 } // supermodule loop
602 pmdloader->WriteDigits("OVERWRITE");
605 // cout << " -------- End of Hits2Digit ----------- " << endl;
609 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
611 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
612 fRunLoader->GetEvent(ievt);
614 treeS = pmdloader->TreeS();
615 AliPMDsdigit *pmdsdigit;
616 TBranch *branch = treeS->GetBranch("PMDSDigit");
617 branch->SetAddress(&fSDigits);
619 treeD = pmdloader->TreeD();
622 pmdloader->MakeTree("D");
623 treeD = pmdloader->TreeD();
625 Int_t bufsize = 16000;
626 treeD->Branch("PMDDigit", &fDigits, bufsize);
628 Int_t trno, det, smn;
632 Int_t nmodules = (Int_t) treeS->GetEntries();
634 for (Int_t imodule = 0; imodule < nmodules; imodule++)
636 treeS->GetEntry(imodule);
637 Int_t nentries = fSDigits->GetLast();
638 //cout << " nentries = " << nentries << endl;
639 for (Int_t ient = 0; ient < nentries+1; ient++)
641 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
642 trno = pmdsdigit->GetTrackNumber();
643 det = pmdsdigit->GetDetector();
644 smn = pmdsdigit->GetSMNumber();
645 cellno = pmdsdigit->GetCellNumber();
646 edep = pmdsdigit->GetCellEdep();
649 AddDigit(trno,det,smn,cellno,adc);
654 pmdloader->WriteDigits("OVERWRITE");
655 // cout << " -------- End of SDigits2Digit ----------- " << endl;
658 void AliPMDDigitizer::TrackAssignment2Cell()
661 // This block assigns the cell id when there are
662 // multiple tracks in a cell according to the
665 Bool_t jsort = false;
677 PMDTrack = new Int_t ***[fTotUM];
678 PMDEdep = new Float_t ***[fTotUM];
679 for (i=0; i<fTotUM; i++)
681 PMDTrack[i] = new Int_t **[fRow];
682 PMDEdep[i] = new Float_t **[fRow];
685 for (i = 0; i < fTotUM; i++)
687 for (j = 0; j < fRow; j++)
689 PMDTrack[i][j] = new Int_t *[fCol];
690 PMDEdep[i][j] = new Float_t *[fCol];
694 for (i = 0; i < fTotUM; i++)
696 for (j = 0; j < fRow; j++)
698 for (k = 0; k < fCol; k++)
700 Int_t nn = fPMDCounter[i][j][k];
703 PMDTrack[i][j][k] = new Int_t[nn];
704 PMDEdep[i][j][k] = new Float_t[nn];
709 PMDTrack[i][j][k] = new Int_t[nn];
710 PMDEdep[i][j][k] = new Float_t[nn];
712 fPMDCounter[i][j][k] = 0;
718 Int_t nentries = fCell->GetEntries();
720 Int_t mtrackno, ism, ixp, iyp;
723 for (i = 0; i < nentries; i++)
725 pmdcell = (AliPMDcell*)fCell->UncheckedAt(i);
727 mtrackno = pmdcell->GetTrackNumber();
728 ism = pmdcell->GetSMNumber();
729 ixp = pmdcell->GetX();
730 iyp = pmdcell->GetY();
731 edep = pmdcell->GetEdep();
732 Int_t nn = fPMDCounter[ism][ixp][iyp];
733 // cout << " nn = " << nn << endl;
734 PMDTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
735 PMDEdep[ism][ixp][iyp][nn] = edep;
736 fPMDCounter[ism][ixp][iyp]++;
743 for (im=0; im<fTotUM; im++)
745 for (ix=0; ix<fRow; ix++)
747 for (iy=0; iy<fCol; iy++)
749 nn = fPMDCounter[im][ix][iy];
752 // This block handles if a cell is fired
753 // many times by many tracks
754 status1 = new Int_t[nn];
755 status2 = new Int_t[nn];
756 trnarray = new Int_t[nn];
757 for (iz = 0; iz < nn; iz++)
759 status1[iz] = PMDTrack[im][ix][iy][iz];
761 TMath::Sort(nn,status1,status2,jsort);
762 Int_t track_old = -99999;
763 Int_t track, tr_count = 0;
764 for (iz = 0; iz < nn; iz++)
766 track = status1[status2[iz]];
767 if (track_old != track)
769 trnarray[tr_count] = track;
776 Float_t tot_edp = 0.;
777 tr_edp = new Float_t[tr_count];
778 frac_edp = new Float_t[tr_count];
779 for (il = 0; il < tr_count; il++)
782 track = trnarray[il];
783 for (iz = 0; iz < nn; iz++)
785 if (track == PMDTrack[im][ix][iy][iz])
787 tr_edp[il] += PMDEdep[im][ix][iy][iz];
790 tot_edp += tr_edp[il];
793 Float_t frac_old = 0.;
795 for (il = 0; il < tr_count; il++)
797 frac_edp[il] = tr_edp[il]/tot_edp;
798 if (frac_old < frac_edp[il])
800 frac_old = frac_edp[il];
804 fPMDTrackNo[im][ix][iy] = trnarray[il_old];
811 // This only handles if a cell is fired
814 fPMDTrackNo[im][ix][iy] = PMDTrack[im][ix][iy][0];
819 // This is if no cell is fired
820 fPMDTrackNo[im][ix][iy] = -999;
826 // Delete all the pointers
828 for (i = 0; i < fTotUM; i++)
830 for (j = 0; j < fRow; j++)
832 for (k = 0; k < fCol; k++)
834 delete [] PMDTrack[i][j][k];
835 delete [] PMDEdep[i][j][k];
840 for (i = 0; i < fTotUM; i++)
842 for (j = 0; j < fRow; j++)
844 delete [] PMDTrack[i][j];
845 delete [] PMDEdep[i][j];
849 for (i = 0; i < fTotUM; i++)
851 delete [] PMDTrack[i];
852 delete [] PMDEdep[i];
857 // End of the cell id assignment
862 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc)
868 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
869 Int_t cellnumber, Float_t adc)
871 TClonesArray &lsdigits = *fSDigits;
872 AliPMDsdigit *newcell;
873 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
874 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
878 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
879 Int_t cellnumber, Float_t adc)
881 TClonesArray &ldigits = *fDigits;
882 AliPMDdigit *newcell;
883 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
884 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
888 Int_t AliPMDDigitizer::Convert2RealSMNumber(Int_t smnumber1)
890 Int_t smnumber = -999;
892 if (smnumber1==1) smnumber = 1;
893 if (smnumber1==2) smnumber = 10;
894 if (smnumber1==3) smnumber = 19;
895 if (smnumber1==4) smnumber = 1;
896 if (smnumber1==5) smnumber = 10;
897 if (smnumber1==6) smnumber = 19;
898 if (smnumber1==7) smnumber = 2;
899 if (smnumber1==8) smnumber = 3;
900 if (smnumber1==9) smnumber = 4;
901 if (smnumber1==10) smnumber = 5;
902 if (smnumber1==11) smnumber = 6;
903 if (smnumber1==12) smnumber = 7;
904 if (smnumber1==13) smnumber = 8;
905 if (smnumber1==14) smnumber = 9;
906 if (smnumber1==15) smnumber = 11;
907 if (smnumber1==16) smnumber = 12;
908 if (smnumber1==17) smnumber = 13;
909 if (smnumber1==18) smnumber = 14;
910 if (smnumber1==19) smnumber = 15;
911 if (smnumber1==20) smnumber = 16;
912 if (smnumber1==21) smnumber = 17;
913 if (smnumber1==22) smnumber = 18;
914 if (smnumber1==23) smnumber = 20;
915 if (smnumber1==24) smnumber = 21;
916 if (smnumber1==25) smnumber = 22;
917 if (smnumber1==26) smnumber = 23;
918 if (smnumber1==27) smnumber = 24;
919 if (smnumber1==28) smnumber = 25;
920 if (smnumber1==29) smnumber = 26;
921 if (smnumber1==30) smnumber = 27;
925 void AliPMDDigitizer::SetZPosition(Float_t zpos)
929 Float_t AliPMDDigitizer::GetZPosition() const
934 void AliPMDDigitizer::ResetCell()
937 for (Int_t i = 0; i < fTotUM; i++)
939 for (Int_t j = 0; j < fRow; j++)
941 for (Int_t k = 0; k < fCol; k++)
943 fPMDCounter[i][j][k] = 0;
948 void AliPMDDigitizer::ResetSDigit()
951 if (fSDigits) fSDigits->Clear();
953 void AliPMDDigitizer::ResetDigit()
956 if (fDigits) fDigits->Clear();
959 void AliPMDDigitizer::ResetCellADC()
961 for (Int_t i = 0; i < fTotUM; i++)
963 for (Int_t j = 0; j < fRow; j++)
965 for (Int_t k = 0; k < fCol; k++)
974 void AliPMDDigitizer::UnLoad(Option_t *option)
976 const char *cS = strstr(option,"S");
977 const char *cD = strstr(option,"D");
979 fRunLoader->UnloadgAlice();
980 fRunLoader->UnloadHeader();
981 fRunLoader->UnloadKinematics();
985 pmdloader->UnloadHits();
989 pmdloader->UnloadHits();
990 pmdloader->UnloadSDigits();