]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONDigitizerv1.cxx
04-mar-2004 NvE Functionality of AliObjMatrix extended to provide the number of refer...
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizerv1.cxx
CommitLineData
8dbbc4e3 1
2
3#include <Riostream.h>
4#include <TDirectory.h>
5#include <TFile.h>
6#include <TObjArray.h>
7#include <TPDGCode.h>
8#include <TTree.h>
88cb7938 9#include <TMath.h>
8dbbc4e3 10
11#include "AliMUON.h"
12#include "AliMUONChamber.h"
13#include "AliMUONConstants.h"
14#include "AliMUONDigit.h"
15#include "AliMUONDigitizerv1.h"
16#include "AliMUONHit.h"
17#include "AliMUONHitMapA1.h"
18#include "AliMUONPadHit.h"
19#include "AliMUONTransientDigit.h"
20#include "AliRun.h"
21#include "AliRunDigitizer.h"
88cb7938 22#include "AliRunLoader.h"
23#include "AliLoader.h"
8dbbc4e3 24
25ClassImp(AliMUONDigitizerv1)
26
27//___________________________________________
85a5290f 28AliMUONDigitizerv1::AliMUONDigitizerv1() :
29 AliDigitizer(),
30 fHitMap(0),
31 fTDList(0),
32 fTDCounter(0),
33 fDebug(0),
34 fMask(0),
35 fSignal(0)
8dbbc4e3 36{
37// Default ctor - don't use it
8dbbc4e3 38 if (GetDebug()>2)
39 cerr<<"AliMUONDigitizerv1::AliMUONDigitizerv1"
40 <<"(AliRunDigitizer* manager) was processed"<<endl;
41}
42
43//___________________________________________
85a5290f 44AliMUONDigitizerv1::AliMUONDigitizerv1(AliRunDigitizer* manager):
45 AliDigitizer(manager),
46 fHitMap(0),
47 fTDList(0),
48 fTDCounter(0),
49 fDebug(0),
50 fMask(0),
51 fSignal(0)
8dbbc4e3 52{
53// ctor which should be used
8dbbc4e3 54 if (GetDebug()>2)
55 cerr<<"AliMUONDigitizerv1::AliMUONDigitizerv1"
56 <<"(AliRunDigitizer* manager) was processed"<<endl;
57}
58
59//------------------------------------------------------------------------
60AliMUONDigitizerv1::~AliMUONDigitizerv1()
61{
62// Destructor
8dbbc4e3 63}
64
65//------------------------------------------------------------------------
66void AliMUONDigitizerv1::AddTransientDigit(AliMUONTransientDigit * mTD)
67{
68 // Choosing the maping of the cathode plane of the chamber:
69 Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
70 fTDList->AddAtAndExpand(mTD, fTDCounter);
71 fHitMap[iNchCpl]->SetHit( mTD->PadX(), mTD->PadY(), fTDCounter);
72 fTDCounter++;
73}
74
75//------------------------------------------------------------------------
76Bool_t AliMUONDigitizerv1::ExistTransientDigit(AliMUONTransientDigit * mTD)
77{
78 // Choosing the maping of the cathode plane of the chamber:
79 Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
80 return( fHitMap[iNchCpl]->TestHit(mTD->PadX(), mTD->PadY()) );
81}
82
83//------------------------------------------------------------------------
84Bool_t AliMUONDigitizerv1::Init()
85{
1391e633 86
8dbbc4e3 87// Initialization
1391e633 88 if (GetDebug()>2) Info("Init","AliMUONDigitizerv1::Init() starts");
89
90 //Loaders (We assume input0 to be the output too)
91 AliRunLoader * runloader; // Input loader
92 AliLoader * gime;
93
94 // Getting runloader
95 runloader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
96 if (runloader == 0x0) {
97 Error("Init","RunLoader is not in input file 0");
98 return kFALSE; // RunDigitizer is not working.
99 }
100 // Getting MUONloader
101 gime = runloader->GetLoader("MUONLoader");
102 gime->LoadHits("READ");
103 gime->LoadDigits("RECREATE");
104
8dbbc4e3 105 return kTRUE;
106}
107
108//------------------------------------------------------------------------
109void AliMUONDigitizerv1::UpdateTransientDigit(Int_t track, AliMUONTransientDigit * mTD)
110{
111 // Choosing the maping of the cathode plane of the chamber:
112 Int_t iNchCpl= mTD->Chamber() + (mTD->Cathode()-1) * AliMUONConstants::NCh();
113 AliMUONTransientDigit *pdigit =
114 static_cast<AliMUONTransientDigit*>(fHitMap[iNchCpl]->GetHit(mTD->PadX(),mTD->PadY()));
115 // update charge
116 //
117 Int_t iqpad = mTD->Signal(); // charge per pad
118 pdigit->AddSignal(iqpad);
119 pdigit->AddPhysicsSignal(iqpad);
120 // update list of tracks
121 //
3351e14b 122 Int_t charge;
8dbbc4e3 123 track=+ fMask;
124 if (fSignal) charge = iqpad;
3351e14b 125 //else charge = kBgTag;
126 else charge = iqpad + fMask;
127
8dbbc4e3 128 pdigit->UpdateTrackList(track,charge);
129}
130
131
132//--------------------------------------------------------------------------
133void AliMUONDigitizerv1::MakeTransientDigit(Int_t track, Int_t iHit, AliMUONHit * mHit)
134{
135 AliMUON *pMUON = (AliMUON *) gAlice->GetModule("MUON");
136 if (!pMUON) {
137 cerr<<"AliMUONDigitizerv1::Digitize Error:"
138 <<" module MUON not found in the input file"<<endl;
139 }
140 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit starts"<<endl;
141 Int_t ichamber = mHit->Chamber()-1;
142 AliMUONChamber & chamber = pMUON->Chamber(ichamber);
143 Float_t xhit = mHit->X();
144 Float_t yhit = mHit->Y();
145 Float_t zhit = mHit->Z();
146 Float_t eloss= mHit->Eloss();
147 Float_t tof = mHit->Age();
148 // Variables for chamber response from AliMUONChamber::DisIntegration
149 Float_t newdigit[6][500]; // Pad information
150 Int_t nnew=0; // Number of touched Pads per hit
151 Int_t digits[6];
152
153 //
154 // Calls the charge disintegration method of the current chamber
155 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit calling AliMUONChamber::DisIngtegration starts"<<endl;
156 chamber.DisIntegration(eloss, tof, xhit, yhit, zhit, nnew, newdigit);
157 // Creating a new TransientDigits from hit
158 for(Int_t iTD=0; iTD<nnew; iTD++) {
159 digits[0] = Int_t(newdigit[1][iTD]); // Padx of the Digit
160 digits[1] = Int_t(newdigit[2][iTD]); // Pady of the Digit
161 digits[2] = Int_t(newdigit[5][iTD]); // Cathode plane
162 digits[3] = Int_t(newdigit[3][iTD]); // Induced charge in the Pad
163 if (fSignal) digits[4] = Int_t(newdigit[3][iTD]);
164 else digits[4] = 0;
165 digits[5] = iHit+fMask; // Hit number in the list
166 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit " <<
167 "PadX "<< digits[0] << " " <<
168 "PadY "<< digits[1] << " " <<
169 "Plane " << digits[2] << " " <<
170 "Charge " << digits[3] <<" " <<
171 "Hit " << digits[5] << endl;
172 // list of tracks
3351e14b 173 Int_t charge;
8dbbc4e3 174 track += fMask;
175 if (fSignal) charge = digits[3];
3351e14b 176 //else charge = kBgTag;
177 else charge = digits[3] + fMask;
178
8dbbc4e3 179 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit Creating AliMUONTransientDigit"<<endl;
180 AliMUONTransientDigit * mTD = new AliMUONTransientDigit(ichamber, digits);
181 mTD->AddToTrackList(track,charge);
8dbbc4e3 182 if (!ExistTransientDigit(mTD)) {
183 AddTransientDigit(mTD);
184 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit Adding TransientDigit"<<endl;
185 }
186 else {
187 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::MakeTransientDigit updating TransientDigit"<<endl;
188 UpdateTransientDigit(track, mTD);
189 delete mTD;
190 }
191 }
192}
193//-----------------------------------------------------------------------
194void AliMUONDigitizerv1::Exec(Option_t* option)
195{
196 TString optionString = option;
197 if (optionString.Data() == "deb") {
ce3f5e87 198 Info("Digitize","Called with option deb ");
8dbbc4e3 199 fDebug = 3;
200 }
ce3f5e87 201
8dbbc4e3 202 AliMUONChamber* chamber;
203 AliSegmentation* c1Segmentation; //Cathode plane c1 of the chamber
204 AliSegmentation* c2Segmentation; //Cathode place c2 of the chamber
205
ce3f5e87 206 if (GetDebug()>2) Info("Digitize","AliMUONDigitizerv1::Digitize() starts");
8dbbc4e3 207 fTDList = new TObjArray;
208
88cb7938 209 //Loaders (We assume input0 to be the output too)
210 AliRunLoader * runloader; // Input loader
211 AliLoader * gime;
212
ce3f5e87 213 // Getting runloader
88cb7938 214 runloader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
215 if (runloader == 0x0) {
ce3f5e87 216 Error("Digitize","RunLoader is not in input file 0");
88cb7938 217 return; // RunDigitizer is not working.
218 }
ce3f5e87 219 // Getting MUONloader
220 gime = runloader->GetLoader("MUONLoader");
88cb7938 221 if (gime->TreeH()==0x0) {
ce3f5e87 222 if (GetDebug()>2) Info("Digitize","TreeH is not loaded yet. Loading...");
88cb7938 223 gime->LoadHits("READ");
ce3f5e87 224 if (GetDebug()>2) Info("Digitize","Now treeH is %#x. MUONLoader is %#x",gime->TreeH(),gime);
88cb7938 225 }
226
ce3f5e87 227 if (GetDebug()>2) Info("Digitize","Loaders ready");
88cb7938 228
ce3f5e87 229 if (runloader->GetAliRun() == 0x0) runloader->LoadgAlice();
88cb7938 230 gAlice = runloader->GetAliRun();
231
232 // Getting Module MUON
233 AliMUON *pMUON = (AliMUON *) gAlice->GetDetector("MUON");
8dbbc4e3 234 if (!pMUON) {
ce3f5e87 235 Error("Digitize","Module MUON not found in the input file");
8dbbc4e3 236 return;
ce3f5e87 237 }
238 // Getting Muon data
239 AliMUONData * muondata = pMUON->GetMUONData();
240 muondata->SetLoader(gime);
241 muondata->SetTreeAddress("H");
88cb7938 242
243 // Loading Event
244 Int_t currentevent = fManager->GetOutputEventNr();
245
246 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::Digitize() Event Number is "<<currentevent <<endl;
247 if ( (currentevent<10) ||
248 (Int_t(TMath::Log10(currentevent)) == TMath::Log10(currentevent) ) )
249 cout <<"ALiMUONDigitizerv1::Digitize() Event Number is "<< currentevent <<endl;
250
ce3f5e87 251 // Output file for digits same as hits
252 // AliRunLoader * runloaderout = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
253 //AliLoader * gimeout = runloaderout->GetLoader("MUONLoader");
88cb7938 254 // New branch per chamber for MUON digit in the tree of digits
b28974de 255 if (gime->TreeD() == 0x0) {
256 gime->MakeDigitsContainer();
257 }
ce3f5e87 258 TTree* treeD = gime->TreeD();
259 muondata->MakeBranch("D");
260 muondata->SetTreeAddress("D");
8dbbc4e3 261
262 // Array of pointer of the AliMUONHitMapA1:
263 // two HitMaps per chamber, or one HitMap per cahtode plane
264 fHitMap= new AliMUONHitMapA1* [2*AliMUONConstants::NCh()];
265
266 //Loop over chambers for the definition AliMUONHitMap
267 for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
268 chamber = &(pMUON->Chamber(i));
269 c1Segmentation = chamber->SegmentationModel(1); // Cathode plane 1
270 fHitMap[i] = new AliMUONHitMapA1(c1Segmentation, fTDList);
271 c2Segmentation = chamber->SegmentationModel(2); // Cathode plane 2
272 fHitMap[i+AliMUONConstants::NCh()] = new AliMUONHitMapA1(c2Segmentation, fTDList);
273 }
274
275// Loop over files to merge and to digitize
276 fSignal = kTRUE;
277 for (Int_t inputFile=0; inputFile<fManager->GetNinputs(); inputFile++) {
88cb7938 278 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::Digitize() Input File is "<<inputFile<<endl;
279
88cb7938 280
281 // Connect MUON Hit branch
282 if (inputFile > 0 ) {
283 fSignal = kFALSE;
284 runloader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
285 if (runloader == 0x0) {
286 cerr<<"AliMUONDigitizerv1::Digitize() RunLoader for inputFile "<<inputFile<< " not found !!! "<<endl;
287 }
288 gime = runloader->GetLoader("MUONLoader");
289 if (gime->TreeH() == 0x0) gime->LoadHits("READ");
ce3f5e87 290 muondata->SetLoader(gime);
291 muondata->SetTreeAddress("H");
88cb7938 292 }
293
ce3f5e87 294 // Setting the address
88cb7938 295 TTree *treeH = gime->TreeH();
296 if (treeH == 0x0) {
297 Error("Digitize","Can not get TreeH from input %d",inputFile);
298 Info("Digitize","Now treeH is %#x. MUONLoader is %#x",gime->TreeH(),gime);
299 return;
300 }
8dbbc4e3 301 if (GetDebug()>2) {
302 cerr<<"AliMUONDigitizerv1::Exec inputFile is "<<inputFile<<" "<<endl;
ce3f5e87 303 cerr<<"AliMUONDigitizerv1::Exec treeH" << treeH <<endl;
8dbbc4e3 304 }
ce3f5e87 305
306 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::Exec Setting tree addresses"<<endl;
88cb7938 307
8dbbc4e3 308 fMask = fManager->GetMask(inputFile);
309 //
310 // Loop over tracks
311 Int_t itrack;
312 Int_t ntracks = (Int_t) treeH->GetEntries();
313 for (itrack = 0; itrack < ntracks; itrack++) {
314 if (GetDebug()>2) cerr<<"AliMUONDigitizerv1::Exec itrack = "<<itrack<<endl;
ce3f5e87 315 muondata->ResetHits();
88cb7938 316 treeH->GetEvent(itrack);
8dbbc4e3 317 //
318 // Loop over hits
319 Int_t ihit, ichamber;
320 AliMUONHit* mHit;
ce3f5e87 321 TClonesArray* hits = muondata->Hits();
322 for(ihit = 0; ihit < hits->GetEntriesFast(); ihit++) {
323 mHit = static_cast<AliMUONHit*>(hits->At(ihit));
8dbbc4e3 324 ichamber = mHit->Chamber()-1; // chamber number
325 if (ichamber > AliMUONConstants::NCh()-1) {
326 cerr<<"AliMUONDigitizer: ERROR: "
327 <<"fNch > AliMUONConstants::NCh()-1, fNch, NCh(): "
328 <<ichamber<<", "<< AliMUONConstants::NCh()<<endl;
329 return;
330 }
331 chamber = &(pMUON->Chamber(ichamber));
332 //
333 //Dumping Hit content:
334 if (GetDebug()>2) {
335 cerr<<"AliMuonDigitizerv1::Exec ihit, ichamber, x, y, z, eloss " <<
336 ihit << " " <<
337 mHit->Chamber() << " " <<
338 mHit->X() << " " <<
339 mHit->Y() << " " <<
340 mHit->Z() << " " <<
341 mHit->Eloss() << " " << endl;
342 }
343 //
344 // Inititializing Correlation
345 chamber->ChargeCorrelationInit();
346 if (ichamber < AliMUONConstants::NTrackingCh()) {
347 // Tracking Chamber
348 // Initialize hit position (cursor) in the segmentation model
349 chamber->SigGenInit(mHit->X(), mHit->Y(), mHit->Z());
350 } else {
351 // Trigger Chamber
352 }
353 MakeTransientDigit(itrack, ihit, mHit);
354 } // hit loop
355 } // track loop
356 } // end file loop
357 if (GetDebug()>2) cerr<<"AliMUONDigitizer::Exec End of hits, track and file loops"<<endl;
358
69e452ff 359 runloader = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
360 gime = runloader->GetLoader("MUONLoader");
361 muondata->SetLoader(gime);
362
391cd891 363 // Loop on cathodes
364 Int_t icat;
365 for(icat=0; icat<2; icat++) {
8dbbc4e3 366 //
391cd891 367 // Filling Digit List
368 Int_t tracks[kMAXTRACKS];
369 Int_t charges[kMAXTRACKS];
370 Int_t nentries = fTDList->GetEntriesFast();
371 Int_t digits[6];
372 for (Int_t nent = 0; nent < nentries; nent++) {
373 AliMUONTransientDigit *address = (AliMUONTransientDigit*)fTDList->At(nent);
374 if (address == 0) continue;
375 Int_t ich = address->Chamber();
376 Int_t q = address->Signal();
377 chamber = &(pMUON->Chamber(ich));
378 //
379 // Digit Response (noise, threshold, saturation, ...)
380 AliMUONResponse * response = chamber->ResponseModel();
381 q = response->DigitResponse(q,address);
382
383 if (!q) continue;
384
385 digits[0] = address->PadX();
386 digits[1] = address->PadY();
387 digits[2] = address->Cathode()-1;
388 digits[3] = q;
389 digits[4] = address->Physics();
390 digits[5] = address->Hit();
391
392 Int_t nptracks = address->GetNTracks();
393
394 if (nptracks > kMAXTRACKS) {
395 if (GetDebug() >0) {
396 cerr<<"AliMUONDigitizer:Exec nptracks > 10 "<<nptracks;
397 cerr<<"reset to max value "<<kMAXTRACKS<<endl;
398 }
399 nptracks = kMAXTRACKS;
8dbbc4e3 400 }
391cd891 401 if (nptracks > 2 && GetDebug() >2) {
402 cerr<<"AliMUONDigitizer::Exec nptracks > 2 "<<nptracks<<endl;
403 // printf("cat,ich,ix,iy,q %d %d %d %d %d \n",icat,ich,digits[0],digits[1],q);
404 }
405 for (Int_t tr = 0; tr < nptracks; tr++) {
406 tracks[tr] = address->GetTrack(tr);
407 charges[tr] = address->GetCharge(tr);
408 } //end loop over list of tracks for one pad
409 // Sort list of tracks according to charge
410 if (nptracks > 1) {
411 SortTracks(tracks,charges,nptracks);
8dbbc4e3 412 }
391cd891 413 if (nptracks < kMAXTRACKS ) {
414 for (Int_t i = nptracks; i < kMAXTRACKS; i++) {
88cb7938 415 tracks[i] = -1;
391cd891 416 charges[i] = 0;
417 }
418 }
419
ce3f5e87 420 // Add digits
88cb7938 421 if (GetDebug()>3) cerr<<"AliMUONDigitzerv1::Exex TransientDigit to Digit"<<endl;
ce3f5e87 422 if ( digits[2] == icat ) muondata->AddDigit(ich,tracks,charges,digits);
b28974de 423// printf("test rm ich %d padX %d padY %d \n",ich, digits[0], digits[1]);
8dbbc4e3 424 }
88cb7938 425 // Filling list of digits per chamber for a given cathode.
1a1cdff8 426 muondata->Fill("D");
ce3f5e87 427 muondata->ResetDigits();
391cd891 428 } // end loop cathode
429 fTDList->Delete();
8dbbc4e3 430
431 for(Int_t ii = 0; ii < 2*AliMUONConstants::NCh(); ++ii) {
432 if (fHitMap[ii]) {
433 delete fHitMap[ii];
434 fHitMap[ii] = 0;
435 }
436 }
437
438 if (GetDebug()>2)
439 cerr<<"AliMUONDigitizer::Exec: writing the TreeD: "
88cb7938 440 <<treeD->GetName()<<endl;
441
3d08b8a3 442 runloader = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
443 gime = runloader->GetLoader("MUONLoader");
ce3f5e87 444 gime->WriteDigits("OVERWRITE");
8dbbc4e3 445 delete [] fHitMap;
446 delete fTDList;
ce3f5e87 447 muondata->ResetHits();
b28974de 448 gime->UnloadHits();
449 gime->UnloadDigits();
8dbbc4e3 450}
8dbbc4e3 451//------------------------------------------------------------------------
452void AliMUONDigitizerv1::SortTracks(Int_t *tracks,Int_t *charges,Int_t ntr)
453{
454 //
455 // Sort the list of tracks contributing to a given digit
456 // Only the 3 most significant tracks are acctually sorted
457 //
458
459 //
460 // Loop over signals, only 3 times
461 //
462
463 Int_t qmax;
464 Int_t jmax;
465 Int_t idx[3] = {-2,-2,-2};
466 Int_t jch[3] = {-2,-2,-2};
467 Int_t jtr[3] = {-2,-2,-2};
468 Int_t i,j,imax;
469
470 if (ntr<3) imax=ntr;
471 else imax=3;
472 for(i=0;i<imax;i++){
473 qmax=0;
474 jmax=0;
475
476 for(j=0;j<ntr;j++){
477
478 if((i == 1 && j == idx[i-1])
479 ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
480
481 if(charges[j] > qmax) {
482 qmax = charges[j];
483 jmax=j;
484 }
485 }
486
487 if(qmax > 0) {
488 idx[i]=jmax;
489 jch[i]=charges[jmax];
3351e14b 490 jtr[i]=tracks[jmax];
8dbbc4e3 491 }
492
493 }
494
495 for(i=0;i<3;i++){
496 if (jtr[i] == -2) {
497 charges[i]=0;
498 tracks[i]=0;
499 } else {
500 charges[i]=jch[i];
501 tracks[i]=jtr[i];
502 }
503 }
504}