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