The access to several data members was changed from public to protected. The digitisa...
[u/mrichter/AliRoot.git] / RICH / AliRICHMerger.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 /*
17 $Log$
18 Revision 1.4  2001/10/21 18:31:24  hristov
19 Several pointers were set to zero in the default constructors to avoid memory management problems
20
21 Revision 1.3  2001/05/16 14:57:20  alibrary
22 New files for folders and Stack
23
24 Revision 1.2  2001/03/14 18:16:08  jbarbosa
25 Corrected bug (more to correct).
26 File "points.dat" is no longer created.
27
28 Revision 1.1  2001/02/27 22:13:34  jbarbosa
29 Implementing merger class.
30
31 */
32 #include <iostream> 
33
34 #include <TTree.h> 
35 #include <TVector.h>
36 #include <TObjArray.h>
37 #include <TFile.h>
38 #include <TDirectory.h>
39 #include <TParticle.h>
40
41
42 // #include "AliMerger.h"
43 // #include "AliMergable.h"
44 #include "AliRICHMerger.h"
45 #include "AliRICHChamber.h"
46 #include "AliHitMap.h"
47 #include "AliRICHHitMapA1.h"
48 #include "AliRICH.h"
49 #include "AliRICHHit.h"
50 #include "AliRICHSDigit.h"
51 #include "AliRICHDigit.h"
52 #include "AliRICHTransientDigit.h"
53 #include "AliRun.h"
54 #include "AliPDG.h"
55
56 ClassImp(AliRICHMerger)
57
58 //___________________________________________
59 AliRICHMerger::AliRICHMerger()
60 {
61 // Default constructor    
62     fEvNrSig = 0;
63     fEvNrBgr = 0;
64     fMerge   = kDigitize;
65     fFnBgr   = 0;
66     fHitMap  = 0;
67     fList    = 0;
68     fTrH1    = 0;
69     fHitsBgr = 0;
70     fSDigitsBgr = 0;
71     fHitMap     = 0;
72     fList=new TObjArray;
73     fAddress    = 0; 
74     fBgrFile    = 0;
75 }
76
77 //------------------------------------------------------------------------
78 AliRICHMerger::~AliRICHMerger()
79 {
80 // Destructor
81     if (fTrH1)       delete fTrH1;
82     if (fHitsBgr)    delete fHitsBgr;
83     if (fSDigitsBgr) delete fSDigitsBgr;
84     if (fHitMap)     delete fHitMap;
85     if (fList)       delete fList;
86     if (fAddress)    delete fAddress; 
87 }
88
89 //------------------------------------------------------------------------
90 Bool_t AliRICHMerger::Exists(const AliRICHSDigit *mergable)
91 {
92     AliRICHSDigit *padhit = (AliRICHSDigit*) mergable;
93     return (fHitMap[fNch]->TestHit(padhit->PadX(),padhit->PadY()));
94 }
95
96 //------------------------------------------------------------------------
97 void AliRICHMerger::Update(AliRICHSDigit *mergable)
98 {
99     AliRICHSDigit *padhit = (AliRICHSDigit*) mergable;    
100     AliRICHTransientDigit* pdigit;
101     Int_t ipx      = padhit->PadX();        // pad number on X
102     Int_t ipy      = padhit->PadY();        // pad number on Y
103     Int_t iqpad    = Int_t(padhit->QPad()); // charge per pad
104
105     pdigit = (AliRICHTransientDigit*) fHitMap[fNch]->GetHit(ipx, ipy);
106     // update charge
107     //
108     (*pdigit).AddSignal(iqpad);
109     (*pdigit).AddPhysicsSignal(iqpad);          
110     // update list of tracks
111     //
112     TObjArray* digitTrList = (TObjArray*)pdigit->TrackList();
113     Int_t lastEntry = digitTrList->GetLast();
114     TVector *pTrack = (TVector*)digitTrList->At(lastEntry);
115     TVector &ptrk   = *pTrack;
116     TVector &trinfo = *((TVector*) (*fAddress)[fCountadr-1]);
117     Int_t lastTrack = Int_t(ptrk(0));
118
119     if (trinfo(0) != kBgTag) {
120         if (lastTrack == fTrack) {
121             Int_t lastCharge = Int_t(ptrk(1));
122             lastCharge += iqpad;
123             digitTrList->RemoveAt(lastEntry);
124             trinfo(1) = lastCharge;
125             digitTrList->AddAt(&trinfo,lastEntry);
126         } else {
127             digitTrList->Add(&trinfo);
128         }
129     } else {
130         if (lastTrack != -1) digitTrList->Add(&trinfo);
131     }
132 }
133
134 //------------------------------------------------------------------------
135 void AliRICHMerger::CreateNew(AliRICHSDigit *mergable)
136 {
137     AliRICHSDigit *padhit = (AliRICHSDigit*) mergable;    
138     AliRICHTransientDigit* pdigit;
139
140     Int_t ipx      = padhit->PadX();       // pad number on X
141     Int_t ipy      = padhit->PadY();       // pad number on Y
142     fList->AddAtAndExpand(
143         new AliRICHTransientDigit(fNch,fDigits),fCounter);
144     fHitMap[fNch]->SetHit(ipx, ipy, fCounter);
145     fCounter++;
146     pdigit = (AliRICHTransientDigit*)fList->At(fList->GetLast());
147     // list of tracks
148     TObjArray *digitTrList = (TObjArray*)pdigit->TrackList();
149     TVector &trinfo    = *((TVector*) (*fAddress)[fCountadr-1]);
150     digitTrList->Add(&trinfo);
151 }
152
153
154 //------------------------------------------------------------------------
155 void AliRICHMerger::Init()
156 {
157 // Initialisation
158     
159     if (fMerge) fBgrFile = InitBgr();
160 }
161
162
163
164 //------------------------------------------------------------------------
165 TFile* AliRICHMerger::InitBgr()
166 {
167 // Initialise background event
168     TFile *file = new TFile(fFnBgr);
169 // add error checking later
170     printf("\n AliRICHMerger has opened %s file with background event \n", fFnBgr);
171     fHitsBgr     = new TClonesArray("AliRICHHit",1000);
172     fSDigitsBgr  = new TClonesArray("AliRICHSDigit",1000);
173     return file;
174 }
175
176 //------------------------------------------------------------------------
177 void AliRICHMerger::Digitise(Int_t nev, Int_t flag)
178 {
179
180  // keep galice.root for signal and name differently the file for 
181     // background when add! otherwise the track info for signal will be lost !
182
183   Int_t particle;
184
185   //FILE* points; //these will be the digits...
186   
187   //points=fopen("points.dat","w");
188   
189   AliRICHChamber*       iChamber;
190   AliSegmentation*  segmentation;
191   
192     Int_t digitise=0;
193     Int_t trk[50];
194     Int_t chtrk[50];  
195     Int_t digits[5]; 
196     
197     AliRICH *pRICH = (AliRICH *) gAlice->GetDetector("RICH");
198     AliHitMap* pHitMap[10];
199     Int_t i;
200     for (i=0; i<10; i++) {pHitMap[i]=0;}
201     
202     if (fMerge ) {
203       fBgrFile->cd();
204       //fBgrFile->ls();
205       //
206       // Get Hits Tree header from file
207       if(fHitsBgr) fHitsBgr->Clear();
208       if(fSDigitsBgr) fSDigitsBgr->Clear();
209       if(fTrH1) delete fTrH1;
210       fTrH1 = 0;
211       
212       char treeName[20];
213       sprintf(treeName,"TreeH%d",fEvNrBgr);
214       fTrH1 = (TTree*)gDirectory->Get(treeName);
215       if (!fTrH1) {
216         printf("ERROR: cannot find Hits Tree for event:%d\n",fEvNrBgr);
217       }
218       //
219       // Set branch addresses
220       TBranch *branch;
221       char branchname[20];
222       sprintf(branchname,"%s",pRICH->GetName());
223       if (fTrH1 && fHitsBgr) {
224         branch = fTrH1->GetBranch(branchname);
225         if (branch) branch->SetAddress(&fHitsBgr);
226       }
227       if (fTrH1 && fSDigitsBgr) {
228         branch = fTrH1->GetBranch("RICHSDigits");
229         if (branch) branch->SetAddress(&fSDigitsBgr);
230       }
231     }
232     
233     AliHitMap* hm;
234     Int_t counter=0;
235     for (i =0; i<kNCH; i++) {
236       iChamber= &(pRICH->Chamber(i));
237       segmentation=iChamber->GetSegmentationModel(1);
238       pHitMap[i] = new AliRICHHitMapA1(segmentation, fList);
239     }
240     //
241     //   Loop over tracks
242     //
243     
244     TTree *treeH = gAlice->TreeH();
245     Int_t ntracks =(Int_t) treeH->GetEntries();
246     for (Int_t track=0; track<ntracks; track++) {
247       gAlice->ResetHits();
248       treeH->GetEvent(track);
249       //
250       //   Loop over hits
251       for(AliRICHHit* mHit=(AliRICHHit*)pRICH->FirstHit(-1); 
252           mHit;
253           mHit=(AliRICHHit*)pRICH->NextHit()) 
254         {
255           
256           Int_t   nch   = mHit->Chamber()-1;  // chamber number
257           Int_t   index = mHit->Track();
258           if (nch >kNCH) continue;
259           iChamber = &(pRICH->Chamber(nch));
260           
261           TParticle *current = (TParticle*)gAlice->Particle(index);
262           
263           if (current->GetPdgCode() >= 50000050)
264             {
265               TParticle *motherofcurrent = (TParticle*)gAlice->Particle(current->GetFirstMother());
266               particle = motherofcurrent->GetPdgCode();
267             }
268           else
269             {
270               particle = current->GetPdgCode();
271             }
272
273           
274           //printf("Flag:%d\n",flag);
275           //printf("Track:%d\n",track);
276           //printf("Particle:%d\n",particle);
277           
278           digitise=1;
279           
280           if (flag == 1) 
281             if(TMath::Abs(particle)==211 || TMath::Abs(particle)==111)
282               digitise=0;
283           
284           if (flag == 2)
285             if(TMath::Abs(particle)==321 || TMath::Abs(particle)==130 || TMath::Abs(particle)==310 
286                || TMath::Abs(particle)==311)
287               digitise=0;
288           
289           if (flag == 3 && TMath::Abs(particle)==2212)
290             digitise=0;
291           
292           if (flag == 4 && TMath::Abs(particle)==13)
293             digitise=0;
294           
295           if (flag == 5 && TMath::Abs(particle)==11)
296             digitise=0;
297           
298           if (flag == 6 && TMath::Abs(particle)==2112)
299             digitise=0;
300           
301           
302           //printf ("Particle: %d, Flag: %d, Digitise: %d\n",particle,flag,digitise); 
303           
304           
305           if (digitise)
306             {
307               
308               //
309               // Loop over pad hits
310               for (AliRICHSDigit* mPad=
311                      (AliRICHSDigit*)pRICH->FirstPad(mHit,pRICH->SDigits());
312                    mPad;
313                    mPad=(AliRICHSDigit*)pRICH->NextPad(pRICH->SDigits()))
314                 {
315                   Int_t ipx      = mPad->fPadX;       // pad number on X
316                   Int_t ipy      = mPad->fPadY;       // pad number on Y
317                   Int_t iqpad    = mPad->fQpad;       // charge per pad
318                   //
319                   //
320                   //printf("X:%d, Y:%d, Q:%d\n",ipx,ipy,iqpad);
321                   
322                   segmentation=iChamber->GetSegmentationModel(0);
323                   TVector & trinfo = *(new TVector(2));
324                   trinfo(0)=(Float_t)track;
325                   trinfo(1)=(Float_t)iqpad;
326                   
327                   digits[0]=ipx;
328                   digits[1]=ipy;
329                   digits[2]=iqpad;
330                   
331                   AliRICHTransientDigit* pdigit;
332                   // build the list of fired pads and update the info
333                   if (!pHitMap[nch]->TestHit(ipx, ipy)) {
334                     fList->AddAtAndExpand(new AliRICHTransientDigit(nch,digits),counter);
335                     pHitMap[nch]->SetHit(ipx, ipy, counter);
336                     counter++;
337                     pdigit=(AliRICHTransientDigit*)fList->At(fList->GetLast());
338                     // list of tracks
339                     TObjArray *trlist=(TObjArray*)pdigit->TrackList();
340                     trlist->Add(&trinfo);
341                   } else {
342                     pdigit=(AliRICHTransientDigit*) pHitMap[nch]->GetHit(ipx, ipy);
343                     // update charge
344                     //              (*pdigit).fSignal+=iqpad;
345                     pdigit->AddSignal(iqpad);
346                     // update list of tracks
347                     TObjArray* trlist=(TObjArray*)pdigit->TrackList();
348                     Int_t lastEntry=trlist->GetLast();
349                     TVector *ptrkP=(TVector*)trlist->At(lastEntry);
350                     TVector &ptrk=*ptrkP;
351                     Int_t lastTrack=Int_t(ptrk(0));
352                     Int_t lastCharge=Int_t(ptrk(1));
353                     if (lastTrack==track) {
354                       lastCharge+=iqpad;
355                       trlist->RemoveAt(lastEntry);
356                       trinfo(0)=lastTrack;
357                       trinfo(1)=lastCharge;
358                       trlist->AddAt(&trinfo,lastEntry);
359                     } else {
360                       trlist->Add(&trinfo);
361                     }
362                     // check the track list
363                     Int_t nptracks=trlist->GetEntriesFast();
364                     if (nptracks > 2) {
365                       printf("Attention - tracks:  %d (>2)\n",nptracks);
366                       //printf("cat,nch,ix,iy %d %d %d %d  \n",icat+1,nch,ipx,ipy);
367                       for (Int_t tr=0;tr<nptracks;tr++) {
368                         TVector *pptrkP=(TVector*)trlist->At(tr);
369                         TVector &pptrk=*pptrkP;
370                         trk[tr]=Int_t(pptrk(0));
371                         chtrk[tr]=Int_t(pptrk(1));
372                       }
373                     } // end if nptracks
374                   } //  end if pdigit
375                 } //end loop over clusters
376             }// track type condition
377         } // hit loop
378     } // track loop
379     
380     // open the file with background
381     
382     if (fMerge) {
383       ntracks = (Int_t)fTrH1->GetEntries();
384       //
385       //   Loop over tracks
386       //
387       for (fTrack = 0; fTrack < ntracks; fTrack++) {
388         
389         if (fHitsBgr)       fHitsBgr->Clear();
390         if (fSDigitsBgr)    fSDigitsBgr->Clear();
391         
392         fTrH1->GetEvent(fTrack);
393         //
394         //   Loop over hits
395         AliRICHHit* mHit;
396         for(int i = 0; i < fHitsBgr->GetEntriesFast(); ++i) 
397           {     
398             mHit   = (AliRICHHit*) (*fHitsBgr)[i];
399             fNch   = mHit->Chamber()-1;  // chamber number
400             iChamber = &(pRICH->Chamber(fNch));
401             //Float_t xbgr = mHit->X();
402             //Float_t ybgr = mHit->Y();
403             Bool_t cond  = kFALSE;
404             cond  = kTRUE;
405             //
406             // Loop over pad hits
407             for (AliRICHSDigit* mPad =
408                    (AliRICHSDigit*)pRICH->FirstPad(mHit,fSDigitsBgr);
409                  mPad;
410                  mPad = (AliRICHSDigit*)pRICH->NextPad(fSDigitsBgr))
411               {
412                 Int_t ipx      = mPad->PadX();        // pad number on X
413                 Int_t ipy      = mPad->PadY();        // pad number on Y
414                 Int_t iqpad    = Int_t(mPad->QPad()); // charge per pad
415                 
416                 new((*fAddress)[fCountadr++]) TVector(2);
417                 TVector &trinfo = *((TVector*) (*fAddress)[fCountadr-1]);
418                 trinfo(0) = kBgTag;  // tag background
419                 trinfo(1) = kBgTag;
420                 
421                 fDigits[0] = ipx;
422                 fDigits[1] = ipy;
423                 fDigits[3] = iqpad;
424                 
425                 // build the list of fired pads and update the info
426                 if (!Exists(mPad)) {
427                   CreateNew(mPad);
428                 } else {
429                   Update(mPad);
430                 } //  end if !Exists
431               } //end loop over clusters
432           } // hit loop
433       } // track loop
434       
435       TTree *fAli = gAlice->TreeK();
436       TFile *file = NULL;
437       
438       if (fAli) file = fAli->GetCurrentFile();
439       file->cd();
440     } // if fMerge
441     
442     Int_t tracks[10];
443     Int_t charges[10];
444     //cout<<"Start filling digits \n "<<endl;
445     Int_t nentries=fList->GetEntriesFast();
446     printf(" \n \n nentries %d \n",nentries);
447     
448     // start filling the digits
449     
450     for (Int_t nent=0;nent<nentries;nent++) {
451       AliRICHTransientDigit *transDigit=(AliRICHTransientDigit*)fList->At(nent);
452       if (transDigit==0) continue; 
453       
454       Int_t ich=transDigit->GetChamber();
455       Int_t q=transDigit->Signal(); 
456       iChamber=&(pRICH->Chamber(ich));
457       AliRICHResponse * response=iChamber->GetResponseModel();
458       Int_t adcmax= (Int_t) response->MaxAdc();
459       
460       
461       // add white noise and do zero-suppression and signal truncation (new electronics,old electronics gaus 1.2,0.2)
462       //printf("Treshold: %d\n",iChamber->fTresh->GetHitIndex(transDigit->PadX(),transDigit->PadY()));
463       Int_t pedestal = iChamber->fTresh->GetHitIndex(transDigit->PadX(),transDigit->PadY());
464
465       //printf("Pedestal:%d\n",pedestal);
466       //Int_t pedestal=0;
467       Float_t treshold = (pedestal + 4*2.2);
468       
469       Float_t meanNoise = gRandom->Gaus(2.2, 0.3);
470       Float_t noise     = gRandom->Gaus(0, meanNoise);
471       q+=(Int_t)(noise + pedestal);
472       //q+=(Int_t)(noise);
473       //          magic number to be parametrised !!! 
474       if ( q <= treshold) 
475         {
476           q = q - pedestal;
477           continue;
478         }
479       q = q - pedestal;
480       if ( q >= adcmax) q=adcmax;
481       digits[0]=transDigit->PadX();
482       digits[1]=transDigit->PadY();
483       digits[2]=q;
484       
485       TObjArray* trlist=(TObjArray*)transDigit->TrackList();
486       Int_t nptracks=trlist->GetEntriesFast();
487       
488       // this was changed to accomodate the real number of tracks
489       if (nptracks > 10) {
490         printf("Attention - tracks > 10 %d\n",nptracks);
491         nptracks=10;
492       }
493       if (nptracks > 2) {
494         printf("Attention - tracks > 2  %d \n",nptracks);
495         //printf("cat,ich,ix,iy,q %d %d %d %d %d \n",
496         //icat,ich,digits[0],digits[1],q);
497       }
498       for (Int_t tr=0;tr<nptracks;tr++) {
499         TVector *ppP=(TVector*)trlist->At(tr);
500         TVector &pp  =*ppP;
501         tracks[tr]=Int_t(pp(0));
502         charges[tr]=Int_t(pp(1));
503       }      //end loop over list of tracks for one pad
504       if (nptracks < 10 ) {
505         for (Int_t t=nptracks; t<10; t++) {
506           tracks[t]=0;
507           charges[t]=0;
508         }
509       }
510       //write file
511       //if (ich==2)
512         //fprintf(points,"%4d,      %4d,      %4d\n",digits[0],digits[1],digits[2]);
513       
514       // fill digits
515       pRICH->AddDigits(ich,tracks,charges,digits);
516     }   
517     gAlice->TreeD()->Fill();
518
519     fList->Delete();
520     for(Int_t ii=0;ii<kNCH;++ii) {
521       if (pHitMap[ii]) {
522         hm=pHitMap[ii];
523         delete hm;
524         pHitMap[ii]=0;
525       }
526     }
527     
528     //TTree *TD=gAlice->TreeD();
529     //Stat_t ndig=TD->GetEntries();
530     //cout<<"number of digits  "<<ndig<<endl;
531     TClonesArray *fDch;
532     for (int k=0;k<kNCH;k++) {
533       fDch= pRICH->DigitsAddress(k);
534       int ndigit=fDch->GetEntriesFast();
535       printf ("Chamber %d digits %d \n",k,ndigit);
536     }
537     pRICH->ResetDigits();
538     // char hname[30];
539     // sprintf(hname,"TreeD%d",nev);
540     // gAlice->TreeD()->Write(hname);
541     gAlice->TreeD()->Write(0,TObject::kOverwrite);
542     // reset tree
543     //    gAlice->TreeD()->Reset();
544     //    delete fList; // deleted in dtor
545     // gObjectTable->Print();
546
547 }
548
549
550
551
552