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