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()
56 // Default Constructor
58 if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
60 if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
63 for (Int_t i = 0; i < fgkTotUM; i++)
65 for (Int_t j = 0; j < fgkRow; j++)
67 for (Int_t k = 0; k < fgkCol; k++)
75 if (!fCell) fCell = new TObjArray();
77 fZPos = 361.5; // in units of cm, This is the default position of PMD
79 AliPMDDigitizer::~AliPMDDigitizer()
90 void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
92 // Loads galice.root file and corresponding header, kinematics
93 // hits and sdigits or digits depending on the option
95 fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName);
99 Error("Open","Can not open session for file %s.",file);
102 fRunLoader->LoadgAlice();
103 fRunLoader->LoadHeader();
104 fRunLoader->LoadKinematics();
106 gAlice = fRunLoader->GetAliRun();
110 printf("<AliPMDdigitizer::Open> ");
111 printf("AliRun object found on file.\n");
115 printf("<AliPMDdigitizer::Open> ");
116 printf("Could not find AliRun object.\n");
119 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
120 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
121 if (fPMDLoader == 0x0)
123 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
126 const char *cHS = strstr(option,"HS");
127 const char *cHD = strstr(option,"HD");
128 const char *cSD = strstr(option,"SD");
132 fPMDLoader->LoadHits("READ");
133 fPMDLoader->LoadSDigits("recreate");
137 fPMDLoader->LoadHits("READ");
138 fPMDLoader->LoadDigits("recreate");
142 fPMDLoader->LoadSDigits("READ");
143 fPMDLoader->LoadDigits("recreate");
147 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
149 // This reads the PMD Hits tree and assigns the right track number
150 // to a cell and stores in the summable digits tree
152 // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
154 const Int_t kPi0 = 111;
155 const Int_t kGamma = 22;
163 Float_t xPos, yPos, zPos;
164 Int_t xpad = -1, ypad = -1;
166 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
171 printf("Event Number = %d \n",ievt);
172 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
173 printf("Number of Particles = %d \n", nparticles);
174 fRunLoader->GetEvent(ievt);
175 // fPArray = gAlice->GetMCApp()->Particles();
176 // ------------------------------------------------------- //
177 // Pointer to specific detector hits.
178 // Get pointers to Alice detectors and Hits containers
180 fTreeH = fPMDLoader->TreeH();
182 Int_t ntracks = (Int_t) fTreeH->GetEntries();
183 printf("Number of Tracks in the TreeH = %d \n", ntracks);
185 fTreeS = fPMDLoader->TreeS();
188 fPMDLoader->MakeTree("S");
189 fTreeS = fPMDLoader->TreeS();
191 Int_t bufsize = 16000;
192 fTreeS->Branch("PMDSDigit", &fSDigits, bufsize);
194 if (fPMD) fHits = fPMD->Hits();
196 // Start loop on tracks in the hits containers
198 for (Int_t track=0; track<ntracks;track++)
201 fTreeH->GetEvent(track);
204 npmd = fHits->GetEntriesFast();
205 for (int ipmd = 0; ipmd < npmd; ipmd++)
207 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
208 trackno = fPMDHit->GetTrack();
209 // get kinematics of the particles
211 fParticle = gAlice->GetMCApp()->Particle(trackno);
212 trackpid = fParticle->GetPdgCode();
221 TParticle* mparticle = fParticle;
222 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
223 if (mparticle->GetFirstMother() == -1)
225 tracknoOld = trackno;
226 trackpidOld = trackpid;
230 while((imo = mparticle->GetFirstMother()) >= 0)
234 mparticle = gAlice->GetMCApp()->Particle(imo);
235 idmo = mparticle->GetPdgCode();
237 vx = mparticle->Vx();
238 vy = mparticle->Vy();
239 vz = mparticle->Vz();
241 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
242 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
243 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
251 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
260 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
272 mtrackno = tracknoOld;
273 mtrackpid = trackpidOld;
278 edep = fPMDHit->fEnergy;
279 Int_t vol1 = fPMDHit->fVolume[1]; // Column
280 Int_t vol2 = fPMDHit->fVolume[2]; // Row
281 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
282 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
283 // -----------------------------------------//
284 // For Super Module 1 & 2 //
285 // nrow = 96, ncol = 48 //
286 // For Super Module 3 & 4 //
287 // nrow = 48, ncol = 96 //
288 // -----------------------------------------//
290 smnumber = (vol6-1)*6 + vol3;
292 if (vol6 == 1 || vol6 == 2)
297 else if (vol6 == 3 || vol6 == 4)
303 //cout << "zpos = " << zPos << " edep = " << edep << endl;
305 Float_t zposition = TMath::Abs(zPos);
306 if (zposition < fZPos)
311 else if (zposition > fZPos)
316 Int_t smn = smnumber - 1;
317 Int_t ixx = xpad - 1;
318 Int_t iyy = ypad - 1;
321 fPRE[smn][ixx][iyy] += edep;
322 fPRECounter[smn][ixx][iyy]++;
324 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
326 fCell->Add(fPMDcell);
330 fCPV[smn][ixx][iyy] += edep;
331 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
335 } // Track Loop ended
337 TrackAssignment2Cell();
345 for (Int_t idet = 0; idet < 2; idet++)
347 for (Int_t ism = 0; ism < fgkTotUM; ism++)
349 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
351 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
353 cellno = jrow*fgkCol + kcol;
356 deltaE = fPRE[ism][jrow][kcol];
357 trno = fPRETrackNo[ism][jrow][kcol];
362 deltaE = fCPV[ism][jrow][kcol];
363 trno = fCPVTrackNo[ism][jrow][kcol];
368 AddSDigit(trno,detno,ism,cellno,deltaE);
376 fPMDLoader->WriteSDigits("OVERWRITE");
379 // cout << " -------- End of Hits2SDigit ----------- " << endl;
382 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
384 // This reads the PMD Hits tree and assigns the right track number
385 // to a cell and stores in the digits tree
387 const Int_t kPi0 = 111;
388 const Int_t kGamma = 22;
396 Float_t xPos, yPos, zPos;
397 Int_t xpad = -1, ypad = -1;
399 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
403 printf("Event Number = %d \n",ievt);
405 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
406 printf("Number of Particles = %d \n", nparticles);
407 fRunLoader->GetEvent(ievt);
408 // fPArray = gAlice->GetMCApp()->Particles();
409 // ------------------------------------------------------- //
410 // Pointer to specific detector hits.
411 // Get pointers to Alice detectors and Hits containers
413 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
414 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
416 if (fPMDLoader == 0x0)
418 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
420 fTreeH = fPMDLoader->TreeH();
421 Int_t ntracks = (Int_t) fTreeH->GetEntries();
422 printf("Number of Tracks in the TreeH = %d \n", ntracks);
423 fPMDLoader->LoadDigits("recreate");
424 fTreeD = fPMDLoader->TreeD();
427 fPMDLoader->MakeTree("D");
428 fTreeD = fPMDLoader->TreeD();
430 Int_t bufsize = 16000;
431 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
433 if (fPMD) fHits = fPMD->Hits();
435 // Start loop on tracks in the hits containers
437 for (Int_t track=0; track<ntracks;track++)
440 fTreeH->GetEvent(track);
444 npmd = fHits->GetEntriesFast();
445 for (int ipmd = 0; ipmd < npmd; ipmd++)
447 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
448 trackno = fPMDHit->GetTrack();
450 // get kinematics of the particles
452 fParticle = gAlice->GetMCApp()->Particle(trackno);
453 trackpid = fParticle->GetPdgCode();
462 TParticle* mparticle = fParticle;
463 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
464 if (mparticle->GetFirstMother() == -1)
466 tracknoOld = trackno;
467 trackpidOld = trackpid;
472 while((imo = mparticle->GetFirstMother()) >= 0)
476 mparticle = gAlice->GetMCApp()->Particle(imo);
477 idmo = mparticle->GetPdgCode();
479 vx = mparticle->Vx();
480 vy = mparticle->Vy();
481 vz = mparticle->Vz();
483 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
484 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
485 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
493 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
502 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
514 mtrackno = tracknoOld;
515 mtrackpid = trackpidOld;
521 edep = fPMDHit->fEnergy;
522 Int_t vol1 = fPMDHit->fVolume[1]; // Column
523 Int_t vol2 = fPMDHit->fVolume[2]; // Row
524 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
525 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
527 // -----------------------------------------//
528 // For Super Module 1 & 2 //
529 // nrow = 96, ncol = 48 //
530 // For Super Module 3 & 4 //
531 // nrow = 48, ncol = 96 //
532 // -----------------------------------------//
534 smnumber = (vol6-1)*6 + vol3;
536 if (vol6 == 1 || vol6 == 2)
541 else if (vol6 == 3 || vol6 == 4)
547 //cout << "-zpos = " << -zPos << endl;
549 Float_t zposition = TMath::Abs(zPos);
551 if (zposition < fZPos)
556 else if (zposition > fZPos)
562 Int_t smn = smnumber - 1;
563 Int_t ixx = xpad - 1;
564 Int_t iyy = ypad - 1;
567 fPRE[smn][ixx][iyy] += edep;
568 fPRECounter[smn][ixx][iyy]++;
570 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
572 fCell->Add(fPMDcell);
576 fCPV[smn][ixx][iyy] += edep;
577 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
581 } // Track Loop ended
583 TrackAssignment2Cell();
591 for (Int_t idet = 0; idet < 2; idet++)
593 for (Int_t ism = 0; ism < fgkTotUM; ism++)
595 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
597 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
599 cellno = jrow*fgkCol + kcol;
602 deltaE = fPRE[ism][jrow][kcol];
603 trno = fPRETrackNo[ism][jrow][kcol];
608 deltaE = fCPV[ism][jrow][kcol];
609 trno = fCPVTrackNo[ism][jrow][kcol];
614 AddDigit(trno,detno,ism,cellno,deltaE);
618 } // supermodule loop
623 fPMDLoader->WriteDigits("OVERWRITE");
626 // cout << " -------- End of Hits2Digit ----------- " << endl;
630 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
632 // This reads the PMD sdigits tree and converts energy deposition
633 // in a cell to ADC and stores in the digits tree
635 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
636 fRunLoader->GetEvent(ievt);
638 fTreeS = fPMDLoader->TreeS();
639 AliPMDsdigit *pmdsdigit;
640 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
641 branch->SetAddress(&fSDigits);
643 fTreeD = fPMDLoader->TreeD();
646 fPMDLoader->MakeTree("D");
647 fTreeD = fPMDLoader->TreeD();
649 Int_t bufsize = 16000;
650 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
652 Int_t trno, det, smn;
656 Int_t nmodules = (Int_t) fTreeS->GetEntries();
658 for (Int_t imodule = 0; imodule < nmodules; imodule++)
660 fTreeS->GetEntry(imodule);
661 Int_t nentries = fSDigits->GetLast();
662 //cout << " nentries = " << nentries << endl;
663 for (Int_t ient = 0; ient < nentries+1; ient++)
665 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
666 trno = pmdsdigit->GetTrackNumber();
667 det = pmdsdigit->GetDetector();
668 smn = pmdsdigit->GetSMNumber();
669 cellno = pmdsdigit->GetCellNumber();
670 edep = pmdsdigit->GetCellEdep();
673 AddDigit(trno,det,smn,cellno,adc);
678 fPMDLoader->WriteDigits("OVERWRITE");
679 // cout << " -------- End of SDigits2Digit ----------- " << endl;
682 void AliPMDDigitizer::TrackAssignment2Cell()
685 // This block assigns the cell id when there are
686 // multiple tracks in a cell according to the
689 Bool_t jsort = false;
701 pmdTrack = new Int_t ***[fgkTotUM];
702 pmdEdep = new Float_t ***[fgkTotUM];
703 for (i=0; i<fgkTotUM; i++)
705 pmdTrack[i] = new Int_t **[fgkRow];
706 pmdEdep[i] = new Float_t **[fgkRow];
709 for (i = 0; i < fgkTotUM; i++)
711 for (j = 0; j < fgkRow; j++)
713 pmdTrack[i][j] = new Int_t *[fgkCol];
714 pmdEdep[i][j] = new Float_t *[fgkCol];
718 for (i = 0; i < fgkTotUM; i++)
720 for (j = 0; j < fgkRow; j++)
722 for (k = 0; k < fgkCol; k++)
724 Int_t nn = fPRECounter[i][j][k];
727 pmdTrack[i][j][k] = new Int_t[nn];
728 pmdEdep[i][j][k] = new Float_t[nn];
733 pmdTrack[i][j][k] = new Int_t[nn];
734 pmdEdep[i][j][k] = new Float_t[nn];
736 fPRECounter[i][j][k] = 0;
742 Int_t nentries = fCell->GetEntries();
744 Int_t mtrackno, ism, ixp, iyp;
747 for (i = 0; i < nentries; i++)
749 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
751 mtrackno = fPMDcell->GetTrackNumber();
752 ism = fPMDcell->GetSMNumber();
753 ixp = fPMDcell->GetX();
754 iyp = fPMDcell->GetY();
755 edep = fPMDcell->GetEdep();
756 Int_t nn = fPRECounter[ism][ixp][iyp];
757 // cout << " nn = " << nn << endl;
758 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
759 pmdEdep[ism][ixp][iyp][nn] = edep;
760 fPRECounter[ism][ixp][iyp]++;
767 for (im=0; im<fgkTotUM; im++)
769 for (ix=0; ix<fgkRow; ix++)
771 for (iy=0; iy<fgkCol; iy++)
773 nn = fPRECounter[im][ix][iy];
776 // This block handles if a cell is fired
777 // many times by many tracks
778 status1 = new Int_t[nn];
779 status2 = new Int_t[nn];
780 trnarray = new Int_t[nn];
781 for (iz = 0; iz < nn; iz++)
783 status1[iz] = pmdTrack[im][ix][iy][iz];
785 TMath::Sort(nn,status1,status2,jsort);
786 Int_t trackOld = -99999;
787 Int_t track, trCount = 0;
788 for (iz = 0; iz < nn; iz++)
790 track = status1[status2[iz]];
791 if (trackOld != track)
793 trnarray[trCount] = track;
801 trEdp = new Float_t[trCount];
802 fracEdp = new Float_t[trCount];
803 for (il = 0; il < trCount; il++)
806 track = trnarray[il];
807 for (iz = 0; iz < nn; iz++)
809 if (track == pmdTrack[im][ix][iy][iz])
811 trEdp[il] += pmdEdep[im][ix][iy][iz];
817 Float_t fracOld = 0.;
819 for (il = 0; il < trCount; il++)
821 fracEdp[il] = trEdp[il]/totEdp;
822 if (fracOld < fracEdp[il])
824 fracOld = fracEdp[il];
828 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
835 // This only handles if a cell is fired
838 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
843 // This is if no cell is fired
844 fPRETrackNo[im][ix][iy] = -999;
850 // Delete all the pointers
852 for (i = 0; i < fgkTotUM; i++)
854 for (j = 0; j < fgkRow; j++)
856 for (k = 0; k < fgkCol; k++)
858 delete [] pmdTrack[i][j][k];
859 delete [] pmdEdep[i][j][k];
864 for (i = 0; i < fgkTotUM; i++)
866 for (j = 0; j < fgkRow; j++)
868 delete [] pmdTrack[i][j];
869 delete [] pmdEdep[i][j];
873 for (i = 0; i < fgkTotUM; i++)
875 delete [] pmdTrack[i];
876 delete [] pmdEdep[i];
881 // End of the cell id assignment
885 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
887 // This converts the simulated edep to ADC according to the
893 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
894 Int_t cellnumber, Float_t adc)
898 TClonesArray &lsdigits = *fSDigits;
899 AliPMDsdigit *newcell;
900 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
901 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
905 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
906 Int_t cellnumber, Float_t adc)
910 TClonesArray &ldigits = *fDigits;
911 AliPMDdigit *newcell;
912 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
913 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
917 void AliPMDDigitizer::SetZPosition(Float_t zpos)
921 Float_t AliPMDDigitizer::GetZPosition() const
926 void AliPMDDigitizer::ResetCell()
928 // clears the cell array and also the counter
932 for (Int_t i = 0; i < fgkTotUM; i++)
934 for (Int_t j = 0; j < fgkRow; j++)
936 for (Int_t k = 0; k < fgkCol; k++)
938 fPRECounter[i][j][k] = 0;
943 void AliPMDDigitizer::ResetSDigit()
947 if (fSDigits) fSDigits->Clear();
949 void AliPMDDigitizer::ResetDigit()
953 if (fDigits) fDigits->Clear();
956 void AliPMDDigitizer::ResetCellADC()
958 // Clears individual cells edep
959 for (Int_t i = 0; i < fgkTotUM; i++)
961 for (Int_t j = 0; j < fgkRow; j++)
963 for (Int_t k = 0; k < fgkCol; k++)
972 void AliPMDDigitizer::UnLoad(Option_t *option)
974 // Unloads all the root files
976 const char *cS = strstr(option,"S");
977 const char *cD = strstr(option,"D");
979 fRunLoader->UnloadgAlice();
980 fRunLoader->UnloadHeader();
981 fRunLoader->UnloadKinematics();
985 fPMDLoader->UnloadHits();
989 fPMDLoader->UnloadHits();
990 fPMDLoader->UnloadSDigits();