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)
56 AliPMDDigitizer::AliPMDDigitizer()
58 // Default Constructor
60 if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
62 if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
65 for (Int_t i = 0; i < fgkTotUM; i++)
67 for (Int_t j = 0; j < fgkRow; j++)
69 for (Int_t k = 0; k < fgkCol; k++)
77 if (!fCell) fCell = new TObjArray();
79 fZPos = 361.5; // in units of cm, This is the default position of PMD
81 AliPMDDigitizer::~AliPMDDigitizer()
92 void AliPMDDigitizer::OpengAliceFile(Char_t *file, Option_t *option)
94 // Loads galice.root file and corresponding header, kinematics
95 // hits and sdigits or digits depending on the option
97 fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
102 Error("Open","Can not open session for file %s.",file);
105 fRunLoader->LoadgAlice();
106 fRunLoader->LoadHeader();
107 fRunLoader->LoadKinematics();
109 fAlice = fRunLoader->GetAliRun();
113 printf("<AliPMDdigitizer::Open> ");
114 printf("AliRun object found on file.\n");
118 printf("<AliPMDdigitizer::Open> ");
119 printf("Could not find AliRun object.\n");
122 fPMD = (AliPMD*)fAlice->GetDetector("PMD");
123 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
124 if (fPMDLoader == 0x0)
126 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
129 const char *cHS = strstr(option,"HS");
130 const char *cHD = strstr(option,"HD");
131 const char *cSD = strstr(option,"SD");
135 fPMDLoader->LoadHits("READ");
136 fPMDLoader->LoadSDigits("recreate");
140 fPMDLoader->LoadHits("READ");
141 fPMDLoader->LoadDigits("recreate");
145 fPMDLoader->LoadSDigits("READ");
146 fPMDLoader->LoadDigits("recreate");
150 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
152 // This reads the PMD Hits tree and assigns the right track number
153 // to a cell and stores in the summable digits tree
155 // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
157 const Int_t kPi0 = 111;
158 const Int_t kGamma = 22;
166 Float_t xPos, yPos, zPos;
167 Int_t xpad = -1, ypad = -1;
169 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
174 printf("Event Number = %d \n",ievt);
175 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
176 printf("Number of Particles = %d \n", nparticles);
177 fRunLoader->GetEvent(ievt);
178 fPArray = fAlice->GetMCApp()->Particles();
179 // ------------------------------------------------------- //
180 // Pointer to specific detector hits.
181 // Get pointers to Alice detectors and Hits containers
183 fTreeH = fPMDLoader->TreeH();
185 Int_t ntracks = (Int_t) fTreeH->GetEntries();
186 printf("Number of Tracks in the TreeH = %d \n", ntracks);
188 fTreeS = fPMDLoader->TreeS();
191 fPMDLoader->MakeTree("S");
192 fTreeS = fPMDLoader->TreeS();
194 Int_t bufsize = 16000;
195 fTreeS->Branch("PMDSDigit", &fSDigits, bufsize);
197 if (fPMD) fHits = fPMD->Hits();
199 // Start loop on tracks in the hits containers
202 for (Int_t track=0; track<ntracks;track++)
205 fTreeH->GetEvent(track);
209 npmd = fHits->GetEntriesFast();
210 for (int ipmd = 0; ipmd < npmd; ipmd++)
212 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
213 trackno = fPMDHit->GetTrack();
215 // get kinematics of the particles
217 fParticle = fAlice->GetMCApp()->Particle(trackno);
218 trackpid = fParticle->GetPdgCode();
227 TParticle* mparticle = fParticle;
228 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
229 if (mparticle->GetFirstMother() == -1)
231 tracknoOld = trackno;
232 trackpidOld = trackpid;
237 while((imo = mparticle->GetFirstMother()) >= 0)
241 mparticle = fAlice->GetMCApp()->Particle(imo);
242 idmo = mparticle->GetPdgCode();
244 vx = mparticle->Vx();
245 vy = mparticle->Vy();
246 vz = mparticle->Vz();
248 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
249 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
250 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
258 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
267 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
279 mtrackno = tracknoOld;
280 mtrackpid = trackpidOld;
286 edep = fPMDHit->fEnergy;
287 Int_t vol1 = fPMDHit->fVolume[1]; // Column
288 Int_t vol2 = fPMDHit->fVolume[2]; // Row
289 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
290 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
291 // -----------------------------------------//
292 // For Super Module 1 & 2 //
293 // nrow = 96, ncol = 48 //
294 // For Super Module 3 & 4 //
295 // nrow = 48, ncol = 96 //
296 // -----------------------------------------//
298 smnumber = (vol6-1)*6 + vol3;
300 if (vol6 == 1 || vol6 == 2)
305 else if (vol6 == 3 || vol6 == 4)
311 //cout << "zpos = " << zPos << " edep = " << edep << endl;
313 Float_t zposition = TMath::Abs(zPos);
314 if (zposition < fZPos)
319 else if (zposition > fZPos)
324 Int_t smn = smnumber - 1;
325 Int_t ixx = xpad - 1;
326 Int_t iyy = ypad - 1;
329 fPRE[smn][ixx][iyy] += edep;
330 fPRECounter[smn][ixx][iyy]++;
332 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
334 fCell->Add(fPMDcell);
338 fCPV[smn][ixx][iyy] += edep;
339 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
343 } // Track Loop ended
345 TrackAssignment2Cell();
353 for (Int_t idet = 0; idet < 2; idet++)
355 for (Int_t ism = 0; ism < fgkTotUM; ism++)
357 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
359 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
361 cellno = jrow*fgkCol + kcol;
364 deltaE = fPRE[ism][jrow][kcol];
365 trno = fPRETrackNo[ism][jrow][kcol];
370 deltaE = fCPV[ism][jrow][kcol];
371 trno = fCPVTrackNo[ism][jrow][kcol];
376 AddSDigit(trno,detno,ism,cellno,deltaE);
384 fPMDLoader->WriteSDigits("OVERWRITE");
387 // cout << " -------- End of Hits2SDigit ----------- " << endl;
390 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
392 // This reads the PMD Hits tree and assigns the right track number
393 // to a cell and stores in the digits tree
395 const Int_t kPi0 = 111;
396 const Int_t kGamma = 22;
404 Float_t xPos, yPos, zPos;
405 Int_t xpad = -1, ypad = -1;
407 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
412 printf("Event Number = %d \n",ievt);
414 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
415 printf("Number of Particles = %d \n", nparticles);
416 fRunLoader->GetEvent(ievt);
417 fPArray = fAlice->GetMCApp()->Particles();
418 // ------------------------------------------------------- //
419 // Pointer to specific detector hits.
420 // Get pointers to Alice detectors and Hits containers
422 fPMD = (AliPMD*)fAlice->GetDetector("PMD");
423 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
425 if (fPMDLoader == 0x0)
427 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
429 fTreeH = fPMDLoader->TreeH();
430 Int_t ntracks = (Int_t) fTreeH->GetEntries();
431 printf("Number of Tracks in the TreeH = %d \n", ntracks);
432 fPMDLoader->LoadDigits("recreate");
433 fTreeD = fPMDLoader->TreeD();
436 fPMDLoader->MakeTree("D");
437 fTreeD = fPMDLoader->TreeD();
439 Int_t bufsize = 16000;
440 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
442 if (fPMD) fHits = fPMD->Hits();
444 // Start loop on tracks in the hits containers
446 for (Int_t track=0; track<ntracks;track++)
449 fTreeH->GetEvent(track);
453 npmd = fHits->GetEntriesFast();
454 for (int ipmd = 0; ipmd < npmd; ipmd++)
456 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
457 trackno = fPMDHit->GetTrack();
459 // get kinematics of the particles
461 fParticle = fAlice->GetMCApp()->Particle(trackno);
462 trackpid = fParticle->GetPdgCode();
471 TParticle* mparticle = fParticle;
472 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
473 if (mparticle->GetFirstMother() == -1)
475 tracknoOld = trackno;
476 trackpidOld = trackpid;
481 while((imo = mparticle->GetFirstMother()) >= 0)
485 mparticle = fAlice->GetMCApp()->Particle(imo);
486 idmo = mparticle->GetPdgCode();
488 vx = mparticle->Vx();
489 vy = mparticle->Vy();
490 vz = mparticle->Vz();
492 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
493 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
494 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
502 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
511 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
523 mtrackno = tracknoOld;
524 mtrackpid = trackpidOld;
530 edep = fPMDHit->fEnergy;
531 Int_t vol1 = fPMDHit->fVolume[1]; // Column
532 Int_t vol2 = fPMDHit->fVolume[2]; // Row
533 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
534 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
536 // -----------------------------------------//
537 // For Super Module 1 & 2 //
538 // nrow = 96, ncol = 48 //
539 // For Super Module 3 & 4 //
540 // nrow = 48, ncol = 96 //
541 // -----------------------------------------//
543 smnumber = (vol6-1)*6 + vol3;
545 if (vol6 == 1 || vol6 == 2)
550 else if (vol6 == 3 || vol6 == 4)
556 //cout << "-zpos = " << -zPos << endl;
558 Float_t zposition = TMath::Abs(zPos);
560 if (zposition < fZPos)
565 else if (zposition > fZPos)
571 Int_t smn = smnumber - 1;
572 Int_t ixx = xpad - 1;
573 Int_t iyy = ypad - 1;
576 fPRE[smn][ixx][iyy] += edep;
577 fPRECounter[smn][ixx][iyy]++;
579 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
581 fCell->Add(fPMDcell);
585 fCPV[smn][ixx][iyy] += edep;
586 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
590 } // Track Loop ended
592 TrackAssignment2Cell();
600 for (Int_t idet = 0; idet < 2; idet++)
602 for (Int_t ism = 0; ism < fgkTotUM; ism++)
604 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
606 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
608 cellno = jrow*fgkCol + kcol;
611 deltaE = fPRE[ism][jrow][kcol];
612 trno = fPRETrackNo[ism][jrow][kcol];
617 deltaE = fCPV[ism][jrow][kcol];
618 trno = fCPVTrackNo[ism][jrow][kcol];
623 AddDigit(trno,detno,ism,cellno,deltaE);
627 } // supermodule loop
632 fPMDLoader->WriteDigits("OVERWRITE");
635 // cout << " -------- End of Hits2Digit ----------- " << endl;
639 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
641 // This reads the PMD sdigits tree and converts energy deposition
642 // in a cell to ADC and stores in the digits tree
644 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
645 fRunLoader->GetEvent(ievt);
647 fTreeS = fPMDLoader->TreeS();
648 AliPMDsdigit *pmdsdigit;
649 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
650 branch->SetAddress(&fSDigits);
652 fTreeD = fPMDLoader->TreeD();
655 fPMDLoader->MakeTree("D");
656 fTreeD = fPMDLoader->TreeD();
658 Int_t bufsize = 16000;
659 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
661 Int_t trno, det, smn;
665 Int_t nmodules = (Int_t) fTreeS->GetEntries();
667 for (Int_t imodule = 0; imodule < nmodules; imodule++)
669 fTreeS->GetEntry(imodule);
670 Int_t nentries = fSDigits->GetLast();
671 //cout << " nentries = " << nentries << endl;
672 for (Int_t ient = 0; ient < nentries+1; ient++)
674 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
675 trno = pmdsdigit->GetTrackNumber();
676 det = pmdsdigit->GetDetector();
677 smn = pmdsdigit->GetSMNumber();
678 cellno = pmdsdigit->GetCellNumber();
679 edep = pmdsdigit->GetCellEdep();
682 AddDigit(trno,det,smn,cellno,adc);
687 fPMDLoader->WriteDigits("OVERWRITE");
688 // cout << " -------- End of SDigits2Digit ----------- " << endl;
691 void AliPMDDigitizer::TrackAssignment2Cell()
694 // This block assigns the cell id when there are
695 // multiple tracks in a cell according to the
698 Bool_t jsort = false;
710 pmdTrack = new Int_t ***[fgkTotUM];
711 pmdEdep = new Float_t ***[fgkTotUM];
712 for (i=0; i<fgkTotUM; i++)
714 pmdTrack[i] = new Int_t **[fgkRow];
715 pmdEdep[i] = new Float_t **[fgkRow];
718 for (i = 0; i < fgkTotUM; i++)
720 for (j = 0; j < fgkRow; j++)
722 pmdTrack[i][j] = new Int_t *[fgkCol];
723 pmdEdep[i][j] = new Float_t *[fgkCol];
727 for (i = 0; i < fgkTotUM; i++)
729 for (j = 0; j < fgkRow; j++)
731 for (k = 0; k < fgkCol; k++)
733 Int_t nn = fPRECounter[i][j][k];
736 pmdTrack[i][j][k] = new Int_t[nn];
737 pmdEdep[i][j][k] = new Float_t[nn];
742 pmdTrack[i][j][k] = new Int_t[nn];
743 pmdEdep[i][j][k] = new Float_t[nn];
745 fPRECounter[i][j][k] = 0;
751 Int_t nentries = fCell->GetEntries();
753 Int_t mtrackno, ism, ixp, iyp;
756 for (i = 0; i < nentries; i++)
758 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
760 mtrackno = fPMDcell->GetTrackNumber();
761 ism = fPMDcell->GetSMNumber();
762 ixp = fPMDcell->GetX();
763 iyp = fPMDcell->GetY();
764 edep = fPMDcell->GetEdep();
765 Int_t nn = fPRECounter[ism][ixp][iyp];
766 // cout << " nn = " << nn << endl;
767 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
768 pmdEdep[ism][ixp][iyp][nn] = edep;
769 fPRECounter[ism][ixp][iyp]++;
776 for (im=0; im<fgkTotUM; im++)
778 for (ix=0; ix<fgkRow; ix++)
780 for (iy=0; iy<fgkCol; iy++)
782 nn = fPRECounter[im][ix][iy];
785 // This block handles if a cell is fired
786 // many times by many tracks
787 status1 = new Int_t[nn];
788 status2 = new Int_t[nn];
789 trnarray = new Int_t[nn];
790 for (iz = 0; iz < nn; iz++)
792 status1[iz] = pmdTrack[im][ix][iy][iz];
794 TMath::Sort(nn,status1,status2,jsort);
795 Int_t trackOld = -99999;
796 Int_t track, trCount = 0;
797 for (iz = 0; iz < nn; iz++)
799 track = status1[status2[iz]];
800 if (trackOld != track)
802 trnarray[trCount] = track;
810 trEdp = new Float_t[trCount];
811 fracEdp = new Float_t[trCount];
812 for (il = 0; il < trCount; il++)
815 track = trnarray[il];
816 for (iz = 0; iz < nn; iz++)
818 if (track == pmdTrack[im][ix][iy][iz])
820 trEdp[il] += pmdEdep[im][ix][iy][iz];
826 Float_t fracOld = 0.;
828 for (il = 0; il < trCount; il++)
830 fracEdp[il] = trEdp[il]/totEdp;
831 if (fracOld < fracEdp[il])
833 fracOld = fracEdp[il];
837 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
844 // This only handles if a cell is fired
847 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
852 // This is if no cell is fired
853 fPRETrackNo[im][ix][iy] = -999;
859 // Delete all the pointers
861 for (i = 0; i < fgkTotUM; i++)
863 for (j = 0; j < fgkRow; j++)
865 for (k = 0; k < fgkCol; k++)
867 delete [] pmdTrack[i][j][k];
868 delete [] pmdEdep[i][j][k];
873 for (i = 0; i < fgkTotUM; i++)
875 for (j = 0; j < fgkRow; j++)
877 delete [] pmdTrack[i][j];
878 delete [] pmdEdep[i][j];
882 for (i = 0; i < fgkTotUM; i++)
884 delete [] pmdTrack[i];
885 delete [] pmdEdep[i];
890 // End of the cell id assignment
895 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc)
897 // This converts the simulated edep to ADC according to the
903 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
904 Int_t cellnumber, Float_t adc)
908 TClonesArray &lsdigits = *fSDigits;
909 AliPMDsdigit *newcell;
910 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
911 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
915 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
916 Int_t cellnumber, Float_t adc)
920 TClonesArray &ldigits = *fDigits;
921 AliPMDdigit *newcell;
922 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
923 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
927 void AliPMDDigitizer::SetZPosition(Float_t zpos)
931 Float_t AliPMDDigitizer::GetZPosition() const
936 void AliPMDDigitizer::ResetCell()
938 // clears the cell array and also the counter
942 for (Int_t i = 0; i < fgkTotUM; i++)
944 for (Int_t j = 0; j < fgkRow; j++)
946 for (Int_t k = 0; k < fgkCol; k++)
948 fPRECounter[i][j][k] = 0;
953 void AliPMDDigitizer::ResetSDigit()
957 if (fSDigits) fSDigits->Clear();
959 void AliPMDDigitizer::ResetDigit()
963 if (fDigits) fDigits->Clear();
966 void AliPMDDigitizer::ResetCellADC()
968 // Clears individual cells edep
969 for (Int_t i = 0; i < fgkTotUM; i++)
971 for (Int_t j = 0; j < fgkRow; j++)
973 for (Int_t k = 0; k < fgkCol; k++)
982 void AliPMDDigitizer::UnLoad(Option_t *option)
984 // Unloads all the root files
986 const char *cS = strstr(option,"S");
987 const char *cD = strstr(option,"D");
989 fRunLoader->UnloadgAlice();
990 fRunLoader->UnloadHeader();
991 fRunLoader->UnloadKinematics();
995 fPMDLoader->UnloadHits();
999 fPMDLoader->UnloadHits();
1000 fPMDLoader->UnloadSDigits();