1 /***************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15 //-----------------------------------------------------//
17 // Source File : PMDDigitizer.cxx, Version 00 //
19 // Date : September 20 2002 //
21 //-----------------------------------------------------//
23 #include <Riostream.h>
27 #include <TGeometry.h>
28 #include <TObjArray.h>
29 #include <TClonesArray.h>
32 #include <TParticle.h>
37 #include "AliDetector.h"
38 #include "AliRunLoader.h"
39 #include "AliLoader.h"
40 #include "AliConfig.h"
42 #include "AliRunDigitizer.h"
43 #include "AliHeader.h"
45 #include "AliPMDcell.h"
46 #include "AliPMDsdigit.h"
47 #include "AliPMDdigit.h"
48 #include "AliPMDDigitizer.h"
49 #include "AliPMDClustering.h"
52 ClassImp(AliPMDDigitizer)
54 AliPMDDigitizer::AliPMDDigitizer() :
65 fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
66 fDigits(new TClonesArray("AliPMDdigit", 1000)),
67 fCell(new TObjArray()),
72 fZPos(361.5)// in units of cm, This is the default position of PMD
74 // Default Constructor
76 for (Int_t i = 0; i < fgkTotUM; i++)
78 for (Int_t j = 0; j < fgkRow; j++)
80 for (Int_t k = 0; k < fgkCol; k++)
84 fPRECounter[i][j][k] = 0;
85 fPRETrackNo[i][j][k] = -1;
86 fCPVTrackNo[i][j][k] = -1;
92 AliPMDDigitizer::~AliPMDDigitizer()
115 void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
117 // Loads galice.root file and corresponding header, kinematics
118 // hits and sdigits or digits depending on the option
120 fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
125 Error("Open","Can not open session for file %s.",file);
128 fRunLoader->LoadgAlice();
129 fRunLoader->LoadHeader();
130 fRunLoader->LoadKinematics();
132 gAlice = fRunLoader->GetAliRun();
136 printf("<AliPMDdigitizer::Open> ");
137 printf("AliRun object found on file.\n");
141 printf("<AliPMDdigitizer::Open> ");
142 printf("Could not find AliRun object.\n");
145 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
146 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
147 if (fPMDLoader == 0x0)
149 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
152 const char *cHS = strstr(option,"HS");
153 const char *cHD = strstr(option,"HD");
154 const char *cSD = strstr(option,"SD");
158 fPMDLoader->LoadHits("READ");
159 fPMDLoader->LoadSDigits("recreate");
163 fPMDLoader->LoadHits("READ");
164 fPMDLoader->LoadDigits("recreate");
168 fPMDLoader->LoadSDigits("READ");
169 fPMDLoader->LoadDigits("recreate");
173 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
175 // This reads the PMD Hits tree and assigns the right track number
176 // to a cell and stores in the summable digits tree
178 // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
180 const Int_t kPi0 = 111;
181 const Int_t kGamma = 22;
189 Float_t xPos, yPos, zPos;
190 Int_t xpad = -1, ypad = -1;
192 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
197 printf("Event Number = %d \n",ievt);
198 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
199 printf("Number of Particles = %d \n", nparticles);
200 fRunLoader->GetEvent(ievt);
201 // fPArray = gAlice->GetMCApp()->Particles();
202 // ------------------------------------------------------- //
203 // Pointer to specific detector hits.
204 // Get pointers to Alice detectors and Hits containers
206 fTreeH = fPMDLoader->TreeH();
208 Int_t ntracks = (Int_t) fTreeH->GetEntries();
209 printf("Number of Tracks in the TreeH = %d \n", ntracks);
211 fTreeS = fPMDLoader->TreeS();
214 fPMDLoader->MakeTree("S");
215 fTreeS = fPMDLoader->TreeS();
217 Int_t bufsize = 16000;
218 fTreeS->Branch("PMDSDigit", &fSDigits, bufsize);
220 if (fPMD) fHits = fPMD->Hits();
222 // Start loop on tracks in the hits containers
224 for (Int_t track=0; track<ntracks;track++)
227 fTreeH->GetEvent(track);
230 npmd = fHits->GetEntriesFast();
231 for (int ipmd = 0; ipmd < npmd; ipmd++)
233 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
234 trackno = fPMDHit->GetTrack();
235 // get kinematics of the particles
237 fParticle = gAlice->GetMCApp()->Particle(trackno);
238 trackpid = fParticle->GetPdgCode();
247 TParticle* mparticle = fParticle;
248 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
249 if (mparticle->GetFirstMother() == -1)
251 tracknoOld = trackno;
252 trackpidOld = trackpid;
256 while((imo = mparticle->GetFirstMother()) >= 0)
260 mparticle = gAlice->GetMCApp()->Particle(imo);
261 idmo = mparticle->GetPdgCode();
263 vx = mparticle->Vx();
264 vy = mparticle->Vy();
265 vz = mparticle->Vz();
267 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
268 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
269 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
277 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
286 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
298 mtrackno = tracknoOld;
299 mtrackpid = trackpidOld;
304 edep = fPMDHit->fEnergy;
305 Int_t vol1 = fPMDHit->fVolume[1]; // Column
306 Int_t vol2 = fPMDHit->fVolume[2]; // Row
307 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
308 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
309 // -----------------------------------------//
310 // For Super Module 1 & 2 //
311 // nrow = 96, ncol = 48 //
312 // For Super Module 3 & 4 //
313 // nrow = 48, ncol = 96 //
314 // -----------------------------------------//
316 smnumber = (vol6-1)*6 + vol3;
318 if (vol6 == 1 || vol6 == 2)
323 else if (vol6 == 3 || vol6 == 4)
329 //cout << "zpos = " << zPos << " edep = " << edep << endl;
331 Float_t zposition = TMath::Abs(zPos);
332 if (zposition < fZPos)
337 else if (zposition > fZPos)
342 Int_t smn = smnumber - 1;
343 Int_t ixx = xpad - 1;
344 Int_t iyy = ypad - 1;
347 fPRE[smn][ixx][iyy] += edep;
348 fPRECounter[smn][ixx][iyy]++;
350 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
352 fCell->Add(fPMDcell);
356 fCPV[smn][ixx][iyy] += edep;
357 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
361 } // Track Loop ended
363 TrackAssignment2Cell();
371 for (Int_t idet = 0; idet < 2; idet++)
373 for (Int_t ism = 0; ism < fgkTotUM; ism++)
375 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
377 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
379 cellno = jrow*fgkCol + kcol;
382 deltaE = fPRE[ism][jrow][kcol];
383 trno = fPRETrackNo[ism][jrow][kcol];
388 deltaE = fCPV[ism][jrow][kcol];
389 trno = fCPVTrackNo[ism][jrow][kcol];
394 AddSDigit(trno,detno,ism,cellno,deltaE);
402 fPMDLoader->WriteSDigits("OVERWRITE");
405 // cout << " -------- End of Hits2SDigit ----------- " << endl;
408 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
410 // This reads the PMD Hits tree and assigns the right track number
411 // to a cell and stores in the digits tree
413 const Int_t kPi0 = 111;
414 const Int_t kGamma = 22;
422 Float_t xPos, yPos, zPos;
423 Int_t xpad = -1, ypad = -1;
425 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
429 printf("Event Number = %d \n",ievt);
431 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
432 printf("Number of Particles = %d \n", nparticles);
433 fRunLoader->GetEvent(ievt);
434 // fPArray = gAlice->GetMCApp()->Particles();
435 // ------------------------------------------------------- //
436 // Pointer to specific detector hits.
437 // Get pointers to Alice detectors and Hits containers
439 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
440 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
442 if (fPMDLoader == 0x0)
444 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
446 fTreeH = fPMDLoader->TreeH();
447 Int_t ntracks = (Int_t) fTreeH->GetEntries();
448 printf("Number of Tracks in the TreeH = %d \n", ntracks);
449 fPMDLoader->LoadDigits("recreate");
450 fTreeD = fPMDLoader->TreeD();
453 fPMDLoader->MakeTree("D");
454 fTreeD = fPMDLoader->TreeD();
456 Int_t bufsize = 16000;
457 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
459 if (fPMD) fHits = fPMD->Hits();
461 // Start loop on tracks in the hits containers
463 for (Int_t track=0; track<ntracks;track++)
466 fTreeH->GetEvent(track);
470 npmd = fHits->GetEntriesFast();
471 for (int ipmd = 0; ipmd < npmd; ipmd++)
473 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
474 trackno = fPMDHit->GetTrack();
476 // get kinematics of the particles
478 fParticle = gAlice->GetMCApp()->Particle(trackno);
479 trackpid = fParticle->GetPdgCode();
488 TParticle* mparticle = fParticle;
489 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
490 if (mparticle->GetFirstMother() == -1)
492 tracknoOld = trackno;
493 trackpidOld = trackpid;
498 while((imo = mparticle->GetFirstMother()) >= 0)
502 mparticle = gAlice->GetMCApp()->Particle(imo);
503 idmo = mparticle->GetPdgCode();
505 vx = mparticle->Vx();
506 vy = mparticle->Vy();
507 vz = mparticle->Vz();
509 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
510 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
511 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
519 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
528 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
540 mtrackno = tracknoOld;
541 mtrackpid = trackpidOld;
547 edep = fPMDHit->fEnergy;
548 Int_t vol1 = fPMDHit->fVolume[1]; // Column
549 Int_t vol2 = fPMDHit->fVolume[2]; // Row
550 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
551 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
553 // -----------------------------------------//
554 // For Super Module 1 & 2 //
555 // nrow = 96, ncol = 48 //
556 // For Super Module 3 & 4 //
557 // nrow = 48, ncol = 96 //
558 // -----------------------------------------//
560 smnumber = (vol6-1)*6 + vol3;
562 if (vol6 == 1 || vol6 == 2)
567 else if (vol6 == 3 || vol6 == 4)
573 //cout << "-zpos = " << -zPos << endl;
575 Float_t zposition = TMath::Abs(zPos);
577 if (zposition < fZPos)
582 else if (zposition > fZPos)
588 Int_t smn = smnumber - 1;
589 Int_t ixx = xpad - 1;
590 Int_t iyy = ypad - 1;
593 fPRE[smn][ixx][iyy] += edep;
594 fPRECounter[smn][ixx][iyy]++;
596 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
598 fCell->Add(fPMDcell);
602 fCPV[smn][ixx][iyy] += edep;
603 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
607 } // Track Loop ended
609 TrackAssignment2Cell();
617 for (Int_t idet = 0; idet < 2; idet++)
619 for (Int_t ism = 0; ism < fgkTotUM; ism++)
621 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
623 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
625 cellno = jrow*fgkCol + kcol;
628 deltaE = fPRE[ism][jrow][kcol];
629 trno = fPRETrackNo[ism][jrow][kcol];
634 deltaE = fCPV[ism][jrow][kcol];
635 trno = fCPVTrackNo[ism][jrow][kcol];
640 AddDigit(trno,detno,ism,cellno,deltaE);
644 } // supermodule loop
649 fPMDLoader->WriteDigits("OVERWRITE");
652 // cout << " -------- End of Hits2Digit ----------- " << endl;
656 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
658 // This reads the PMD sdigits tree and converts energy deposition
659 // in a cell to ADC and stores in the digits tree
661 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
662 fRunLoader->GetEvent(ievt);
664 fTreeS = fPMDLoader->TreeS();
665 AliPMDsdigit *pmdsdigit;
666 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
667 branch->SetAddress(&fSDigits);
669 fTreeD = fPMDLoader->TreeD();
672 fPMDLoader->MakeTree("D");
673 fTreeD = fPMDLoader->TreeD();
675 Int_t bufsize = 16000;
676 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
678 Int_t trno, det, smn;
682 Int_t nmodules = (Int_t) fTreeS->GetEntries();
684 for (Int_t imodule = 0; imodule < nmodules; imodule++)
686 fTreeS->GetEntry(imodule);
687 Int_t nentries = fSDigits->GetLast();
688 //cout << " nentries = " << nentries << endl;
689 for (Int_t ient = 0; ient < nentries+1; ient++)
691 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
692 trno = pmdsdigit->GetTrackNumber();
693 det = pmdsdigit->GetDetector();
694 smn = pmdsdigit->GetSMNumber();
695 cellno = pmdsdigit->GetCellNumber();
696 edep = pmdsdigit->GetCellEdep();
699 AddDigit(trno,det,smn,cellno,adc);
704 fPMDLoader->WriteDigits("OVERWRITE");
705 // cout << " -------- End of SDigits2Digit ----------- " << endl;
708 void AliPMDDigitizer::TrackAssignment2Cell()
711 // This block assigns the cell id when there are
712 // multiple tracks in a cell according to the
715 Bool_t jsort = false;
727 pmdTrack = new Int_t ***[fgkTotUM];
728 pmdEdep = new Float_t ***[fgkTotUM];
729 for (i=0; i<fgkTotUM; i++)
731 pmdTrack[i] = new Int_t **[fgkRow];
732 pmdEdep[i] = new Float_t **[fgkRow];
735 for (i = 0; i < fgkTotUM; i++)
737 for (j = 0; j < fgkRow; j++)
739 pmdTrack[i][j] = new Int_t *[fgkCol];
740 pmdEdep[i][j] = new Float_t *[fgkCol];
744 for (i = 0; i < fgkTotUM; i++)
746 for (j = 0; j < fgkRow; j++)
748 for (k = 0; k < fgkCol; k++)
750 Int_t nn = fPRECounter[i][j][k];
753 pmdTrack[i][j][k] = new Int_t[nn];
754 pmdEdep[i][j][k] = new Float_t[nn];
759 pmdTrack[i][j][k] = new Int_t[nn];
760 pmdEdep[i][j][k] = new Float_t[nn];
762 fPRECounter[i][j][k] = 0;
768 Int_t nentries = fCell->GetEntries();
770 Int_t mtrackno, ism, ixp, iyp;
773 for (i = 0; i < nentries; i++)
775 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
777 mtrackno = fPMDcell->GetTrackNumber();
778 ism = fPMDcell->GetSMNumber();
779 ixp = fPMDcell->GetX();
780 iyp = fPMDcell->GetY();
781 edep = fPMDcell->GetEdep();
782 Int_t nn = fPRECounter[ism][ixp][iyp];
783 // cout << " nn = " << nn << endl;
784 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
785 pmdEdep[ism][ixp][iyp][nn] = edep;
786 fPRECounter[ism][ixp][iyp]++;
793 for (im=0; im<fgkTotUM; im++)
795 for (ix=0; ix<fgkRow; ix++)
797 for (iy=0; iy<fgkCol; iy++)
799 nn = fPRECounter[im][ix][iy];
802 // This block handles if a cell is fired
803 // many times by many tracks
804 status1 = new Int_t[nn];
805 status2 = new Int_t[nn];
806 trnarray = new Int_t[nn];
807 for (iz = 0; iz < nn; iz++)
809 status1[iz] = pmdTrack[im][ix][iy][iz];
811 TMath::Sort(nn,status1,status2,jsort);
812 Int_t trackOld = -99999;
813 Int_t track, trCount = 0;
814 for (iz = 0; iz < nn; iz++)
816 track = status1[status2[iz]];
817 if (trackOld != track)
819 trnarray[trCount] = track;
827 trEdp = new Float_t[trCount];
828 fracEdp = new Float_t[trCount];
829 for (il = 0; il < trCount; il++)
832 track = trnarray[il];
833 for (iz = 0; iz < nn; iz++)
835 if (track == pmdTrack[im][ix][iy][iz])
837 trEdp[il] += pmdEdep[im][ix][iy][iz];
843 Float_t fracOld = 0.;
845 for (il = 0; il < trCount; il++)
847 fracEdp[il] = trEdp[il]/totEdp;
848 if (fracOld < fracEdp[il])
850 fracOld = fracEdp[il];
854 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
861 // This only handles if a cell is fired
864 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
869 // This is if no cell is fired
870 fPRETrackNo[im][ix][iy] = -999;
876 // Delete all the pointers
878 for (i = 0; i < fgkTotUM; i++)
880 for (j = 0; j < fgkRow; j++)
882 for (k = 0; k < fgkCol; k++)
884 delete [] pmdTrack[i][j][k];
885 delete [] pmdEdep[i][j][k];
890 for (i = 0; i < fgkTotUM; i++)
892 for (j = 0; j < fgkRow; j++)
894 delete [] pmdTrack[i][j];
895 delete [] pmdEdep[i][j];
899 for (i = 0; i < fgkTotUM; i++)
901 delete [] pmdTrack[i];
902 delete [] pmdEdep[i];
907 // End of the cell id assignment
911 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
913 // This converts the simulated edep to ADC according to the
919 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
920 Int_t cellnumber, Float_t adc)
924 TClonesArray &lsdigits = *fSDigits;
925 AliPMDsdigit *newcell;
926 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
927 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
931 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
932 Int_t cellnumber, Float_t adc)
936 TClonesArray &ldigits = *fDigits;
937 AliPMDdigit *newcell;
938 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
939 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
943 void AliPMDDigitizer::SetZPosition(Float_t zpos)
947 Float_t AliPMDDigitizer::GetZPosition() const
952 void AliPMDDigitizer::ResetCell()
954 // clears the cell array and also the counter
958 for (Int_t i = 0; i < fgkTotUM; i++)
960 for (Int_t j = 0; j < fgkRow; j++)
962 for (Int_t k = 0; k < fgkCol; k++)
964 fPRECounter[i][j][k] = 0;
969 void AliPMDDigitizer::ResetSDigit()
973 if (fSDigits) fSDigits->Clear();
975 void AliPMDDigitizer::ResetDigit()
979 if (fDigits) fDigits->Clear();
982 void AliPMDDigitizer::ResetCellADC()
984 // Clears individual cells edep
985 for (Int_t i = 0; i < fgkTotUM; i++)
987 for (Int_t j = 0; j < fgkRow; j++)
989 for (Int_t k = 0; k < fgkCol; k++)
998 void AliPMDDigitizer::UnLoad(Option_t *option)
1000 // Unloads all the root files
1002 const char *cS = strstr(option,"S");
1003 const char *cD = strstr(option,"D");
1005 fRunLoader->UnloadgAlice();
1006 fRunLoader->UnloadHeader();
1007 fRunLoader->UnloadKinematics();
1011 fPMDLoader->UnloadHits();
1015 fPMDLoader->UnloadHits();
1016 fPMDLoader->UnloadSDigits();