]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDigitizer.cxx
Newline added at the end of file.
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2000, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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
16 /*
17 $Log$
18 Revision 1.8  2002/02/13 09:03:24  jchudoba
19 Remove some deletes from dtor, those objects are deleted earlier in Exec() method (where they are created)
20
21 Revision 1.7  2001/11/22 11:15:41  jchudoba
22 Proper deletion of arrays (thanks to Rene Brun)
23
24 Revision 1.6  2001/11/02 12:55:45  jchudoba
25 cleanup of the code, add const to Get methods
26
27 Revision 1.4  2001/10/18 14:44:09  jchudoba
28 Define constant MAXTRACKS for maximum number of tracks associated with 1 digit
29
30 Revision 1.3  2001/10/04 20:01:54  jchudoba
31 changes for TTask implementation, some other small editing
32
33 Revision 1.2  2001/07/28 10:46:04  hristov
34 AliRunDigitizer.h included; typos corrected
35
36 Revision 1.1  2001/07/27 15:41:01  jchudoba
37 merging/digitization classes
38
39 */
40 #include <TTree.h> 
41 #include <TObjArray.h>
42 #include <TFile.h>
43 #include <TDirectory.h>
44 #include <iostream.h>
45
46 #include "AliMUONDigitizer.h"
47 #include "AliMUONConstants.h"
48 #include "AliMUONChamber.h"
49 #include "AliMUONHitMapA1.h"
50 #include "AliMUON.h"
51 #include "AliMUONHit.h"
52 #include "AliMUONPadHit.h"
53 #include "AliMUONDigit.h"
54 #include "AliMUONTransientDigit.h"
55 #include "AliRun.h"
56 #include "AliPDG.h"
57 #include "AliRunDigitizer.h"
58
59 ClassImp(AliMUONDigitizer)
60
61 //___________________________________________
62 AliMUONDigitizer::AliMUONDigitizer() :AliDigitizer()
63 {
64 // Default ctor - don't use it
65   fHits = 0;
66   fPadHits = 0;
67   fHitMap = 0;
68   fTDList = 0;
69 }
70
71 //___________________________________________
72 AliMUONDigitizer::AliMUONDigitizer(AliRunDigitizer* manager) 
73     :AliDigitizer(manager)
74 {
75 // ctor which should be used
76   fHitMap  = 0;
77   fTDList  = 0;
78   fHits    = 0;
79   fPadHits = 0;
80   fDebug   = 0; 
81   if (GetDebug()>2) 
82     cerr<<"AliMUONDigitizer::AliMUONDigitizer"
83         <<"(AliRunDigitizer* manager) was processed"<<endl;
84 }
85
86 //------------------------------------------------------------------------
87 AliMUONDigitizer::~AliMUONDigitizer()
88 {
89 // Destructor
90   if (fHits)       delete fHits;
91   if (fPadHits)    delete fPadHits;
92 }
93
94 //------------------------------------------------------------------------
95 Bool_t AliMUONDigitizer::Exists(const AliMUONPadHit *padhit) const
96 {
97   return (fHitMap[fNch]->TestHit(padhit->PadX(),padhit->PadY()));
98 }
99
100 //------------------------------------------------------------------------
101 void AliMUONDigitizer::Update(AliMUONPadHit *padhit)
102 {
103     AliMUONTransientDigit *pdigit = 
104       static_cast<AliMUONTransientDigit*>(
105       fHitMap[fNch]->GetHit(padhit->PadX(),padhit->PadY()));
106
107     // update charge
108     //
109     Int_t iqpad    = padhit->QPad();        // charge per pad
110     pdigit->AddSignal(iqpad);
111     pdigit->AddPhysicsSignal(iqpad);            
112
113     // update list of tracks
114     //
115     Int_t track, charge;    
116     track = fTrack+fMask;
117     if (fSignal) {
118       charge = iqpad;
119     } else {
120       charge = kBgTag;
121     }
122     pdigit->UpdateTrackList(track,charge);
123 }
124
125 //------------------------------------------------------------------------
126 void AliMUONDigitizer::CreateNew(AliMUONPadHit *padhit)
127 {
128 // Create new AliMUONTransientDigit and add it to the fTDList
129
130   fTDList->AddAtAndExpand(
131     new AliMUONTransientDigit(fNch,fDigits),fCounter);
132   fHitMap[fNch]->SetHit(padhit->PadX(),padhit->PadY(),fCounter);
133   AliMUONTransientDigit* pdigit = 
134     (AliMUONTransientDigit*)fTDList->Last();
135   // list of tracks
136   Int_t track, charge;    
137   track = fTrack+fMask;
138   if (fSignal) {
139     charge = padhit->QPad();
140   } else {
141     charge = kBgTag;
142   }
143   pdigit->AddToTrackList(track,charge);
144   fCounter++;
145 }
146
147
148 //------------------------------------------------------------------------
149 Bool_t AliMUONDigitizer::Init()
150 {
151 // Initialization 
152     
153   fHits     = new TClonesArray("AliMUONHit",1000);
154   fPadHits  = new TClonesArray("AliMUONPadHit",1000);
155   return kTRUE;
156 }
157
158
159 //------------------------------------------------------------------------
160 //void AliMUONDigitizer::Digitize()
161 void AliMUONDigitizer::Exec(Option_t* option)
162 {
163
164   TString optionString = option;
165   if (optionString.Data() == "deb") {
166     cout<<"AliMUONDigitizer::Exec: called with option deb "<<endl;
167     fDebug = 3;
168   }
169   AliMUONChamber*   iChamber;
170   AliSegmentation*  segmentation;
171   
172   if (GetDebug()>2) cerr<<"   AliMUONDigitizer::Digitize() starts"<<endl;
173   fTDList = new TObjArray;
174
175   AliMUON *pMUON  = (AliMUON *) gAlice->GetModule("MUON");
176   if (!pMUON) {
177     cerr<<"AliMUONDigitizer::Digitize Error:"
178         <<" module MUON not found in the input file"<<endl;
179     return;
180   }
181   pMUON->MakeBranchInTreeD(fManager->GetTreeD());
182   fHitMap= new AliMUONHitMapA1* [AliMUONConstants::NCh()];
183
184   //
185   // loop over cathodes
186   //
187
188   for (int icat = 0; icat < 2; icat++) { 
189     fCounter = 0;
190     for (Int_t i = 0; i < AliMUONConstants::NCh(); i++) {
191       iChamber = &(pMUON->Chamber(i));
192 //      if (!(iChamber->Nsec() == 1 && icat == 1)) {
193         segmentation = iChamber->SegmentationModel(icat+1);
194         fHitMap[i] = new AliMUONHitMapA1(segmentation, fTDList);
195 //      }
196     }
197
198
199 // Loop over files to digitize
200     fSignal = kTRUE;
201     for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
202          inputFile++) {
203 // Connect MUON branches
204
205       if (inputFile > 0 ) fSignal = kFALSE;
206       TBranch *branchHits = 0;
207       TBranch *branchPadHits = 0;
208       TTree *treeH = fManager->GetInputTreeH(inputFile);
209       if (GetDebug()>2) {
210         cerr<<"   inputFile , cathode = "<<inputFile<<" "
211             <<icat<<endl;
212         cerr<<"   treeH, fHits "<<treeH<<" "<<fHits<<endl;
213       }
214       if (treeH && fHits) {
215         branchHits = treeH->GetBranch("MUON");
216         if (branchHits) {
217           fHits->Clear();
218           branchHits->SetAddress(&fHits);
219         }
220         else
221           Error("Exec","branch MUON was not found");
222       }
223       if (GetDebug()>2) cerr<<"   branchHits = "<<branchHits<<endl;
224
225       if (treeH && fPadHits) {
226         branchPadHits = treeH->GetBranch("MUONCluster");
227         if (branchPadHits) 
228           branchPadHits->SetAddress(&fPadHits);
229         else
230           Error("Exec","branch MUONCluster was not found");
231       }
232       if (GetDebug()>2) cerr<<"   branchPadHits = "<<branchPadHits<<endl;
233
234 //
235 //   Loop over tracks
236 //
237
238       Int_t ntracks = (Int_t) treeH->GetEntries();
239
240       for (fTrack = 0; fTrack < ntracks; fTrack++) {
241         if (GetDebug()>2) cerr<<"   fTrack = "<<fTrack<<endl;
242         fHits->Clear();
243         fPadHits->Clear();
244         branchHits->GetEntry(fTrack);
245         branchPadHits->GetEntry(fTrack);
246         
247 //
248 //   Loop over hits
249
250         AliMUONHit* mHit;
251         for(Int_t i = 0; i < fHits->GetEntriesFast(); ++i) {
252           mHit = static_cast<AliMUONHit*>(fHits->At(i));
253           fNch = mHit->Chamber()-1;  // chamber number
254           if (fNch > AliMUONConstants::NCh()-1) {
255             cerr<<"AliMUONDigitizer: ERROR: "
256                 <<"fNch > AliMUONConstants::NCh()-1, fNch, NCh(): "
257                 <<fNch<<", "<< AliMUONConstants::NCh()<<endl;
258             return;
259           }
260           iChamber = &(pMUON->Chamber(fNch));
261 //
262 // Loop over pad hits
263           for (AliMUONPadHit* mPad =
264                  (AliMUONPadHit*)pMUON->FirstPad(mHit,fPadHits);
265                mPad;
266                mPad = (AliMUONPadHit*)pMUON->NextPad(fPadHits))
267           {
268             Int_t cathode  = mPad->Cathode();      // cathode number
269             Int_t ipx      = mPad->PadX();         // pad number on X
270             Int_t ipy      = mPad->PadY();         // pad number on Y
271             Int_t iqpad    = Int_t(mPad->QPad());  // charge per pad
272             if (cathode != (icat+1)) continue;
273
274             fMask = fManager->GetMask(inputFile);
275             fDigits[0] = ipx;
276             fDigits[1] = ipy;
277             fDigits[2] = icat;
278             fDigits[3] = iqpad;
279             if (inputFile == 0) {
280               fDigits[4] = iqpad;
281             } else {
282               fDigits[4] = 0;
283             }
284             if (mHit->Particle() == kMuonPlus ||
285                 mHit->Particle() == kMuonMinus) {
286               fDigits[5] = (mPad->HitNumber()) + fMask; 
287             } else fDigits[5] = -1;
288
289             // build the list of fired pads and update the info, 
290             // fDigits is input for Update(mPad)
291
292             if (!Exists(mPad)) {
293               CreateNew(mPad);
294             } else {
295               Update(mPad);
296             } //  end if Exists(mPad)
297           } //end loop over clusters
298         } // hit loop
299       } // track loop
300     } // end file loop
301     if (GetDebug()>2) cerr<<"END OF FILE LOOP"<<endl;
302
303     Int_t tracks[kMAXTRACKS];
304     Int_t charges[kMAXTRACKS];
305     Int_t nentries = fTDList->GetEntriesFast();
306         
307     for (Int_t nent = 0; nent < nentries; nent++) {
308       AliMUONTransientDigit *address = (AliMUONTransientDigit*)fTDList->At(nent);
309       if (address == 0) continue; 
310       Int_t ich = address->Chamber();
311       Int_t   q = address->Signal(); 
312       iChamber = &(pMUON->Chamber(ich));
313 //
314 //  Digit Response (noise, threshold, saturation, ...)
315       AliMUONResponse * response = iChamber->ResponseModel();
316       q = response->DigitResponse(q);
317             
318       if (!q) continue;
319             
320       fDigits[0] = address->PadX();
321       fDigits[1] = address->PadY();
322       fDigits[2] = address->Cathode();
323       fDigits[3] = q;
324       fDigits[4] = address->Physics();
325       fDigits[5] = address->Hit();
326             
327       Int_t nptracks = address->GetNTracks();
328
329       if (nptracks > kMAXTRACKS) {
330         if (GetDebug() >0) {
331           cerr<<"AliMUONDigitizer: nptracks > 10 "<<nptracks;
332           cerr<<"reset to max value "<<kMAXTRACKS<<endl;
333         }
334         nptracks = kMAXTRACKS;
335       }
336       if (nptracks > 2 && GetDebug() >2) {
337         cerr<<"AliMUONDigitizer: nptracks > 2 "<<nptracks<<endl;
338         printf("cat,ich,ix,iy,q %d %d %d %d %d \n",icat,ich,fDigits[0],fDigits[1],q);
339       }
340       for (Int_t tr = 0; tr < nptracks; tr++) {
341         tracks[tr]   = address->GetTrack(tr);
342         charges[tr]  = address->GetCharge(tr);
343       }      //end loop over list of tracks for one pad
344       // Sort list of tracks according to charge
345       if (nptracks > 1) {
346         SortTracks(tracks,charges,nptracks);
347       }
348       if (nptracks < kMAXTRACKS ) {
349         for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
350           tracks[i]  = 0;
351           charges[i] = 0;
352         }
353       }
354             
355       // fill digits
356       pMUON->AddDigits(ich,tracks,charges,fDigits);
357     }
358
359     fManager->GetTreeD()->Fill();
360
361     pMUON->ResetDigits();  //
362     fTDList->Clear();
363
364         
365     for(Int_t ii = 0; ii < AliMUONConstants::NCh(); ++ii) {
366       if (fHitMap[ii]) {
367         delete fHitMap[ii];
368         fHitMap[ii] = 0;
369       }
370     }
371   } //end loop over cathodes
372   if (GetDebug()>2) 
373     cerr<<"AliMUONDigitizer::Exec: writing the TreeD: "
374         <<fManager->GetTreeD()->GetName()<<endl;
375   fManager->GetTreeD()->Write(0,TObject::kOverwrite);
376   delete [] fHitMap;
377   delete fTDList;
378     
379   if (fHits)    fHits->Delete();
380   if (fPadHits) fPadHits->Delete();
381 }
382
383
384 //------------------------------------------------------------------------
385 void AliMUONDigitizer::SortTracks(Int_t *tracks,Int_t *charges,Int_t ntr)
386 {
387   //
388   // Sort the list of tracks contributing to a given digit
389   // Only the 3 most significant tracks are acctually sorted
390   //
391   
392   //
393   //  Loop over signals, only 3 times
394   //
395   
396   Int_t qmax;
397   Int_t jmax;
398   Int_t idx[3] = {-2,-2,-2};
399   Int_t jch[3] = {-2,-2,-2};
400   Int_t jtr[3] = {-2,-2,-2};
401   Int_t i,j,imax;
402   
403   if (ntr<3) imax=ntr;
404   else imax=3;
405   for(i=0;i<imax;i++){
406     qmax=0;
407     jmax=0;
408     
409     for(j=0;j<ntr;j++){
410       
411       if((i == 1 && j == idx[i-1]) 
412          ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
413       
414       if(charges[j] > qmax) {
415         qmax = charges[j];
416         jmax=j;
417       }       
418     } 
419     
420     if(qmax > 0) {
421       idx[i]=jmax;
422       jch[i]=charges[jmax]; 
423       jtr[i]=tracks[jmax]; 
424     }
425     
426   } 
427   
428   for(i=0;i<3;i++){
429     if (jtr[i] == -2) {
430       charges[i]=0;
431       tracks[i]=0;
432     } else {
433       charges[i]=jch[i];
434       tracks[i]=jtr[i];
435     }
436   }
437 }