]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDigitizer.cxx
Remove obselete members & methods (Ch. Finck)
[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 /* $Id$ */
17
18 #include <assert.h>
19
20 #include "AliRun.h"
21 #include "AliRunDigitizer.h"
22 #include "AliRunLoader.h"
23
24 #include "AliMUONDigitizer.h"
25 #include "AliMUONConstants.h"
26 #include "AliMUONChamber.h"
27 #include "AliMUONHitMapA1.h"
28 #include "AliMUON.h"
29 #include "AliMUONLoader.h"
30 #include "AliMUONConstants.h"
31 #include "AliMUONTransientDigit.h"
32 #include "AliMUONTriggerDecision.h"
33 #include "AliLog.h"
34 #include "AliMUONGeometryModule.h"
35 #include "AliMUONGeometryStore.h"
36
37 /////////////////////////////////////////////////////////////////////////////////////
38 //
39 // AliMUONDigitizer should be base abstract class of all digitisers in the MUON 
40 // module. It implements the common functionality of looping over input streams
41 // filling the fTDList and writing the fTDList to the output stream. 
42 // Inheriting digitizers need to override certain methods to choose and initialize
43 // the correct input and output trees, apply the correct detector response if any
44 // and implement how the transient digits are generated from the input stream.
45 //
46 /////////////////////////////////////////////////////////////////////////////////////
47
48 ClassImp(AliMUONDigitizer)
49
50 //___________________________________________
51 AliMUONDigitizer::AliMUONDigitizer() : 
52         AliDigitizer(),
53         fHitMap(0),
54         fTDList(0),
55         fTDCounter(0),
56         fMask(0),
57         fSignal(0),
58         fDebug(0)
59 {
60 // Default constructor.
61 // Initializes all pointers to NULL.
62
63         fRunLoader = NULL;
64         fGime = NULL;
65         fMUON = NULL;
66         fMUONData = NULL;
67         fTrigDec = NULL;
68 }
69
70 //___________________________________________
71 AliMUONDigitizer::AliMUONDigitizer(AliRunDigitizer* manager) : 
72         AliDigitizer(manager),
73         fHitMap(0),
74         fTDList(0),
75         fTDCounter(0),
76         fMask(0),
77         fSignal(0),
78         fDebug(0)
79 {
80 // Constructor which should be used rather than the default constructor.
81 // Initializes all pointers to NULL.
82
83         fRunLoader = NULL;
84         fGime = NULL;
85         fMUON = NULL;
86         fMUONData = NULL;
87         fTrigDec = NULL;
88 }
89
90 //___________________________________________
91 AliMUONDigitizer::AliMUONDigitizer(const AliMUONDigitizer& rhs)
92   : AliDigitizer(rhs)
93 {
94 // Protected copy constructor
95
96   AliFatal("Not implemented.");
97 }
98
99 //___________________________________________
100 AliMUONDigitizer::~AliMUONDigitizer() 
101 {
102 // Destructor
103   if (fMUONData)
104     delete fMUONData;
105
106   if (fTrigDec)
107     delete fTrigDec;
108 }
109
110 //-------------------------------------------------------------------
111 AliMUONDigitizer&  
112 AliMUONDigitizer::operator=(const AliMUONDigitizer& rhs)
113 {
114 // Protected assignement operator
115
116   if (this == &rhs) return *this;
117
118   AliFatal("Not implemented.");
119     
120   return *this;  
121 }    
122          
123 //------------------------------------------------------------------------
124 Bool_t AliMUONDigitizer::Init()
125 {
126 // Does nothing. 
127         return kTRUE;
128 }
129
130 //------------------------------------------------------------------------
131 void AliMUONDigitizer::Exec(Option_t* option)
132 {
133 // The main work loop starts here. 
134 // The digitization process is broken up into two steps: 
135 // 1) Loop over input streams and create transient digits from the input.
136 //    Done in GenerateTransientDigits()
137 // 2) Loop over the generated transient digits and write them to the output
138 //    stream. Done in CreateDigits()
139
140         AliDebug(1, "Running digitiser.");
141         ParseOptions(option);
142
143         if (fManager->GetNinputs() == 0)
144         {
145                 AliWarning("No inputs set, nothing to do.");
146                 return;
147         }
148
149         if (!FetchLoaders(fManager->GetInputFolderName(0), fRunLoader, fGime) ) return;
150         if (! FetchGlobalPointers(fRunLoader) ) return;
151         if (! FetchTriggerPointer(fGime) ) return;
152
153         InitArrays();
154         
155         AliDebug(2, Form("Event Number is %d.", fManager->GetOutputEventNr()));
156
157         // Loop over files to merge and to digitize
158         fSignal = kTRUE;
159         for (Int_t inputFile = 0; inputFile < fManager->GetNinputs(); inputFile++)
160         {
161                 fMask = fManager->GetMask(inputFile);
162                 AliDebug(2, Form("Digitising folder %d, with fMask = %d: %s", inputFile, fMask,
163                              (const char*)fManager->GetInputFolderName(inputFile)));
164
165                 if (inputFile != 0)
166                         // If this is the first file then we already have the loaders loaded.
167                         if (! FetchLoaders(fManager->GetInputFolderName(inputFile), fRunLoader, fGime) )
168                                 continue;
169                 else
170                         // If this is not the first file then it is assumed to be background.
171                         fSignal = kFALSE;
172
173                 if (! InitInputData(fGime) ) continue;
174                 GenerateTransientDigits();
175                 CleanupInputData(fGime);
176         }
177
178         Bool_t ok = FetchLoaders(fManager->GetOutputFolderName(), fRunLoader, fGime);
179         if (ok) ok = InitOutputData(fGime);
180         if (ok) CreateDigits();
181         if (ok) CreateTrigger();
182         if (ok) CleanupOutputData(fGime);
183
184         CleanupArrays();
185         CleanupTriggerArrays();
186 }
187
188 //--------------------------------------------------------------------------
189 void AliMUONDigitizer::AddOrUpdateTransientDigit(AliMUONTransientDigit* mTD)
190 {
191 // Checks to see if the transient digit exists in the corresponding fHitMap.
192 // If it does then the digit is updated otherwise it is added. 
193
194         if (ExistTransientDigit(mTD)) 
195         {
196                 UpdateTransientDigit(mTD);
197                 delete mTD;  // The new digit can be deleted because it was not added.
198         }
199         else 
200                 AddTransientDigit(mTD);
201 }
202
203 //------------------------------------------------------------------------
204 void AliMUONDigitizer::UpdateTransientDigit(AliMUONTransientDigit* mTD)
205 {
206 // Update the transient digit that is already in the fTDList by adding the new
207 // transient digits charges and track lists to the existing one.
208
209         AliDebug(4,Form( "Updating transient digit 0x%X", (void*)mTD));
210         // Choosing the maping of the cathode plane of the chamber:
211         Int_t detElemId =  mTD->DetElemId();
212
213         Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
214
215         AliMUONTransientDigit *pdigit = 
216                 static_cast<AliMUONTransientDigit*>( fHitMap[iNchCpl]->GetHit(mTD->PadX(),mTD->PadY()) );
217
218         // update charge
219         pdigit->AddSignal( mTD->Signal() );
220         pdigit->AddPhysicsSignal( mTD->Physics() );  
221         
222         // update list of tracks
223         Int_t ntracks = mTD->GetNTracks();
224         if (ntracks > kMAXTRACKS)  // Truncate the number of tracks to kMAXTRACKS if we have to.
225         {
226                 AliDebug(1,Form( 
227                         "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
228                         ntracks));
229                 AliDebug(1,Form( "Reseting the number of tracks to be %d.", kMAXTRACKS));
230                 ntracks = kMAXTRACKS;
231         }
232         
233         for (Int_t i = 0; i < ntracks; i++)
234         {
235                 pdigit->UpdateTrackList( mTD->GetTrack(i), mTD->GetCharge(i) );
236         }
237 }
238
239 //------------------------------------------------------------------------
240 void AliMUONDigitizer::AddTransientDigit(AliMUONTransientDigit* mTD)
241 {
242 // Adds the transient digit to the fTDList and sets the appropriate entry
243 // in the fHitMap arrays. 
244
245         AliDebug(4,Form( "Adding transient digit 0x%X", (void*)mTD));
246         // Choosing the maping of the cathode plane of the chamber:
247
248         Int_t detElemId =  mTD->DetElemId();
249         Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
250
251         fTDList->AddAtAndExpand(mTD, fTDCounter);
252         if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem()) {
253           fHitMap[iNchCpl]->SetHit( mTD->PadX(), mTD->PadY(), fTDCounter);
254           fTDCounter++;
255         }
256 }
257
258 //------------------------------------------------------------------------
259 Bool_t AliMUONDigitizer::ExistTransientDigit(AliMUONTransientDigit* mTD)
260 {
261 // Checks if the transient digit already exists on the corresponding fHitMap.
262 // i.e. is there a transient digit on the same chamber, cathode and pad position 
263 // as mTD. If yes then kTRUE is returned else kFASLE is returned.
264
265         // Choosing the maping of the cathode plane of the chamber:
266         Int_t detElemId =  mTD->DetElemId();
267
268         Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) *AliMUONConstants::NDetElem() ;
269
270         //      Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
271         if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem())
272           return( fHitMap[iNchCpl]->TestHit(mTD->PadX(), mTD->PadY()) );
273         else return kFALSE;
274 }
275
276 //-----------------------------------------------------------------------
277 void AliMUONDigitizer::CreateDigits()
278 {
279 // Loops over the fTDList for each cathode, gets the correct signal for the 
280 // digit and adds the new digit to the output stream.
281
282         fTDList->Sort(); // sort by idDE
283         AliDebug(2, "Creating digits...");
284         //      for (Int_t icat = 0; icat < 2; icat++) {
285
286           Int_t digitindex[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
287
288           //
289           // Filling Digit List
290           Int_t nentries = fTDList->GetEntriesFast();
291           for (Int_t nent = 0; nent < nentries; nent++) {
292
293             AliMUONTransientDigit* td = (AliMUONTransientDigit*)fTDList->At(nent);
294             if (td == NULL) continue; 
295                         
296             // Must be the same cathode, otherwise we will fill a mixture
297             // of digits from both cathodes.
298             //if (icat != td->Cathode() - 1) continue;
299                         
300             AliDebug(3,Form( "Creating digit from transient digit 0x%X", (void*)td));
301
302             Int_t q = GetSignalFrom(td);
303             assert( 0 <= td->Chamber() && td->Chamber() <= 13 );
304             if (q > 0) AddDigit(td, q, digitindex[td->Chamber()]++);
305           }
306           FillOutputData();
307           //    }
308           fTDCounter = 0;
309 }
310
311 //------------------------------------------------------------------------
312 void AliMUONDigitizer::AddDigit(
313                 AliMUONTransientDigit* td, Int_t responseCharge,
314                 Int_t digitindex
315         )
316 {
317 // Prepares the digits, track and charge arrays in preparation for a call to
318 // AddDigit(Int_t, Int_t[kMAXTRACKS], Int_t[kMAXTRACKS], Int_t[6])
319 // This method is called by CreateDigits() whenever a new digit needs to be added
320 // to the output stream trees.
321 // The responseCharge value is used as the Signal of the new digit. 
322 // The OnWriteTransientDigit method is also called just before the adding the 
323 // digit to allow inheriting digitizers to be able to do some specific processing 
324 // at this point. 
325
326         Int_t tracks[kMAXTRACKS];
327         Int_t charges[kMAXTRACKS];
328         Int_t digits[7];
329       
330         digits[0] = td->PadX();
331         digits[1] = td->PadY();
332         digits[2] = td->Cathode() - 1;
333         digits[3] = responseCharge;
334         digits[4] = td->Physics();
335         digits[5] = td->Hit();
336         digits[6] =  td->DetElemId();
337
338         Int_t nptracks = td->GetNTracks();
339         if (nptracks > kMAXTRACKS) {
340
341           AliDebug(1, Form(
342                            "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
343                            nptracks));
344           AliDebug(1, Form("Reseting the number of tracks to be %d.", kMAXTRACKS));
345           nptracks = kMAXTRACKS;
346         }
347         
348         for (Int_t i = 0; i < nptracks; i++) {
349
350           tracks[i]   = td->GetTrack(i);
351           charges[i]  = td->GetCharge(i);
352         }
353
354         // Sort list of tracks according to charge
355         SortTracks(tracks,charges,nptracks);
356
357         if (nptracks < kMAXTRACKS ) {
358
359           for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
360             tracks[i]  = -1;
361             charges[i] = 0;
362           }
363         }
364
365         AliDebug(4,Form( "Adding digit with charge %d.", responseCharge));
366
367         OnWriteTransientDigit(td);
368         AddDigit(td->Chamber(), tracks, charges, digits);
369         AddDigitTrigger(td->Chamber(), tracks, charges, digits, digitindex);
370 }
371
372 //------------------------------------------------------------------------
373 void AliMUONDigitizer::OnCreateTransientDigit(AliMUONTransientDigit* /*digit*/, TObject* /*source_object*/)
374 {
375         // Does nothing.
376         //
377         // This is derived by Digitisers that want to trace which digits were made from
378         // which hits.
379 }
380
381 //------------------------------------------------------------------------
382 void AliMUONDigitizer::OnWriteTransientDigit(AliMUONTransientDigit* /*digit*/)
383 {
384         // Does nothing.
385         //
386         // This is derived by Digitisers that want to trace which digits were made from
387         // which hits.
388 }
389
390 //------------------------------------------------------------------------
391 Bool_t AliMUONDigitizer::FetchLoaders(const char* foldername, AliRunLoader*& runloader, AliMUONLoader*& muonloader)
392 {
393 // Fetches the run loader from the current folder, specified by 'foldername'. 
394 // The muon loader is then loaded from the fetched run loader. 
395 // kTRUE is returned if no error occurred otherwise kFALSE is returned. 
396
397         AliDebug(3, Form("Fetching run loader and muon loader from folder: %s", foldername));
398
399         runloader = AliRunLoader::GetRunLoader(foldername);
400         if (runloader == NULL)
401         {
402                 AliError(Form("RunLoader not found in folder: %s", foldername));
403                 return kFALSE; 
404         }                                                                                       
405         muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
406         if (muonloader == NULL) 
407         {
408                 AliError(Form("MUONLoader not found in folder: %s", foldername));
409                 return kFALSE; 
410         }
411         return kTRUE;
412
413 }
414
415 //------------------------------------------------------------------------
416 Bool_t AliMUONDigitizer::FetchGlobalPointers(AliRunLoader* runloader)
417 {
418 // Fetches the AliRun object into the global gAlice pointer from the specified
419 // run loader. The AliRun object is loaded into memory using the run loader if 
420 // not yet loaded. The MUON module object is then loaded from gAlice and 
421 // AliMUONData fetched from the MUON module. 
422 // kTRUE is returned if no error occurred otherwise kFALSE is returned. 
423
424         AliDebug(3, Form("Fetching gAlice, MUON module and AliMUONData from runloader 0x%X.",
425                         (void*)runloader
426                     ));
427
428         if (runloader->GetAliRun() == NULL) runloader->LoadgAlice();
429         gAlice = runloader->GetAliRun();
430         if (gAlice == NULL)
431         {
432                 AliError(Form("Could not find the AliRun object in runloader 0x%X.", (void*)runloader));
433                 return kFALSE;
434         }
435         fMUON = (AliMUON*) gAlice->GetDetector("MUON");
436         if (fMUON == NULL)
437         {
438                 AliError(Form("Could not find the MUON module in runloader 0x%X.", (void*)runloader));
439                 return kFALSE;
440         }
441
442         AliMUONLoader *muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
443         if (muonloader == NULL) 
444         {
445                 AliError( "MUONLoader not found ");
446                 return kFALSE; 
447         }
448
449
450         if (fMUONData == NULL) fMUONData = new AliMUONData(muonloader,"MUON","MUON");
451         if (fMUONData == NULL)
452         {
453                 AliError(Form("Could not find AliMUONData object in runloader 0x%X.", (void*)runloader));
454                 return kFALSE;
455         }
456
457         return kTRUE;
458 }
459 //-----------------------------------------------------------------------
460 Bool_t  AliMUONDigitizer::FetchTriggerPointer(AliMUONLoader* loader)
461 {
462   if (fMUONData == NULL) {
463     AliError("MUONData not found");
464     return kFALSE;
465   }
466
467   if (fTrigDec == NULL) 
468       fTrigDec = new AliMUONTriggerDecision(loader,0,fMUONData);
469   
470   return kTRUE;
471 }
472 //------------------------------------------------------------------------
473 void AliMUONDigitizer::ParseOptions(Option_t* options)
474 {
475 // Called by the Exec method. ParseOptions should parse the option string given to the Exec method.
476 // 
477 // The following options are defined:
478 //      "debug" - Sets the debug level to 99, which will show all debug messages.
479 //      "deb"   - Same as "debug", implemented for backward comparability.
480 //
481 // If an invalid option is specified it is simply ignored.
482
483         TString optionString = options;
484         if (optionString.Data() == "debug" || 
485                 optionString.Data() == "deb"   // maintained for compatability.
486            )
487         {
488                 AliInfo("Called with option \"debug\".");
489                 SetDebug(99);
490         }
491 }
492
493 //------------------------------------------------------------------------
494 void AliMUONDigitizer::InitArrays()
495 {
496 // Creates a new fTDList object. 
497 // Also creates an array of 2 * chamber_number AliMUONHitMapA1 objects
498 // in the fHitMaps array. Each one is set to a chamber and cathode
499 // specific segmentation model. 
500 //
501 // Note: the fTDList and fHitMap arrays must be NULL before calling this method.
502
503     AliDebug(2, "Initialising internal arrays.");
504     AliDebug(4, "Creating transient digits list.");
505     fTDList = new TObjArray;
506         
507     // Array of pointer of the AliMUONHitMapA1:
508     //  two HitMaps per chamber, or one HitMap per cahtode plane
509     fHitMap = new AliMUONHitMapA1* [2*AliMUONConstants::NDetElem()];
510     for (Int_t i=0; i<2*AliMUONConstants::NDetElem(); i++) fHitMap[i] = 0x0;
511
512     Int_t k = 0;
513     Int_t idDE;
514
515     for (Int_t i = 0; i < AliMUONConstants::NCh(); i++) {
516
517
518       AliDebug(4,Form( "Creating hit map for chamber %d, cathode 1.", i+1));
519       AliMUONChamber* chamber = &(fMUON->Chamber(i));
520       AliMUONGeometrySegmentation* c1Segmentation = chamber->SegmentationModel2(1); // Cathode plane 1
521       AliDebug(4,Form( "Creating hit map for chamber %d, cathode 2.", i+1));
522       AliMUONGeometrySegmentation* c2Segmentation = chamber->SegmentationModel2(2); // Cathode plane 2
523
524       AliMUONGeometryModule* geometry    = fMUON->Chamber(i).GetGeometry();
525       AliMUONGeometryStore*  detElements = geometry->GetDetElementStore();
526     
527
528     // Loop over detection elements
529       for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
530        
531         idDE = detElements->GetEntry(j)->GetUniqueID();
532         fNDetElemId[idDE] = k;
533
534         fHitMap[k] = new AliMUONHitMapA1(idDE,c1Segmentation, fTDList); 
535      
536         fHitMap[k+AliMUONConstants::NDetElem()] = new AliMUONHitMapA1(idDE,c2Segmentation, fTDList);
537         k++;
538       }
539     }
540 }
541 //------------------------------------------------------------------------
542 void AliMUONDigitizer::CleanupArrays()
543 {
544 // The arrays fTDList and fHitMap are deleted and the pointers set to NULL.
545
546         AliDebug(2, "Deleting internal arrays.");
547         for(Int_t i = 0; i < 2*AliMUONConstants::NDetElem(); i++) {
548                 delete fHitMap[i];
549         }
550         delete [] fHitMap;
551         fHitMap = NULL;
552         
553         AliDebug(4, "Deleting transient digits list.");
554         fTDList->Delete();
555         delete fTDList;
556         fTDList = NULL;
557
558 }
559
560 //------------------------------------------------------------------------
561 void AliMUONDigitizer::SortTracks(Int_t *tracks, Int_t *charges, Int_t ntr) const
562 {
563 //
564 // Sort the list of tracks contributing to a given digit
565 // Only the 3 most significant tracks are actually sorted
566 //
567
568        if (ntr <= 1) return;
569
570        //
571        //  Loop over signals, only 3 times
572        //
573
574        Int_t qmax;
575        Int_t jmax;
576        Int_t idx[3] = {-2,-2,-2};
577        Int_t jch[3] = {-2,-2,-2};
578        Int_t jtr[3] = {-2,-2,-2};
579        Int_t i, j, imax;
580
581        if (ntr < 3) imax = ntr;
582        else imax=3;
583         
584        for(i = 0; i < imax; i++)
585          {
586            qmax=0;
587            jmax=0;
588
589            for(j = 0; j < ntr; j++)
590              {
591                if (     (i == 1 && j == idx[i-1]) || 
592                         (i == 2 && (j == idx[i-1] || j == idx[i-2]))
593                         ) 
594                  continue;
595
596                if(charges[j] > qmax) 
597                  {
598                    qmax = charges[j];
599                    jmax = j;
600                  }       
601              } 
602
603            if(qmax > 0)
604              {
605                idx[i] = jmax;
606                jch[i] = charges[jmax]; 
607                jtr[i] = tracks[jmax]; 
608              }
609
610          } 
611
612        for(i = 0; i < 3; i++)
613          {
614            if (jtr[i] == -2) 
615              {
616                charges[i] = 0;
617                tracks[i] = 0;
618              } 
619            else 
620              {
621                charges[i] = jch[i];
622                tracks[i] = jtr[i];
623              }
624          }
625 }