Using common HLT track data format for TRD tracks (Theodor)
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDClusterizerComponent.cxx
CommitLineData
0af7cb2e 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 AliHLTTRDClusterizerComponent.cxx
20 @author Timm Steinbeck, Matthias Richter
21 @date
22 @brief A TRDClusterizer processing component for the HLT. */
23
c7500dae 24// see header file for class documentation //
25// or //
26// refer to README to build package //
27// or //
28// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt //
29
0af7cb2e 30#if __GNUC__ >= 3
31using namespace std;
32#endif
33
051a0e2d 34#include "TTree.h"
35#include "TFile.h"
36#include "TBranch.h"
37
0af7cb2e 38#include "AliHLTTRDClusterizerComponent.h"
39#include "AliHLTTRDDefinitions.h"
d679dd6c 40#include "AliHLTTRDCluster.h"
0af7cb2e 41
886e8d3d 42#include "AliGeomManager.h"
43#include "AliTRDReconstructor.h"
0af7cb2e 44#include "AliCDBManager.h"
dc2e6604 45#include "AliHLTTRDClusterizer.h"
519f385f 46#include "AliTRDrecoParam.h"
47#include "AliTRDrawStreamBase.h"
d679dd6c 48#include "AliTRDcluster.h"
519f385f 49
0af7cb2e 50#include "AliRawReaderMemory.h"
51
d679dd6c 52#ifdef HAVE_VALGRIND_CALLGRIND_H
53#include <valgrind/callgrind.h>
54#else
e3e5ac39 55#define CALLGRIND_START_INSTRUMENTATION do { } while (0)
56#define CALLGRIND_STOP_INSTRUMENTATION do { } while (0)
d679dd6c 57#endif
58
0af7cb2e 59#include <cstdlib>
60#include <cerrno>
61#include <string>
62
18ada816 63ClassImp(AliHLTTRDClusterizerComponent)
3b021c25 64
d679dd6c 65AliHLTTRDClusterizerComponent::AliHLTTRDClusterizerComponent():
66 AliHLTProcessor(),
67 fOutputPercentage(100), // By default we copy to the output exactly what we got as input
162637e4 68 fStrorageDBpath("local://$ALICE_ROOT/OCDB"),
d679dd6c 69 fClusterizer(NULL),
70 fRecoParam(NULL),
71 fCDB(NULL),
72 fMemReader(NULL),
73 fReconstructor(NULL),
dc2e6604 74 fGeometryFileName("")
0af7cb2e 75{
3b021c25 76 // Default constructor
051a0e2d 77
78 fGeometryFileName = getenv("ALICE_ROOT");
79 fGeometryFileName += "/HLT/TRD/geometry.root";
0af7cb2e 80}
81
82AliHLTTRDClusterizerComponent::~AliHLTTRDClusterizerComponent()
83{
3b021c25 84 // Destructor
886e8d3d 85 // Work is Done in DoDeInit()
0af7cb2e 86}
d679dd6c 87
d679dd6c 88
0af7cb2e 89const char* AliHLTTRDClusterizerComponent::GetComponentID()
90{
3b021c25 91 // Return the component ID const char *
0af7cb2e 92 return "TRDClusterizer"; // The ID of this component
93}
94
95void AliHLTTRDClusterizerComponent::GetInputDataTypes( vector<AliHLTComponent_DataType>& list)
96{
3b021c25 97 // Get the list of input data
0af7cb2e 98 list.clear(); // We do not have any requirements for our input data type(s).
efbd3157 99 list.push_back( AliHLTTRDDefinitions::fgkDDLRawDataType );
0af7cb2e 100}
101
102AliHLTComponent_DataType AliHLTTRDClusterizerComponent::GetOutputDataType()
103{
3b021c25 104 // Get the output data type
efbd3157 105 return AliHLTTRDDefinitions::fgkClusterDataType;
0af7cb2e 106}
107
108void AliHLTTRDClusterizerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
109{
3b021c25 110 // Get the output data size
0af7cb2e 111 constBase = 0;
112 inputMultiplier = ((double)fOutputPercentage)/100.0;
113}
114
0af7cb2e 115AliHLTComponent* AliHLTTRDClusterizerComponent::Spawn()
116{
3b021c25 117 // Spawn function, return new instance of this class
0af7cb2e 118 return new AliHLTTRDClusterizerComponent;
119};
120
121int AliHLTTRDClusterizerComponent::DoInit( int argc, const char** argv )
122{
123 // perform initialization. We check whether our relative output size is specified in the arguments.
124 fOutputPercentage = 100;
d137c50b 125 Int_t iRawDataVersion = 2;
0af7cb2e 126 int i = 0;
127 char* cpErr;
519f385f 128
129 Int_t iRecoParamType = -1; // default will be the low flux
130
131 // the data type will become obsolete as soon as the formats are established
132 Int_t iRecoDataType = -1; // default will be simulation
18ada816 133 Int_t iyPosMethod = 1; // 0=COG 1=LUT 2=Gauss
519f385f 134
0af7cb2e 135 while ( i < argc )
136 {
886e8d3d 137 HLTDebug("argv[%d] == %s", i, argv[i] );
0af7cb2e 138 if ( !strcmp( argv[i], "output_percentage" ) )
139 {
140 if ( i+1>=argc )
141 {
886e8d3d 142 HLTError("Missing output_percentage parameter");
0af7cb2e 143 return ENOTSUP;
144 }
886e8d3d 145 HLTDebug("argv[%d+1] == %s", i, argv[i+1] );
0af7cb2e 146 fOutputPercentage = strtoul( argv[i+1], &cpErr, 0 );
147 if ( *cpErr )
148 {
886e8d3d 149 HLTError("Cannot convert output_percentage parameter '%s'", argv[i+1] );
0af7cb2e 150 return EINVAL;
151 }
886e8d3d 152 HLTInfo("Output percentage set to %lu %%", fOutputPercentage );
0af7cb2e 153 i += 2;
0af7cb2e 154 }
9aea5deb 155 else if ( strcmp( argv[i], "-cdb" ) == 0)
0af7cb2e 156 {
157 if ( i+1 >= argc )
158 {
886e8d3d 159 HLTError("Missing -cdb argument");
0af7cb2e 160 return ENOTSUP;
161 }
162 fStrorageDBpath = argv[i+1];
886e8d3d 163 HLTInfo("DB storage is %s", fStrorageDBpath.c_str() );
0af7cb2e 164 i += 2;
18ada816 165 }
9aea5deb 166 else if ( strcmp( argv[i], "-lowflux" ) == 0)
519f385f 167 {
168 iRecoParamType = 0;
169 HLTDebug("Low flux reco selected.");
170 i++;
18ada816 171 }
9aea5deb 172 else if ( strcmp( argv[i], "-highflux" ) == 0)
519f385f 173 {
174 iRecoParamType = 1;
886e8d3d 175 HLTDebug("High flux reco selected.");
176 i++;
18ada816 177 }
9aea5deb 178 else if ( strcmp( argv[i], "-cosmics" ) == 0)
886e8d3d 179 {
180 iRecoParamType = 2;
181 HLTDebug("Cosmic test reco selected.");
519f385f 182 i++;
18ada816 183 }
519f385f 184 // raw data type - sim or experiment
9aea5deb 185 else if ( strcmp( argv[i], "-simulation" ) == 0)
519f385f 186 {
187 iRecoDataType = 0;
188 i++;
519f385f 189 }
9aea5deb 190 else if ( strcmp( argv[i], "-experiment" ) == 0)
519f385f 191 {
192 iRecoDataType = 1;
193 i++;
519f385f 194 }
9aea5deb 195 else if ( strcmp( argv[i], "-rawver" ) == 0)
0af7cb2e 196 {
197 if ( i+1 >= argc )
198 {
886e8d3d 199 HLTError("Missing -rawver argument");
0af7cb2e 200 return ENOTSUP;
201 }
d137c50b 202 iRawDataVersion = atoi( argv[i+1] );
886e8d3d 203 HLTInfo("Raw data version is %d", iRawDataVersion );
0af7cb2e 204 i += 2;
9aea5deb 205
0af7cb2e 206 }
207
9aea5deb 208 else if ( strcmp( argv[i], "-geometry" ) == 0)
051a0e2d 209 {
210 if ( i+1 >= argc )
211 {
886e8d3d 212 HLTError("Missing -geometry argument");
051a0e2d 213 return ENOTSUP;
214 }
215 fGeometryFileName = argv[i+1];
886e8d3d 216 HLTInfo("GeomFile storage is %s", fGeometryFileName.c_str() );
051a0e2d 217 i += 2;
18ada816 218 }
219
220 else if ( strcmp( argv[i], "-yPosMethod" ) == 0)
221 {
222 if ( i+1 >= argc )
223 {
224 HLTError("Missing -yPosMethod argument");
225 return ENOTSUP;
226 }
227 if( strcmp(argv[i], "COG") )
228 iyPosMethod=0;
229 else if( strcmp(argv[i], "LUT") )
230 iyPosMethod=1;
231 else if( strcmp(argv[i], "Gauss") )
232 iyPosMethod=2;
233 else {
234 HLTError("Unknown -yPosMethod argument");
235 return ENOTSUP;
236 }
237 i += 2;
238 }
239
9aea5deb 240 else{
241 HLTError("Unknown option '%s'", argv[i] );
242 return EINVAL;
243 }
244
0af7cb2e 245 }
246
519f385f 247 // THE "REAL" INIT COMES HERE
248
886e8d3d 249 if (iRecoParamType < 0 || iRecoParamType > 2)
519f385f 250 {
251 HLTWarning("No reco param selected. Use -lowflux or -highflux flag. Defaulting to low flux.");
252 iRecoParamType = 0;
253 }
254
255 if (iRecoParamType == 0)
256 {
257 fRecoParam = AliTRDrecoParam::GetLowFluxParam();
258 HLTDebug("Low flux params init.");
259 }
260
261 if (iRecoParamType == 1)
262 {
263 fRecoParam = AliTRDrecoParam::GetHighFluxParam();
264 HLTDebug("High flux params init.");
265 }
266
886e8d3d 267 if (iRecoParamType == 2)
268 {
269 fRecoParam = AliTRDrecoParam::GetCosmicTestParam();
270 HLTDebug("Cosmic Test params init.");
271 }
272
519f385f 273 if (fRecoParam == 0)
274 {
275 HLTError("No reco params initialized. Sniffing big trouble!");
276 return -1;
277 }
278
0d66dbf5 279 fReconstructor = new AliTRDReconstructor();
280 fReconstructor->SetRecoParam(fRecoParam);
0134491a 281 fReconstructor->SetStreamLevel(0, AliTRDReconstructor::kClusterizer); // default value
dc2e6604 282 HLTInfo("Not writing clusters. I.e. output is a TClonesArray of clusters");
18ada816 283 TString recoOptions="hlt,!cw,sl_cf_0";
e3e5ac39 284 switch(iRecoDataType){
285 case 0: recoOptions += ",tc"; break;
286 case 1: recoOptions += ",!tc"; break;
287 }
18ada816 288 switch(iyPosMethod){
289 case 0: recoOptions += ",!gs,!lut"; break;
290 case 1: recoOptions += ",!gs,lut"; break;
291 case 2: recoOptions += ",gs,!lut"; break;
292 }
0134491a 293
e3e5ac39 294 fReconstructor->SetOption(recoOptions.Data());
0134491a 295
519f385f 296 // init the raw data type to be used...
297 // the switch here will become obsolete as soon as the data structures is fixed
298 // both: in sim and reality
299 if (iRecoDataType < 0 || iRecoDataType > 1)
300 {
301 HLTWarning("No data type selected. Use -simulation or -experiment flag. Defaulting to simulation.");
302 iRecoDataType = 0;
303 }
304
305 if (iRecoDataType == 0)
306 {
307 AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDsimStream);
308 HLTDebug("Data type expected is SIMULATION!");
309 }
310
311 if (iRecoDataType == 1)
312 {
313 AliTRDrawStreamBase::SetRawStreamVersion(AliTRDrawStreamBase::kTRDrealStream);
314 HLTDebug("Data type expected is EXPERIMENT!");
315 }
316
317 // the DATA BASE STUFF
3b021c25 318 fCDB = AliCDBManager::Instance();
319 if (!fCDB)
0af7cb2e 320 {
9aea5deb 321 HLTError("Could not get CDB instance", "fCDB 0x%x", fCDB);
0af7cb2e 322 }
323 else
324 {
3b021c25 325 fCDB->SetRun(0); // THIS HAS TO BE RETRIEVED !!!
326 fCDB->SetDefaultStorage(fStrorageDBpath.c_str());
886e8d3d 327 HLTDebug("CDB instance; fCDB 0x%x", fCDB);
0af7cb2e 328 }
329
d679dd6c 330 if((AliGeomManager::GetGeometry()) == NULL){
331
332 if ( TFile::Open(fGeometryFileName.c_str())) {
886e8d3d 333 AliGeomManager::LoadGeometry(fGeometryFileName.c_str());
051a0e2d 334 }
d679dd6c 335 else {
336 HLTError("Cannot load geometry from file %s",fGeometryFileName.c_str());
337 return EINVAL;
051a0e2d 338 }
d679dd6c 339 }
340 else
341 HLTInfo("Geometry Already Loaded");
342
3b021c25 343 fMemReader = new AliRawReaderMemory;
051a0e2d 344
dc2e6604 345 fClusterizer = new AliHLTTRDClusterizer("TRDCclusterizer", "TRDCclusterizer");
9aea5deb 346 fClusterizer->SetReconstructor(fReconstructor);
56397b53 347 fClusterizer->SetUseLabels(kFALSE);
d137c50b 348 fClusterizer->SetRawVersion(iRawDataVersion);
0af7cb2e 349 return 0;
350}
351
352int AliHLTTRDClusterizerComponent::DoDeinit()
353{
3b021c25 354 // Deinitialization of the component
355 delete fMemReader;
356 fMemReader = 0;
357 delete fClusterizer;
358 fClusterizer = 0;
9aea5deb 359
360 fReconstructor->SetClusters(0x0);
0d66dbf5 361 delete fReconstructor;
362 fReconstructor = 0x0;
0af7cb2e 363 return 0;
364
051a0e2d 365
3b021c25 366 if (fCDB)
0af7cb2e 367 {
886e8d3d 368 HLTDebug("destroy fCDB");
3b021c25 369 fCDB->Destroy();
370 fCDB = 0;
0af7cb2e 371 }
519f385f 372
373 if (fRecoParam)
374 {
375 HLTDebug("Deleting fRecoParam");
376 delete fRecoParam;
377 fRecoParam = 0;
378 }
0af7cb2e 379}
380
886e8d3d 381int AliHLTTRDClusterizerComponent::DoEvent( const AliHLTComponentEventData& evtData,
382 const AliHLTComponentBlockData* blocks,
383 AliHLTComponent_TriggerData& /*trigData*/,
d679dd6c 384 AliHLTUInt8_t* outputPtr,
a28bfa0d 385 AliHLTUInt32_t& size,
d679dd6c 386 vector<AliHLTComponent_BlockData>& outputBlocks )
0af7cb2e 387{
3b021c25 388 // Process an event
886e8d3d 389 HLTDebug( "NofBlocks %lu", evtData.fBlockCnt );
0af7cb2e 390 // Process an event
d679dd6c 391 AliHLTUInt32_t totalSize = 0, offset = 0;
0af7cb2e 392
051a0e2d 393 //implement a usage of the following
519f385f 394 // AliHLTUInt32_t triggerDataStructSize = trigData.fStructSize;
395 // AliHLTUInt32_t triggerDataSize = trigData.fDataSize;
396 // void *triggerData = trigData.fData;
886e8d3d 397 //HLTDebug( "Trigger data received. Struct size %d Data size %d Data location 0x%x", trigData.fStructSize, trigData.fDataSize, (UInt_t*)trigData.fData);
051a0e2d 398
0af7cb2e 399 // Loop over all input blocks in the event
886e8d3d 400 AliHLTComponentDataType expectedDataType = (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD);
0af7cb2e 401 for ( unsigned long i = 0; i < evtData.fBlockCnt; i++ )
402 {
d679dd6c 403 if (evtData.fEventID == 1)
e3e5ac39 404 CALLGRIND_START_INSTRUMENTATION;
d679dd6c 405
406 const AliHLTComponentBlockData &block = blocks[i];
519f385f 407 // lets not use the internal TRD data types here : AliHLTTRDDefinitions::fgkDDLRawDataType
408 // which is depreciated - we use HLT global defs instead
d679dd6c 409 // if ( block.fDataType != (kAliHLTDataTypeDDLRaw | kAliHLTDataOriginTRD) )
410 AliHLTComponentDataType inputDataType = block.fDataType;
886e8d3d 411 if ( inputDataType != expectedDataType)
0af7cb2e 412 {
9aea5deb 413 HLTDebug( "Block # %i/%i; Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s; Skipping",
414 i, evtData.fBlockCnt,
415 evtData.fEventID, evtData.fEventID,
416 DataType2Text(inputDataType).c_str(),
417 DataType2Text(expectedDataType).c_str());
0af7cb2e 418 continue;
419 }
9aea5deb 420 else
421 {
d679dd6c 422 HLTDebug("We get the right data type: Block # %i/%i; Event 0x%08LX (%Lu) Received datatype: %s",
423 i, evtData.fBlockCnt,
424 evtData.fEventID, evtData.fEventID,
425 DataType2Text(inputDataType).c_str());
9aea5deb 426 }
427
886e8d3d 428 // fMemReader->Reset();
d679dd6c 429 fMemReader->SetMemory((UChar_t*) block.fPtr, block.fSize);
886e8d3d 430
d679dd6c 431 AliHLTUInt32_t spec = block.fSpecification;
886e8d3d 432
433 Int_t id = 1024;
434
435 for ( Int_t ii = 0; ii < 18 ; ii++ ) {
436 if ( spec & 0x00000001 ) {
437 id += ii;
438 break;
439 }
440 spec = spec >> 1 ;
441 }
886e8d3d 442
d679dd6c 443 fMemReader->SetEquipmentID( id );
444
18ada816 445 fClusterizer->SetMemBlock((AliHLTUInt8_t*)(outputPtr+offset));
886e8d3d 446 Bool_t iclustered = fClusterizer->Raw2ClustersChamber(fMemReader);
447 if (iclustered == kTRUE)
0af7cb2e 448 {
886e8d3d 449 HLTDebug( "Clustered successfully");
450 }
451 else
452 {
453 HLTError("Clustering ERROR");
0af7cb2e 454 return -1;
455 }
519f385f 456
d679dd6c 457 // put the tree into output
886e8d3d 458 //fcTree->Print();
dc2e6604 459
460 AliHLTUInt32_t addedSize = fClusterizer->GetAddedSize();
461 if (addedSize > 0){
462 // Using low-level interface
463 // with interface classes
464 totalSize += addedSize;
465 if ( totalSize > size )
0134491a 466 {
dc2e6604 467 HLTError("Too much data; Data written over allowed buffer. Amount written: %lu, allowed amount: %lu.",
468 totalSize, size );
469 return EMSGSIZE;
0134491a 470 }
d679dd6c 471
dc2e6604 472 // Fill block
473 AliHLTComponentBlockData bd;
474 FillBlockData( bd );
475 bd.fOffset = offset;
476 bd.fSize = addedSize;
477 //bd.fSpecification = spec;
478 bd.fSpecification = gkAliEventTypeData;
479 bd.fDataType = AliHLTTRDDefinitions::fgkClusterDataType;
480 outputBlocks.push_back( bd );
18ada816 481 HLTDebug( "BD fPtr 0x%x, fOffset %i, size %i, dataType %s, spec 0x%x ", bd.fPtr, bd.fOffset, bd.fSize, DataType2Text(bd.fDataType).c_str(), spec);
482 offset = totalSize;
d679dd6c 483
0134491a 484 }
dc2e6604 485 else
486 HLTWarning("Array of clusters is empty!");
487 }
d679dd6c 488 fReconstructor->SetClusters(0x0);
b39f18ce 489
d679dd6c 490 size = totalSize;
491 HLTDebug("Event is done. size written to the output is %i", size);
492 return 0;
493}
494
d679dd6c 495void AliHLTTRDClusterizerComponent::PrintObject( TClonesArray* inClustersArray)
496{
497 AliTRDcluster* cluster=0x0;
9aea5deb 498
d679dd6c 499 for (Int_t i=0; i < inClustersArray->GetEntriesFast(); i++){
500 cluster = dynamic_cast<AliTRDcluster*>(inClustersArray->At(i));
501 HLTDebug("cluster[%i]",i);
502 HLTDebug(" PadCol = %i; PadRow = %i; PadTime = %i", cluster->GetPadCol(), cluster->GetPadRow(), cluster->GetPadTime());
503 HLTDebug(" Detector = %i, Amplitude = %f, Center = %f", cluster->GetDetector(), cluster->GetQ(), cluster->GetCenter());
504 HLTDebug(" LocalTimeBin = %i; NPads = %i; maskedPosition: %s, status: %s", cluster->GetLocalTimeBin(), cluster->GetNPads(),cluster->GetPadMaskedPosition(),cluster->GetPadMaskedPosition());
505 }
9aea5deb 506
0af7cb2e 507}
d679dd6c 508