Update TPCCEda to write output file in parts (to avoid too big files produced in...
[u/mrichter/AliRoot.git] / TPC / AliTPCDigitizer.cxx
CommitLineData
3c038d07 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
88cb7938 16/* $Id$ */
d37f8c34 17
f94f8f87 18/*
19 Class for creating of the sumable digits and digits from MC data
20 //
21 The input : ideal signals (Hits->Diffusion->Attachment -Ideal signal)
22 The output: raw digits
23
24 Effect implemented:
25 1. Pad by pad gain map
26 2. Noise map
27 3. The dead channels identified - zerro noise for corresponding pads
28 In this case the outpu equal zerro
29
30*/
31
32
33
34
d37f8c34 35#include <stdlib.h>
3c038d07 36#include <TTree.h>
37#include <TObjArray.h>
38#include <TFile.h>
39#include <TDirectory.h>
19364939 40#include <Riostream.h>
f546a4b4 41#include <TParameter.h>
3c038d07 42
43#include "AliTPCDigitizer.h"
44
45#include "AliTPC.h"
46#include "AliTPCParam.h"
7a09f434 47#include "AliTPCParamSR.h"
3c038d07 48#include "AliRun.h"
49#include "AliPDG.h"
50#include "AliRunDigitizer.h"
51#include "AliSimDigits.h"
f77f13c8 52#include "AliLog.h"
3c038d07 53
13116aec 54#include "AliTPCcalibDB.h"
55#include "AliTPCCalPad.h"
56#include "AliTPCCalROC.h"
57
3c038d07 58ClassImp(AliTPCDigitizer)
59
60//___________________________________________
179c6296 61 AliTPCDigitizer::AliTPCDigitizer() :AliDigitizer(),fDebug(0)
3c038d07 62{
179c6296 63 //
3c038d07 64// Default ctor - don't use it
179c6296 65//
66
3c038d07 67}
68
69//___________________________________________
70AliTPCDigitizer::AliTPCDigitizer(AliRunDigitizer* manager)
179c6296 71 :AliDigitizer(manager),fDebug(0)
3c038d07 72{
179c6296 73 //
3c038d07 74// ctor which should be used
179c6296 75//
f77f13c8 76 AliDebug(2,"(AliRunDigitizer* manager) was processed");
3c038d07 77}
78
79//------------------------------------------------------------------------
80AliTPCDigitizer::~AliTPCDigitizer()
81{
82// Destructor
83}
84
85
86
87//------------------------------------------------------------------------
88Bool_t AliTPCDigitizer::Init()
89{
90// Initialization
91
92 return kTRUE;
93}
94
95
96//------------------------------------------------------------------------
97void AliTPCDigitizer::Exec(Option_t* option)
98{
f648982e 99 ExecFast(option);
100}
101//------------------------------------------------------------------------
102void AliTPCDigitizer::ExecFast(Option_t* option)
103{
3c038d07 104
105 // merge input tree's with summable digits
106 //output stored in TreeTPCD
7a09f434 107 char s[100];
108 char ss[100];
3c038d07 109 TString optionString = option;
94bf739c 110 if (!strcmp(optionString.Data(),"deb")) {
3c038d07 111 cout<<"AliTPCDigitizer::Exec: called with option deb "<<endl;
112 fDebug = 3;
113 }
114 //get detector and geometry
88cb7938 115
116
117 AliRunLoader *rl, *orl;
118 AliLoader *gime, *ogime;
119
120 if (gAlice == 0x0)
121 {
122 Warning("ExecFast","gAlice is NULL. Loading from input 0");
123 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
124 if (rl == 0x0)
125 {
126 Error("ExecFast","Can not find Run Loader for input 0. Can not proceed.");
127 return;
128 }
129 rl->LoadgAlice();
130 rl->GetAliRun();
131 }
3c038d07 132 AliTPC *pTPC = (AliTPC *) gAlice->GetModule("TPC");
133 AliTPCParam * param = pTPC->GetParam();
7a09f434 134
135 sprintf(s,param->GetTitle());
136 sprintf(ss,"75x40_100x60");
137 if(strcmp(s,ss)==0){
138 printf("2 pad-length geom hits with 3 pad-lenght geom digits...\n");
139 delete param;
140 param=new AliTPCParamSR();
141 }
142 else{
143 sprintf(ss,"75x40_100x60_150x60");
144 if(strcmp(s,ss)!=0) {
145 printf("No TPC parameters found...\n");
146 exit(2);
147 }
148 }
149
f546a4b4 150 pTPC->GenerNoise(500000); //create table with noise
407ff276 151 //
152 Int_t nInputs = fManager->GetNinputs();
153 Int_t * masks = new Int_t[nInputs];
154 for (Int_t i=0; i<nInputs;i++)
155 masks[i]= fManager->GetMask(i);
156 Short_t **pdig= new Short_t*[nInputs]; //pointers to the expanded digits array
f546a4b4 157 Int_t **ptr= new Int_t*[nInputs]; //pointers to the expanded tracks array
4df28b43 158 Bool_t *active= new Bool_t[nInputs]; //flag for active input segments
f546a4b4 159 Char_t phname[100];
88cb7938 160
3c038d07 161 //create digits array for given sectors
f648982e 162 // make indexes
f546a4b4 163 //
164 //create branch's in TPC treeD
165 orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
166 ogime = orl->GetLoader("TPCLoader");
167 TTree * tree = ogime->TreeD();
168 AliSimDigits * digrow = new AliSimDigits;
169
170 if (tree == 0x0)
171 {
172 ogime->MakeTree("D");
173 tree = ogime->TreeD();
174 }
175 tree->Branch("Segment","AliSimDigits",&digrow);
176 //
407ff276 177 AliSimDigits ** digarr = new AliSimDigits*[nInputs];
88cb7938 178 for (Int_t i1=0;i1<nInputs; i1++)
179 {
180 digarr[i1]=0;
181 // intree[i1]
182 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i1));
183 gime = rl->GetLoader("TPCLoader");
184 gime->LoadSDigits("read");
185 TTree * treear = gime->TreeS();
186
187 if (!treear)
188 {
189 cerr<<"AliTPCDigitizer: Input tree with SDigits not found in"
190 <<" input "<< i1<<endl;
191 return;
192 }
f546a4b4 193
194 sprintf(phname,"lhcphase%d",i1);
195 TParameter<float> *ph = (TParameter<float>*)treear->GetUserInfo()
196 ->FindObject("lhcphase0");
197 if(!ph){
198 cerr<<"AliTPCDigitizer: LHC phase not found in"
199 <<" input "<< i1<<endl;
200 return;
201 }
202 tree->GetUserInfo()->Add(new TParameter<float>(phname,ph->GetVal()));
203 //
88cb7938 204 if (treear->GetIndex()==0)
205 treear->BuildIndex("fSegmentID","fSegmentID");
206 treear->GetBranch("Segment")->SetAddress(&digarr[i1]);
12d8d654 207 }
88cb7938 208
3c038d07 209
88cb7938 210
f546a4b4 211
3c038d07 212 //
213
407ff276 214 param->SetZeroSup(2);
215
f648982e 216 Int_t zerosup = param->GetZeroSup();
5033a39a 217 AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetDedxGainFactor();
1ac191a6 218 AliTPCCalPad * noiseTPC = AliTPCcalibDB::Instance()->GetPadNoise();
f648982e 219 //
3c038d07 220 //Loop over segments of the TPC
221
4df28b43 222 for (Int_t segmentID=0; segmentID<param->GetNRowsTotal(); segmentID++)
88cb7938 223 {
4df28b43 224 Int_t sec, row;
225 if (!param->AdjustSectorRow(segmentID,sec,row))
226 {
227 cerr<<"AliTPC warning: invalid segment ID ! "<<segmentID<<endl;
228 continue;
229 }
13116aec 230 AliTPCCalROC * gainROC = gainTPC->GetCalROC(sec); // pad gains per given sector
1ac191a6 231 AliTPCCalROC * noiseROC = noiseTPC->GetCalROC(sec); // noise per given sector
4df28b43 232 digrow->SetID(segmentID);
233
234 Int_t nrows = 0;
235 Int_t ncols = 0;
236
237 Bool_t digitize = kFALSE;
238 for (Int_t i=0;i<nInputs; i++)
88cb7938 239 {
240
241 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
242 gime = rl->GetLoader("TPCLoader");
243
4df28b43 244 if (gime->TreeS()->GetEntryWithIndex(segmentID,segmentID) >= 0) {
245 digarr[i]->ExpandBuffer();
246 digarr[i]->ExpandTrackBuffer();
247 nrows = digarr[i]->GetNRows();
248 ncols = digarr[i]->GetNCols();
249 active[i] = kTRUE;
250 if (!fRegionOfInterest || (i == 0)) digitize = kTRUE;
251 } else {
252 active[i] = kFALSE;
253 }
254 if (fRegionOfInterest && !digitize) break;
88cb7938 255 }
4df28b43 256 if (!digitize) continue;
3c038d07 257
3c038d07 258 digrow->Allocate(nrows,ncols);
259 digrow->AllocateTrack(3);
260
407ff276 261 Float_t q=0;
262 Int_t label[1000]; //stack for 300 events
263 Int_t labptr = 0;
264
f648982e 265 Int_t nElems = nrows*ncols;
266
88cb7938 267 for (Int_t i=0;i<nInputs; i++)
4df28b43 268 if (active[i]) {
88cb7938 269 pdig[i] = digarr[i]->GetDigits();
270 ptr[i] = digarr[i]->GetTracks();
4df28b43 271 }
88cb7938 272
407ff276 273 Short_t *pdig1= digrow->GetDigits();
274 Int_t *ptr1= digrow->GetTracks() ;
275
f648982e 276
407ff276 277
88cb7938 278 for (Int_t elem=0;elem<nElems; elem++)
279 {
280
281 q=0;
282 labptr=0;
283 // looop over digits
4df28b43 284 for (Int_t i=0;i<nInputs; i++) if (active[i])
88cb7938 285 {
286 // q += digarr[i]->GetDigitFast(rows,col);
287 q += *(pdig[i]);
288
289 for (Int_t tr=0;tr<3;tr++)
290 {
291 // Int_t lab = digarr[i]->GetTrackIDFast(rows,col,tr);
292 Int_t lab = ptr[i][tr*nElems];
293 if ( (lab > 1) && *(pdig[i])>zerosup)
294 {
295 label[labptr]=lab+masks[i];
296 labptr++;
297 }
298 }
299 pdig[i]++;
300 ptr[i]++;
301 }
302 q/=16.; //conversion factor
13116aec 303 Float_t gain = gainROC->GetValue(row,elem/nrows); // get gain for given - pad-row pad
ebf9a79e 304 //if (gain<0.5){
305 //printf("problem\n");
306 //}
13116aec 307 q*= gain;
1ac191a6 308 Float_t noisePad = noiseROC->GetValue(row,elem/nrows);
88cb7938 309 // Float_t noise = gRandom->Gaus(0,param->GetNoise()*param->GetNoiseNormFac());
310 Float_t noise = pTPC->GetNoise();
1ac191a6 311 q+=noise*noisePad;
3c038d07 312 q=TMath::Nint(q);
88cb7938 313 if (q > zerosup)
314 {
e93a083a 315 if(q >= param->GetADCSat()) q = (Short_t)(param->GetADCSat() - 1);
88cb7938 316 //digrow->SetDigitFast((Short_t)q,rows,col);
317 *pdig1 =Short_t(q);
318 for (Int_t tr=0;tr<3;tr++)
319 {
320 if (tr<labptr)
321 ptr1[tr*nElems] = label[tr];
322 }
323 }
324 pdig1++;
325 ptr1++;
326 }
58c45a5e 327 //
328 // glitch filter
329 //
330 digrow->GlitchFilter();
331 //
3c038d07 332 digrow->CompresBuffer(1,zerosup);
333 digrow->CompresTrackBuffer(1);
334 tree->Fill();
335 if (fDebug>0) cerr<<sec<<"\t"<<row<<"\n";
4df28b43 336 } //for (Int_t n=0; n<param->GetNRowsTotal(); n++)
88cb7938 337
338
339 orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
340 ogime = orl->GetLoader("TPCLoader");
341 ogime->WriteDigits("OVERWRITE");
342
343 //fManager->GetTreeDTPC()->Write(0,TObject::kOverwrite);
344
3c038d07 345 delete digrow;
407ff276 346 for (Int_t i1=0;i1<nInputs; i1++) delete digarr[i1];
347 delete []masks;
4df28b43 348 delete []pdig;
349 delete []ptr;
350 delete []active;
351 delete []digarr;
3c038d07 352}
353
f648982e 354
355
356//------------------------------------------------------------------------
357void AliTPCDigitizer::ExecSave(Option_t* option)
358{
359
360 // merge input tree's with summable digits
361 //output stored in TreeTPCD
362
363 TString optionString = option;
94bf739c 364 if (!strcmp(optionString.Data(),"deb")) {
f648982e 365 cout<<"AliTPCDigitizer::Exec: called with option deb "<<endl;
366 fDebug = 3;
367 }
368 //get detector and geometry
88cb7938 369 AliRunLoader *rl, *orl;
370 AliLoader *gime, *ogime;
371
372
373 orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
374 ogime = orl->GetLoader("TPCLoader");
375
376 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
377 gime = rl->GetLoader("TPCLoader");
378
379 rl->LoadgAlice();
380 AliRun* alirun = rl->GetAliRun();
381
382 AliTPC *pTPC = (AliTPC *) alirun->GetModule("TPC");
f648982e 383 AliTPCParam * param = pTPC->GetParam();
384 pTPC->GenerNoise(500000); //create teble with noise
385 printf("noise %f \n", param->GetNoise()*param->GetNoiseNormFac());
386 //
387 Int_t nInputs = fManager->GetNinputs();
388 Int_t * masks = new Int_t[nInputs];
389 for (Int_t i=0; i<nInputs;i++)
390 masks[i]= fManager->GetMask(i);
f648982e 391
f648982e 392 AliSimDigits ** digarr = new AliSimDigits*[nInputs];
88cb7938 393 for (Int_t i1=0;i1<nInputs; i1++)
394 {
f648982e 395 digarr[i1]=0;
396 // intree[i1]
88cb7938 397 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i1));
398 gime = rl->GetLoader("TPCLoader");
399
400 TTree * treear = gime->TreeS();
f648982e 401 TBranch * br = treear->GetBranch("fSegmentID");
402 if (br) br->GetFile()->cd();
f648982e 403 if (!treear) {
404 cerr<<" TPC - not existing input = \n"<<i1<<" ";
405 }
f648982e 406 treear->GetBranch("Segment")->SetAddress(&digarr[i1]);
f648982e 407 }
88cb7938 408
409 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
410 gime = rl->GetLoader("TPCLoader");
411 Stat_t nentries = gime->TreeS()->GetEntries();
f648982e 412
413
414 //create branch's in TPC treeD
415 AliSimDigits * digrow = new AliSimDigits;
88cb7938 416 TTree * tree = ogime->TreeD();
417
f648982e 418 tree->Branch("Segment","AliSimDigits",&digrow);
f648982e 419 param->SetZeroSup(2);
420
421 Int_t zerosup = param->GetZeroSup();
422 //Loop over segments of the TPC
423
5033a39a 424 AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetDedxGainFactor();
1ac191a6 425 AliTPCCalPad * noiseTPC = AliTPCcalibDB::Instance()->GetPadNoise();
f648982e 426 for (Int_t n=0; n<nentries; n++) {
88cb7938 427 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
428 gime = rl->GetLoader("TPCLoader");
429 gime->TreeS()->GetEvent(n);
430
f648982e 431 digarr[0]->ExpandBuffer();
432 digarr[0]->ExpandTrackBuffer();
13116aec 433
434
f648982e 435 for (Int_t i=1;i<nInputs; i++){
88cb7938 436// fManager->GetInputTreeTPCS(i)->GetEntryWithIndex(digarr[0]->GetID(),digarr[0]->GetID());
437 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
438 gime = rl->GetLoader("TPCLoader");
439 gime->TreeS()->GetEntryWithIndex(digarr[0]->GetID(),digarr[0]->GetID());
f648982e 440 digarr[i]->ExpandBuffer();
441 digarr[i]->ExpandTrackBuffer();
442 if ((digarr[0]->GetID()-digarr[i]->GetID())>0)
88cb7938 443 printf("problem\n");
f648982e 444
445 }
446
447 Int_t sec, row;
448 if (!param->AdjustSectorRow(digarr[0]->GetID(),sec,row)) {
449 cerr<<"AliTPC warning: invalid segment ID ! "<<digarr[0]->GetID()<<endl;
450 continue;
451 }
452
13116aec 453 AliTPCCalROC * gainROC = gainTPC->GetCalROC(sec); // pad gains per given sector
1ac191a6 454 AliTPCCalROC * noiseROC = noiseTPC->GetCalROC(sec); // noise per given sector
f648982e 455 digrow->SetID(digarr[0]->GetID());
456
457 Int_t nrows = digarr[0]->GetNRows();
458 Int_t ncols = digarr[0]->GetNCols();
459 digrow->Allocate(nrows,ncols);
460 digrow->AllocateTrack(3);
461
462 Float_t q=0;
463 Int_t label[1000]; //stack for 300 events
464 Int_t labptr = 0;
465
466
467
468 for (Int_t rows=0;rows<nrows; rows++){
469 for (Int_t col=0;col<ncols; col++){
470
88cb7938 471 q=0;
472 labptr=0;
473 // looop over digits
f648982e 474 for (Int_t i=0;i<nInputs; i++){
88cb7938 475 q += digarr[i]->GetDigitFast(rows,col);
f648982e 476 //q += *(pdig[i]);
88cb7938 477
f648982e 478 for (Int_t tr=0;tr<3;tr++) {
88cb7938 479 Int_t lab = digarr[i]->GetTrackIDFast(rows,col,tr);
480 //Int_t lab = ptr[i][tr*nElems];
f648982e 481 if ( (lab > 1) ) {
482 label[labptr]=lab+masks[i];
483 labptr++;
88cb7938 484 }
f648982e 485 }
88cb7938 486 // pdig[i]++;
487 //ptr[i]++;
488
f648982e 489 }
88cb7938 490 q/=16.; //conversion factor
491 // Float_t noise = gRandom->Gaus(0,param->GetNoise()*param->GetNoiseNormFac());
13116aec 492 Float_t gain = gainROC->GetValue(row,col);
493 q*= gain;
1ac191a6 494 Float_t noisePad = noiseROC->GetValue(row, col);
495
88cb7938 496 Float_t noise = pTPC->GetNoise();
1ac191a6 497 q+=noise*noisePad;
633fa37f 498
f648982e 499 q=TMath::Nint(q);
500 if (q > zerosup){
88cb7938 501
e93a083a 502 if(q >= param->GetADCSat()) q = (Short_t)(param->GetADCSat() - 1);
88cb7938 503 digrow->SetDigitFast((Short_t)q,rows,col);
504 // *pdig1 =Short_t(q);
505 for (Int_t tr=0;tr<3;tr++){
506 if (tr<labptr)
507 ((AliSimDigits*)digrow)->SetTrackIDFast(label[tr],rows,col,tr);
508 //ptr1[tr*nElems] = label[tr];
509 //else
510 // ((AliSimDigits*)digrow)->SetTrackIDFast(-1,rows,col,tr);
511 // ptr1[tr*nElems] = 1;
512 }
513 }
514 //pdig1++;
515 //ptr1++;
f648982e 516 }
517 }
518
519 digrow->CompresBuffer(1,zerosup);
520 digrow->CompresTrackBuffer(1);
521 tree->Fill();
522 if (fDebug>0) cerr<<sec<<"\t"<<row<<"\n";
523 }
88cb7938 524// printf("end TPC merging - end -Tree %s\t%p\n",fManager->GetInputTreeH(0)->GetName(),fManager->GetInputTreeH(0)->GetListOfBranches()->At(3));
525 //fManager->GetTreeDTPC()->Write(0,TObject::kOverwrite);
526 ogime->WriteDigits("OVERWRITE");
527
528 for (Int_t i=1;i<nInputs; i++)
529 {
530 rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
531 gime = rl->GetLoader("TPCLoader");
532 gime->UnloadSDigits();
533 }
534 ogime->UnloadDigits();
535
f648982e 536 delete digrow;
537 for (Int_t i1=0;i1<nInputs; i1++) delete digarr[i1];
4ce766eb 538 delete [] masks;
539 delete [] digarr;
f648982e 540}