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