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(Char_t *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,
100 Error("Open","Can not open session for file %s.",file);
103 fRunLoader->LoadgAlice();
104 fRunLoader->LoadHeader();
105 fRunLoader->LoadKinematics();
107 gAlice = fRunLoader->GetAliRun();
111 printf("<AliPMDdigitizer::Open> ");
112 printf("AliRun object found on file.\n");
116 printf("<AliPMDdigitizer::Open> ");
117 printf("Could not find AliRun object.\n");
120 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
121 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
122 if (fPMDLoader == 0x0)
124 cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
127 const char *cHS = strstr(option,"HS");
128 const char *cHD = strstr(option,"HD");
129 const char *cSD = strstr(option,"SD");
133 fPMDLoader->LoadHits("READ");
134 fPMDLoader->LoadSDigits("recreate");
138 fPMDLoader->LoadHits("READ");
139 fPMDLoader->LoadDigits("recreate");
143 fPMDLoader->LoadSDigits("READ");
144 fPMDLoader->LoadDigits("recreate");
148 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
150 // This reads the PMD Hits tree and assigns the right track number
151 // to a cell and stores in the summable digits tree
153 // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
155 const Int_t kPi0 = 111;
156 const Int_t kGamma = 22;
164 Float_t xPos, yPos, zPos;
165 Int_t xpad = -1, ypad = -1;
167 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
172 printf("Event Number = %d \n",ievt);
173 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
174 printf("Number of Particles = %d \n", nparticles);
175 fRunLoader->GetEvent(ievt);
176 // fPArray = gAlice->GetMCApp()->Particles();
177 // ------------------------------------------------------- //
178 // Pointer to specific detector hits.
179 // Get pointers to Alice detectors and Hits containers
181 fTreeH = fPMDLoader->TreeH();
183 Int_t ntracks = (Int_t) fTreeH->GetEntries();
184 printf("Number of Tracks in the TreeH = %d \n", ntracks);
186 fTreeS = fPMDLoader->TreeS();
189 fPMDLoader->MakeTree("S");
190 fTreeS = fPMDLoader->TreeS();
192 Int_t bufsize = 16000;
193 fTreeS->Branch("PMDSDigit", &fSDigits, bufsize);
195 if (fPMD) fHits = fPMD->Hits();
197 // Start loop on tracks in the hits containers
199 for (Int_t track=0; track<ntracks;track++)
202 fTreeH->GetEvent(track);
205 npmd = fHits->GetEntriesFast();
206 for (int ipmd = 0; ipmd < npmd; ipmd++)
208 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
209 trackno = fPMDHit->GetTrack();
210 // get kinematics of the particles
212 fParticle = gAlice->GetMCApp()->Particle(trackno);
213 trackpid = fParticle->GetPdgCode();
222 TParticle* mparticle = fParticle;
223 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
224 if (mparticle->GetFirstMother() == -1)
226 tracknoOld = trackno;
227 trackpidOld = trackpid;
231 while((imo = mparticle->GetFirstMother()) >= 0)
235 mparticle = gAlice->GetMCApp()->Particle(imo);
236 idmo = mparticle->GetPdgCode();
238 vx = mparticle->Vx();
239 vy = mparticle->Vy();
240 vz = mparticle->Vz();
242 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
243 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
244 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
252 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
261 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
273 mtrackno = tracknoOld;
274 mtrackpid = trackpidOld;
279 edep = fPMDHit->fEnergy;
280 Int_t vol1 = fPMDHit->fVolume[1]; // Column
281 Int_t vol2 = fPMDHit->fVolume[2]; // Row
282 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
283 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
284 // -----------------------------------------//
285 // For Super Module 1 & 2 //
286 // nrow = 96, ncol = 48 //
287 // For Super Module 3 & 4 //
288 // nrow = 48, ncol = 96 //
289 // -----------------------------------------//
291 smnumber = (vol6-1)*6 + vol3;
293 if (vol6 == 1 || vol6 == 2)
298 else if (vol6 == 3 || vol6 == 4)
304 //cout << "zpos = " << zPos << " edep = " << edep << endl;
306 Float_t zposition = TMath::Abs(zPos);
307 if (zposition < fZPos)
312 else if (zposition > fZPos)
317 Int_t smn = smnumber - 1;
318 Int_t ixx = xpad - 1;
319 Int_t iyy = ypad - 1;
322 fPRE[smn][ixx][iyy] += edep;
323 fPRECounter[smn][ixx][iyy]++;
325 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
327 fCell->Add(fPMDcell);
331 fCPV[smn][ixx][iyy] += edep;
332 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
336 } // Track Loop ended
338 TrackAssignment2Cell();
346 for (Int_t idet = 0; idet < 2; idet++)
348 for (Int_t ism = 0; ism < fgkTotUM; ism++)
350 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
352 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
354 cellno = jrow*fgkCol + kcol;
357 deltaE = fPRE[ism][jrow][kcol];
358 trno = fPRETrackNo[ism][jrow][kcol];
363 deltaE = fCPV[ism][jrow][kcol];
364 trno = fCPVTrackNo[ism][jrow][kcol];
369 AddSDigit(trno,detno,ism,cellno,deltaE);
377 fPMDLoader->WriteSDigits("OVERWRITE");
380 // cout << " -------- End of Hits2SDigit ----------- " << endl;
383 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
385 // This reads the PMD Hits tree and assigns the right track number
386 // to a cell and stores in the digits tree
388 const Int_t kPi0 = 111;
389 const Int_t kGamma = 22;
397 Float_t xPos, yPos, zPos;
398 Int_t xpad = -1, ypad = -1;
400 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
404 printf("Event Number = %d \n",ievt);
406 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
407 printf("Number of Particles = %d \n", nparticles);
408 fRunLoader->GetEvent(ievt);
409 // fPArray = gAlice->GetMCApp()->Particles();
410 // ------------------------------------------------------- //
411 // Pointer to specific detector hits.
412 // Get pointers to Alice detectors and Hits containers
414 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
415 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
417 if (fPMDLoader == 0x0)
419 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
421 fTreeH = fPMDLoader->TreeH();
422 Int_t ntracks = (Int_t) fTreeH->GetEntries();
423 printf("Number of Tracks in the TreeH = %d \n", ntracks);
424 fPMDLoader->LoadDigits("recreate");
425 fTreeD = fPMDLoader->TreeD();
428 fPMDLoader->MakeTree("D");
429 fTreeD = fPMDLoader->TreeD();
431 Int_t bufsize = 16000;
432 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
434 if (fPMD) fHits = fPMD->Hits();
436 // Start loop on tracks in the hits containers
438 for (Int_t track=0; track<ntracks;track++)
441 fTreeH->GetEvent(track);
445 npmd = fHits->GetEntriesFast();
446 for (int ipmd = 0; ipmd < npmd; ipmd++)
448 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
449 trackno = fPMDHit->GetTrack();
451 // get kinematics of the particles
453 fParticle = gAlice->GetMCApp()->Particle(trackno);
454 trackpid = fParticle->GetPdgCode();
463 TParticle* mparticle = fParticle;
464 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
465 if (mparticle->GetFirstMother() == -1)
467 tracknoOld = trackno;
468 trackpidOld = trackpid;
473 while((imo = mparticle->GetFirstMother()) >= 0)
477 mparticle = gAlice->GetMCApp()->Particle(imo);
478 idmo = mparticle->GetPdgCode();
480 vx = mparticle->Vx();
481 vy = mparticle->Vy();
482 vz = mparticle->Vz();
484 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
485 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
486 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
494 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
503 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
515 mtrackno = tracknoOld;
516 mtrackpid = trackpidOld;
522 edep = fPMDHit->fEnergy;
523 Int_t vol1 = fPMDHit->fVolume[1]; // Column
524 Int_t vol2 = fPMDHit->fVolume[2]; // Row
525 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
526 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
528 // -----------------------------------------//
529 // For Super Module 1 & 2 //
530 // nrow = 96, ncol = 48 //
531 // For Super Module 3 & 4 //
532 // nrow = 48, ncol = 96 //
533 // -----------------------------------------//
535 smnumber = (vol6-1)*6 + vol3;
537 if (vol6 == 1 || vol6 == 2)
542 else if (vol6 == 3 || vol6 == 4)
548 //cout << "-zpos = " << -zPos << endl;
550 Float_t zposition = TMath::Abs(zPos);
552 if (zposition < fZPos)
557 else if (zposition > fZPos)
563 Int_t smn = smnumber - 1;
564 Int_t ixx = xpad - 1;
565 Int_t iyy = ypad - 1;
568 fPRE[smn][ixx][iyy] += edep;
569 fPRECounter[smn][ixx][iyy]++;
571 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
573 fCell->Add(fPMDcell);
577 fCPV[smn][ixx][iyy] += edep;
578 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
582 } // Track Loop ended
584 TrackAssignment2Cell();
592 for (Int_t idet = 0; idet < 2; idet++)
594 for (Int_t ism = 0; ism < fgkTotUM; ism++)
596 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
598 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
600 cellno = jrow*fgkCol + kcol;
603 deltaE = fPRE[ism][jrow][kcol];
604 trno = fPRETrackNo[ism][jrow][kcol];
609 deltaE = fCPV[ism][jrow][kcol];
610 trno = fCPVTrackNo[ism][jrow][kcol];
615 AddDigit(trno,detno,ism,cellno,deltaE);
619 } // supermodule loop
624 fPMDLoader->WriteDigits("OVERWRITE");
627 // cout << " -------- End of Hits2Digit ----------- " << endl;
631 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
633 // This reads the PMD sdigits tree and converts energy deposition
634 // in a cell to ADC and stores in the digits tree
636 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
637 fRunLoader->GetEvent(ievt);
639 fTreeS = fPMDLoader->TreeS();
640 AliPMDsdigit *pmdsdigit;
641 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
642 branch->SetAddress(&fSDigits);
644 fTreeD = fPMDLoader->TreeD();
647 fPMDLoader->MakeTree("D");
648 fTreeD = fPMDLoader->TreeD();
650 Int_t bufsize = 16000;
651 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
653 Int_t trno, det, smn;
657 Int_t nmodules = (Int_t) fTreeS->GetEntries();
659 for (Int_t imodule = 0; imodule < nmodules; imodule++)
661 fTreeS->GetEntry(imodule);
662 Int_t nentries = fSDigits->GetLast();
663 //cout << " nentries = " << nentries << endl;
664 for (Int_t ient = 0; ient < nentries+1; ient++)
666 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
667 trno = pmdsdigit->GetTrackNumber();
668 det = pmdsdigit->GetDetector();
669 smn = pmdsdigit->GetSMNumber();
670 cellno = pmdsdigit->GetCellNumber();
671 edep = pmdsdigit->GetCellEdep();
674 AddDigit(trno,det,smn,cellno,adc);
679 fPMDLoader->WriteDigits("OVERWRITE");
680 // cout << " -------- End of SDigits2Digit ----------- " << endl;
683 void AliPMDDigitizer::TrackAssignment2Cell()
686 // This block assigns the cell id when there are
687 // multiple tracks in a cell according to the
690 Bool_t jsort = false;
702 pmdTrack = new Int_t ***[fgkTotUM];
703 pmdEdep = new Float_t ***[fgkTotUM];
704 for (i=0; i<fgkTotUM; i++)
706 pmdTrack[i] = new Int_t **[fgkRow];
707 pmdEdep[i] = new Float_t **[fgkRow];
710 for (i = 0; i < fgkTotUM; i++)
712 for (j = 0; j < fgkRow; j++)
714 pmdTrack[i][j] = new Int_t *[fgkCol];
715 pmdEdep[i][j] = new Float_t *[fgkCol];
719 for (i = 0; i < fgkTotUM; i++)
721 for (j = 0; j < fgkRow; j++)
723 for (k = 0; k < fgkCol; k++)
725 Int_t nn = fPRECounter[i][j][k];
728 pmdTrack[i][j][k] = new Int_t[nn];
729 pmdEdep[i][j][k] = new Float_t[nn];
734 pmdTrack[i][j][k] = new Int_t[nn];
735 pmdEdep[i][j][k] = new Float_t[nn];
737 fPRECounter[i][j][k] = 0;
743 Int_t nentries = fCell->GetEntries();
745 Int_t mtrackno, ism, ixp, iyp;
748 for (i = 0; i < nentries; i++)
750 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
752 mtrackno = fPMDcell->GetTrackNumber();
753 ism = fPMDcell->GetSMNumber();
754 ixp = fPMDcell->GetX();
755 iyp = fPMDcell->GetY();
756 edep = fPMDcell->GetEdep();
757 Int_t nn = fPRECounter[ism][ixp][iyp];
758 // cout << " nn = " << nn << endl;
759 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
760 pmdEdep[ism][ixp][iyp][nn] = edep;
761 fPRECounter[ism][ixp][iyp]++;
768 for (im=0; im<fgkTotUM; im++)
770 for (ix=0; ix<fgkRow; ix++)
772 for (iy=0; iy<fgkCol; iy++)
774 nn = fPRECounter[im][ix][iy];
777 // This block handles if a cell is fired
778 // many times by many tracks
779 status1 = new Int_t[nn];
780 status2 = new Int_t[nn];
781 trnarray = new Int_t[nn];
782 for (iz = 0; iz < nn; iz++)
784 status1[iz] = pmdTrack[im][ix][iy][iz];
786 TMath::Sort(nn,status1,status2,jsort);
787 Int_t trackOld = -99999;
788 Int_t track, trCount = 0;
789 for (iz = 0; iz < nn; iz++)
791 track = status1[status2[iz]];
792 if (trackOld != track)
794 trnarray[trCount] = track;
802 trEdp = new Float_t[trCount];
803 fracEdp = new Float_t[trCount];
804 for (il = 0; il < trCount; il++)
807 track = trnarray[il];
808 for (iz = 0; iz < nn; iz++)
810 if (track == pmdTrack[im][ix][iy][iz])
812 trEdp[il] += pmdEdep[im][ix][iy][iz];
818 Float_t fracOld = 0.;
820 for (il = 0; il < trCount; il++)
822 fracEdp[il] = trEdp[il]/totEdp;
823 if (fracOld < fracEdp[il])
825 fracOld = fracEdp[il];
829 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
836 // This only handles if a cell is fired
839 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
844 // This is if no cell is fired
845 fPRETrackNo[im][ix][iy] = -999;
851 // Delete all the pointers
853 for (i = 0; i < fgkTotUM; i++)
855 for (j = 0; j < fgkRow; j++)
857 for (k = 0; k < fgkCol; k++)
859 delete [] pmdTrack[i][j][k];
860 delete [] pmdEdep[i][j][k];
865 for (i = 0; i < fgkTotUM; i++)
867 for (j = 0; j < fgkRow; j++)
869 delete [] pmdTrack[i][j];
870 delete [] pmdEdep[i][j];
874 for (i = 0; i < fgkTotUM; i++)
876 delete [] pmdTrack[i];
877 delete [] pmdEdep[i];
882 // End of the cell id assignment
886 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
888 // This converts the simulated edep to ADC according to the
894 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
895 Int_t cellnumber, Float_t adc)
899 TClonesArray &lsdigits = *fSDigits;
900 AliPMDsdigit *newcell;
901 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
902 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
906 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
907 Int_t cellnumber, Float_t adc)
911 TClonesArray &ldigits = *fDigits;
912 AliPMDdigit *newcell;
913 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
914 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
918 void AliPMDDigitizer::SetZPosition(Float_t zpos)
922 Float_t AliPMDDigitizer::GetZPosition() const
927 void AliPMDDigitizer::ResetCell()
929 // clears the cell array and also the counter
933 for (Int_t i = 0; i < fgkTotUM; i++)
935 for (Int_t j = 0; j < fgkRow; j++)
937 for (Int_t k = 0; k < fgkCol; k++)
939 fPRECounter[i][j][k] = 0;
944 void AliPMDDigitizer::ResetSDigit()
948 if (fSDigits) fSDigits->Clear();
950 void AliPMDDigitizer::ResetDigit()
954 if (fDigits) fDigits->Clear();
957 void AliPMDDigitizer::ResetCellADC()
959 // Clears individual cells edep
960 for (Int_t i = 0; i < fgkTotUM; i++)
962 for (Int_t j = 0; j < fgkRow; j++)
964 for (Int_t k = 0; k < fgkCol; k++)
973 void AliPMDDigitizer::UnLoad(Option_t *option)
975 // Unloads all the root files
977 const char *cS = strstr(option,"S");
978 const char *cD = strstr(option,"D");
980 fRunLoader->UnloadgAlice();
981 fRunLoader->UnloadHeader();
982 fRunLoader->UnloadKinematics();
986 fPMDLoader->UnloadHits();
990 fPMDLoader->UnloadHits();
991 fPMDLoader->UnloadSDigits();