Use event specie to identufy laser events
[u/mrichter/AliRoot.git] / TPC / AliTPCCalibRawBase.cxx
1  /**************************************************************************
2  * Copyright(c) 1998-1999, 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: AliTPCCalibRawBase.cxx */
17
18 /////////////////////////////////////////////////////////////////////////////////////////
19 //                                                                                     //
20 //          Base class for the calibration algorithms using raw data as input          //
21 //                                                                                     //
22 //   Origin: Jens Wiechula   J.Wiechula@gsi.de                                         //
23 //                                                                                     //
24 /////////////////////////////////////////////////////////////////////////////////////////
25
26 //Root includes
27 #include <TDirectory.h>
28 #include <TFile.h>
29
30 //Aliroot includes
31 #include "AliRawReaderDate.h"
32 #include "AliRawReader.h"
33 #include "AliRawEventHeaderBase.h"
34 #include "AliAltroMapping.h"
35 #include "AliAltroRawStream.h"
36 #include "AliTPCROC.h"
37 #include "AliTPCRawStreamFast.h"
38 #include "AliTPCRawStreamV3.h"
39 #include "AliTPCRawStream.h"
40 #include "AliLog.h"
41 #include "TTreeStream.h"
42 #include "event.h"
43
44 #include "AliTPCCalibRawBase.h"
45
46 ClassImp(AliTPCCalibRawBase)
47
48 AliTPCCalibRawBase::AliTPCCalibRawBase() :
49   TNamed(),
50   fFirstTimeBin(0),
51   fLastTimeBin(1000),
52   fNevents(0),
53   fDebugLevel(0),
54   fStreamLevel(0),
55   fRunNumber(0),
56   fFirstTimeStamp(0),
57   fLastTimeStamp(0),
58   fTimeStamp(0),
59   fEventType(0),
60   fAltroL1Phase(0),
61   fAltroL1PhaseTB(0),
62   fCurrRCUId(-1),
63   fPrevRCUId(-1),
64   fCurrDDLNum(-1),
65   fPrevDDLNum(-1),
66   fUseL1Phase(kTRUE),
67   fDebugStreamer(0x0),
68   fAltroRawStream(0x0),
69   fMapping(0x0),
70   fROC(AliTPCROC::Instance())
71 {
72     //
73     // default ctor
74     //
75
76 }
77 //_____________________________________________________________________
78 AliTPCCalibRawBase::AliTPCCalibRawBase(const AliTPCCalibRawBase &calib) :
79   TNamed(calib),
80   fFirstTimeBin(calib.fFirstTimeBin),
81   fLastTimeBin(calib.fLastTimeBin),
82   fNevents(calib.fNevents),
83   fDebugLevel(calib.fDebugLevel),
84   fStreamLevel(calib.fStreamLevel),
85   fRunNumber(calib.fRunNumber),
86   fFirstTimeStamp(calib.fFirstTimeStamp),
87   fLastTimeStamp(calib.fLastTimeStamp),
88   fTimeStamp(0),
89   fEventType(0),
90   fAltroL1Phase(0),
91   fAltroL1PhaseTB(0),
92   fCurrRCUId(-1),
93   fPrevRCUId(-1),
94   fCurrDDLNum(-1),
95   fPrevDDLNum(-1),
96   fUseL1Phase(kTRUE),
97   fDebugStreamer(0x0),
98   fAltroRawStream(0x0),
99   fMapping(0x0),
100   fROC(AliTPCROC::Instance())
101 {
102     //
103     // copy ctor
104     //
105   
106 }
107 //_____________________________________________________________________
108 AliTPCCalibRawBase::~AliTPCCalibRawBase()
109 {
110   //
111   // dtor
112   //
113   if (fDebugStreamer) delete fDebugStreamer;
114 }
115 //_____________________________________________________________________
116   AliTPCCalibRawBase& AliTPCCalibRawBase::operator = (const  AliTPCCalibRawBase &source)
117   {
118     //
119     // assignment operator
120     //
121     if (&source == this) return *this;
122     new (this) AliTPCCalibRawBase(source);
123     
124     return *this;
125   }
126 //_____________________________________________________________________
127 Bool_t AliTPCCalibRawBase::ProcessEventFast(AliTPCRawStreamFast * const rawStreamFast)
128 {
129   //
130   // Event Processing loop - AliTPCRawStreamFast
131   //
132   ResetEvent();
133   Bool_t withInput = kFALSE;
134   while ( rawStreamFast->NextDDL() ){
135     while ( rawStreamFast->NextChannel() ){
136       Int_t isector  = rawStreamFast->GetSector();                       //  current sector
137       Int_t iRow     = rawStreamFast->GetRow();                          //  current row
138       Int_t iPad     = rawStreamFast->GetPad();                          //  current pad
139       
140       while ( rawStreamFast->NextBunch() ){
141         Int_t startTbin = (Int_t)rawStreamFast->GetStartTimeBin();
142         Int_t endTbin = (Int_t)rawStreamFast->GetEndTimeBin();
143         for (Int_t iTimeBin = startTbin; iTimeBin < endTbin; iTimeBin++){
144           Float_t signal=(Float_t)rawStreamFast->GetSignals()[iTimeBin-startTbin];
145             Update(isector,iRow,iPad,iTimeBin+1,signal);
146           withInput = kTRUE;
147         }
148       }
149     }
150   }
151   if (withInput){
152     EndEvent();
153   }
154   return withInput;
155 }
156 //_____________________________________________________________________
157 Bool_t AliTPCCalibRawBase::ProcessEventFast(AliRawReader * const rawReader)
158 {
159   //
160   //  Event processing loop - AliRawReader
161   //
162   AliRawEventHeaderBase* eventHeader = (AliRawEventHeaderBase*)rawReader->GetEventHeader();
163   if (eventHeader){
164     fTimeStamp   = eventHeader->Get("Timestamp");
165     fRunNumber = eventHeader->Get("RunNb");
166     fEventType = eventHeader->Get("Type");
167   }
168   if (!fFirstTimeStamp) fFirstTimeStamp=fTimeStamp;
169   fLastTimeStamp=fTimeStamp;
170   
171   AliTPCRawStreamFast *rawStreamFast = new AliTPCRawStreamFast(rawReader, (AliAltroMapping**)fMapping);
172   Bool_t res=ProcessEventFast(rawStreamFast);
173   delete rawStreamFast;
174   return res;
175 }
176 //_____________________________________________________________________
177 Bool_t AliTPCCalibRawBase::ProcessEvent(AliTPCRawStreamV3 * const rawStreamV3)
178 {
179   //
180   // Event Processing loop - AliTPCRawStreamV3
181   //
182   ResetEvent();
183   Bool_t withInput = kFALSE;
184   fAltroL1Phase=0;
185   fAltroL1PhaseTB=0;
186 //   fAltroRawStream = static_cast<AliAltroRawStream*>(rawStreamV3);
187   while ( rawStreamV3->NextDDL() ){
188     if (AliLog::GetGlobalDebugLevel()>2) rawStreamV3->PrintRCUTrailer();
189     fPrevDDLNum=-1;
190     fCurrRCUId=rawStreamV3->GetRCUId();
191     fCurrDDLNum=rawStreamV3->GetDDLNumber();
192     if (fUseL1Phase){
193 //         fAltroL1Phase  = fAltroRawStream->GetL1Phase();
194       fAltroL1Phase  = rawStreamV3->GetL1Phase();
195       fAltroL1PhaseTB = (fAltroL1Phase*1e09/100.);
196       AliDebug(1, Form("L1Phase: %.2e (%03d)\n",fAltroL1PhaseTB,fCurrDDLNum));
197     }
198     UpdateDDL();
199     while ( rawStreamV3->NextChannel() ){
200       Int_t isector  = rawStreamV3->GetSector();                       //  current sector
201       Int_t iRow     = rawStreamV3->GetRow();                          //  current row
202       Int_t iPad     = rawStreamV3->GetPad();                          //  current pad
203       while ( rawStreamV3->NextBunch() ){
204         UInt_t  startTbin    = rawStreamV3->GetStartTimeBin();
205 //         Int_t  endTbin      = (Int_t)rawStreamV3->GetEndTimeBin();
206         Int_t  bunchlength  = rawStreamV3->GetBunchLength();
207         const UShort_t *sig = rawStreamV3->GetSignals();
208         ProcessBunch(isector,iRow,iPad,bunchlength,startTbin,sig);
209         for (Int_t iTimeBin = 0; iTimeBin<bunchlength; iTimeBin++){
210           Float_t signal=(Float_t)sig[iTimeBin];
211 //            printf("%02d - %03d - %03d - %04d: %.1f\n",isector,iRow,iPad,startTbin,signal);
212           Update(isector,iRow,iPad,startTbin--,signal);
213           fPrevRCUId=fCurrRCUId;
214           fPrevDDLNum=fCurrDDLNum;
215           withInput = kTRUE;
216         }
217       }
218     }
219   }
220   if (withInput){
221     EndEvent();
222   }
223   return withInput;
224 }
225 //_____________________________________________________________________
226 Bool_t AliTPCCalibRawBase::ProcessEvent(AliRawReader * const rawReader)
227 {
228   //
229   //  Event processing loop - AliRawReader
230   //
231   AliRawEventHeaderBase* eventHeader = (AliRawEventHeaderBase*)rawReader->GetEventHeader();
232   if (eventHeader){
233     fTimeStamp   = eventHeader->Get("Timestamp");
234     fRunNumber = eventHeader->Get("RunNb");
235     fEventType = eventHeader->Get("Type");
236   }
237   if (!fFirstTimeStamp) fFirstTimeStamp=fTimeStamp;
238
239   AliTPCRawStreamV3 *rawStreamV3 = new AliTPCRawStreamV3(rawReader, (AliAltroMapping**)fMapping);
240   Bool_t res=ProcessEvent(rawStreamV3);
241   
242   fLastTimeStamp=fTimeStamp;
243   
244   delete rawStreamV3;
245   return res;
246 }
247 //_____________________________________________________________________
248 Bool_t AliTPCCalibRawBase::ProcessEvent(AliTPCRawStream * const rawStream)
249 {
250   //
251   // Event Processing loop - AliTPCRawStream
252   //
253
254   ResetEvent();
255
256   Bool_t withInput = kFALSE;
257   fAltroL1Phase=0;
258   fAltroL1PhaseTB=0;
259   fAltroRawStream = static_cast<AliAltroRawStream*>(rawStream);
260   while (rawStream->Next()) {
261     if (fUseL1Phase){
262       fAltroL1Phase  = fAltroRawStream->GetL1Phase();
263       fAltroL1PhaseTB = (fAltroL1Phase*1e09/100.);
264     }
265     fPrevRCUId=fCurrRCUId;
266     fCurrRCUId=rawStream->GetRCUId();
267     fPrevDDLNum=fCurrDDLNum;
268     fCurrDDLNum=rawStream->GetDDLNumber();
269     Int_t isector  = rawStream->GetSector();                       //  current sector
270     Int_t iRow     = rawStream->GetRow();                          //  current row
271     Int_t iPad     = rawStream->GetPad();                          //  current pad
272     Int_t iTimeBin = rawStream->GetTime();                         //  current time bin
273     Float_t signal = rawStream->GetSignal();                       //  current ADC signal
274     
275     Update(isector,iRow,iPad,iTimeBin,signal);
276     withInput = kTRUE;
277   }
278   fAltroRawStream=0x0;
279   if (withInput){
280     EndEvent();
281   }
282   return withInput;
283 }
284 //_____________________________________________________________________
285 Bool_t AliTPCCalibRawBase::ProcessEventOld(AliRawReader * const rawReader)
286 {
287   //
288   //  Event processing loop - AliRawReader
289   //
290   AliRawEventHeaderBase* eventHeader = (AliRawEventHeaderBase*)rawReader->GetEventHeader();
291   if (eventHeader){
292     fTimeStamp   = eventHeader->Get("Timestamp");
293     fRunNumber = eventHeader->Get("RunNb");
294     fEventType = eventHeader->Get("Type");
295   }
296
297   AliTPCRawStream rawStream(rawReader, (AliAltroMapping**)fMapping);
298   rawReader->Select("TPC");
299   return ProcessEvent(&rawStream);
300 }
301 //_____________________________________________________________________
302 Bool_t AliTPCCalibRawBase::ProcessEvent(eventHeaderStruct * const event)
303 {
304   //
305   //  Event processing loop - date event
306   //
307
308   fRunNumber=event->eventRunNb;
309   fTimeStamp=event->eventTimestamp;
310   if (!fFirstTimeStamp) fFirstTimeStamp=fTimeStamp;
311   fLastTimeStamp=fTimeStamp;
312   fEventType=event->eventType;
313   AliRawReader *rawReader = new AliRawReaderDate((void*)event);
314   AliTPCRawStreamV3 *rawStreamV3 = new AliTPCRawStreamV3(rawReader, (AliAltroMapping**)fMapping);
315   Bool_t result=ProcessEvent(rawStreamV3);
316   delete rawStreamV3;
317   delete rawReader;
318   return result;
319
320 }
321 //_____________________________________________________________________
322 void AliTPCCalibRawBase::DumpToFile(const Char_t *filename, const Char_t *dir, Bool_t append)
323 {
324     //
325     //  Write class to file
326     //
327   
328   TString sDir(dir);
329   TString option;
330   
331   if ( append )
332     option = "update";
333   else
334     option = "recreate";
335   
336   TDirectory *backup = gDirectory;
337   TFile f(filename,option.Data());
338   f.cd();
339   if ( !sDir.IsNull() ){
340     f.mkdir(sDir.Data());
341     f.cd(sDir);
342   }
343   this->Write();
344   f.Close();
345   
346   if ( backup ) backup->cd();
347 }
348 //_____________________________________________________________________
349 TTreeSRedirector *AliTPCCalibRawBase::GetDebugStreamer(){
350   //
351   // Get Debug streamer
352   // In case debug streamer not yet initialized and StreamLevel>0 create new one
353   //
354   if (fStreamLevel==0) return 0;
355   if (fDebugStreamer) return fDebugStreamer;
356   TString dsName;
357   dsName=GetName();
358   dsName+="Debug.root";
359   dsName.ReplaceAll(" ","");
360   fDebugStreamer = new TTreeSRedirector(dsName.Data());
361   return fDebugStreamer;
362 }
363 //_____________________________________________________________________
364 void AliTPCCalibRawBase::MergeBase(const AliTPCCalibRawBase *calib)
365 {
366   //
367   // merge this with base
368   //
369   if (calib->fFirstTimeStamp<fFirstTimeStamp) fFirstTimeStamp=calib->fFirstTimeStamp;
370   if (calib->fLastTimeStamp>fLastTimeStamp)   fLastTimeStamp =calib->fLastTimeStamp;
371 }
372