]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCDigitizer.cxx
Adapt macro to read old SDD map format
[u/mrichter/AliRoot.git] / TPC / AliTPCDigitizer.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 /* $Id$ */
17
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
35 #include <stdlib.h>
36 #include <TTree.h> 
37 #include <TObjArray.h>
38 #include <TFile.h>
39 #include <TDirectory.h>
40 #include <Riostream.h>
41 #include <TParameter.h>
42
43 #include "AliTPCDigitizer.h"
44
45 #include "AliTPC.h"
46 #include "AliTPCParam.h"
47 #include "AliTPCParamSR.h" 
48 #include "AliRun.h"
49 #include "AliPDG.h"
50 #include "AliRunDigitizer.h"
51 #include "AliSimDigits.h"
52 #include "AliLog.h"
53
54 #include "AliTPCcalibDB.h"
55 #include "AliTPCCalPad.h"
56 #include "AliTPCCalROC.h"
57
58 ClassImp(AliTPCDigitizer)
59
60 //___________________________________________
61   AliTPCDigitizer::AliTPCDigitizer() :AliDigitizer(),fDebug(0)
62 {
63   //
64 // Default ctor - don't use it
65 //
66   
67 }
68
69 //___________________________________________
70 AliTPCDigitizer::AliTPCDigitizer(AliRunDigitizer* manager) 
71   :AliDigitizer(manager),fDebug(0)
72 {
73   //
74 // ctor which should be used
75 //  
76   AliDebug(2,"(AliRunDigitizer* manager) was processed");
77 }
78
79 //------------------------------------------------------------------------
80 AliTPCDigitizer::~AliTPCDigitizer()
81 {
82 // Destructor
83 }
84
85
86
87 //------------------------------------------------------------------------
88 Bool_t AliTPCDigitizer::Init()
89 {
90 // Initialization 
91     
92  return kTRUE;
93 }
94
95
96 //------------------------------------------------------------------------
97 void AliTPCDigitizer::Exec(Option_t* option)
98 {
99   ExecFast(option);  
100 }
101 //------------------------------------------------------------------------
102 void AliTPCDigitizer::ExecFast(Option_t* option)
103 {
104   
105   // merge input tree's with summable digits
106   //output stored in TreeTPCD
107   char s[100]; 
108   char ss[100];
109   TString optionString = option;
110   if (!strcmp(optionString.Data(),"deb")) {
111     cout<<"AliTPCDigitizer::Exec: called with option deb "<<endl;
112     fDebug = 3;
113   }
114   //get detector and geometry
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    }
132   AliTPC *pTPC  = (AliTPC *) gAlice->GetModule("TPC");
133   AliTPCParam * param = pTPC->GetParam();
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   
150   pTPC->GenerNoise(500000); //create table with noise
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
157   Int_t **ptr=  new Int_t*[nInputs];       //pointers to the expanded tracks array
158   Bool_t *active=  new Bool_t[nInputs];    //flag for active input segments
159   Char_t phname[100];
160   
161   //create digits array for given sectors
162   // make indexes
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   //  
177   AliSimDigits ** digarr = new AliSimDigits*[nInputs]; 
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         for (Int_t i2=0;i2<i1+1; i2++){ 
192           
193           if(digarr[i2])  delete digarr[i2];
194         }
195         delete [] digarr;
196         delete [] active;
197         delete []masks;
198         delete []pdig;
199         delete []ptr;
200         return;
201        }
202
203       sprintf(phname,"lhcphase%d",i1);
204       TParameter<float> *ph = (TParameter<float>*)treear->GetUserInfo()
205                                ->FindObject("lhcphase0");
206       if(!ph){
207         cerr<<"AliTPCDigitizer: LHC phase  not found in"
208             <<" input "<< i1<<endl;
209         for (Int_t i2=0;i2<i1+1; i2++){ 
210           if(digarr[i2])  delete digarr[i2];
211         }
212         delete [] digarr;
213         delete [] active;
214         delete []masks;
215         delete []pdig;
216         delete []ptr;
217         return;
218       }
219       tree->GetUserInfo()->Add(new TParameter<float>(phname,ph->GetVal()));
220               //
221       if (treear->GetIndex()==0)  
222         treear->BuildIndex("fSegmentID","fSegmentID");
223       treear->GetBranch("Segment")->SetAddress(&digarr[i1]);
224     }
225
226
227
228
229   //
230
231   param->SetZeroSup(2);
232
233   Int_t zerosup = param->GetZeroSup(); 
234   AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetDedxGainFactor(); 
235   AliTPCCalPad * noiseTPC = AliTPCcalibDB::Instance()->GetPadNoise(); 
236   //
237   //Loop over segments of the TPC
238     
239   for (Int_t segmentID=0; segmentID<param->GetNRowsTotal(); segmentID++) 
240    {
241     Int_t sec, row;
242     if (!param->AdjustSectorRow(segmentID,sec,row)) 
243      {
244       cerr<<"AliTPC warning: invalid segment ID ! "<<segmentID<<endl;
245       continue;
246      }
247     AliTPCCalROC * gainROC = gainTPC->GetCalROC(sec);  // pad gains per given sector
248     AliTPCCalROC * noiseROC = noiseTPC->GetCalROC(sec);  // noise per given sector
249     digrow->SetID(segmentID);
250
251     Int_t nrows = 0;
252     Int_t ncols = 0;
253
254     Bool_t digitize = kFALSE;
255     for (Int_t i=0;i<nInputs; i++) 
256      { 
257
258       rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
259       gime = rl->GetLoader("TPCLoader");
260       
261       if (gime->TreeS()->GetEntryWithIndex(segmentID,segmentID) >= 0) {
262         digarr[i]->ExpandBuffer();
263         digarr[i]->ExpandTrackBuffer();
264         nrows = digarr[i]->GetNRows();
265         ncols = digarr[i]->GetNCols();
266         active[i] = kTRUE;
267         if (!fRegionOfInterest || (i == 0)) digitize = kTRUE;
268       } else {
269         active[i] = kFALSE;
270       }
271       if (fRegionOfInterest && !digitize) break;
272      }   
273     if (!digitize) continue;
274
275     digrow->Allocate(nrows,ncols);
276     digrow->AllocateTrack(3);
277
278     Float_t q=0;
279     Int_t label[1000]; //stack for 300 events 
280     Int_t labptr = 0;
281
282     Int_t nElems = nrows*ncols;     
283  
284     for (Int_t i=0;i<nInputs; i++)
285      if (active[i]) { 
286        pdig[i] = digarr[i]->GetDigits();
287        ptr[i]  = digarr[i]->GetTracks();
288       }
289      
290     Short_t *pdig1= digrow->GetDigits();
291     Int_t   *ptr1= digrow->GetTracks() ;
292
293     
294
295     for (Int_t elem=0;elem<nElems; elem++)
296      {    
297
298        q=0;
299        labptr=0;
300        // looop over digits 
301         for (Int_t i=0;i<nInputs; i++) if (active[i]) 
302          { 
303           //          q  += digarr[i]->GetDigitFast(rows,col);
304             q  += *(pdig[i]);
305          
306            for (Int_t tr=0;tr<3;tr++) 
307             {
308              //             Int_t lab = digarr[i]->GetTrackIDFast(rows,col,tr);
309              Int_t lab = ptr[i][tr*nElems];
310              if ( (lab > 1) && *(pdig[i])>zerosup) 
311               {
312                 label[labptr]=lab+masks[i];
313                 labptr++;
314               }          
315             }
316            pdig[i]++;
317            ptr[i]++;
318          }
319         q/=16.;  //conversion factor
320         Float_t gain = gainROC->GetValue(row,elem/nrows);  // get gain for given - pad-row pad
321         //if (gain<0.5){
322           //printf("problem\n");
323         //}
324         q*= gain;
325         Float_t noisePad = noiseROC->GetValue(row,elem/nrows);
326         //       Float_t noise  = gRandom->Gaus(0,param->GetNoise()*param->GetNoiseNormFac());  
327         Float_t noise  = pTPC->GetNoise();
328         q+=noise*noisePad;
329         q=TMath::Nint(q);
330         if (q > zerosup)
331          { 
332           if(q >= param->GetADCSat()) q = (Short_t)(param->GetADCSat() - 1);
333           //digrow->SetDigitFast((Short_t)q,rows,col);  
334           *pdig1 =Short_t(q);
335           for (Int_t tr=0;tr<3;tr++)
336            {
337             if (tr<labptr) 
338              ptr1[tr*nElems] = label[tr];
339            }
340           }
341         pdig1++;
342         ptr1++;
343      }
344     //
345     //  glitch filter
346     //
347     digrow->GlitchFilter();
348     //
349     digrow->CompresBuffer(1,zerosup);
350     digrow->CompresTrackBuffer(1);
351     tree->Fill();
352     if (fDebug>0) cerr<<sec<<"\t"<<row<<"\n";  
353    } //for (Int_t n=0; n<param->GetNRowsTotal(); n++) 
354   
355
356   orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
357   ogime = orl->GetLoader("TPCLoader");
358   ogime->WriteDigits("OVERWRITE");
359   
360   //fManager->GetTreeDTPC()->Write(0,TObject::kOverwrite);
361   
362   delete digrow;     
363   for (Int_t i1=0;i1<nInputs; i1++) delete digarr[i1];
364   delete []masks;
365   delete []pdig;
366   delete []ptr;
367   delete []active;
368   delete []digarr;  
369 }
370
371
372
373 //------------------------------------------------------------------------
374 void AliTPCDigitizer::ExecSave(Option_t* option)
375 {
376   
377   // merge input tree's with summable digits
378   //output stored in TreeTPCD
379
380   TString optionString = option;
381   if (!strcmp(optionString.Data(),"deb")) {
382     cout<<"AliTPCDigitizer::Exec: called with option deb "<<endl;
383     fDebug = 3;
384   }
385   //get detector and geometry 
386   AliRunLoader *rl, *orl;
387   AliLoader *gime, *ogime;
388
389   
390   orl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
391   ogime = orl->GetLoader("TPCLoader");
392   
393   rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
394   gime = rl->GetLoader("TPCLoader");
395   
396   rl->LoadgAlice();
397   AliRun* alirun = rl->GetAliRun();
398   
399   AliTPC *pTPC  = (AliTPC *) alirun->GetModule("TPC");
400   AliTPCParam * param = pTPC->GetParam();
401   pTPC->GenerNoise(500000); //create teble with noise
402   printf("noise %f \n",  param->GetNoise()*param->GetNoiseNormFac());
403   //
404   Int_t nInputs = fManager->GetNinputs();
405   Int_t * masks = new Int_t[nInputs];
406   for (Int_t i=0; i<nInputs;i++)
407     masks[i]= fManager->GetMask(i);
408
409   //AliSimDigits ** digarr = new AliSimDigits*[nInputs]; 
410   AliSimDigits ** digarr =0x0;
411   digarr = new AliSimDigits*[nInputs];
412   for (Int_t i1=0;i1<nInputs; i1++)
413    {
414     digarr[i1]=0;
415     //    intree[i1]
416     rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i1));
417     gime = rl->GetLoader("TPCLoader");
418
419     TTree * treear =  gime->TreeS();
420     TBranch * br = treear->GetBranch("fSegmentID");
421     if (br) br->GetFile()->cd();
422     if (!treear) {      
423       cerr<<" TPC -  not existing input = \n"<<i1<<" ";      
424     } 
425     treear->GetBranch("Segment")->SetAddress(&digarr[i1]);
426   }
427   
428   rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
429   gime = rl->GetLoader("TPCLoader");
430   Stat_t nentries = gime->TreeS()->GetEntries();
431   
432
433   //create branch's in TPC treeD
434   AliSimDigits * digrow = new AliSimDigits;
435   TTree * tree  = ogime->TreeD();
436
437   tree->Branch("Segment","AliSimDigits",&digrow);
438   param->SetZeroSup(2);
439
440   Int_t zerosup = param->GetZeroSup();
441   //Loop over segments of the TPC
442     
443   AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetDedxGainFactor();
444   AliTPCCalPad * noiseTPC = AliTPCcalibDB::Instance()->GetPadNoise();
445   for (Int_t n=0; n<nentries; n++) {
446     rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(0));
447     gime = rl->GetLoader("TPCLoader");
448     gime->TreeS()->GetEvent(n);
449
450     digarr[0]->ExpandBuffer();
451     digarr[0]->ExpandTrackBuffer();
452
453
454     for (Int_t i=1;i<nInputs; i++){ 
455 //      fManager->GetInputTreeTPCS(i)->GetEntryWithIndex(digarr[0]->GetID(),digarr[0]->GetID());      
456       rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
457       gime = rl->GetLoader("TPCLoader");
458       gime->TreeS()->GetEntryWithIndex(digarr[0]->GetID(),digarr[0]->GetID());  
459       digarr[i]->ExpandBuffer();
460       digarr[i]->ExpandTrackBuffer();
461       if ((digarr[0]->GetID()-digarr[i]->GetID())>0) 
462        printf("problem\n");
463     
464     }   
465     
466     Int_t sec, row;
467     if (!param->AdjustSectorRow(digarr[0]->GetID(),sec,row)) {
468       cerr<<"AliTPC warning: invalid segment ID ! "<<digarr[0]->GetID()<<endl;
469       continue;
470     }
471
472     AliTPCCalROC * gainROC = gainTPC->GetCalROC(sec);  // pad gains per given sector
473     AliTPCCalROC * noiseROC = noiseTPC->GetCalROC(sec);  // noise per given sector
474     digrow->SetID(digarr[0]->GetID());
475
476     Int_t nrows = digarr[0]->GetNRows();
477     Int_t ncols = digarr[0]->GetNCols();
478     digrow->Allocate(nrows,ncols);
479     digrow->AllocateTrack(3);
480
481     Float_t q=0;
482     Int_t label[1000]; //stack for 300 events 
483     Int_t labptr = 0;
484
485     
486
487     for (Int_t rows=0;rows<nrows; rows++){
488       for (Int_t col=0;col<ncols; col++){
489     
490        q=0;
491        labptr=0;
492        // looop over digits 
493         for (Int_t i=0;i<nInputs; i++){ 
494          q  += digarr[i]->GetDigitFast(rows,col);
495           //q  += *(pdig[i]);
496          
497           for (Int_t tr=0;tr<3;tr++) {
498            Int_t lab = digarr[i]->GetTrackIDFast(rows,col,tr);
499            //Int_t lab = ptr[i][tr*nElems];
500             if ( (lab > 1) ) {
501               label[labptr]=lab+masks[i];
502               labptr++;
503             }          
504           }
505          // pdig[i]++;
506          //ptr[i]++;
507          
508         }
509        q/=16.;  //conversion factor
510        //       Float_t noise  = gRandom->Gaus(0,param->GetNoise()*param->GetNoiseNormFac());  
511        Float_t gain = gainROC->GetValue(row,col);
512        q*= gain;
513        Float_t noisePad = noiseROC->GetValue(row, col);
514
515        Float_t noise  = pTPC->GetNoise();
516        q+=noise*noisePad;
517
518         q=TMath::Nint(q);
519         if (q > zerosup){ 
520          
521          if(q >= param->GetADCSat()) q = (Short_t)(param->GetADCSat() - 1);
522          digrow->SetDigitFast((Short_t)q,rows,col);  
523          // *pdig1 =Short_t(q);
524          for (Int_t tr=0;tr<3;tr++){
525            if (tr<labptr) 
526              ((AliSimDigits*)digrow)->SetTrackIDFast(label[tr],rows,col,tr);
527            //ptr1[tr*nElems] = label[tr];
528            //else
529              //           ((AliSimDigits*)digrow)->SetTrackIDFast(-1,rows,col,tr);          
530            //  ptr1[tr*nElems] = 1;
531          }
532        }
533        //pdig1++;
534        //ptr1++;
535     }
536     }
537     
538     digrow->CompresBuffer(1,zerosup);
539     digrow->CompresTrackBuffer(1);
540     tree->Fill();
541     if (fDebug>0) cerr<<sec<<"\t"<<row<<"\n";  
542   } 
543 //  printf("end TPC merging - end -Tree %s\t%p\n",fManager->GetInputTreeH(0)->GetName(),fManager->GetInputTreeH(0)->GetListOfBranches()->At(3));
544   //fManager->GetTreeDTPC()->Write(0,TObject::kOverwrite);
545     ogime->WriteDigits("OVERWRITE");
546
547     for (Int_t i=1;i<nInputs; i++) 
548      { 
549       rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(i));
550       gime = rl->GetLoader("TPCLoader");
551       gime->UnloadSDigits();
552      }
553     ogime->UnloadDigits();
554     
555   delete digrow;     
556   for (Int_t i1=0;i1<nInputs; i1++) delete digarr[i1];
557   delete [] masks;
558   delete [] digarr;  
559 }