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()
108 //-------------------------------------------------------------------
110 AliMUONDigitizer::operator=(const AliMUONDigitizer& rhs)
112 /// Protected assignement operator
114 if (this == &rhs) return *this;
116 AliFatal("Not implemented.");
121 //------------------------------------------------------------------------
122 Bool_t AliMUONDigitizer::Init()
124 /// Initialize - Does nothing.
129 //------------------------------------------------------------------------
130 void AliMUONDigitizer::Exec(Option_t* /*option*/)
132 /// The main work loop starts here.
133 /// The digitization process is broken up into two steps:
134 /// 1) Loop over input streams and create transient digits from the input.
135 /// Done in GenerateTransientDigits()
136 /// 2) Loop over the generated transient digits and write them to the output
137 /// stream. Done in CreateDigits()
139 AliDebug(1, "Running digitiser.");
141 if (fManager->GetNinputs() == 0)
143 AliWarning("No inputs set, nothing to do.");
147 if (!FetchLoaders(fManager->GetInputFolderName(0), fRunLoader, fGime) ) return;
148 if (! FetchGlobalPointers(fRunLoader) ) return;
149 if (! FetchTriggerPointer(fGime) ) return;
153 AliDebug(2, Form("Event Number is %d.", fManager->GetOutputEventNr()));
155 // Loop over files to merge and to digitize
157 for (Int_t inputFile = 0; inputFile < fManager->GetNinputs(); inputFile++)
159 fMask = fManager->GetMask(inputFile);
160 AliDebug(2, Form("Digitising folder %d, with fMask = %d: %s", inputFile, fMask,
161 (const char*)fManager->GetInputFolderName(inputFile)));
164 // If this is the first file then we already have the loaders loaded.
165 if (! FetchLoaders(fManager->GetInputFolderName(inputFile), fRunLoader, fGime) )
168 // If this is not the first file then it is assumed to be background.
171 if (! InitInputData(fGime) ) continue;
172 GenerateTransientDigits();
173 CleanupInputData(fGime);
176 Bool_t ok = FetchLoaders(fManager->GetOutputFolderName(), fRunLoader, fGime);
177 if (ok) ok = InitOutputData(fGime);
178 if (ok) CreateDigits();
179 if (ok) CreateTrigger();
180 if (ok) CleanupOutputData(fGime);
183 CleanupTriggerArrays();
186 //--------------------------------------------------------------------------
187 void AliMUONDigitizer::AddOrUpdateTransientDigit(AliMUONTransientDigit* mTD)
189 /// Checks to see if the transient digit exists in the corresponding fHitMap.
190 /// If it does then the digit is updated otherwise it is added.
192 if (ExistTransientDigit(mTD))
194 UpdateTransientDigit(mTD);
195 delete mTD; // The new digit can be deleted because it was not added.
198 AddTransientDigit(mTD);
201 //------------------------------------------------------------------------
202 void AliMUONDigitizer::UpdateTransientDigit(AliMUONTransientDigit* mTD)
204 /// Update the transient digit that is already in the fTDList by adding the new
205 /// transient digits charges and track lists to the existing one.
207 AliDebug(4,Form( "Updating transient digit 0x%X", (void*)mTD));
208 // Choosing the maping of the cathode plane of the chamber:
209 Int_t detElemId = mTD->DetElemId();
211 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
213 AliMUONTransientDigit *pdigit =
214 static_cast<AliMUONTransientDigit*>( fHitMap[iNchCpl]->GetHit(mTD->PadX(),mTD->PadY()) );
217 pdigit->AddSignal( mTD->Signal() );
218 pdigit->AddPhysicsSignal( mTD->Physics() );
220 // update list of tracks
221 Int_t ntracks = mTD->GetNTracks();
222 if (ntracks > kMAXTRACKS) // Truncate the number of tracks to kMAXTRACKS if we have to.
225 "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
227 AliDebug(1,Form( "Reseting the number of tracks to be %d.", kMAXTRACKS));
228 ntracks = kMAXTRACKS;
231 for (Int_t i = 0; i < ntracks; i++)
233 pdigit->UpdateTrackList( mTD->GetTrack(i), mTD->GetCharge(i) );
237 //------------------------------------------------------------------------
238 void AliMUONDigitizer::AddTransientDigit(AliMUONTransientDigit* mTD)
240 /// Adds the transient digit to the fTDList and sets the appropriate entry
241 /// in the fHitMap arrays.
243 AliDebug(4,Form( "Adding transient digit 0x%X", (void*)mTD));
244 // Choosing the maping of the cathode plane of the chamber:
246 Int_t detElemId = mTD->DetElemId();
247 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) * AliMUONConstants::NDetElem();
249 fTDList->AddAtAndExpand(mTD, fTDCounter);
250 if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem()) {
251 fHitMap[iNchCpl]->SetHit( mTD->PadX(), mTD->PadY(), fTDCounter);
256 //------------------------------------------------------------------------
257 Bool_t AliMUONDigitizer::ExistTransientDigit(AliMUONTransientDigit* mTD)
259 /// Checks if the transient digit already exists on the corresponding fHitMap.
260 /// i.e. is there a transient digit on the same chamber, cathode and pad position
261 /// as mTD. If yes then kTRUE is returned else kFASLE is returned.
263 // Choosing the maping of the cathode plane of the chamber:
264 Int_t detElemId = mTD->DetElemId();
266 Int_t iNchCpl= fNDetElemId[detElemId] + (mTD->Cathode()-1) *AliMUONConstants::NDetElem() ;
268 // Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
269 if (iNchCpl>-1 && iNchCpl<2*AliMUONConstants::NDetElem())
270 return( fHitMap[iNchCpl]->TestHit(mTD->PadX(), mTD->PadY()) );
274 //-----------------------------------------------------------------------
275 void AliMUONDigitizer::CreateDigits()
277 /// Loops over the fTDList for each cathode, gets the correct signal for the
278 /// digit and adds the new digit to the output stream.
280 fTDList->Sort(); // sort by idDE
281 AliDebug(2, "Creating digits...");
282 // for (Int_t icat = 0; icat < 2; icat++) {
284 Int_t digitindex[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
287 // Filling Digit List
288 Int_t nentries = fTDList->GetEntriesFast();
289 for (Int_t nent = 0; nent < nentries; nent++) {
291 AliMUONTransientDigit* td = (AliMUONTransientDigit*)fTDList->At(nent);
292 if (td == NULL) continue;
294 // Must be the same cathode, otherwise we will fill a mixture
295 // of digits from both cathodes.
296 //if (icat != td->Cathode() - 1) continue;
298 AliDebug(3,Form( "Creating digit from transient digit 0x%X", (void*)td));
300 Int_t q = GetSignalFrom(td);
302 Int_t chamber = td->Chamber();
303 if (0 <= chamber && chamber <= 13 )
304 AddDigit(td, q, digitindex[chamber]++);
306 AliError(Form("Invalid chamber %d\n",chamber));
314 //------------------------------------------------------------------------
315 void AliMUONDigitizer::AddDigit(
316 AliMUONTransientDigit* td, Int_t responseCharge,
320 /// Prepares the digits, track and charge arrays in preparation for a call to
321 /// AddDigit(Int_t, Int_t[kMAXTRACKS], Int_t[kMAXTRACKS], Int_t[6])
322 /// This method is called by CreateDigits() whenever a new digit needs to be added
323 /// to the output stream trees.
324 /// The responseCharge value is used as the Signal of the new digit.
325 /// The OnWriteTransientDigit method is also called just before the adding the
326 /// digit to allow inheriting digitizers to be able to do some specific processing
329 Int_t tracks[kMAXTRACKS];
330 Int_t charges[kMAXTRACKS];
333 digits[0] = td->PadX();
334 digits[1] = td->PadY();
335 digits[2] = td->Cathode() - 1;
336 digits[3] = responseCharge;
337 digits[4] = td->Physics();
338 digits[5] = td->Hit();
339 digits[6] = td->DetElemId();
341 Int_t nptracks = td->GetNTracks();
342 if (nptracks > kMAXTRACKS) {
345 "TransientDigit returned the number of tracks to be %d, which is bigger than kMAXTRACKS.",
347 AliDebug(1, Form("Reseting the number of tracks to be %d.", kMAXTRACKS));
348 nptracks = kMAXTRACKS;
351 for (Int_t i = 0; i < nptracks; i++) {
353 tracks[i] = td->GetTrack(i);
354 charges[i] = td->GetCharge(i);
357 // Sort list of tracks according to charge
358 SortTracks(tracks,charges,nptracks);
360 if (nptracks < kMAXTRACKS ) {
362 for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
368 AliDebug(4,Form( "Adding digit with charge %d.", responseCharge));
370 OnWriteTransientDigit(td);
371 AddDigit(td->Chamber(), tracks, charges, digits);
372 AddDigitTrigger(td->Chamber(), tracks, charges, digits, digitindex);
375 //------------------------------------------------------------------------
376 void AliMUONDigitizer::OnCreateTransientDigit(AliMUONTransientDigit* /*digit*/, TObject* /*source_object*/)
380 /// This is derived by Digitisers that want to trace which digits were made from
384 //------------------------------------------------------------------------
385 void AliMUONDigitizer::OnWriteTransientDigit(AliMUONTransientDigit* /*digit*/)
389 /// This is derived by Digitisers that want to trace which digits were made from
393 //------------------------------------------------------------------------
394 Bool_t AliMUONDigitizer::FetchLoaders(const char* foldername, AliRunLoader*& runloader, AliMUONLoader*& muonloader)
396 /// Fetches the run loader from the current folder, specified by 'foldername'.
397 /// The muon loader is then loaded from the fetched run loader.
398 /// kTRUE is returned if no error occurred otherwise kFALSE is returned.
400 AliDebug(3, Form("Fetching run loader and muon loader from folder: %s", foldername));
402 runloader = AliRunLoader::GetRunLoader(foldername);
403 if (runloader == NULL)
405 AliError(Form("RunLoader not found in folder: %s", foldername));
408 muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
409 if (muonloader == NULL)
411 AliError(Form("MUONLoader not found in folder: %s", foldername));
418 //------------------------------------------------------------------------
419 Bool_t AliMUONDigitizer::FetchGlobalPointers(AliRunLoader* runloader)
421 /// Fetches the AliRun object into the global gAlice pointer from the specified
422 /// run loader. The AliRun object is loaded into memory using the run loader if
423 /// not yet loaded. The MUON module object is then loaded from gAlice and
424 /// AliMUONData fetched from the MUON module.
425 /// kTRUE is returned if no error occurred otherwise kFALSE is returned.
427 AliDebug(3, Form("Fetching gAlice, MUON module and AliMUONData from runloader 0x%X.",
431 if (runloader->GetAliRun() == NULL) runloader->LoadgAlice();
432 gAlice = runloader->GetAliRun();
435 AliError(Form("Could not find the AliRun object in runloader 0x%X.", (void*)runloader));
438 fMUON = (AliMUON*) gAlice->GetDetector("MUON");
441 AliError(Form("Could not find the MUON module in runloader 0x%X.", (void*)runloader));
445 AliMUONLoader *muonloader = (AliMUONLoader*) runloader->GetLoader("MUONLoader");
446 if (muonloader == NULL)
448 AliError( "MUONLoader not found ");
453 if (fMUONData == NULL) fMUONData = new AliMUONData(muonloader,"MUON","MUON");
454 if (fMUONData == NULL)
456 AliError(Form("Could not find AliMUONData object in runloader 0x%X.", (void*)runloader));
462 //-----------------------------------------------------------------------
463 Bool_t AliMUONDigitizer::FetchTriggerPointer(AliMUONLoader* loader)
465 /// \todo add description
467 if (fMUONData == NULL) {
468 AliError("MUONData not found");
472 if (fTrigDec == NULL)
473 fTrigDec = new AliMUONTriggerDecision(loader,0,fMUONData);
478 //------------------------------------------------------------------------
479 void AliMUONDigitizer::InitArrays()
481 /// Creates a new fTDList object.
482 /// Also creates an array of 2 * chamber_number AliMUONHitMapA1 objects
483 /// in the fHitMaps array. Each one is set to a chamber and cathode
484 /// specific segmentation model.
486 /// Note: the fTDList and fHitMap arrays must be NULL before calling this method.
488 AliDebug(2, "Initialising internal arrays.");
489 AliDebug(4, "Creating transient digits list.");
490 fTDList = new TObjArray;
492 // Array of pointer of the AliMUONHitMapA1:
493 // two HitMaps per chamber, or one HitMap per cahtode plane
494 fHitMap = new AliMUONHitMapA1* [2*AliMUONConstants::NDetElem()];
495 for (Int_t i=0; i<2*AliMUONConstants::NDetElem(); i++) fHitMap[i] = 0x0;
500 for (Int_t i = 0; i < AliMUONConstants::NCh(); i++) {
503 AliDebug(4,Form( "Creating hit map for chamber %d, cathode 1.", i+1));
504 AliMUONSegmentation* segmentation = fMUON->GetSegmentation();
505 AliMUONGeometrySegmentation* c1Segmentation
506 = segmentation->GetModuleSegmentation(i, 0); // Cathode plane 1
507 AliDebug(4,Form( "Creating hit map for chamber %d, cathode 2.", i+1));
508 AliMUONGeometrySegmentation* c2Segmentation
509 = segmentation->GetModuleSegmentation(i, 1); // Cathode plane 2
511 const AliMUONGeometryTransformer* kGeometryTransformer
512 = fMUON->GetGeometryTransformer();
514 AliMUONGeometryStore* detElements
515 = kGeometryTransformer->GetModuleTransformer(i)->GetDetElementStore();
518 // Loop over detection elements
519 for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
521 idDE = detElements->GetEntry(j)->GetUniqueID();
522 fNDetElemId[idDE] = k;
524 Int_t npx1 = c1Segmentation->Npx(idDE)+1;
525 Int_t npy1 = c1Segmentation->Npy(idDE)+1;
526 fHitMap[k] = new AliMUONHitMapA1(npx1, npy1, fTDList);
528 Int_t npx2 = c2Segmentation->Npx(idDE)+1;
529 Int_t npy2 = c2Segmentation->Npy(idDE)+1;
530 fHitMap[k+AliMUONConstants::NDetElem()] = new AliMUONHitMapA1(npx2, npy2, fTDList);
535 //------------------------------------------------------------------------
536 void AliMUONDigitizer::CleanupArrays()
538 /// The arrays fTDList and fHitMap are deleted and the pointers set to NULL.
540 AliDebug(2, "Deleting internal arrays.");
541 for(Int_t i = 0; i < 2*AliMUONConstants::NDetElem(); i++) {
547 AliDebug(4, "Deleting transient digits list.");
554 //------------------------------------------------------------------------
555 void AliMUONDigitizer::SortTracks(Int_t *tracks, Int_t *charges, Int_t ntr) const
557 /// Sort the list of tracks contributing to a given digit
558 /// Only the 3 most significant tracks are actually sorted
560 if (ntr <= 1) return;
563 // Loop over signals, only 3 times
568 Int_t idx[3] = {-2,-2,-2};
569 Int_t jch[3] = {-2,-2,-2};
570 Int_t jtr[3] = {-2,-2,-2};
573 if (ntr < 3) imax = ntr;
576 for(i = 0; i < imax; i++)
581 for(j = 0; j < ntr; j++)
583 if ( (i == 1 && j == idx[i-1]) ||
584 (i == 2 && (j == idx[i-1] || j == idx[i-2]))
588 if(charges[j] > qmax)
598 jch[i] = charges[jmax];
599 jtr[i] = tracks[jmax];
604 for(i = 0; i < 3; i++)