Using new for arrays with undefined size
[u/mrichter/AliRoot.git] / RICH / AliRICHDigitizer.cxx
CommitLineData
b762c2f6 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
b9d0a01d 16
88cb7938 17//Piotr.Skowronski@cern.ch :
18//Corrections applied in order to compile (only) with new I/O and folder structure
19//To be implemented correctly by responsible
30f2451c 20
7118aef0 21#include <Riostream.h>
b762c2f6 22
23#include <TTree.h>
24#include <TObjArray.h>
25#include <TFile.h>
26#include <TDirectory.h>
27#include <TParticle.h>
28
88cb7938 29#include "AliRunLoader.h"
30#include "AliLoader.h"
31
b762c2f6 32#include "AliRICHDigitizer.h"
33#include "AliRICHChamber.h"
34#include "AliHitMap.h"
35#include "AliRICHHitMapA1.h"
36#include "AliRICH.h"
b762c2f6 37#include "AliRICHSDigit.h"
38#include "AliRICHDigit.h"
39#include "AliRICHTransientDigit.h"
40#include "AliRun.h"
41#include "AliPDG.h"
42#include "AliRunDigitizer.h"
43
44ClassImp(AliRICHDigitizer)
45
c60862bf 46//__________________________________________________________________________________________________
47AliRICHDigitizer::AliRICHDigitizer()
48{//default ctor - don't use it
b762c2f6 49 fHits = 0;
50 fSDigits = 0;
51 fHitMap = 0;
52 fTDList = 0;
c60862bf 53}//default ctor
54//__________________________________________________________________________________________________
55AliRICHDigitizer::AliRICHDigitizer(AliRunDigitizer *pManager)
56 :AliDigitizer(pManager)
57{//main ctor which should be used
543d5224 58 if(fManager->GetDebug())Info("main ctor","Start.");
b762c2f6 59 fHits = 0;
60 fSDigits = 0;
61 fHitMap = 0;
62 fTDList = 0;
c60862bf 63 fDebug = pManager->GetDebug();
64}//main ctor
65//__________________________________________________________________________________________________
b762c2f6 66AliRICHDigitizer::~AliRICHDigitizer()
c60862bf 67{//dtor
543d5224 68 if(fManager->GetDebug())Info("dtor","Start.");
c60862bf 69
543d5224 70 if(fHits) {fHits->Delete(); delete fHits;}
71 if(fSDigits) {fSDigits->Delete();delete fSDigits;}
72 if(fTDList) {fTDList->Delete(); delete fTDList;}
73 for(Int_t i=0; i<kNCH; i++) delete fHitMap[i]; delete [] fHitMap;
c60862bf 74}//dtor
75//__________________________________________________________________________________________________
76Bool_t AliRICHDigitizer::Exists(const AliRICHSDigit *p)
b762c2f6 77{
c60862bf 78 return (fHitMap[fNch]->TestHit(p->PadX(),p->PadY()));
79}//Exists
80//__________________________________________________________________________________________________
b762c2f6 81void AliRICHDigitizer::Update(AliRICHSDigit *padhit)
82{
83 AliRICHTransientDigit *pdigit =
84 static_cast<AliRICHTransientDigit*>(
85 fHitMap[fNch]->GetHit(padhit->PadX(),padhit->PadY()));
86
87 // update charge
88 //
89 Int_t iqpad = Int_t(padhit->QPad()); // charge per pad
90 pdigit->AddSignal(iqpad);
88cb7938 91 pdigit->AddPhysicsSignal(iqpad);
b762c2f6 92
93 // update list of tracks
94 //
95 Int_t track, charge;
96 track = fTrack+fMask;
97 if (fSignal) {
98 charge = iqpad;
99 } else {
100 charge = kBgTag;
101 }
102 pdigit->UpdateTrackList(track,charge);
c60862bf 103}//Update()
104//__________________________________________________________________________________________________
b762c2f6 105void AliRICHDigitizer::CreateNew(AliRICHSDigit *padhit)
106{
107 fTDList->AddAtAndExpand(
108 new AliRICHTransientDigit(fNch,fDigits),fCounter);
109 fHitMap[fNch]->SetHit(padhit->PadX(),padhit->PadY(),fCounter);
110
111 AliRICHTransientDigit* pdigit =
112 static_cast<AliRICHTransientDigit*>(fTDList->Last());
113 // list of tracks
114 Int_t track, charge;
115 if (fSignal) {
116 track = fTrack;
117 charge = padhit->QPad();
118 } else {
119 track = kBgTag;
120 charge = kBgTag;
121 }
122 pdigit->AddToTrackList(track,charge);
123 fCounter++;
c60862bf 124}//CreateNew()
125//__________________________________________________________________________________________________
b762c2f6 126Bool_t AliRICHDigitizer::Init()
c60862bf 127{// Initialisation
128 if(GetDebug())Info("Init","Start.");
853634d3 129 fHits = new TClonesArray("AliRICHhit",1000);
b762c2f6 130 fSDigits = new TClonesArray("AliRICHSDigit",1000);
131 return kTRUE;
c60862bf 132}//Init()
133//__________________________________________________________________________________________________
b762c2f6 134void AliRICHDigitizer::Exec(Option_t* option)
135{
c60862bf 136 if(GetDebug())Info("Exec","Start with option=%s",option);
b762c2f6 137
138 AliRICHChamber* iChamber;
139 AliSegmentation* segmentation;
88cb7938 140
c60862bf 141 AliRunLoader *pInAL, *pOutAL;//in and out Run loaders
142 AliLoader *pInRL, *pOutRL;//in and out RICH loaders
88cb7938 143
c60862bf 144 pOutAL = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
145 pOutRL = pOutAL->GetLoader("RICHLoader");
88cb7938 146
b762c2f6 147 fTDList = new TObjArray;
88cb7938 148
149
b762c2f6 150 AliRICH *pRICH = (AliRICH *) gAlice->GetDetector("RICH");
88cb7938 151
c60862bf 152 if(!pOutRL->TreeD()) pOutRL->MakeTree("D"); pRICH->MakeBranch("D");
543d5224 153
154
b762c2f6 155 fHitMap= new AliHitMap* [kNCH];
156
157 for (Int_t i =0; i<kNCH; i++) {
158 iChamber= &(pRICH->Chamber(i));
cc23c5c6 159 segmentation=iChamber->GetSegmentationModel();
b762c2f6 160 fHitMap[i] = new AliRICHHitMapA1(segmentation, fTDList);
161 }
162
163// Loop over files to digitize
164 fSignal = kTRUE;
165 fCounter = 0;
c60862bf 166 for (Int_t inputFile=0;inputFile<fManager->GetNinputs();inputFile++){//files loop
167 pInAL = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
168 pInRL = pInAL->GetLoader("RICHLoader");
88cb7938 169
b762c2f6 170// Connect RICH branches
171
172 if (inputFile > 0 ) fSignal = kFALSE;
173 TBranch *branchHits = 0;
174 TBranch *branchSDigits = 0;
88cb7938 175
c60862bf 176 pInRL->LoadHits("READ");
177
178 TTree *treeH = pInRL->TreeH();
179 if(pInRL->TreeH()) {
b762c2f6 180 branchHits = treeH->GetBranch("RICH");
c60862bf 181 if(branchHits){
182 fHits->Clear();
183 branchHits->SetAddress(&fHits);}
b762c2f6 184 else
c60862bf 185 Error("Exec","branch RICH was not found");
b762c2f6 186 }
b762c2f6 187
c60862bf 188 if(treeH) {
189 branchSDigits = treeH->GetBranch("RICHSpecials");
190 if(branchSDigits)
191 branchSDigits->SetAddress(&fSDigits);
b762c2f6 192 else
c60862bf 193 Error("exec","branch RICHSpecials was not found");
b762c2f6 194 }
b762c2f6 195
c60862bf 196 for (fTrack=0; fTrack<treeH->GetEntries(); fTrack++) {//prims loop
197 fHits->Clear(); fSDigits->Clear();
198 fHits->Print();
199
200 branchHits->GetEntry(fTrack); branchSDigits->GetEntry(fTrack);
201 for(Int_t i=0;i<fHits->GetEntriesFast();++i){//hits loop
202 AliRICHhit* pHit = static_cast<AliRICHhit*>(fHits->At(i));
203 fNch = pHit->Chamber()-1; // chamber number
b762c2f6 204 if (fNch >= kNCH) {
205 cerr<<"AliRICHDigitizer: chamber nr. fNch out of range: "<<fNch<<endl;
206 cerr<<" track: "<<fTrack<<endl;
207 continue;
208 }
209 iChamber = &(pRICH->Chamber(fNch));
210
543d5224 211 for (AliRICHSDigit* mPad=FirstPad(pHit,fSDigits);mPad;mPad=NextPad(fSDigits)){//specials loop
b762c2f6 212 Int_t iqpad = mPad->QPad(); // charge per pad
213 fDigits[0]=mPad->PadX();
214 fDigits[1]=mPad->PadY();
215 fDigits[2]=iqpad;
216 fDigits[3]=iqpad;
217 fDigits[4]=mPad->HitNumber();
c60862bf 218 if (Exists(mPad)) // build the list of fired pads and update the info
219 Update(mPad);
220 else
221 CreateNew(mPad);
543d5224 222 }//specials loop
c60862bf 223 }//hits loop
224 }//prims loop
225 }//files loop
226 if(GetDebug()) Info("Exec","END OF FILE LOOP");
b762c2f6 227
228 Int_t tracks[kMAXTRACKSPERRICHDIGIT];
229 Int_t charges[kMAXTRACKSPERRICHDIGIT];
230 Int_t nentries=fTDList->GetEntriesFast();
231
543d5224 232 for (Int_t nent=0;nent<nentries;nent++){//transient digits loop
b762c2f6 233 AliRICHTransientDigit *transDigit=(AliRICHTransientDigit*)fTDList->At(nent);
234 if (transDigit==0) continue;
235 Int_t ich=transDigit->GetChamber();
236 Int_t q=transDigit->Signal();
237 iChamber=&(pRICH->Chamber(ich));
238 AliRICHResponse * response=iChamber->GetResponseModel();
239 Int_t adcmax= (Int_t) response->MaxAdc();
240
241
242 // add white noise and do zero-suppression and signal truncation (new electronics,old electronics gaus 1.2,0.2)
243 //printf("Treshold: %d\n",iChamber->fTresh->GetHitIndex(transDigit->PadX(),transDigit->PadY()));
244
245// tmp change
246// Int_t pedestal = iChamber->fTresh->GetHitIndex(transDigit->PadX(),transDigit->PadY());
247 Int_t pedestal = 0;
248
b762c2f6 249 Float_t treshold = (pedestal + 4*2.2);
250
251 Float_t meanNoise = gRandom->Gaus(2.2, 0.3);
252 Float_t noise = gRandom->Gaus(0, meanNoise);
253 q+=(Int_t)(noise + pedestal);
254 //q+=(Int_t)(noise);
255 // magic number to be parametrised !!!
256 if ( q <= treshold)
257 {
258 q = q - pedestal;
259 continue;
260 }
261 q = q - pedestal;
262 if ( q >= adcmax) q=adcmax;
263 fDigits[0]=transDigit->PadX();
264 fDigits[1]=transDigit->PadY();
265 fDigits[2]=q;
266 fDigits[3]=transDigit->Physics();
267 fDigits[4]=transDigit->Hit();
268
269 Int_t nptracks = transDigit->GetNTracks();
270
543d5224 271
272 if(nptracks>kMAXTRACKSPERRICHDIGIT){// this was changed to accomodate the real number of tracks
b762c2f6 273 printf("Attention - tracks > 10 %d\n",nptracks);
274 nptracks=kMAXTRACKSPERRICHDIGIT;
275 }
276 if (nptracks > 2) {
277 printf("Attention - tracks > 2 %d \n",nptracks);
278 }
279 for (Int_t tr=0;tr<nptracks;tr++) {
280 tracks[tr]=transDigit->GetTrack(tr);
281 charges[tr]=transDigit->GetCharge(tr);
282 } //end loop over list of tracks for one pad
283 if (nptracks < kMAXTRACKSPERRICHDIGIT ) {
284 for (Int_t t=nptracks; t<kMAXTRACKSPERRICHDIGIT; t++) {
88cb7938 285 tracks[t]=0;
286 charges[t]=0;
b762c2f6 287 }
288 }
543d5224 289 pRICH->AddDigitOld(ich+1,tracks,charges,fDigits);//OLD
290 }//transient digits loop
c60862bf 291 pOutRL->TreeD()->Fill();
b762c2f6 292
5546f238 293 //pRICH->ResetDigits();
b762c2f6 294 fTDList->Delete(); // or fTDList->Clear(); ???
295 for(Int_t ii=0;ii<kNCH;++ii) {
296 if (fHitMap[ii]) {
297 delete fHitMap[ii];
298 fHitMap[ii]=0;
299 }
300 }
301
302 TClonesArray *richDigits;
303 for (Int_t k=0;k<kNCH;k++) {
304 richDigits = pRICH->DigitsAddress(k);
305 Int_t ndigit=richDigits->GetEntriesFast();
543d5224 306 printf ("Chamber %d digits %d \n",k+1,ndigit);
b762c2f6 307 }
88cb7938 308 pRICH->ResetDigits(); /// ??? should it be here???
309
c60862bf 310 pOutRL->WriteDigits("OVERWRITE");
88cb7938 311
b762c2f6 312 delete [] fHitMap;
313 delete fTDList;
543d5224 314 pOutRL->UnloadHits();
315 pOutRL->UnloadDigits();
c60862bf 316// if (fHits) fHits->Delete();
317// if (fSDigits) fSDigits->Delete();
318 if(GetDebug())Info("Exec","Stop.");
319}//Exec()
320//__________________________________________________________________________________________________
321
322static Int_t sMaxIterPad=0; // Static variables for the pad-hit iterator routines
323static Int_t sCurIterPad=0;
324
325//__________________________________________________________________________________________________
326AliRICHSDigit* AliRICHDigitizer::FirstPad(AliRICHhit* hit,TClonesArray *clusters )
327{// Initialise the pad iterator Return the address of the first sdigit for hit
328 TClonesArray *theClusters = clusters;
329 Int_t nclust = theClusters->GetEntriesFast();
330 if (nclust && hit->PHlast() > 0) {
331 sMaxIterPad=Int_t(hit->PHlast());
332 sCurIterPad=Int_t(hit->PHfirst());
333 return (AliRICHSDigit*) clusters->UncheckedAt(sCurIterPad-1);
334 } else {
335 return 0;
336 }
337
338}//FirstPad
339//__________________________________________________________________________________________________
340AliRICHSDigit* AliRICHDigitizer::NextPad(TClonesArray *clusters)
341{// Iterates over pads
342
343 sCurIterPad++;
344 if (sCurIterPad <= sMaxIterPad) {
345 return (AliRICHSDigit*) clusters->UncheckedAt(sCurIterPad-1);
346 } else {
347 return 0;
348 }
349}//NextPad
350//__________________________________________________________________________________________________