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>
36 #include "AliPMDhit.h"
38 #include "AliDetector.h"
39 #include "AliRunLoader.h"
40 #include "AliLoader.h"
41 #include "AliConfig.h"
43 #include "AliRunDigitizer.h"
44 #include "AliHeader.h"
46 #include "AliPMDcell.h"
47 #include "AliPMDsdigit.h"
48 #include "AliPMDdigit.h"
49 #include "AliPMDDigitizer.h"
50 #include "AliPMDClustering.h"
53 ClassImp(AliPMDDigitizer)
55 AliPMDDigitizer::AliPMDDigitizer() :
66 fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
67 fDigits(new TClonesArray("AliPMDdigit", 1000)),
68 fCell(new TObjArray()),
73 fZPos(361.5)// in units of cm, This is the default position of PMD
75 // Default Constructor
77 for (Int_t i = 0; i < fgkTotUM; i++)
79 for (Int_t j = 0; j < fgkRow; j++)
81 for (Int_t k = 0; k < fgkCol; k++)
85 fPRECounter[i][j][k] = 0;
86 fPRETrackNo[i][j][k] = -1;
87 fCPVTrackNo[i][j][k] = -1;
93 AliPMDDigitizer::~AliPMDDigitizer()
116 void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
118 // Loads galice.root file and corresponding header, kinematics
119 // hits and sdigits or digits depending on the option
121 fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
126 Error("Open","Can not open session for file %s.",file);
129 fRunLoader->LoadgAlice();
130 fRunLoader->LoadHeader();
131 fRunLoader->LoadKinematics();
133 gAlice = fRunLoader->GetAliRun();
137 printf("<AliPMDdigitizer::Open> ");
138 printf("AliRun object found on file.\n");
142 printf("<AliPMDdigitizer::Open> ");
143 printf("Could not find AliRun object.\n");
146 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
147 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
148 if (fPMDLoader == 0x0)
150 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
153 const char *cHS = strstr(option,"HS");
154 const char *cHD = strstr(option,"HD");
155 const char *cSD = strstr(option,"SD");
159 fPMDLoader->LoadHits("READ");
160 fPMDLoader->LoadSDigits("recreate");
164 fPMDLoader->LoadHits("READ");
165 fPMDLoader->LoadDigits("recreate");
169 fPMDLoader->LoadSDigits("READ");
170 fPMDLoader->LoadDigits("recreate");
174 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
176 // This reads the PMD Hits tree and assigns the right track number
177 // to a cell and stores in the summable digits tree
179 // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
181 const Int_t kPi0 = 111;
182 const Int_t kGamma = 22;
190 Float_t xPos, yPos, zPos;
191 Int_t xpad = -1, ypad = -1;
193 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
198 printf("Event Number = %d \n",ievt);
199 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
200 printf("Number of Particles = %d \n", nparticles);
201 fRunLoader->GetEvent(ievt);
202 // fPArray = gAlice->GetMCApp()->Particles();
203 // ------------------------------------------------------- //
204 // Pointer to specific detector hits.
205 // Get pointers to Alice detectors and Hits containers
207 fTreeH = fPMDLoader->TreeH();
209 Int_t ntracks = (Int_t) fTreeH->GetEntries();
210 printf("Number of Tracks in the TreeH = %d \n", ntracks);
212 fTreeS = fPMDLoader->TreeS();
215 fPMDLoader->MakeTree("S");
216 fTreeS = fPMDLoader->TreeS();
218 Int_t bufsize = 16000;
219 fTreeS->Branch("PMDSDigit", &fSDigits, bufsize);
221 if (fPMD) fHits = fPMD->Hits();
223 // Start loop on tracks in the hits containers
225 for (Int_t track=0; track<ntracks;track++)
228 fTreeH->GetEvent(track);
231 npmd = fHits->GetEntriesFast();
232 for (int ipmd = 0; ipmd < npmd; ipmd++)
234 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
235 trackno = fPMDHit->GetTrack();
236 // get kinematics of the particles
238 fParticle = gAlice->GetMCApp()->Particle(trackno);
239 trackpid = fParticle->GetPdgCode();
248 TParticle* mparticle = fParticle;
249 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
250 if (mparticle->GetFirstMother() == -1)
252 tracknoOld = trackno;
253 trackpidOld = trackpid;
257 while((imo = mparticle->GetFirstMother()) >= 0)
261 mparticle = gAlice->GetMCApp()->Particle(imo);
262 idmo = mparticle->GetPdgCode();
264 vx = mparticle->Vx();
265 vy = mparticle->Vy();
266 vz = mparticle->Vz();
268 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
269 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
270 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
278 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
287 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
299 mtrackno = tracknoOld;
300 mtrackpid = trackpidOld;
306 edep = fPMDHit->fEnergy;
307 Int_t vol1 = fPMDHit->fVolume[1]; // Column
308 Int_t vol2 = fPMDHit->fVolume[2]; // Row
309 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
310 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
312 edep = fPMDHit->GetEnergy();
313 Int_t vol1 = fPMDHit->GetVolume(1); // Column
314 Int_t vol2 = fPMDHit->GetVolume(2); // Row
315 Int_t vol3 = fPMDHit->GetVolume(3); // UnitModule
316 Int_t vol6 = fPMDHit->GetVolume(6); // SuperModule
318 // -----------------------------------------//
319 // For Super Module 1 & 2 //
320 // nrow = 96, ncol = 48 //
321 // For Super Module 3 & 4 //
322 // nrow = 48, ncol = 96 //
323 // -----------------------------------------//
325 smnumber = (vol6-1)*6 + vol3;
327 if (vol6 == 1 || vol6 == 2)
332 else if (vol6 == 3 || vol6 == 4)
338 //cout << "zpos = " << zPos << " edep = " << edep << endl;
340 Float_t zposition = TMath::Abs(zPos);
341 if (zposition < fZPos)
346 else if (zposition > fZPos)
351 Int_t smn = smnumber - 1;
352 Int_t ixx = xpad - 1;
353 Int_t iyy = ypad - 1;
356 fPRE[smn][ixx][iyy] += edep;
357 fPRECounter[smn][ixx][iyy]++;
359 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
361 fCell->Add(fPMDcell);
365 fCPV[smn][ixx][iyy] += edep;
366 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
370 } // Track Loop ended
372 TrackAssignment2Cell();
380 for (Int_t idet = 0; idet < 2; idet++)
382 for (Int_t ism = 0; ism < fgkTotUM; ism++)
384 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
386 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
388 cellno = jrow*fgkCol + kcol;
391 deltaE = fPRE[ism][jrow][kcol];
392 trno = fPRETrackNo[ism][jrow][kcol];
397 deltaE = fCPV[ism][jrow][kcol];
398 trno = fCPVTrackNo[ism][jrow][kcol];
403 AddSDigit(trno,detno,ism,cellno,deltaE);
411 fPMDLoader->WriteSDigits("OVERWRITE");
414 // cout << " -------- End of Hits2SDigit ----------- " << endl;
417 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
419 // This reads the PMD Hits tree and assigns the right track number
420 // to a cell and stores in the digits tree
422 const Int_t kPi0 = 111;
423 const Int_t kGamma = 22;
431 Float_t xPos, yPos, zPos;
432 Int_t xpad = -1, ypad = -1;
434 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
438 printf("Event Number = %d \n",ievt);
440 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
441 printf("Number of Particles = %d \n", nparticles);
442 fRunLoader->GetEvent(ievt);
443 // fPArray = gAlice->GetMCApp()->Particles();
444 // ------------------------------------------------------- //
445 // Pointer to specific detector hits.
446 // Get pointers to Alice detectors and Hits containers
448 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
449 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
451 if (fPMDLoader == 0x0)
453 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
455 fTreeH = fPMDLoader->TreeH();
456 Int_t ntracks = (Int_t) fTreeH->GetEntries();
457 printf("Number of Tracks in the TreeH = %d \n", ntracks);
458 fPMDLoader->LoadDigits("recreate");
459 fTreeD = fPMDLoader->TreeD();
462 fPMDLoader->MakeTree("D");
463 fTreeD = fPMDLoader->TreeD();
465 Int_t bufsize = 16000;
466 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
468 if (fPMD) fHits = fPMD->Hits();
470 // Start loop on tracks in the hits containers
472 for (Int_t track=0; track<ntracks;track++)
475 fTreeH->GetEvent(track);
479 npmd = fHits->GetEntriesFast();
480 for (int ipmd = 0; ipmd < npmd; ipmd++)
482 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
483 trackno = fPMDHit->GetTrack();
485 // get kinematics of the particles
487 fParticle = gAlice->GetMCApp()->Particle(trackno);
488 trackpid = fParticle->GetPdgCode();
497 TParticle* mparticle = fParticle;
498 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
499 if (mparticle->GetFirstMother() == -1)
501 tracknoOld = trackno;
502 trackpidOld = trackpid;
507 while((imo = mparticle->GetFirstMother()) >= 0)
511 mparticle = gAlice->GetMCApp()->Particle(imo);
512 idmo = mparticle->GetPdgCode();
514 vx = mparticle->Vx();
515 vy = mparticle->Vy();
516 vz = mparticle->Vz();
518 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
519 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
520 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
528 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
537 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
549 mtrackno = tracknoOld;
550 mtrackpid = trackpidOld;
557 edep = fPMDHit->fEnergy;
558 Int_t vol1 = fPMDHit->fVolume[1]; // Column
559 Int_t vol2 = fPMDHit->fVolume[2]; // Row
560 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
561 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
563 edep = fPMDHit->GetEnergy();
564 Int_t vol1 = fPMDHit->GetVolume(1); // Column
565 Int_t vol2 = fPMDHit->GetVolume(2); // Row
566 Int_t vol3 = fPMDHit->GetVolume(3); // UnitModule
567 Int_t vol6 = fPMDHit->GetVolume(6); // SuperModule
569 // -----------------------------------------//
570 // For Super Module 1 & 2 //
571 // nrow = 96, ncol = 48 //
572 // For Super Module 3 & 4 //
573 // nrow = 48, ncol = 96 //
574 // -----------------------------------------//
576 smnumber = (vol6-1)*6 + vol3;
578 if (vol6 == 1 || vol6 == 2)
583 else if (vol6 == 3 || vol6 == 4)
589 //cout << "-zpos = " << -zPos << endl;
591 Float_t zposition = TMath::Abs(zPos);
593 if (zposition < fZPos)
598 else if (zposition > fZPos)
604 Int_t smn = smnumber - 1;
605 Int_t ixx = xpad - 1;
606 Int_t iyy = ypad - 1;
609 fPRE[smn][ixx][iyy] += edep;
610 fPRECounter[smn][ixx][iyy]++;
612 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
614 fCell->Add(fPMDcell);
618 fCPV[smn][ixx][iyy] += edep;
619 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
623 } // Track Loop ended
625 TrackAssignment2Cell();
633 for (Int_t idet = 0; idet < 2; idet++)
635 for (Int_t ism = 0; ism < fgkTotUM; ism++)
637 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
639 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
641 cellno = jrow*fgkCol + kcol;
644 deltaE = fPRE[ism][jrow][kcol];
645 trno = fPRETrackNo[ism][jrow][kcol];
650 deltaE = fCPV[ism][jrow][kcol];
651 trno = fCPVTrackNo[ism][jrow][kcol];
656 AddDigit(trno,detno,ism,cellno,deltaE);
660 } // supermodule loop
665 fPMDLoader->WriteDigits("OVERWRITE");
668 // cout << " -------- End of Hits2Digit ----------- " << endl;
672 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
674 // This reads the PMD sdigits tree and converts energy deposition
675 // in a cell to ADC and stores in the digits tree
677 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
678 fRunLoader->GetEvent(ievt);
680 fTreeS = fPMDLoader->TreeS();
681 AliPMDsdigit *pmdsdigit;
682 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
683 branch->SetAddress(&fSDigits);
685 fTreeD = fPMDLoader->TreeD();
688 fPMDLoader->MakeTree("D");
689 fTreeD = fPMDLoader->TreeD();
691 Int_t bufsize = 16000;
692 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
694 Int_t trno, det, smn;
698 Int_t nmodules = (Int_t) fTreeS->GetEntries();
700 for (Int_t imodule = 0; imodule < nmodules; imodule++)
702 fTreeS->GetEntry(imodule);
703 Int_t nentries = fSDigits->GetLast();
704 //cout << " nentries = " << nentries << endl;
705 for (Int_t ient = 0; ient < nentries+1; ient++)
707 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
708 trno = pmdsdigit->GetTrackNumber();
709 det = pmdsdigit->GetDetector();
710 smn = pmdsdigit->GetSMNumber();
711 cellno = pmdsdigit->GetCellNumber();
712 edep = pmdsdigit->GetCellEdep();
715 AddDigit(trno,det,smn,cellno,adc);
720 fPMDLoader->WriteDigits("OVERWRITE");
721 // cout << " -------- End of SDigits2Digit ----------- " << endl;
724 void AliPMDDigitizer::TrackAssignment2Cell()
727 // This block assigns the cell id when there are
728 // multiple tracks in a cell according to the
731 Bool_t jsort = false;
743 pmdTrack = new Int_t ***[fgkTotUM];
744 pmdEdep = new Float_t ***[fgkTotUM];
745 for (i=0; i<fgkTotUM; i++)
747 pmdTrack[i] = new Int_t **[fgkRow];
748 pmdEdep[i] = new Float_t **[fgkRow];
751 for (i = 0; i < fgkTotUM; i++)
753 for (j = 0; j < fgkRow; j++)
755 pmdTrack[i][j] = new Int_t *[fgkCol];
756 pmdEdep[i][j] = new Float_t *[fgkCol];
760 for (i = 0; i < fgkTotUM; i++)
762 for (j = 0; j < fgkRow; j++)
764 for (k = 0; k < fgkCol; k++)
766 Int_t nn = fPRECounter[i][j][k];
769 pmdTrack[i][j][k] = new Int_t[nn];
770 pmdEdep[i][j][k] = new Float_t[nn];
775 pmdTrack[i][j][k] = new Int_t[nn];
776 pmdEdep[i][j][k] = new Float_t[nn];
778 fPRECounter[i][j][k] = 0;
784 Int_t nentries = fCell->GetEntries();
786 Int_t mtrackno, ism, ixp, iyp;
789 for (i = 0; i < nentries; i++)
791 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
793 mtrackno = fPMDcell->GetTrackNumber();
794 ism = fPMDcell->GetSMNumber();
795 ixp = fPMDcell->GetX();
796 iyp = fPMDcell->GetY();
797 edep = fPMDcell->GetEdep();
798 Int_t nn = fPRECounter[ism][ixp][iyp];
799 // cout << " nn = " << nn << endl;
800 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
801 pmdEdep[ism][ixp][iyp][nn] = edep;
802 fPRECounter[ism][ixp][iyp]++;
809 for (im=0; im<fgkTotUM; im++)
811 for (ix=0; ix<fgkRow; ix++)
813 for (iy=0; iy<fgkCol; iy++)
815 nn = fPRECounter[im][ix][iy];
818 // This block handles if a cell is fired
819 // many times by many tracks
820 status1 = new Int_t[nn];
821 status2 = new Int_t[nn];
822 trnarray = new Int_t[nn];
823 for (iz = 0; iz < nn; iz++)
825 status1[iz] = pmdTrack[im][ix][iy][iz];
827 TMath::Sort(nn,status1,status2,jsort);
828 Int_t trackOld = -99999;
829 Int_t track, trCount = 0;
830 for (iz = 0; iz < nn; iz++)
832 track = status1[status2[iz]];
833 if (trackOld != track)
835 trnarray[trCount] = track;
843 trEdp = new Float_t[trCount];
844 fracEdp = new Float_t[trCount];
845 for (il = 0; il < trCount; il++)
848 track = trnarray[il];
849 for (iz = 0; iz < nn; iz++)
851 if (track == pmdTrack[im][ix][iy][iz])
853 trEdp[il] += pmdEdep[im][ix][iy][iz];
859 Float_t fracOld = 0.;
861 for (il = 0; il < trCount; il++)
863 fracEdp[il] = trEdp[il]/totEdp;
864 if (fracOld < fracEdp[il])
866 fracOld = fracEdp[il];
870 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
877 // This only handles if a cell is fired
880 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
885 // This is if no cell is fired
886 fPRETrackNo[im][ix][iy] = -999;
892 // Delete all the pointers
894 for (i = 0; i < fgkTotUM; i++)
896 for (j = 0; j < fgkRow; j++)
898 for (k = 0; k < fgkCol; k++)
900 delete [] pmdTrack[i][j][k];
901 delete [] pmdEdep[i][j][k];
906 for (i = 0; i < fgkTotUM; i++)
908 for (j = 0; j < fgkRow; j++)
910 delete [] pmdTrack[i][j];
911 delete [] pmdEdep[i][j];
915 for (i = 0; i < fgkTotUM; i++)
917 delete [] pmdTrack[i];
918 delete [] pmdEdep[i];
923 // End of the cell id assignment
927 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
929 // This converts the simulated edep to ADC according to the
935 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
936 Int_t cellnumber, Float_t adc)
940 TClonesArray &lsdigits = *fSDigits;
941 new(lsdigits[fNsdigit++]) AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
944 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
945 Int_t cellnumber, Float_t adc)
949 TClonesArray &ldigits = *fDigits;
950 new(ldigits[fNdigit++]) AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
953 void AliPMDDigitizer::SetZPosition(Float_t zpos)
957 Float_t AliPMDDigitizer::GetZPosition() const
962 void AliPMDDigitizer::ResetCell()
964 // clears the cell array and also the counter
968 for (Int_t i = 0; i < fgkTotUM; i++)
970 for (Int_t j = 0; j < fgkRow; j++)
972 for (Int_t k = 0; k < fgkCol; k++)
974 fPRECounter[i][j][k] = 0;
979 void AliPMDDigitizer::ResetSDigit()
983 if (fSDigits) fSDigits->Clear();
985 void AliPMDDigitizer::ResetDigit()
989 if (fDigits) fDigits->Clear();
992 void AliPMDDigitizer::ResetCellADC()
994 // Clears individual cells edep
995 for (Int_t i = 0; i < fgkTotUM; i++)
997 for (Int_t j = 0; j < fgkRow; j++)
999 for (Int_t k = 0; k < fgkCol; k++)
1008 void AliPMDDigitizer::UnLoad(Option_t *option)
1010 // Unloads all the root files
1012 const char *cS = strstr(option,"S");
1013 const char *cD = strstr(option,"D");
1015 fRunLoader->UnloadgAlice();
1016 fRunLoader->UnloadHeader();
1017 fRunLoader->UnloadKinematics();
1021 fPMDLoader->UnloadHits();
1025 fPMDLoader->UnloadHits();
1026 fPMDLoader->UnloadSDigits();