1 /**************************************************************************
2 * Copyright(c) 1998-2000, 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 **************************************************************************/
18 #include "AliMUONDigitizer.h"
19 #include "AliMUONConstants.h"
20 #include "AliMUONSegmentation.h"
21 #include "AliMUONHitMapA1.h"
23 #include "AliMUONLoader.h"
24 #include "AliMUONConstants.h"
25 #include "AliMUONTransientDigit.h"
26 #include "AliMUONTriggerDecision.h"
28 #include "AliMUONGeometryTransformer.h"
29 #include "AliMUONGeometryModule.h"
30 #include "AliMUONGeometryStore.h"
33 #include "AliRunDigitizer.h"
34 #include "AliRunLoader.h"
36 /////////////////////////////////////////////////////////////////////////////////////
38 // AliMUONDigitizer should be base abstract class of all digitisers in the MUON
39 // module. It implements the common functionality of looping over input streams
40 // filling the fTDList and writing the fTDList to the output stream.
41 // Inheriting digitizers need to override certain methods to choose and initialize
42 // the correct input and output trees, apply the correct detector response if any
43 // and implement how the transient digits are generated from the input stream.
45 /////////////////////////////////////////////////////////////////////////////////////
47 ClassImp(AliMUONDigitizer)
49 //___________________________________________
50 AliMUONDigitizer::AliMUONDigitizer() :
58 // Default constructor.
59 // Initializes all pointers to NULL.
68 //___________________________________________
69 AliMUONDigitizer::AliMUONDigitizer(AliRunDigitizer* manager) :
70 AliDigitizer(manager),
77 // Constructor which should be used rather than the default constructor.
78 // Initializes all pointers to NULL.
87 //___________________________________________
88 AliMUONDigitizer::AliMUONDigitizer(const AliMUONDigitizer& rhs)
91 // Protected copy constructor
93 AliFatal("Not implemented.");
96 //___________________________________________
97 AliMUONDigitizer::~AliMUONDigitizer()
107 //-------------------------------------------------------------------
109 AliMUONDigitizer::operator=(const AliMUONDigitizer& rhs)
111 // Protected assignement operator
113 if (this == &rhs) return *this;
115 AliFatal("Not implemented.");
120 //------------------------------------------------------------------------
121 Bool_t AliMUONDigitizer::Init()
127 //------------------------------------------------------------------------
128 void AliMUONDigitizer::Exec(Option_t* /*option*/)
130 // The main work loop starts here.
131 // The digitization process is broken up into two steps:
132 // 1) Loop over input streams and create transient digits from the input.
133 // Done in GenerateTransientDigits()
134 // 2) Loop over the generated transient digits and write them to the output
135 // stream. Done in CreateDigits()
137 AliDebug(1, "Running digitiser.");
139 if (fManager->GetNinputs() == 0)
141 AliWarning("No inputs set, nothing to do.");
145 if (!FetchLoaders(fManager->GetInputFolderName(0), fRunLoader, fGime) ) return;
146 if (! FetchGlobalPointers(fRunLoader) ) return;
147 if (! FetchTriggerPointer(fGime) ) return;
151 AliDebug(2, Form("Event Number is %d.", fManager->GetOutputEventNr()));
153 // Loop over files to merge and to digitize
155 for (Int_t inputFile = 0; inputFile < fManager->GetNinputs(); inputFile++)
157 fMask = fManager->GetMask(inputFile);
158 AliDebug(2, Form("Digitising folder %d, with fMask = %d: %s", inputFile, fMask,
159 (const char*)fManager->GetInputFolderName(inputFile)));
162 // If this is the first file then we already have the loaders loaded.
163 if (! FetchLoaders(fManager->GetInputFolderName(inputFile), fRunLoader, fGime) )
166 // If this is not the first file then it is assumed to be background.
169 if (! InitInputData(fGime) ) continue;
170 GenerateTransientDigits();
171 CleanupInputData(fGime);
174 Bool_t ok = FetchLoaders(fManager->GetOutputFolderName(), fRunLoader, fGime);
175 if (ok) ok = InitOutputData(fGime);
176 if (ok) CreateDigits();
177 if (ok) CreateTrigger();
178 if (ok) CleanupOutputData(fGime);
181 CleanupTriggerArrays();
184 //--------------------------------------------------------------------------
185 void AliMUONDigitizer::AddOrUpdateTransientDigit(AliMUONTransientDigit* mTD)
187 // Checks to see if the transient digit exists in the corresponding fHitMap.
188 // If it does then the digit is updated otherwise it is added.
190 if (ExistTransientDigit(mTD))
192 UpdateTransientDigit(mTD);
193 delete mTD; // The new digit can be deleted because it was not added.
196 AddTransientDigit(mTD);
199 //------------------------------------------------------------------------
200 void AliMUONDigitizer::UpdateTransientDigit(AliMUONTransientDigit* mTD)
202 // Update the transient digit that is already in the fTDList by adding the new
203 // transient digits charges and track lists to the existing one.
205 AliDebug(4,Form( "Updating transient digit 0x%X", (void*)mTD));
206 // Choosing the maping of the cathode plane of the chamber:
207 Int_t detElemId = mTD->DetElemId();
209 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
211 AliMUONTransientDigit *pdigit =
212 static_cast<AliMUONTransientDigit*>( fHitMap[iNchCpl]->GetHit(mTD->PadX(),mTD->PadY()) );
215 pdigit->AddSignal( mTD->Signal() );
216 pdigit->AddPhysicsSignal( mTD->Physics() );
218 // update list of tracks
219 Int_t ntracks = mTD->GetNTracks();
220 if (ntracks > kMAXTRACKS) // Truncate the number of tracks to kMAXTRACKS if we have to.
223 "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
225 AliDebug(1,Form( "Reseting the number of tracks to be %d.", kMAXTRACKS));
226 ntracks = kMAXTRACKS;
229 for (Int_t i = 0; i < ntracks; i++)
231 pdigit->UpdateTrackList( mTD->GetTrack(i), mTD->GetCharge(i) );
235 //------------------------------------------------------------------------
236 void AliMUONDigitizer::AddTransientDigit(AliMUONTransientDigit* mTD)
238 // Adds the transient digit to the fTDList and sets the appropriate entry
239 // in the fHitMap arrays.
241 AliDebug(4,Form( "Adding transient digit 0x%X", (void*)mTD));
242 // Choosing the maping of the cathode plane of the chamber:
244 Int_t detElemId = mTD->DetElemId();
245 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
247 fTDList->AddAtAndExpand(mTD, fTDCounter);
248 if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem()) {
249 fHitMap[iNchCpl]->SetHit( mTD->PadX(), mTD->PadY(), fTDCounter);
254 //------------------------------------------------------------------------
255 Bool_t AliMUONDigitizer::ExistTransientDigit(AliMUONTransientDigit* mTD)
257 // Checks if the transient digit already exists on the corresponding fHitMap.
258 // i.e. is there a transient digit on the same chamber, cathode and pad position
259 // as mTD. If yes then kTRUE is returned else kFASLE is returned.
261 // Choosing the maping of the cathode plane of the chamber:
262 Int_t detElemId = mTD->DetElemId();
264 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) *AliMUONConstants::NDetElem() ;
266 // Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
267 if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem())
268 return( fHitMap[iNchCpl]->TestHit(mTD->PadX(), mTD->PadY()) );
272 //-----------------------------------------------------------------------
273 void AliMUONDigitizer::CreateDigits()
275 // Loops over the fTDList for each cathode, gets the correct signal for the
276 // digit and adds the new digit to the output stream.
278 fTDList->Sort(); // sort by idDE
279 AliDebug(2, "Creating digits...");
280 // for (Int_t icat = 0; icat < 2; icat++) {
282 Int_t digitindex[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
285 // Filling Digit List
286 Int_t nentries = fTDList->GetEntriesFast();
287 for (Int_t nent = 0; nent < nentries; nent++) {
289 AliMUONTransientDigit* td = (AliMUONTransientDigit*)fTDList->At(nent);
290 if (td == NULL) continue;
292 // Must be the same cathode, otherwise we will fill a mixture
293 // of digits from both cathodes.
294 //if (icat != td->Cathode() - 1) continue;
296 AliDebug(3,Form( "Creating digit from transient digit 0x%X", (void*)td));
298 Int_t q = GetSignalFrom(td);
300 Int_t chamber = td->Chamber();
301 if (0 <= chamber && chamber <= 13 )
302 AddDigit(td, q, digitindex[chamber]++);
304 AliError(Form("Invalid chamber %d\n",chamber));
312 //------------------------------------------------------------------------
313 void AliMUONDigitizer::AddDigit(
314 AliMUONTransientDigit* td, Int_t responseCharge,
318 // Prepares the digits, track and charge arrays in preparation for a call to
319 // AddDigit(Int_t, Int_t[kMAXTRACKS], Int_t[kMAXTRACKS], Int_t[6])
320 // This method is called by CreateDigits() whenever a new digit needs to be added
321 // to the output stream trees.
322 // The responseCharge value is used as the Signal of the new digit.
323 // The OnWriteTransientDigit method is also called just before the adding the
324 // digit to allow inheriting digitizers to be able to do some specific processing
327 Int_t tracks[kMAXTRACKS];
328 Int_t charges[kMAXTRACKS];
331 digits[0] = td->PadX();
332 digits[1] = td->PadY();
333 digits[2] = td->Cathode() - 1;
334 digits[3] = responseCharge;
335 digits[4] = td->Physics();
336 digits[5] = td->Hit();
337 digits[6] = td->DetElemId();
339 Int_t nptracks = td->GetNTracks();
340 if (nptracks > kMAXTRACKS) {
343 "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
345 AliDebug(1, Form("Reseting the number of tracks to be %d.", kMAXTRACKS));
346 nptracks = kMAXTRACKS;
349 for (Int_t i = 0; i < nptracks; i++) {
351 tracks[i] = td->GetTrack(i);
352 charges[i] = td->GetCharge(i);
355 // Sort list of tracks according to charge
356 SortTracks(tracks,charges,nptracks);
358 if (nptracks < kMAXTRACKS ) {
360 for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
366 AliDebug(4,Form( "Adding digit with charge %d.", responseCharge));
368 OnWriteTransientDigit(td);
369 AddDigit(td->Chamber(), tracks, charges, digits);
370 AddDigitTrigger(td->Chamber(), tracks, charges, digits, digitindex);
373 //------------------------------------------------------------------------
374 void AliMUONDigitizer::OnCreateTransientDigit(AliMUONTransientDigit* /*digit*/, TObject* /*source_object*/)
378 // This is derived by Digitisers that want to trace which digits were made from
382 //------------------------------------------------------------------------
383 void AliMUONDigitizer::OnWriteTransientDigit(AliMUONTransientDigit* /*digit*/)
387 // This is derived by Digitisers that want to trace which digits were made from
391 //------------------------------------------------------------------------
392 Bool_t AliMUONDigitizer::FetchLoaders(const char* foldername, AliRunLoader*& runloader, AliMUONLoader*& muonloader)
394 // Fetches the run loader from the current folder, specified by 'foldername'.
395 // The muon loader is then loaded from the fetched run loader.
396 // kTRUE is returned if no error occurred otherwise kFALSE is returned.
398 AliDebug(3, Form("Fetching run loader and muon loader from folder: %s", foldername));
400 runloader = AliRunLoader::GetRunLoader(foldername);
401 if (runloader == NULL)
403 AliError(Form("RunLoader not found in folder: %s", foldername));
406 muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
407 if (muonloader == NULL)
409 AliError(Form("MUONLoader not found in folder: %s", foldername));
416 //------------------------------------------------------------------------
417 Bool_t AliMUONDigitizer::FetchGlobalPointers(AliRunLoader* runloader)
419 // Fetches the AliRun object into the global gAlice pointer from the specified
420 // run loader. The AliRun object is loaded into memory using the run loader if
421 // not yet loaded. The MUON module object is then loaded from gAlice and
422 // AliMUONData fetched from the MUON module.
423 // kTRUE is returned if no error occurred otherwise kFALSE is returned.
425 AliDebug(3, Form("Fetching gAlice, MUON module and AliMUONData from runloader 0x%X.",
429 if (runloader->GetAliRun() == NULL) runloader->LoadgAlice();
430 gAlice = runloader->GetAliRun();
433 AliError(Form("Could not find the AliRun object in runloader 0x%X.", (void*)runloader));
436 fMUON = (AliMUON*) gAlice->GetDetector("MUON");
439 AliError(Form("Could not find the MUON module in runloader 0x%X.", (void*)runloader));
443 AliMUONLoader *muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
444 if (muonloader == NULL)
446 AliError( "MUONLoader not found ");
451 if (fMUONData == NULL) fMUONData = new AliMUONData(muonloader,"MUON","MUON");
452 if (fMUONData == NULL)
454 AliError(Form("Could not find AliMUONData object in runloader 0x%X.", (void*)runloader));
460 //-----------------------------------------------------------------------
461 Bool_t AliMUONDigitizer::FetchTriggerPointer(AliMUONLoader* loader)
463 if (fMUONData == NULL) {
464 AliError("MUONData not found");
468 if (fTrigDec == NULL)
469 fTrigDec = new AliMUONTriggerDecision(loader,0,fMUONData);
474 //------------------------------------------------------------------------
475 void AliMUONDigitizer::InitArrays()
477 // Creates a new fTDList object.
478 // Also creates an array of 2 * chamber_number AliMUONHitMapA1 objects
479 // in the fHitMaps array. Each one is set to a chamber and cathode
480 // specific segmentation model.
482 // Note: the fTDList and fHitMap arrays must be NULL before calling this method.
484 AliDebug(2, "Initialising internal arrays.");
485 AliDebug(4, "Creating transient digits list.");
486 fTDList = new TObjArray;
488 // Array of pointer of the AliMUONHitMapA1:
489 // two HitMaps per chamber, or one HitMap per cahtode plane
490 fHitMap = new AliMUONHitMapA1* [2*AliMUONConstants::NDetElem()];
491 for (Int_t i=0; i<2*AliMUONConstants::NDetElem(); i++) fHitMap[i] = 0x0;
496 for (Int_t i = 0; i < AliMUONConstants::NCh(); i++) {
499 AliDebug(4,Form( "Creating hit map for chamber %d, cathode 1.", i+1));
500 AliMUONSegmentation* segmentation = fMUON->GetSegmentation();
501 AliMUONGeometrySegmentation* c1Segmentation
502 = segmentation->GetModuleSegmentation(i, 0); // Cathode plane 1
503 AliDebug(4,Form( "Creating hit map for chamber %d, cathode 2.", i+1));
504 AliMUONGeometrySegmentation* c2Segmentation
505 = segmentation->GetModuleSegmentation(i, 1); // Cathode plane 2
507 const AliMUONGeometryTransformer* kGeometryTransformer
508 = fMUON->GetGeometryTransformer();
510 AliMUONGeometryStore* detElements
511 = kGeometryTransformer->GetModuleTransformer(i)->GetDetElementStore();
514 // Loop over detection elements
515 for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
517 idDE = detElements->GetEntry(j)->GetUniqueID();
518 fNDetElemId[idDE] = k;
520 Int_t npx1 = c1Segmentation->Npx(idDE)+1;
521 Int_t npy1 = c1Segmentation->Npy(idDE)+1;
522 fHitMap[k] = new AliMUONHitMapA1(npx1, npy1, fTDList);
524 Int_t npx2 = c2Segmentation->Npx(idDE)+1;
525 Int_t npy2 = c2Segmentation->Npy(idDE)+1;
526 fHitMap[k+AliMUONConstants::NDetElem()] = new AliMUONHitMapA1(npx2, npy2, fTDList);
531 //------------------------------------------------------------------------
532 void AliMUONDigitizer::CleanupArrays()
534 // The arrays fTDList and fHitMap are deleted and the pointers set to NULL.
536 AliDebug(2, "Deleting internal arrays.");
537 for(Int_t i = 0; i < 2*AliMUONConstants::NDetElem(); i++) {
543 AliDebug(4, "Deleting transient digits list.");
550 //------------------------------------------------------------------------
551 void AliMUONDigitizer::SortTracks(Int_t *tracks, Int_t *charges, Int_t ntr) const
554 // Sort the list of tracks contributing to a given digit
555 // Only the 3 most significant tracks are actually sorted
558 if (ntr <= 1) return;
561 // Loop over signals, only 3 times
566 Int_t idx[3] = {-2,-2,-2};
567 Int_t jch[3] = {-2,-2,-2};
568 Int_t jtr[3] = {-2,-2,-2};
571 if (ntr < 3) imax = ntr;
574 for(i = 0; i < imax; i++)
579 for(j = 0; j < ntr; j++)
581 if ( (i == 1 && j == idx[i-1]) ||
582 (i == 2 && (j == idx[i-1] || j == idx[i-2]))
586 if(charges[j] > qmax)
596 jch[i] = charges[jmax];
597 jtr[i] = tracks[jmax];
602 for(i = 0; i < 3; i++)