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