]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDBaseDA.cxx
adding common functionality for the magnetic field to the component interface
[u/mrichter/AliRoot.git] / FMD / AliFMDBaseDA.cxx
1 /**************************************************************************
2  * Copyright(c) 2004, 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 /** @file    AliFMDBaseDA.cxx
17     @author  Hans Hjersing Dalsgaard <canute@nbi.dk>
18     @date    Wed Mar 26 11:30:45 2008
19     @brief   Base class for detector algorithms.
20 */
21 //
22 // This is the implementation of the (virtual) base class for the FMD
23 // detector algorithms(DA). It implements the creation of the relevant
24 // containers and handles the loop over the raw data. The derived
25 // classes can control the parameters and action to be taken making
26 // this the base class for the Pedestal, Gain and Physics DA.
27 //
28
29 #include "AliFMDBaseDA.h"
30 #include "iostream"
31 #include "AliFMDRawReader.h"
32 #include "AliFMDCalibSampleRate.h"
33 #include "AliFMDCalibStripRange.h"
34 #include "AliLog.h"
35 #include "AliRawEventHeaderBase.h"
36
37 //_____________________________________________________________________
38 ClassImp(AliFMDBaseDA)
39 #if 0 
40 ; // Do not delete  - to let Emacs for mat the code
41 #endif
42
43 //_____________________________________________________________________
44 const char*
45 AliFMDBaseDA::GetStripPath(UShort_t det, 
46                            Char_t   ring, 
47                            UShort_t sec, 
48                            UShort_t str, 
49                            Bool_t   full) const
50 {
51   return Form("%s%sFMD%d%c[%02d,%03d]", 
52               (full ? GetSectorPath(det, ring, sec, full) : ""), 
53               (full ? "/" : ""), det, ring, sec, str);
54 }
55 //_____________________________________________________________________
56 const char*
57 AliFMDBaseDA::GetSectorPath(UShort_t det, 
58                             Char_t   ring, 
59                             UShort_t sec, 
60                             Bool_t   full) const
61 {
62   return Form("%s%sFMD%d%c[%02d]", 
63               (full ? GetRingPath(det, ring, full) : ""), 
64               (full ? "/" : ""), det, ring, sec);
65 }
66 //_____________________________________________________________________
67 const char*
68 AliFMDBaseDA::GetRingPath(UShort_t det, 
69                           Char_t   ring, 
70                           Bool_t   full) const
71 {
72   return Form("%s%sFMD%d%c", 
73               (full ? GetDetectorPath(det, full) : ""), 
74               (full ? "/" : ""), det, ring);
75 }
76 //_____________________________________________________________________
77 const char*
78 AliFMDBaseDA::GetDetectorPath(UShort_t det, 
79                               Bool_t   full) const
80 {
81   return Form("%s%sFMD%d", 
82               (full ? fDiagnosticsFilename.Data() : ""), 
83               (full ? ":/" : ""), det);
84 }
85
86 //_____________________________________________________________________
87 AliFMDBaseDA::AliFMDBaseDA() : 
88   TNamed(),
89   fDiagnosticsFilename("diagnosticsHistograms.root"),
90   fOutputFile(),
91   fConditionsFile(),
92   fSaveHistograms(kFALSE),
93   fDetectorArray(),
94   fPulseSize(10),
95   fPulseLength(10),
96   fRequiredEvents(0),
97   fCurrentEvent(0)
98  {
99    fSeenDetectors[0] = fSeenDetectors[1] = fSeenDetectors[2] = kFALSE;
100   fDetectorArray.SetOwner();
101   fConditionsFile.open("conditions.csv");
102 }
103 //_____________________________________________________________________
104 AliFMDBaseDA::AliFMDBaseDA(const AliFMDBaseDA & baseDA) : 
105   TNamed(baseDA),
106   fDiagnosticsFilename(baseDA.fDiagnosticsFilename.Data()),
107   fOutputFile(),
108   fConditionsFile(),
109   fSaveHistograms(baseDA.fSaveHistograms),
110   fDetectorArray(baseDA.fDetectorArray),
111   fPulseSize(baseDA.fPulseSize),
112   fPulseLength(baseDA.fPulseLength),
113   fRequiredEvents(baseDA.fRequiredEvents),
114   fCurrentEvent(baseDA.fCurrentEvent)
115 {
116   fSeenDetectors[0] = baseDA.fSeenDetectors[0];
117   fSeenDetectors[1] = baseDA.fSeenDetectors[1];
118   fSeenDetectors[2] = baseDA.fSeenDetectors[2];
119
120   fDetectorArray.SetOwner();
121   
122 }
123
124
125 //_____________________________________________________________________
126 AliFMDBaseDA::~AliFMDBaseDA() 
127 {
128   //destructor
129 }
130
131 //_____________________________________________________________________
132 void AliFMDBaseDA::Run(AliRawReader* reader) 
133 {
134   TFile* diagFile = 0;
135   if (fSaveHistograms)
136     diagFile = TFile::Open(fDiagnosticsFilename.Data(),"RECREATE");
137
138   
139   
140   
141   
142   reader->Reset();
143   
144   AliFMDRawReader* fmdReader  = new AliFMDRawReader(reader,0);
145   TClonesArray*    digitArray = new TClonesArray("AliFMDDigit",0);
146   
147   Bool_t SOD_read = kFALSE;
148   
149   for(Int_t i=0;i<3;i++) {
150     reader->NextEvent(); // Read Start-of-Run / Start-of-Files event
151     
152     UInt_t eventType = reader->GetType();
153     if(eventType == AliRawEventHeaderBase::kStartOfData || 
154        eventType == AliRawEventHeaderBase::kFormatError) { 
155       
156       WriteConditionsData(fmdReader);
157       Init();
158       SOD_read = kTRUE;
159       break;
160     }
161   }
162   
163   InitContainer(diagFile);
164   
165   if(!SOD_read) 
166     AliWarning("No SOD event detected!");
167   
168   int lastProgress = 0;
169   
170   
171   
172   for(Int_t n =1;n <= GetRequiredEvents(); n++) {
173     if(!reader->NextEvent()) continue;
174     SetCurrentEvent(n);
175     digitArray->Clear();
176     fmdReader->ReadAdcs(digitArray);
177     
178     for(Int_t i = 0; i<digitArray->GetEntriesFast();i++) {
179       AliFMDDigit* digit = static_cast<AliFMDDigit*>(digitArray->At(i));
180       fSeenDetectors[digit->Detector()-1] = kTRUE;
181       FillChannels(digit);
182     }
183     
184    
185     FinishEvent();
186     
187     int progress = int((n *100)/ GetRequiredEvents()) ;
188     if (progress <= lastProgress) continue;
189     lastProgress = progress;
190     std::cout << "Progress: " << lastProgress << " / 100 " << std::endl;
191     
192   }
193   
194   AliInfo(Form("Looped over %d events",GetCurrentEvent()));
195   WriteHeaderToFile();
196   
197   for(UShort_t det=1;det<=3;det++) {
198     if (!fSeenDetectors[det-1]) continue;
199     std::cout << "FMD" << det << std::endl;
200     UShort_t FirstRing = (det == 1 ? 1 : 0);
201     for (UShort_t ir = FirstRing; ir < 2; ir++) {
202       Char_t   ring = (ir == 0 ? 'O' : 'I');
203       UShort_t nsec = (ir == 0 ? 40  : 20);
204       UShort_t nstr = (ir == 0 ? 256 : 512);
205       std::cout << " Ring " << ring << ": " << std::flush;
206       for(UShort_t sec =0; sec < nsec;  sec++)  {
207         for(UShort_t strip = 0; strip < nstr; strip++) {
208           Analyse(det,ring,sec,strip);
209         }
210         std::cout << '.' << std::flush;
211       }
212       if(fSaveHistograms)
213         diagFile->Flush();
214       std::cout << "done" << std::endl;
215     }
216   }
217   
218   if(fOutputFile.is_open()) {
219     fOutputFile.write("# EOF\n",6);
220     fOutputFile.close();
221   }
222   
223   Terminate(diagFile);
224     
225   if(fSaveHistograms ) {
226     
227     AliInfo("Closing diagnostics file - please wait ...");
228     // diagFile->Write();
229     diagFile->Close();
230     AliInfo("done");
231     
232   }
233 }
234 //_____________________________________________________________________
235
236 void AliFMDBaseDA::InitContainer(TDirectory* diagFile)
237 {
238   TObjArray* detArray;
239   TObjArray* ringArray;
240   TObjArray* sectorArray;
241     
242   TDirectory* savDir   = gDirectory;
243
244   for(UShort_t det=1;det<=3;det++) {
245     detArray = new TObjArray();
246     detArray->SetOwner();
247     fDetectorArray.AddAtAndExpand(detArray,det);
248
249     TDirectory* detDir = 0;
250     if (diagFile) {
251       diagFile->cd();
252       detDir = diagFile->mkdir(GetDetectorPath(det, kFALSE));
253     }
254
255     UShort_t FirstRing = (det == 1 ? 1 : 0);
256     for (UShort_t ir = FirstRing; ir < 2; ir++) {
257       Char_t   ring = (ir == 0 ? 'O' : 'I');
258       UShort_t nsec = (ir == 0 ? 40  : 20);
259       UShort_t nstr = (ir == 0 ? 256 : 512);
260       ringArray = new TObjArray();
261       ringArray->SetOwner();
262       detArray->AddAtAndExpand(ringArray,ir);
263
264
265       TDirectory* ringDir = 0;
266       if (detDir) { 
267         detDir->cd();
268         ringDir = detDir->mkdir(GetRingPath(det,ring, kFALSE));
269       }
270       
271
272       for(UShort_t sec =0; sec < nsec;  sec++)  {
273         sectorArray = new TObjArray();
274         sectorArray->SetOwner();
275         ringArray->AddAtAndExpand(sectorArray,sec);
276
277
278         TDirectory* secDir = 0;
279         if (ringDir) { 
280           ringDir->cd();
281           secDir = ringDir->mkdir(GetSectorPath(det, ring, sec, kFALSE));
282         }
283         
284         for(UShort_t strip = 0; strip < nstr; strip++) {
285           if (secDir) { 
286             secDir->cd();
287             secDir->mkdir(GetStripPath(det, ring, sec, strip, kFALSE));
288           }
289           AddChannelContainer(sectorArray, det, ring, sec, strip);
290         }
291       }
292     }
293   }
294   savDir->cd();
295 }
296
297 //_____________________________________________________________________ 
298 void AliFMDBaseDA::WriteConditionsData(AliFMDRawReader* fmdReader) 
299 {
300   AliFMDParameters* pars       = AliFMDParameters::Instance();
301   fConditionsFile.write(Form("# %s \n",pars->GetConditionsShuttleID()),14);
302   
303   AliFMDCalibSampleRate* sampleRate = new AliFMDCalibSampleRate();
304   AliFMDCalibStripRange* stripRange = new AliFMDCalibStripRange();
305   
306   fmdReader->ReadSODevent(sampleRate,stripRange,fPulseSize,fPulseLength,
307                           fSeenDetectors);
308
309   sampleRate->WriteToFile(fConditionsFile, fSeenDetectors);
310   stripRange->WriteToFile(fConditionsFile, fSeenDetectors);
311
312  
313   // Zero Suppresion
314   
315   // Strip Range
316   
317   fConditionsFile.write("# Gain Events \n",15);
318   
319   for(UShort_t det=1; det<=3;det++) {
320     if (!fSeenDetectors[det-1]) { 
321       continue;
322     }
323     UShort_t firstring = (det == 1 ? 1 : 0);
324     for(UShort_t iring = firstring; iring <=1;iring++) {
325       Char_t ring = (iring == 1 ? 'I' : 'O');
326       for(UShort_t board =0 ; board <=1; board++) {
327         
328         Int_t idx = GetHalfringIndex(det,ring,board);
329         
330         fConditionsFile << det                     << ','
331                         << ring                    << ','
332                         << board                   << ','
333                         << fPulseLength.At(idx)    << "\n";
334         
335       }
336     }
337   }
338   
339   fConditionsFile.write("# Gain Pulse \n",14);
340   
341   for(UShort_t det=1; det<=3;det++) {
342     if (!fSeenDetectors[det-1]) { 
343       continue;
344     }
345     UShort_t firstring = (det == 1 ? 1 : 0);
346     for(UShort_t iring = firstring; iring <=1;iring++) {
347       Char_t ring = (iring == 1 ? 'I' : 'O');
348       for(UShort_t board =0 ; board <=1; board++) {
349         
350         Int_t idx = GetHalfringIndex(det,ring,board);
351         
352         fConditionsFile << det                     << ','
353                         << ring                    << ','
354                         << board                   << ','
355                         << fPulseSize.At(idx)      << "\n";
356         
357       }
358     }
359   }
360   if(fConditionsFile.is_open()) {
361     
362     fConditionsFile.write("# EOF\n",6);
363     fConditionsFile.close();
364     
365   }
366   
367 }
368 //_____________________________________________________________________ 
369 Int_t AliFMDBaseDA::GetHalfringIndex(UShort_t det, Char_t ring, UShort_t board) {
370
371   UShort_t iring  =  (ring == 'I' ? 1 : 0);
372   
373   Int_t index = (((det-1) << 2) | (iring << 1) | (board << 0));
374   
375   return index-2;
376   
377 }
378
379
380 //_____________________________________________________________________ 
381 //
382 // EOF
383 //