Extacting the OCDB in a separate module. The detectors have write permission in the...
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDCalibrationComponent.cxx
CommitLineData
95259bbb 1// $Id$
2
3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
8 * for The ALICE Off-line Project. *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19/** @file AliHLTTRDCalibrationComponent.cxx
20 @author Timm Steinbeck, Matthias Richter
21 @date
22 @brief A TRDCalibration processing component for the HLT. */
23
24#if __GNUC__ >= 3
25using namespace std;
26#endif
27
28#include "TTree.h"
29#include "TFile.h"
30#include "TBranch.h"
31
32#include "AliHLTTRDCalibrationComponent.h"
33#include "AliHLTTRDDefinitions.h"
d679dd6c 34#include "AliHLTTRDTrack.h"
95259bbb 35
36#include "AliCDBManager.h"
37#include "AliTRDclusterizerHLT.h"
38#include "AliRawReaderMemory.h"
9aea5deb 39#include "AliTRDCalibraFillHisto.h"
95259bbb 40
41#include <cstdlib>
42#include <cerrno>
43#include <string>
44
45// this is a global object used for automatic component registration, do not use this
46AliHLTTRDCalibrationComponent gAliHLTTRDCalibrationComponent;
47
48ClassImp(AliHLTTRDCalibrationComponent);
49
d679dd6c 50AliHLTTRDCalibrationComponent::AliHLTTRDCalibrationComponent():
51 AliHLTCalibrationProcessor(),
52 fTRDCalibraFillHisto(NULL),
53 fUseHLTTracks(kFALSE),
54 fOutputPercentage(100), // By default we copy to the output exactly what we got as input
162637e4 55 fStrorageDBpath("local://$ALICE_ROOT/OCDB"),
d679dd6c 56 fCDB(NULL)
95259bbb 57{
58 // Default constructor
59}
60
61AliHLTTRDCalibrationComponent::~AliHLTTRDCalibrationComponent()
62{
63 // Destructor
64 ;
65}
66
67const char* AliHLTTRDCalibrationComponent::GetComponentID()
68{
69 // Return the component ID const char *
70 return "TRDCalibration"; // The ID of this component
71}
72
73void AliHLTTRDCalibrationComponent::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
74{
75 // Get the list of input data
76 list.clear(); // We do not have any requirements for our input data type(s).
77 list.push_back( AliHLTTRDDefinitions::fgkTRDSATracksDataType );
78}
79
80AliHLTComponent_DataType AliHLTTRDCalibrationComponent::GetOutputDataType()
81{
82 // Get the output data type
83 return AliHLTTRDDefinitions::fgkCalibrationDataType;
84}
85
86void AliHLTTRDCalibrationComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
87{
88 // Get the output data size
89 constBase = 0;
90 inputMultiplier = ((double)fOutputPercentage)/100.0;
91}
92
93AliHLTComponent* AliHLTTRDCalibrationComponent::Spawn()
94{
95 // Spawn function, return new instance of this class
96 return new AliHLTTRDCalibrationComponent;
97};
98
99Int_t AliHLTTRDCalibrationComponent::ScanArgument( int argc, const char** argv )
100{
101 // perform initialization. We check whether our relative output size is specified in the arguments.
102 fOutputPercentage = 100;
103 int i = 0;
104 char* cpErr;
105 while ( i < argc )
106 {
d679dd6c 107 HLTDebug("argv[%d] == %s", i, argv[i] );
95259bbb 108 if ( !strcmp( argv[i], "output_percentage" ) )
109 {
110 if ( i+1>=argc )
111 {
d679dd6c 112 HLTError("Missing output_percentage parameter");
95259bbb 113 return ENOTSUP;
114 }
d679dd6c 115 HLTDebug("argv[%d+1] == %s", i, argv[i+1] );
95259bbb 116 fOutputPercentage = strtoul( argv[i+1], &cpErr, 0 );
117 if ( *cpErr )
118 {
d679dd6c 119 HLTError("Cannot convert output_percentage parameter '%s'", argv[i+1] );
95259bbb 120 return EINVAL;
121 }
d679dd6c 122 HLTInfo("Output percentage set to %lu %%", fOutputPercentage );
95259bbb 123 i += 2;
124 continue;
125 }
d679dd6c 126 else if ( strcmp( argv[i], "-cdb" ) == 0)
95259bbb 127 {
128 if ( i+1 >= argc )
129 {
d679dd6c 130 HLTError("Missing -cdb argument");
95259bbb 131 return ENOTSUP;
132 }
133 fStrorageDBpath = argv[i+1];
d679dd6c 134 HLTInfo("DB storage is %s", fStrorageDBpath.c_str() );
95259bbb 135 i += 2;
136 continue;
d679dd6c 137 }
138 else if ( strcmp( argv[i], "-useHLTTracks" ) == 0){
139 fUseHLTTracks = kTRUE;
140 i++;
141 HLTInfo("Expecting block of AliHLTTracks as input. Using low-level interface");
142 }
143 else{
144 HLTError("Unknown option '%s'", argv[i] );
145 return EINVAL;
146 }
95259bbb 147 }
148 return 0;
149}
150
151Int_t AliHLTTRDCalibrationComponent::InitCalibration()
152{
153 //init the calibration
154 fCDB = AliCDBManager::Instance();
155 if (!fCDB)
156 {
d679dd6c 157 HLTError("Could not get CDB instance, fCDB 0x%x", fCDB);
95259bbb 158 }
159 else
160 {
161 fCDB->SetRun(0); // THIS HAS TO BE RETRIEVED !!!
162 fCDB->SetDefaultStorage(fStrorageDBpath.c_str());
d679dd6c 163 HLTDebug("fCDB 0x%x", fCDB);
95259bbb 164 }
9aea5deb 165 fTRDCalibraFillHisto = AliTRDCalibraFillHisto::Instance();
166 fTRDCalibraFillHisto->SetHisto2d(); // choose to use histograms
167 fTRDCalibraFillHisto->SetCH2dOn(); // choose to calibrate the gain
168 fTRDCalibraFillHisto->SetPH2dOn(); // choose to calibrate the drift velocity
169 fTRDCalibraFillHisto->SetPRF2dOn(); // choose to look at the PRF
170 fTRDCalibraFillHisto->Init2Dhistos(); // initialise the histos
52be7fb0 171 return 0;
95259bbb 172}
173
174Int_t AliHLTTRDCalibrationComponent::DeinitCalibration()
175{
9aea5deb 176 HLTDebug("DeinitCalibration");
177
95259bbb 178 // Deinitialization of the component
9aea5deb 179
95259bbb 180 if (fCDB)
181 {
d679dd6c 182 HLTDebug("destroy fCDB");
95259bbb 183 fCDB->Destroy();
184 fCDB = 0;
185 }
52be7fb0 186 return 0;
95259bbb 187}
188
d679dd6c 189Int_t AliHLTTRDCalibrationComponent::ProcessCalibration(const AliHLTComponent_EventData& evtData,
190 const AliHLTComponent_BlockData* blocks,
191 AliHLTComponent_TriggerData& /*trigData*/,
192 AliHLTUInt8_t* /*outputPtr*/,
193 AliHLTUInt32_t& /*size*/,
194 vector<AliHLTComponent_BlockData>& /*outputBlocks*/)
95259bbb 195{
196 // Process an event
d679dd6c 197 // Logging( kHLTLogInfo, "HLT::TRDCalibration::ProcessCalibration", "Output percentage set", "Output percentage set to %lu %%", fOutputPercentage );
198 HLTDebug("NofBlocks %lu", evtData.fBlockCnt );
95259bbb 199 // Process an event
95259bbb 200
201 // Loop over all input blocks in the event
d679dd6c 202 vector<AliHLTComponent_DataType> expectedDataTypes;
203 GetInputDataTypes(expectedDataTypes);
204 for ( unsigned long iBlock = 0; iBlock < evtData.fBlockCnt; iBlock++ )
95259bbb 205 {
d679dd6c 206 const AliHLTComponentBlockData &block = blocks[iBlock];
207 AliHLTComponentDataType inputDataType = block.fDataType;
208 Bool_t correctDataType = kFALSE;
209
210 for(UInt_t i = 0; i < expectedDataTypes.size(); i++)
211 if( expectedDataTypes.at(i) == inputDataType)
212 correctDataType = kTRUE;
213 if (!correctDataType)
214 {
215 HLTDebug( "Block # %i/%i; Event 0x%08LX (%Lu) Wrong received datatype: %s - Skipping",
216 iBlock, evtData.fBlockCnt,
217 evtData.fEventID, evtData.fEventID,
218 DataType2Text(inputDataType).c_str());
219 continue;
220 }
221 else
222 HLTDebug("We get the right data type: Block # %i/%i; Event 0x%08LX (%Lu) Received datatype: %s",
223 iBlock, evtData.fBlockCnt-1,
224 evtData.fEventID, evtData.fEventID,
225 DataType2Text(inputDataType).c_str());
226
227 TClonesArray* tracksArray = NULL;
228 int ibForce = 0;
229 if (fUseHLTTracks)
230 {
231 tracksArray = new TClonesArray("AliTRDtrackV1");
232 HLTDebug("BLOCK fPtr 0x%x, fOffset %i, fSize %i, fSpec 0x%x, fDataType %s", block.fPtr, block.fOffset, block.fSize, block.fSpecification, DataType2Text(block.fDataType).c_str());
233 ReadTracks(tracksArray, block.fPtr, block.fSize);
234 }
235 else
9aea5deb 236 {
d679dd6c 237 TObject *objIn = NULL;
238 if (iBlock == 0)
239 objIn = (TObject *)GetFirstInputObject( AliHLTTRDDefinitions::fgkTRDSATracksDataType, "TClonesArray", ibForce);
240 else
241 objIn = (TObject *)GetNextInputObject( ibForce );
242 HLTDebug("1stBLOCK, Pointer = 0x%x", objIn);
243 if (objIn){
244 tracksArray = (TClonesArray* )objIn;
245 }
246 }
247
248 if (tracksArray)
249 {
250 Int_t nbEntries = tracksArray->GetEntries();
251 HLTDebug(" %i TRDtracks in tracksArray", nbEntries);
9aea5deb 252 AliTRDtrackV1* trdTrack = 0x0;
253 for (Int_t i = 0; i < nbEntries; i++){
d679dd6c 254 HLTDebug("%i/%i: ", i+1, nbEntries);
255 trdTrack = (AliTRDtrackV1*)tracksArray->At(i);
256 trdTrack->Print();
9aea5deb 257 fTRDCalibraFillHisto->UpdateHistogramsV1(trdTrack);
258 }
9aea5deb 259 }
260
95259bbb 261
d679dd6c 262 TObjArray *outArray = FormOutput();
263 if (outArray){
264 PushBack(outArray, AliHLTTRDDefinitions::fgkCalibrationDataType);
265 delete outArray;
266 }
267
268 }
95259bbb 269 return 0;
d679dd6c 270
95259bbb 271}
272
d679dd6c 273/**
274 * Read track to the TClonesArray from the memory
275 */
276//============================================================================
277Int_t AliHLTTRDCalibrationComponent::ReadTracks(TClonesArray *outArray, void* inputPtr, AliHLTUInt32_t size)
278{
279 AliHLTUInt8_t* iterPtr = (AliHLTUInt8_t* )inputPtr;
280
281 cout << "\nReading tracks from the Memory\n ============= \n";
282 AliHLTTRDTrack * hltTrack;
283 AliHLTUInt32_t trackSize = 0, curSize = 0;
284 Int_t counter=0;
285
286 while (curSize < size)
287 {
288 hltTrack = (AliHLTTRDTrack*) iterPtr;
289 HLTDebug("curSize %i, size %i",curSize, size);
290
291 trackSize = hltTrack->GetSize();
292 HLTDebug("GetSize() %i", trackSize);
293
294 hltTrack->ReadTrackletsFromMemory(iterPtr + sizeof(AliHLTTRDTrack));
295
296 AliTRDtrackV1* curTRDTrack = new((*outArray)[counter]) AliTRDtrackV1();
297 hltTrack->ExportTRDTrack(curTRDTrack);
298
299 curSize += trackSize;
300 iterPtr += trackSize;
301 counter++;
302 }
303
304 //CheckTrackArray(outArray);
305 return counter;
306}
307
308
d76bc02a 309Int_t AliHLTTRDCalibrationComponent::ShipDataToFXS( const AliHLTComponentEventData& /*evtData*/, AliHLTComponentTriggerData& /*trigData*/)
95259bbb 310{
d679dd6c 311// //Int_t PushToFXS(TObject* pObject, const char* pDetector, const char* pFileID, const char* pDDLNumber = "");
312// //ireturn = PushToFXS(object, "TRD ", "TRDCalib", "1024 ");
313
314 TObjArray *outArray = FormOutput();
315 HLTDebug("Shipping data. Dummy. outputArray: pointer = 0x%x; NEntries = %i;", outArray, outArray->GetEntries());
316 Int_t ireturn = PushToFXS(outArray, "TRD ", "TRDCalib");
317
318 if (outArray){
319 outArray->Delete();
320 delete outArray;
321 }
322
95259bbb 323 return ireturn;
324}
d679dd6c 325
326
327/**
328 * Form output array of histrograms
329 */
330//============================================================================
331TObjArray* AliHLTTRDCalibrationComponent::FormOutput()
332{
333 TObjArray *outArray=new TObjArray(3);
334
335 // gain histo
336 TH2I *hCH2d = fTRDCalibraFillHisto->GetCH2d();
337 outArray->Add(hCH2d);
338
339 // drift velocity histo
340 TProfile2D *hPH2d = fTRDCalibraFillHisto->GetPH2d();
341 outArray->Add(hPH2d);
342
343 // PRF histo
344 TProfile2D *hPRF2d = fTRDCalibraFillHisto->GetPRF2d();
345 outArray->Add(hPRF2d);
346
347 HLTDebug("GetCH2d = 0x%x; NEntries = %i; size = %i", hCH2d, hCH2d->GetEntries(), sizeof(hCH2d));
348 hCH2d->Print();
349 HLTDebug("GetPH2d = 0x%x; NEntries = %i; size = %i", hPH2d, hPH2d->GetEntries(), sizeof(hPH2d));
350 hPH2d->Print();
351 HLTDebug("GetPRF2d = 0x%x; NEntries = %i; size = %i", hPRF2d, hPRF2d->GetEntries(), sizeof(hPRF2d));
352 hPRF2d->Print();
353 HLTDebug("output Array: pointer = 0x%x; NEntries = %i; size = %i", outArray, outArray->GetEntries(), sizeof(outArray));
354
355
356
357 return outArray;
358
359}
360