]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFDigitizer.cxx
Classes for reading raw data moved to the RAW module. New on-line MONITORING module...
[u/mrichter/AliRoot.git] / TOF / AliTOFDigitizer.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 // This is a TTask that makes TOF-Digits out of TOF-SDigits. 
18 // The simulation of the detector is performed at sdigits level:
19 // during digitization the unique task is the sum of all sdigits in the
20 // same pad.
21 // Digits are written to TreeD in branch "TOF".
22 //
23 // -- Author :  F. Pierella (Bologna University) pierella@bo.infn.it
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include <TTree.h> 
27 #include <TVector.h>
28 #include <TObjArray.h>
29 #include <TFile.h>
30 #include <TDirectory.h>
31 #include <TRandom.h>
32
33
34 #include "AliTOFDigitizer.h"
35 #include "AliTOF.h"
36 #include "AliTOFSDigitizer.h"
37 #include "AliTOFhit.h"
38 #include "AliTOFdigit.h"
39 #include "AliTOFSDigit.h"
40 #include "AliTOFHitMap.h"
41 #include "AliDigitizer.h"
42 #include "AliRunDigitizer.h"
43
44 #include "AliRun.h"
45 #include "AliRunLoader.h"
46 #include "AliLoader.h"
47 #include "AliPDG.h"
48
49 #include <stdlib.h>
50 #include <Riostream.h>
51
52 ClassImp(AliTOFDigitizer)
53
54 //___________________________________________
55   AliTOFDigitizer::AliTOFDigitizer()  :AliDigitizer()
56 {
57   // Default ctor - don't use it
58   fDigits=0;
59   fSDigitsArray=0;
60   fhitMap=0;
61 }
62
63 //___________________________________________
64 AliTOFDigitizer::AliTOFDigitizer(AliRunDigitizer* manager) 
65     :AliDigitizer(manager) 
66 {
67   fDigits=0;
68   fSDigitsArray=0;
69   fhitMap=0;
70 }
71
72 //------------------------------------------------------------------------
73 AliTOFDigitizer::~AliTOFDigitizer()
74 {
75   // Destructor
76 }
77
78 //---------------------------------------------------------------------
79
80 void AliTOFDigitizer::Exec(Option_t* option)
81 {
82   //
83   // Perform digitization and merging.
84   // The algorithm is the following:
85   // - a hitmap is created to check if a pad is already activated;
86   // - an sdigits container is created to collect all sdigits from
87   //   different files;
88   // - sdigits are summed using the hitmap;
89   // - the sdigits container is used to create the array of AliTOFdigit.
90   //
91
92   if(strstr(option,"deb")) cout<<"AliTOFDigitizer::Exec\n";
93
94
95   // get the ptr to TOF detector
96   AliTOF * tof = (AliTOF *) gAlice->GetDetector("TOF") ;
97
98   //Make branches
99   char branchname[20];
100   sprintf (branchname, "%s", tof->GetName ());
101
102   fDigits=new TClonesArray("AliTOFdigit",4000);
103  
104   AliRunLoader* outrl = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
105   if (outrl == 0x0)
106    {
107      Error("Exec","Can not find Run Loader in output folder.");
108      return;
109    }
110    
111   AliLoader* outgime = outrl->GetLoader("TOFLoader");
112   if (outgime == 0x0)
113    {
114      Error("Exec","Can not get TOF Loader from Output Run Loader.");
115      return;
116    }
117   
118   TTree* treeD = outgime->TreeD();
119   if (treeD == 0x0)
120    {
121      outgime->MakeTree("D");
122      treeD = outgime->TreeD();
123    }
124   //Make branch for digits (to be created in Init())
125   tof->MakeBranchInTree(treeD,branchname,&fDigits,4000);
126
127   // container for all summed sdigits (to be created in Init())
128   fSDigitsArray=new TClonesArray("AliTOFSDigit",1000);
129   
130   // create hit map (to be created in Init())
131   fhitMap = new AliTOFHitMap(fSDigitsArray);
132   
133   // Loop over files to digitize
134
135   for (Int_t inputFile=0; inputFile<fManager->GetNinputs();
136        inputFile++) {
137     ReadSDigit(inputFile);
138    }
139
140   // create digits
141   CreateDigits();
142
143   // free used memory for Hit Map in current event
144   delete fhitMap;
145   fSDigitsArray->Delete();
146   treeD->Fill();
147  
148   outgime->WriteDigits("OVERWRITE");
149   outgime->UnloadDigits();
150   fDigits->Delete();
151   delete fDigits;
152
153 }
154
155 //---------------------------------------------------------------------
156
157 void AliTOFDigitizer::CreateDigits()
158 {
159   // loop on sdigits container to fill the AliTOFdigit TClonesArray
160   // start digitizing all the collected sdigits 
161
162   Int_t ndump=15; // dump the first ndump created digits for each event
163
164   // get the total number of collected sdigits
165   Int_t ndig = fSDigitsArray->GetEntriesFast();
166
167   for (Int_t k = 0; k < ndig; k++) {
168     
169     Int_t    vol[5];       // location for a digit
170     
171     // Get the information for this digit
172     AliTOFSDigit *tofsdigit = (AliTOFSDigit *) fSDigitsArray->UncheckedAt(k);
173     
174     Int_t nslot=tofsdigit->GetNDigits(); // get the number of slots
175     // for current sdigit
176     
177     // TOF sdigit volumes (always the same for all slots)
178     Int_t sector    = tofsdigit->GetSector(); // range [1-18]
179     Int_t plate     = tofsdigit->GetPlate();  // range [1- 5]
180     Int_t strip     = tofsdigit->GetStrip();  // range [1-20]
181     Int_t padz      = tofsdigit->GetPadz();   // range [1- 2]
182     Int_t padx      = tofsdigit->GetPadx();   // range [1-48]
183     
184     vol[0] = sector;
185     vol[1] = plate;
186     vol[2] = strip;
187     vol[3] = padx;
188     vol[4] = padz;
189     
190     //--------------------- QA section ----------------------
191     // in the while, I perform QA
192     Bool_t isSDigitBad = (sector<1 || sector>18 || plate<1 || plate >5 || padz<1 || padz>2 || padx<1 || padx>48);
193     
194     if (isSDigitBad) {
195       cout << "<AliTOFSDigits2Digits>  strange sdigit found" << endl;
196       abort();
197     }
198     //-------------------------------------------------------
199     
200     //------------------- Dump section ----------------------
201     if(k<ndump){
202       cout << k << "-th | " << "Sector " << sector << " | Plate " << plate << " | Strip " << strip << " | PadZ " << padz << " | PadX " << padx << endl;
203       cout << k << "-th sdigit" << endl;
204       cout << "----------------------------------------------------"<< endl;
205     }
206     // ------------------------------------------------------
207     
208     // start loop on number of slots for current sdigit
209     for (Int_t islot = 0; islot < nslot; islot++) {
210       Float_t  digit[2];     // TOF digit variables
211       Int_t tracknum[kMAXDIGITS];     // contributing tracks for the current slot
212       
213       Float_t tdc=tofsdigit->GetTdc(islot); digit[0]=tdc;
214       Float_t adc=tofsdigit->GetAdc(islot); digit[1]=adc;
215       
216       tracknum[0]=tofsdigit->GetTrack(islot,0);
217       tracknum[1]=tofsdigit->GetTrack(islot,1);
218       tracknum[2]=tofsdigit->GetTrack(islot,2);
219       
220       // new with placement must be used
221       // adding a TOF digit for each slot
222       TClonesArray &aDigits = *fDigits;
223       Int_t last=fDigits->GetEntriesFast();
224       new (aDigits[last]) AliTOFdigit(tracknum, vol, digit);
225
226     }
227     
228   } // end loop on sdigits - end digitizing all collected sdigits
229
230 }
231
232 //---------------------------------------------------------------------
233
234 void AliTOFDigitizer::ReadSDigit(Int_t inputFile )
235 {
236   // Read sdigits for current event and inputFile; 
237   // store them into the sdigits container
238   // and update the hit map
239   // SDigits from different files are assumed to
240   // be created with the same simulation parameters.
241   
242   // get the treeS from manager
243   AliRunLoader* rl = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
244   if (rl == 0x0)
245    {
246      Error("ReadSDigit","Can not find Run Loader in input %d folder.",inputFile);
247      return;
248    }
249
250   AliLoader* gime = rl->GetLoader("TOFLoader");
251   if (gime == 0x0)
252    {
253      Error("ReadSDigit","Can not get TOF Loader from Input %d Run Loader.",inputFile);
254      return;
255    }
256
257   TTree* currentTreeS=gime->TreeS();
258   if (currentTreeS == 0x0)
259    {
260      Int_t retval = gime->LoadSDigits();
261      if (retval) 
262       {
263          Error("ReadSDigit","Error occured while loading S. Digits for Input %d",inputFile);
264          return;
265       }
266      currentTreeS=gime->TreeS();
267      if (currentTreeS == 0x0)
268       {
269          Error("ReadSDigit","Can not get S. Digits Tree for Input %d",inputFile);
270          return;
271       }
272    } 
273   // get the branch TOF inside the treeS
274   TClonesArray * sdigitsDummyContainer= new TClonesArray("AliTOFSDigit",  1000); 
275
276   // check if the branch exist
277   TBranch* tofBranch=currentTreeS->GetBranch("TOF");
278
279   if(!tofBranch){
280     Fatal("ReadSDigit","TOF branch not found for input %d",inputFile);
281   }
282   
283   tofBranch->SetAddress(&sdigitsDummyContainer);           
284   
285   Int_t nEntries = (Int_t)tofBranch->GetEntries();                                
286
287   // Loop through all entries in the tree
288   Int_t nbytes;
289   
290   for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
291     
292     // Import the tree
293     nbytes += tofBranch->GetEvent(iEntry);
294     
295     // Get the number of sdigits
296     Int_t ndig = sdigitsDummyContainer->GetEntriesFast();
297     
298     for (Int_t k=0; k<ndig; k++) {
299       AliTOFSDigit *tofSdigit= (AliTOFSDigit*) sdigitsDummyContainer->UncheckedAt(k);
300       
301       Int_t    vol[5];       // location for a sdigit
302       
303       // check the sdigit volume
304       vol[0] = tofSdigit->GetSector();
305       vol[1] = tofSdigit->GetPlate();
306       vol[2] = tofSdigit->GetStrip();
307       vol[3] = tofSdigit->GetPadx();
308       vol[4] = tofSdigit->GetPadz();
309       
310       if (fhitMap->TestHit(vol) != kEmpty) {
311         AliTOFSDigit *sdig = static_cast<AliTOFSDigit*>(fhitMap->GetHit(vol));
312         sdig->Update(tofSdigit);
313
314       } else {
315
316         CollectSDigit(tofSdigit); // collect the current sdigit
317         fhitMap->SetHit(vol);     // update the hitmap for location vol
318
319       } // if (hitMap->TestHit(vol) != kEmpty)
320       
321     } // for (Int_t k=0; k<ndig; k++)
322
323   } // end loop on entries
324
325   sdigitsDummyContainer->Delete();
326   sdigitsDummyContainer=0;
327
328 }
329
330
331 //_____________________________________________________________________________
332 void AliTOFDigitizer::CollectSDigit(AliTOFSDigit * sdigit)
333 {
334   //
335   // Add a TOF sdigit in container
336   // new with placement must be used
337   TClonesArray &aSDigitsArray = *fSDigitsArray;
338   Int_t last=fSDigitsArray->GetEntriesFast();
339   // make a copy of the current sdigit and
340   // put it into tmp array
341   new (aSDigitsArray[last]) AliTOFSDigit(*sdigit);
342 }