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 gAlice = 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*)gAlice->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 = gAlice->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
201 for (Int_t track=0; track<ntracks;track++)
204 fTreeH->GetEvent(track);
207 npmd = fHits->GetEntriesFast();
208 cout << " npmd = " << npmd << endl;
209 for (int ipmd = 0; ipmd < npmd; ipmd++)
211 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
212 trackno = fPMDHit->GetTrack();
214 // get kinematics of the particles
216 fParticle = gAlice->GetMCApp()->Particle(trackno);
217 trackpid = fParticle->GetPdgCode();
219 cout << " trackno = " << trackno << " track = " << track << endl;
220 //cout << " trackpid = " << trackpid << endl;
228 TParticle* mparticle = fParticle;
229 Int_t jjnu = mparticle->GetFirstMother();
230 cout << " Coming to this step - step 0 jjnu = " << jjnu << endl;
232 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
233 if (mparticle->GetFirstMother() == -1)
235 tracknoOld = trackno;
236 trackpidOld = trackpid;
240 while((imo = mparticle->GetFirstMother()) >= 0)
244 mparticle = gAlice->GetMCApp()->Particle(imo);
245 idmo = mparticle->GetPdgCode();
247 vx = mparticle->Vx();
248 vy = mparticle->Vy();
249 vz = mparticle->Vz();
251 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
252 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
253 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
261 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
270 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
282 mtrackno = tracknoOld;
283 mtrackpid = trackpidOld;
288 edep = fPMDHit->fEnergy;
289 Int_t vol1 = fPMDHit->fVolume[1]; // Column
290 Int_t vol2 = fPMDHit->fVolume[2]; // Row
291 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
292 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
293 // -----------------------------------------//
294 // For Super Module 1 & 2 //
295 // nrow = 96, ncol = 48 //
296 // For Super Module 3 & 4 //
297 // nrow = 48, ncol = 96 //
298 // -----------------------------------------//
300 smnumber = (vol6-1)*6 + vol3;
302 if (vol6 == 1 || vol6 == 2)
307 else if (vol6 == 3 || vol6 == 4)
313 //cout << "zpos = " << zPos << " edep = " << edep << endl;
315 Float_t zposition = TMath::Abs(zPos);
316 if (zposition < fZPos)
321 else if (zposition > fZPos)
326 Int_t smn = smnumber - 1;
327 Int_t ixx = xpad - 1;
328 Int_t iyy = ypad - 1;
331 fPRE[smn][ixx][iyy] += edep;
332 fPRECounter[smn][ixx][iyy]++;
334 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
336 fCell->Add(fPMDcell);
340 fCPV[smn][ixx][iyy] += edep;
341 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
345 } // Track Loop ended
347 TrackAssignment2Cell();
355 for (Int_t idet = 0; idet < 2; idet++)
357 for (Int_t ism = 0; ism < fgkTotUM; ism++)
359 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
361 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
363 cellno = jrow*fgkCol + kcol;
366 deltaE = fPRE[ism][jrow][kcol];
367 trno = fPRETrackNo[ism][jrow][kcol];
372 deltaE = fCPV[ism][jrow][kcol];
373 trno = fCPVTrackNo[ism][jrow][kcol];
378 AddSDigit(trno,detno,ism,cellno,deltaE);
386 fPMDLoader->WriteSDigits("OVERWRITE");
389 // cout << " -------- End of Hits2SDigit ----------- " << endl;
392 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
394 // This reads the PMD Hits tree and assigns the right track number
395 // to a cell and stores in the digits tree
397 const Int_t kPi0 = 111;
398 const Int_t kGamma = 22;
406 Float_t xPos, yPos, zPos;
407 Int_t xpad = -1, ypad = -1;
409 Float_t vx = -999.0, vy = -999.0, vz = -999.0;
414 printf("Event Number = %d \n",ievt);
416 Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
417 printf("Number of Particles = %d \n", nparticles);
418 fRunLoader->GetEvent(ievt);
419 fPArray = gAlice->GetMCApp()->Particles();
420 // ------------------------------------------------------- //
421 // Pointer to specific detector hits.
422 // Get pointers to Alice detectors and Hits containers
424 fPMD = (AliPMD*)gAlice->GetDetector("PMD");
425 fPMDLoader = fRunLoader->GetLoader("PMDLoader");
427 if (fPMDLoader == 0x0)
429 cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
431 fTreeH = fPMDLoader->TreeH();
432 Int_t ntracks = (Int_t) fTreeH->GetEntries();
433 printf("Number of Tracks in the TreeH = %d \n", ntracks);
434 fPMDLoader->LoadDigits("recreate");
435 fTreeD = fPMDLoader->TreeD();
438 fPMDLoader->MakeTree("D");
439 fTreeD = fPMDLoader->TreeD();
441 Int_t bufsize = 16000;
442 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
444 if (fPMD) fHits = fPMD->Hits();
446 // Start loop on tracks in the hits containers
448 for (Int_t track=0; track<ntracks;track++)
451 fTreeH->GetEvent(track);
455 npmd = fHits->GetEntriesFast();
456 for (int ipmd = 0; ipmd < npmd; ipmd++)
458 fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
459 trackno = fPMDHit->GetTrack();
461 // get kinematics of the particles
463 fParticle = gAlice->GetMCApp()->Particle(trackno);
464 trackpid = fParticle->GetPdgCode();
473 TParticle* mparticle = fParticle;
474 Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
475 if (mparticle->GetFirstMother() == -1)
477 tracknoOld = trackno;
478 trackpidOld = trackpid;
483 while((imo = mparticle->GetFirstMother()) >= 0)
487 mparticle = gAlice->GetMCApp()->Particle(imo);
488 idmo = mparticle->GetPdgCode();
490 vx = mparticle->Vx();
491 vy = mparticle->Vy();
492 vz = mparticle->Vz();
494 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
495 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
496 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
504 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
513 if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
525 mtrackno = tracknoOld;
526 mtrackpid = trackpidOld;
532 edep = fPMDHit->fEnergy;
533 Int_t vol1 = fPMDHit->fVolume[1]; // Column
534 Int_t vol2 = fPMDHit->fVolume[2]; // Row
535 Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
536 Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
538 // -----------------------------------------//
539 // For Super Module 1 & 2 //
540 // nrow = 96, ncol = 48 //
541 // For Super Module 3 & 4 //
542 // nrow = 48, ncol = 96 //
543 // -----------------------------------------//
545 smnumber = (vol6-1)*6 + vol3;
547 if (vol6 == 1 || vol6 == 2)
552 else if (vol6 == 3 || vol6 == 4)
558 //cout << "-zpos = " << -zPos << endl;
560 Float_t zposition = TMath::Abs(zPos);
562 if (zposition < fZPos)
567 else if (zposition > fZPos)
573 Int_t smn = smnumber - 1;
574 Int_t ixx = xpad - 1;
575 Int_t iyy = ypad - 1;
578 fPRE[smn][ixx][iyy] += edep;
579 fPRECounter[smn][ixx][iyy]++;
581 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
583 fCell->Add(fPMDcell);
587 fCPV[smn][ixx][iyy] += edep;
588 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
592 } // Track Loop ended
594 TrackAssignment2Cell();
602 for (Int_t idet = 0; idet < 2; idet++)
604 for (Int_t ism = 0; ism < fgkTotUM; ism++)
606 for (Int_t jrow = 0; jrow < fgkRow; jrow++)
608 for (Int_t kcol = 0; kcol < fgkCol; kcol++)
610 cellno = jrow*fgkCol + kcol;
613 deltaE = fPRE[ism][jrow][kcol];
614 trno = fPRETrackNo[ism][jrow][kcol];
619 deltaE = fCPV[ism][jrow][kcol];
620 trno = fCPVTrackNo[ism][jrow][kcol];
625 AddDigit(trno,detno,ism,cellno,deltaE);
629 } // supermodule loop
634 fPMDLoader->WriteDigits("OVERWRITE");
637 // cout << " -------- End of Hits2Digit ----------- " << endl;
641 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
643 // This reads the PMD sdigits tree and converts energy deposition
644 // in a cell to ADC and stores in the digits tree
646 // cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
647 fRunLoader->GetEvent(ievt);
649 fTreeS = fPMDLoader->TreeS();
650 AliPMDsdigit *pmdsdigit;
651 TBranch *branch = fTreeS->GetBranch("PMDSDigit");
652 branch->SetAddress(&fSDigits);
654 fTreeD = fPMDLoader->TreeD();
657 fPMDLoader->MakeTree("D");
658 fTreeD = fPMDLoader->TreeD();
660 Int_t bufsize = 16000;
661 fTreeD->Branch("PMDDigit", &fDigits, bufsize);
663 Int_t trno, det, smn;
667 Int_t nmodules = (Int_t) fTreeS->GetEntries();
669 for (Int_t imodule = 0; imodule < nmodules; imodule++)
671 fTreeS->GetEntry(imodule);
672 Int_t nentries = fSDigits->GetLast();
673 //cout << " nentries = " << nentries << endl;
674 for (Int_t ient = 0; ient < nentries+1; ient++)
676 pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
677 trno = pmdsdigit->GetTrackNumber();
678 det = pmdsdigit->GetDetector();
679 smn = pmdsdigit->GetSMNumber();
680 cellno = pmdsdigit->GetCellNumber();
681 edep = pmdsdigit->GetCellEdep();
684 AddDigit(trno,det,smn,cellno,adc);
689 fPMDLoader->WriteDigits("OVERWRITE");
690 // cout << " -------- End of SDigits2Digit ----------- " << endl;
693 void AliPMDDigitizer::TrackAssignment2Cell()
696 // This block assigns the cell id when there are
697 // multiple tracks in a cell according to the
700 Bool_t jsort = false;
712 pmdTrack = new Int_t ***[fgkTotUM];
713 pmdEdep = new Float_t ***[fgkTotUM];
714 for (i=0; i<fgkTotUM; i++)
716 pmdTrack[i] = new Int_t **[fgkRow];
717 pmdEdep[i] = new Float_t **[fgkRow];
720 for (i = 0; i < fgkTotUM; i++)
722 for (j = 0; j < fgkRow; j++)
724 pmdTrack[i][j] = new Int_t *[fgkCol];
725 pmdEdep[i][j] = new Float_t *[fgkCol];
729 for (i = 0; i < fgkTotUM; i++)
731 for (j = 0; j < fgkRow; j++)
733 for (k = 0; k < fgkCol; k++)
735 Int_t nn = fPRECounter[i][j][k];
738 pmdTrack[i][j][k] = new Int_t[nn];
739 pmdEdep[i][j][k] = new Float_t[nn];
744 pmdTrack[i][j][k] = new Int_t[nn];
745 pmdEdep[i][j][k] = new Float_t[nn];
747 fPRECounter[i][j][k] = 0;
753 Int_t nentries = fCell->GetEntries();
755 Int_t mtrackno, ism, ixp, iyp;
758 for (i = 0; i < nentries; i++)
760 fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
762 mtrackno = fPMDcell->GetTrackNumber();
763 ism = fPMDcell->GetSMNumber();
764 ixp = fPMDcell->GetX();
765 iyp = fPMDcell->GetY();
766 edep = fPMDcell->GetEdep();
767 Int_t nn = fPRECounter[ism][ixp][iyp];
768 // cout << " nn = " << nn << endl;
769 pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
770 pmdEdep[ism][ixp][iyp][nn] = edep;
771 fPRECounter[ism][ixp][iyp]++;
778 for (im=0; im<fgkTotUM; im++)
780 for (ix=0; ix<fgkRow; ix++)
782 for (iy=0; iy<fgkCol; iy++)
784 nn = fPRECounter[im][ix][iy];
787 // This block handles if a cell is fired
788 // many times by many tracks
789 status1 = new Int_t[nn];
790 status2 = new Int_t[nn];
791 trnarray = new Int_t[nn];
792 for (iz = 0; iz < nn; iz++)
794 status1[iz] = pmdTrack[im][ix][iy][iz];
796 TMath::Sort(nn,status1,status2,jsort);
797 Int_t trackOld = -99999;
798 Int_t track, trCount = 0;
799 for (iz = 0; iz < nn; iz++)
801 track = status1[status2[iz]];
802 if (trackOld != track)
804 trnarray[trCount] = track;
812 trEdp = new Float_t[trCount];
813 fracEdp = new Float_t[trCount];
814 for (il = 0; il < trCount; il++)
817 track = trnarray[il];
818 for (iz = 0; iz < nn; iz++)
820 if (track == pmdTrack[im][ix][iy][iz])
822 trEdp[il] += pmdEdep[im][ix][iy][iz];
828 Float_t fracOld = 0.;
830 for (il = 0; il < trCount; il++)
832 fracEdp[il] = trEdp[il]/totEdp;
833 if (fracOld < fracEdp[il])
835 fracOld = fracEdp[il];
839 fPRETrackNo[im][ix][iy] = trnarray[ilOld];
846 // This only handles if a cell is fired
849 fPRETrackNo[im][ix][iy] = pmdTrack[im][ix][iy][0];
854 // This is if no cell is fired
855 fPRETrackNo[im][ix][iy] = -999;
861 // Delete all the pointers
863 for (i = 0; i < fgkTotUM; i++)
865 for (j = 0; j < fgkRow; j++)
867 for (k = 0; k < fgkCol; k++)
869 delete [] pmdTrack[i][j][k];
870 delete [] pmdEdep[i][j][k];
875 for (i = 0; i < fgkTotUM; i++)
877 for (j = 0; j < fgkRow; j++)
879 delete [] pmdTrack[i][j];
880 delete [] pmdEdep[i][j];
884 for (i = 0; i < fgkTotUM; i++)
886 delete [] pmdTrack[i];
887 delete [] pmdEdep[i];
892 // End of the cell id assignment
897 void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc)
899 // This converts the simulated edep to ADC according to the
905 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
906 Int_t cellnumber, Float_t adc)
910 TClonesArray &lsdigits = *fSDigits;
911 AliPMDsdigit *newcell;
912 newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
913 new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
917 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
918 Int_t cellnumber, Float_t adc)
922 TClonesArray &ldigits = *fDigits;
923 AliPMDdigit *newcell;
924 newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
925 new(ldigits[fNdigit++]) AliPMDdigit(newcell);
929 void AliPMDDigitizer::SetZPosition(Float_t zpos)
933 Float_t AliPMDDigitizer::GetZPosition() const
938 void AliPMDDigitizer::ResetCell()
940 // clears the cell array and also the counter
944 for (Int_t i = 0; i < fgkTotUM; i++)
946 for (Int_t j = 0; j < fgkRow; j++)
948 for (Int_t k = 0; k < fgkCol; k++)
950 fPRECounter[i][j][k] = 0;
955 void AliPMDDigitizer::ResetSDigit()
959 if (fSDigits) fSDigits->Clear();
961 void AliPMDDigitizer::ResetDigit()
965 if (fDigits) fDigits->Clear();
968 void AliPMDDigitizer::ResetCellADC()
970 // Clears individual cells edep
971 for (Int_t i = 0; i < fgkTotUM; i++)
973 for (Int_t j = 0; j < fgkRow; j++)
975 for (Int_t k = 0; k < fgkCol; k++)
984 void AliPMDDigitizer::UnLoad(Option_t *option)
986 // Unloads all the root files
988 const char *cS = strstr(option,"S");
989 const char *cD = strstr(option,"D");
991 fRunLoader->UnloadgAlice();
992 fRunLoader->UnloadHeader();
993 fRunLoader->UnloadKinematics();
997 fPMDLoader->UnloadHits();
1001 fPMDLoader->UnloadHits();
1002 fPMDLoader->UnloadSDigits();