]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerCalibratedDataMaker.cxx
Add missing target/projectile information
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerCalibratedDataMaker.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$
17
18 #include "AliMUONTrackerCalibratedDataMaker.h"
19
20 #include "AliCDBManager.h"
21 #include "AliCDBStorage.h"
22 #include "AliCodeTimer.h"
23 #include "AliDAQ.h"
24 #include "AliLog.h"
25 #include "AliMUON2DMap.h"
26 #include "AliMUONCalibParamND.h"
27 #include "AliMUONCalibrationData.h"
28 #include "AliMUONDigitCalibrator.h"
29 #include "AliMUONRawStreamTracker.h"
30 #include "AliMUONRawStreamTrackerHP.h"
31 #include "AliMUONRecoParam.h"
32 #include "AliMUONReconstructor.h"
33 #include "AliMUONTrackerData.h"
34 #include "AliMpDDLStore.h"
35 #include "AliRawEventHeaderBase.h"
36 #include "AliRawReader.h"
37 #include <Riostream.h>
38
39 ///\class AliMUONTrackerCalibratedDataMaker
40 ///
41 /// Creator of AliMUONVTrackerData from AliRawReader
42 /// 
43 ///\author Laurent Aphecetche, Subatech
44
45 ///\cond CLASSIMP
46 ClassImp(AliMUONTrackerCalibratedDataMaker)
47 ///\endcond
48
49 Int_t AliMUONTrackerCalibratedDataMaker::fgkCounter(0);
50
51 //_____________________________________________________________________________
52 AliMUONTrackerCalibratedDataMaker::AliMUONTrackerCalibratedDataMaker(TRootIOCtor*)
53 : AliMUONVTrackerDataMaker(),
54 fRawReader(0x0),
55 fIsOwnerOfRawReader(kFALSE),
56 fAccumulatedData(0x0),
57 fOneEventData(0x0),
58 fSource(""),
59 fIsRunning(kFALSE),
60 fDigitCalibrator(0x0),
61 fCalibrationData(0x0),
62 fCDBPath(""),
63 fNumberOfEvents(0),
64 fUseHPDecoder(kTRUE)
65 {
66   /// Root IO ctor
67 }
68
69 //_____________________________________________________________________________
70 AliMUONTrackerCalibratedDataMaker::AliMUONTrackerCalibratedDataMaker(const AliMUONRecoParam* recoParam,
71                                                                      Int_t runNumber,
72                                                                      AliRawReader* reader, 
73                                                                      const char* cdbpath,
74                                                                      const char* calibMode,
75                                                                      Bool_t histogram,
76                                                                      Double_t xmin,
77                                                                      Double_t xmax,                                                                     
78                                                                      Bool_t useHPdecoder)
79 : AliMUONVTrackerDataMaker(),
80 fRawReader(reader),
81 fIsOwnerOfRawReader(kFALSE),
82 fAccumulatedData(0x0),
83 fOneEventData(new AliMUON2DMap(true)),
84 fSource("unspecified"),
85 fIsRunning(kFALSE),
86 fDigitCalibrator(0x0),
87 fCalibrationData(0x0),
88 fCDBPath(cdbpath),
89 fNumberOfEvents(0),
90 fUseHPDecoder(useHPdecoder)
91 {
92   /// Ctor in which this object will NOT be owner of the reader,
93   /// and can NOT apply rewind to it, nor use Next on it
94   
95   Ctor(recoParam,runNumber,calibMode,histogram,xmin,xmax);
96 }
97
98 //_____________________________________________________________________________
99 AliMUONTrackerCalibratedDataMaker::AliMUONTrackerCalibratedDataMaker(const AliMUONRecoParam* recoParam,
100                                                                      AliRawReader* reader,
101                                                                      const char* cdbpath,
102                                                                      const char* calibMode,
103                                                                      Bool_t histogram,
104                                                                      Double_t xmin,
105                                                                      Double_t xmax,
106                                                                      Bool_t useHPDecoder)
107 : AliMUONVTrackerDataMaker(),
108 fRawReader(reader),
109 fIsOwnerOfRawReader(kTRUE),
110 fAccumulatedData(0x0),
111 fOneEventData(new AliMUON2DMap(true)),
112 fSource("unspecified"),
113 fIsRunning(kFALSE),
114 fDigitCalibrator(0x0),
115 fCalibrationData(0x0),
116 fCDBPath(cdbpath),
117 fNumberOfEvents(0),
118 fUseHPDecoder(useHPDecoder)
119 {
120   /// Ctor, in which we are the owner of the reader, so we can rewind and advance it
121   /// as we wish
122
123   Int_t runNumber(0);
124   
125   if ( fRawReader ) 
126   {
127     fRawReader->NextEvent(); // to be sure to get run number available
128     runNumber = reader->GetRunNumber();
129     fRawReader->RewindEvents();
130   }
131   
132   Ctor(recoParam,runNumber,calibMode,histogram,xmin,xmax);
133 }
134
135 //_____________________________________________________________________________
136 void
137 AliMUONTrackerCalibratedDataMaker::Ctor(const AliMUONRecoParam* recoParam, 
138                                         Int_t runNumber, const char* calibMode,
139                                         Bool_t histogram, Double_t xmin, Double_t xmax)
140 {
141   /// "designated" constructor.
142
143   ++fgkCounter;
144   
145   Bool_t calibrate = ( fCDBPath.Length() > 0 );
146   TString name;
147   TString basename("RAW");
148   
149   if ( calibrate ) 
150   {
151     TString scalib(calibMode);
152     scalib.ToUpper();
153     if ( scalib == "GAIN" ) basename = "CALC";
154     if ( scalib == "NOGAIN" ) basename = "CALZ";
155     if ( scalib == "GAINCONSTANTCAPA" ) basename = "CALG";
156   }
157   
158   if (!runNumber)
159   {
160     name = Form("%s%s_%d",
161                 (histogram ? "H" : ""),
162                 basename.Data(),
163                 fgkCounter);
164   }
165   else
166   {
167     name = Form("%s%s%d",
168                 (histogram ? "H" : ""),
169                 basename.Data(),
170                 runNumber);
171   }
172   
173   fAccumulatedData = new AliMUONTrackerData(name.Data(),"charge values",1);
174   fAccumulatedData->SetDimensionName(0,(calibrate ? "Calibrated charge" : "Raw charge"));
175   if ( histogram ) 
176   {
177     fAccumulatedData->MakeHistogramForDimension(0,kTRUE,xmin,xmax);
178     AliInfo(Form("Will histogram between %e and %e",xmin,xmax));
179   }
180   
181   if ( calibrate ) 
182   {
183     fCalibrationData = new AliMUONCalibrationData(runNumber);
184     
185     // force the reading of calibration NOW
186     // FIXME: not really elegant and error prone (as we have the list of calib data twice, 
187     // once here and once in the digitcalibrator class, hence the change of them getting
188     // out of sync)
189     // But with the current CDBManager implementation, I don't know how to solve
190     // this better (e.g. to avoid clearing cache messages and so on).
191     
192     AliCDBStorage* storage = AliCDBManager::Instance()->GetDefaultStorage();
193     
194     if ( storage->GetURI() != fCDBPath.Data() ) 
195     {
196       AliCDBManager::Instance()->SetDefaultStorage(fCDBPath.Data());
197     }
198     
199     fCalibrationData->Pedestals();
200     fCalibrationData->Gains();
201     fCalibrationData->Neighbours();
202     fCalibrationData->HV();
203     fCalibrationData->Capacitances();
204     
205     if ( storage->GetURI() != fCDBPath.Data() ) 
206     {
207       AliCDBManager::Instance()->SetDefaultStorage(storage);
208     }
209     
210     fDigitCalibrator = new AliMUONDigitCalibrator(*fCalibrationData,recoParam,calibMode);
211                 //FIXME: get the reco param from GUI and/or from OCDB if not used from the QA code ?
212   }
213 }
214
215 //_____________________________________________________________________________
216 AliMUONTrackerCalibratedDataMaker::~AliMUONTrackerCalibratedDataMaker()
217 {
218   /// dtor
219   delete fOneEventData;
220   delete fAccumulatedData;
221   if ( fIsOwnerOfRawReader ) delete fRawReader;
222   delete fCalibrationData;
223   delete fDigitCalibrator;
224 }
225
226 //_____________________________________________________________________________
227 Long64_t 
228 AliMUONTrackerCalibratedDataMaker::Merge(TCollection*)
229 {
230   /// Merge
231   AliError("Not implemented yet");
232   return 0;
233 }
234
235 //_____________________________________________________________________________
236 Bool_t 
237 AliMUONTrackerCalibratedDataMaker::NextEvent()
238 {
239   /// Read and process next event
240  
241   if ( !fIsOwnerOfRawReader ) 
242   {
243     AliError("I'm not the owner of the raw reader. Cannot use NextEvent");
244     return kFALSE;
245   }
246   
247   AliCodeTimerAuto("");
248   
249   static Int_t nphysics(0);
250   static Int_t ngood(0);
251
252   if ( !IsRunning() ) return kTRUE;
253   
254   Bool_t ok = fRawReader->NextEvent();
255
256   if (!ok) 
257   {
258     return kFALSE;
259   }
260   
261   Int_t eventType = fRawReader->GetType();
262
263   ++fNumberOfEvents;
264   
265   if (eventType != AliRawEventHeaderBase::kPhysicsEvent ) 
266   {
267     return kTRUE; // for the moment
268   }
269
270   ++nphysics;
271
272         Bool_t pok = ProcessEvent();
273         
274         if ( pok ) ++ngood;
275         
276         AliDebug(1,Form("n %10d nphysics %10d ngood %10d",fNumberOfEvents,nphysics,ngood));
277         
278         return kTRUE;
279 }
280
281 //_____________________________________________________________________________
282 Bool_t
283 AliMUONTrackerCalibratedDataMaker::ProcessEvent()
284 {
285         /// Process current event 
286   /// Note that we do not simply reuse the AliMUONDigitCalibrator::Calibrate(AliMUONVDigitStore&)
287   /// method, as this would require filling first a digitStore, and then calibrate it, and
288   /// then convert it into a VStore, all this taking too much time.
289   ///
290   /// But we *do* reuse the AliMUONDigitCalibrator::CalibrateDigit in order not to 
291   /// duplicate this critical piece of calibration code !
292   ///
293   
294   AliCodeTimerAuto("");
295
296   AliMUONVRawStreamTracker* stream = 0x0;
297   
298   if ( fUseHPDecoder ) 
299   {
300     stream = new AliMUONRawStreamTrackerHP(fRawReader);
301   }
302   else
303   {
304     stream = new AliMUONRawStreamTracker(fRawReader);
305   }
306   
307   stream->EnabbleErrorLogger();
308   
309   stream->First();
310   
311   Int_t buspatchId;
312   UShort_t manuId;
313   UChar_t manuChannel;
314         UShort_t adc;
315   const Int_t nddls = AliDAQ::NumberOfDdls("MUONTRK");
316   TArrayI nevents(nddls);
317
318   for ( Int_t i = 0; i < nddls; ++i ) 
319   {
320     nevents[i] = 0;
321   }
322   
323   fOneEventData->Clear();
324   
325   while ( stream->Next(buspatchId,manuId,manuChannel,adc) )
326   {    
327     Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(buspatchId);
328     
329     Int_t ddl = AliMpDDLStore::Instance()->GetDDLfromBus(buspatchId);
330
331     nevents[ddl] = 1;
332     
333     AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(fOneEventData->FindObject(detElemId,manuId));
334     if (!param)
335     {
336       param = new AliMUONCalibParamND(1,64,detElemId,manuId,
337                                       AliMUONVCalibParam::InvalidFloatValue());
338       fOneEventData->Add(param);
339     }
340
341     Bool_t ok = fDigitCalibrator->IsValidDigit(detElemId, manuId, manuChannel);
342
343     if ( ok ) 
344     {
345       Float_t charge = fDigitCalibrator->CalibrateDigit(detElemId, manuId, manuChannel,adc,3.0);
346     
347       if (charge > 0.0 ) 
348       {
349         param->SetValueAsDouble(manuChannel,0,charge);
350       }
351     }
352   }    
353   
354         Bool_t good(kFALSE);
355         
356   if ( !stream->IsErrorMessage() )
357   {
358     good = kTRUE;
359     fAccumulatedData->Add(*fOneEventData,&nevents);    
360   }
361   
362   delete stream;
363   
364   return good;  
365 }
366
367 //_____________________________________________________________________________
368 void
369 AliMUONTrackerCalibratedDataMaker::Print(Option_t*) const
370 {
371   /// Printout
372   
373   cout << "Source=" << Source() << " Running=" << ( IsRunning() ? "YES" : "NO")
374   << endl;
375   
376 }
377
378 //_____________________________________________________________________________
379 void 
380 AliMUONTrackerCalibratedDataMaker::Rewind()
381 {
382   /// Rewind events
383   fRawReader->RewindEvents();
384   fNumberOfEvents=0;
385 }
386
387 //_____________________________________________________________________________
388 void 
389 AliMUONTrackerCalibratedDataMaker::SetRawReader(AliRawReader* rawReader)
390 {
391   /// Points to another raw reader
392         
393         if ( fIsOwnerOfRawReader ) 
394         {
395     AliFatal("Improper use of this class ! Cannot change raw reader in this case");
396         }
397         fRawReader = rawReader;
398 }