ROOT logo
//-*- Mode: C++ -*-
// $Id$

#ifndef ALIHLTTASK_H
#define ALIHLTTASK_H
///* This file is property of and copyright by the                          * 
///* ALICE Experiment at CERN, All rights reserved.                         *
///* See cxx source for full Copyright notice                               *

/// @file   AliHLTTask.h
/// @author Matthias Richter
/// @date   
/// @brief  base class for HLT tasks
///

#include <vector>
#include <TObject.h>
#include <TList.h>
#include "AliHLTDataTypes.h"
#include "AliHLTLogging.h"
#include "AliHLTDataBuffer.h"

using std::vector;

struct AliHLTComponentBlockData;
class AliHLTComponent;
class AliHLTComponentHandler;
class AliHLTConfiguration;
class AliHLTTask;

typedef vector<AliHLTTask*> AliHLTTaskPList;

/******************************************************************************/

/**
 * @class AliHLTTask
 * A task collects all the information which is necessary to process a certain
 * step in the HLT data processing chain.
 * - the instance of the component
 *   the task object creates and deletes the component object
 * - the data buffer which receives the result of the component and provides
 *   the data to other tasks/components
 * - a list of all dependencies
 * - a list of consumers
 * - the task object holds an external pointer to the configuration object; 
 *   \b Note: the configuration object must exist through the existence of the
 *   task object!!!
 *  
 *
 * @note This class is only used for the @ref alihlt_system.
 *
 * @ingroup alihlt_system
 */
class AliHLTTask : public TObject, public AliHLTLogging {
 public:
  /** standard constructor */
  AliHLTTask();
  /** constructor 
      @param pConf pointer to configuration descriptor
   */
  AliHLTTask(AliHLTConfiguration* pConf);
  /** destructor */
  virtual ~AliHLTTask();

  /**
   * Initialize the task.
   * The task is initialized with a configuration descriptor. It needs a
   * component handler instance to create the analysis component. The
   * component is created and initialized.
   * @param pConf pointer to configuration descriptor, can be NULL if it
   *              was already provided to the constructor
   * @param pCH   the HLT component handler
   */
  int Init(AliHLTConfiguration* pConf, AliHLTComponentHandler* pCH);

  /**
   * Create the component.
   * @param pConf    configuration descritption
   * @param pCH      component handler
   * @param pComponent [OUT] target to get the component instance
   * @return component instance
   */
  virtual int CreateComponent(AliHLTConfiguration* pConf, AliHLTComponentHandler* pCH, AliHLTComponent*& pComponent) const;

  /**
   * De-Initialize the task.
   * Final cleanup after the run. The @ref AliHLTComponent::Deinit method of
   * the component is called. The analysis component is deleted.
   */
  int Deinit();

  /**
   * Get the name of the object.
   * This is an overridden TObject function in order to return the configuration
   * name instead of the class name. Enables use of TList standard functions.
   * @return name of the configuration
   */
  const char *GetName() const;

  /**
   * Return pointer to configuration.
   * The tasks holds internally the configuration object.
   * @return pointer to configuration
   */
  AliHLTConfiguration* GetConf() const;

  /**
   * Return pointer to component, which the task internally holds.
   * <b>Never delete this object!!!</b>
   * @return instance of the component
   */
  AliHLTComponent* GetComponent() const;

  /**
   * Find a dependency with a certain <i>name id</i>. 
   * Searches in the list of dependencies for a task.
   * @param id      the id of the <b>CONFIGURATION</b><br>
   *                <b>NOTE:</b> the id does NOT specify a COMPONENT
   * @return pointer to task
   */
  AliHLTTask* FindDependency(const char* id);

  /**
   * Add a dependency for the task.
   * The task maintains a list of other tasks it depends on.
   * @param   pDep  pointer to a task descriptor
   * @return 0 if suceeded, neg error code if failed <br>
   *    -EEXIST : the dependencie exists already
   *
   */
  int SetDependency(AliHLTTask* pDep);

  /**
   * Clear a dependency.
   * The ROOT TList touches the object which is in the list, even though
   * it shouldn't care about. Thats why all lists have to be cleared before
   * objects are deleted.
   */
  int UnsetDependency(AliHLTTask* pDep);

  /**
   * Return number of unresolved dependencies.
   * Iterate through all the configurations the task depends on and check
   * whether a corresponding task is available in the list.
   * @return number of unresolved dependencies
   */
  int CheckDependencies();

  /**
   * Check whether the current task depends on the task pTask.
   * @param pTask pointer to Task descriptor
   * @return 1 the current task depends on pTask <br>
   *         0 no dependency <br>
   *         neg. error code if failed
   */
  int Depends(AliHLTTask* pTask);

  /**
   * Find a target with a certain id.
   * Tasks which depend on the current task are referred to be <i>targets</i>. 
   * @param id      configuration id to search for
   * @return pointer to task instance
   */
  AliHLTTask* FindTarget(const char* id);

  /**
   * Insert task into target list.
   * The target list specifies all the tasks which depend on the current task.
   * @param pDep    pointer task object
   * @return >=0 if succeeded, neg. error code if failed 
   */
  int SetTarget(AliHLTTask* pDep);

  /**
   * Clear a target.
   * The ROOT TList touches the object which is in the list, even though
   * it shouldn't care about. Thats why all lists have to be cleared before
   * objects are deleted.
   */
  int UnsetTarget(AliHLTTask* pTarget);

  /**
   * Prepare the task for event processing.
   * The method initializes the Data Buffer and calls the
   * @ref AliHLTComponent::Init method of the component.<br>
   * The @ref ProcessTask method can be called an arbitrary number of times
   * as soon as the task is in <i>running</i> mode. 
   */
  int StartRun();

  /**
   * Clean-up the task after event processing.
   * The method cleans up internal structures.
   */
  int EndRun();

  /**
   * Process the task.
   * If all dependencies are resolved the tasks subscribes to the data of 
   * all source tasks, builds the block descriptor and calls the
   * @ref AliHLTComponent::ProcessEvent method of the component, after
   * processing, the data blocks are released. <br>
   * The @ref StartRun method must be called before.
   */
  int ProcessTask(Int_t eventNo, AliHLTUInt32_t eventType,
		  AliHLTTriggerMask_t trgMask, AliHLTUInt32_t timestamp,
		  AliHLTUInt32_t participatingDetectors = 0);

  /**
   * Determine the number of matching data block between the component and the
   * data buffer of a consumer component. It checks which data types from the
   * list of input data types of the consumer component can be provided by data
   * blocks of the current component.
   * @param pConsumerTask   the task which subscribes to the data
   * @return number of matching data blocks
   */
  int GetNofMatchingDataBlocks(const AliHLTTask* pConsumerTask) const;

  /**
   * Determine the number of matching data types between the component and a
   * consumer component. It checks which data types from the list of input data
   * types of the consumer component can be provided by the current component.
   * @param pConsumerTask   the task which subscribes to the data
   * @return number of matching data types
   */
  int GetNofMatchingDataTypes(const AliHLTTask* pConsumerTask) const;

  /**
   * Subscribe to the data of a source task.
   * The function prepares the block descriptors for subsequent use with the
   * @ref AliHLTComponent::ProcessEvent method, the method prepares all block
   * descriptors which match the input data type of the consumer the function
   * returns the number of blocks which would be prepared in case the target
   * array is big enough.
   * @param pConsumerTask   the task which subscribes to the data
   * @param blockDescList   block descriptor list to be filled
   * @return number of matching data blocks, negative error code if failed
   */
  int Subscribe(const AliHLTTask* pConsumerTask, AliHLTComponentBlockDataList& blockDescList);

  /**
   * Release a block descriptor.
   * Notification from consumer task.  
   * @param pBlockDesc      descriptor of the data segment
   * @param pConsumerTask   the task which subscribed to the data
   * @return: >0 if success, negative error code if failed
   */
  int Release(AliHLTComponentBlockData* pBlockDesc,
	      const AliHLTTask* pConsumerTask);

  /**
   * Cleanup function if the event processing is in error state.
   * In order to handle in particular forwarded segments in the source
   * tasks correctly the tasks of the chain have to subscribe to the
   * parents even if the event is already in error state. This function
   * is used instead of ProcessTask.
   * Subscribes to all source tasks and releases them with out any event
   * processing
   */
  int SubscribeSourcesAndSkip();

  /**
   * Print the status of the task with component, dependencies and targets.
   */
  void PrintStatus();

  /**
   * Overloaded from TObject
   */
  void Print(const char* options) const;

  /**
   * Search task dependency list recursively to find a dependency.
   * @param id              id of the task to search for
   * @param pTgtList        (optional) target list to receive dependency tree
   * @return 0 if not found, >0 found in the n-th level, 
             dependency list in the target list  
   */
  int FollowDependency(const char* id, TList* pTgtList=NULL);

  /**
   * Print the tree for a certain dependency either from the task or
   * configuration list.
   * Each task posseses two "link lists": The configurations are the origin
   * of the  task list. In case of an error during the built of the task list,
   * the dependencies for the task list might be incomplete. In this case the
   * configurations can give infomation on the error reason.  
   * @param id              id of the dependency to search for
   * @param bMode           0 (default) from task dependency list, <br> 
   *                        1 from configuration list
   */
  void PrintDependencyTree(const char* id, int bMode=0);

  /**
   * Get number of source tasks.
   * @return number of source tasks
   */
  int GetNofSources() const {return fListDependencies.GetSize();}

  /**
   * Customized logging function.
   * The task id and pointer is added at the beginning of each message.
   */
  int LoggingVarargs(AliHLTComponentLogSeverity severity, 
		     const char* originClass, const char* originFunc,
		     const char* file, int line, ... ) const;

 private:
  /** prohibited copy constructor */
  AliHLTTask(const AliHLTTask&);
  /** prohibited assignment operator */
  AliHLTTask& operator=(const AliHLTTask&);

  /**
   * Custom initialization for child tasks.
   */
  virtual int CustomInit(AliHLTComponentHandler* pCH);

  /**
   * Custom clean up for child tasks.
   */
  virtual int CustomCleanup();

 protected:
  /** the configuration descriptor (external pointer) */
  AliHLTConfiguration* fpConfiguration;                           //! transient
  /** the component described by this task (created and deleted internally) */
  AliHLTComponent* fpComponent;                                   //! transient
  /** the data buffer for the component processing */
  AliHLTDataBuffer* fpDataBuffer;                                 //! transient

 private:
  /** the list of targets (tasks which depend upon the current one) */
  TList fListTargets;                                             // see above
  /** the list of sources (tasks upon which the current one depends) */ 
  TList fListDependencies;                                        // see above

  /**
   * block data array to be passed as argument to the 
   * @ref AliHLTComponent::ProcessEvent method. 
   * Filled through subscription to source tasks (@ref Subscribe).
   */
  vector<AliHLTComponentBlockData> fBlockDataArray;               //! transient

  ClassDef(AliHLTTask, 0);
};

#endif
 AliHLTTask.h:1
 AliHLTTask.h:2
 AliHLTTask.h:3
 AliHLTTask.h:4
 AliHLTTask.h:5
 AliHLTTask.h:6
 AliHLTTask.h:7
 AliHLTTask.h:8
 AliHLTTask.h:9
 AliHLTTask.h:10
 AliHLTTask.h:11
 AliHLTTask.h:12
 AliHLTTask.h:13
 AliHLTTask.h:14
 AliHLTTask.h:15
 AliHLTTask.h:16
 AliHLTTask.h:17
 AliHLTTask.h:18
 AliHLTTask.h:19
 AliHLTTask.h:20
 AliHLTTask.h:21
 AliHLTTask.h:22
 AliHLTTask.h:23
 AliHLTTask.h:24
 AliHLTTask.h:25
 AliHLTTask.h:26
 AliHLTTask.h:27
 AliHLTTask.h:28
 AliHLTTask.h:29
 AliHLTTask.h:30
 AliHLTTask.h:31
 AliHLTTask.h:32
 AliHLTTask.h:33
 AliHLTTask.h:34
 AliHLTTask.h:35
 AliHLTTask.h:36
 AliHLTTask.h:37
 AliHLTTask.h:38
 AliHLTTask.h:39
 AliHLTTask.h:40
 AliHLTTask.h:41
 AliHLTTask.h:42
 AliHLTTask.h:43
 AliHLTTask.h:44
 AliHLTTask.h:45
 AliHLTTask.h:46
 AliHLTTask.h:47
 AliHLTTask.h:48
 AliHLTTask.h:49
 AliHLTTask.h:50
 AliHLTTask.h:51
 AliHLTTask.h:52
 AliHLTTask.h:53
 AliHLTTask.h:54
 AliHLTTask.h:55
 AliHLTTask.h:56
 AliHLTTask.h:57
 AliHLTTask.h:58
 AliHLTTask.h:59
 AliHLTTask.h:60
 AliHLTTask.h:61
 AliHLTTask.h:62
 AliHLTTask.h:63
 AliHLTTask.h:64
 AliHLTTask.h:65
 AliHLTTask.h:66
 AliHLTTask.h:67
 AliHLTTask.h:68
 AliHLTTask.h:69
 AliHLTTask.h:70
 AliHLTTask.h:71
 AliHLTTask.h:72
 AliHLTTask.h:73
 AliHLTTask.h:74
 AliHLTTask.h:75
 AliHLTTask.h:76
 AliHLTTask.h:77
 AliHLTTask.h:78
 AliHLTTask.h:79
 AliHLTTask.h:80
 AliHLTTask.h:81
 AliHLTTask.h:82
 AliHLTTask.h:83
 AliHLTTask.h:84
 AliHLTTask.h:85
 AliHLTTask.h:86
 AliHLTTask.h:87
 AliHLTTask.h:88
 AliHLTTask.h:89
 AliHLTTask.h:90
 AliHLTTask.h:91
 AliHLTTask.h:92
 AliHLTTask.h:93
 AliHLTTask.h:94
 AliHLTTask.h:95
 AliHLTTask.h:96
 AliHLTTask.h:97
 AliHLTTask.h:98
 AliHLTTask.h:99
 AliHLTTask.h:100
 AliHLTTask.h:101
 AliHLTTask.h:102
 AliHLTTask.h:103
 AliHLTTask.h:104
 AliHLTTask.h:105
 AliHLTTask.h:106
 AliHLTTask.h:107
 AliHLTTask.h:108
 AliHLTTask.h:109
 AliHLTTask.h:110
 AliHLTTask.h:111
 AliHLTTask.h:112
 AliHLTTask.h:113
 AliHLTTask.h:114
 AliHLTTask.h:115
 AliHLTTask.h:116
 AliHLTTask.h:117
 AliHLTTask.h:118
 AliHLTTask.h:119
 AliHLTTask.h:120
 AliHLTTask.h:121
 AliHLTTask.h:122
 AliHLTTask.h:123
 AliHLTTask.h:124
 AliHLTTask.h:125
 AliHLTTask.h:126
 AliHLTTask.h:127
 AliHLTTask.h:128
 AliHLTTask.h:129
 AliHLTTask.h:130
 AliHLTTask.h:131
 AliHLTTask.h:132
 AliHLTTask.h:133
 AliHLTTask.h:134
 AliHLTTask.h:135
 AliHLTTask.h:136
 AliHLTTask.h:137
 AliHLTTask.h:138
 AliHLTTask.h:139
 AliHLTTask.h:140
 AliHLTTask.h:141
 AliHLTTask.h:142
 AliHLTTask.h:143
 AliHLTTask.h:144
 AliHLTTask.h:145
 AliHLTTask.h:146
 AliHLTTask.h:147
 AliHLTTask.h:148
 AliHLTTask.h:149
 AliHLTTask.h:150
 AliHLTTask.h:151
 AliHLTTask.h:152
 AliHLTTask.h:153
 AliHLTTask.h:154
 AliHLTTask.h:155
 AliHLTTask.h:156
 AliHLTTask.h:157
 AliHLTTask.h:158
 AliHLTTask.h:159
 AliHLTTask.h:160
 AliHLTTask.h:161
 AliHLTTask.h:162
 AliHLTTask.h:163
 AliHLTTask.h:164
 AliHLTTask.h:165
 AliHLTTask.h:166
 AliHLTTask.h:167
 AliHLTTask.h:168
 AliHLTTask.h:169
 AliHLTTask.h:170
 AliHLTTask.h:171
 AliHLTTask.h:172
 AliHLTTask.h:173
 AliHLTTask.h:174
 AliHLTTask.h:175
 AliHLTTask.h:176
 AliHLTTask.h:177
 AliHLTTask.h:178
 AliHLTTask.h:179
 AliHLTTask.h:180
 AliHLTTask.h:181
 AliHLTTask.h:182
 AliHLTTask.h:183
 AliHLTTask.h:184
 AliHLTTask.h:185
 AliHLTTask.h:186
 AliHLTTask.h:187
 AliHLTTask.h:188
 AliHLTTask.h:189
 AliHLTTask.h:190
 AliHLTTask.h:191
 AliHLTTask.h:192
 AliHLTTask.h:193
 AliHLTTask.h:194
 AliHLTTask.h:195
 AliHLTTask.h:196
 AliHLTTask.h:197
 AliHLTTask.h:198
 AliHLTTask.h:199
 AliHLTTask.h:200
 AliHLTTask.h:201
 AliHLTTask.h:202
 AliHLTTask.h:203
 AliHLTTask.h:204
 AliHLTTask.h:205
 AliHLTTask.h:206
 AliHLTTask.h:207
 AliHLTTask.h:208
 AliHLTTask.h:209
 AliHLTTask.h:210
 AliHLTTask.h:211
 AliHLTTask.h:212
 AliHLTTask.h:213
 AliHLTTask.h:214
 AliHLTTask.h:215
 AliHLTTask.h:216
 AliHLTTask.h:217
 AliHLTTask.h:218
 AliHLTTask.h:219
 AliHLTTask.h:220
 AliHLTTask.h:221
 AliHLTTask.h:222
 AliHLTTask.h:223
 AliHLTTask.h:224
 AliHLTTask.h:225
 AliHLTTask.h:226
 AliHLTTask.h:227
 AliHLTTask.h:228
 AliHLTTask.h:229
 AliHLTTask.h:230
 AliHLTTask.h:231
 AliHLTTask.h:232
 AliHLTTask.h:233
 AliHLTTask.h:234
 AliHLTTask.h:235
 AliHLTTask.h:236
 AliHLTTask.h:237
 AliHLTTask.h:238
 AliHLTTask.h:239
 AliHLTTask.h:240
 AliHLTTask.h:241
 AliHLTTask.h:242
 AliHLTTask.h:243
 AliHLTTask.h:244
 AliHLTTask.h:245
 AliHLTTask.h:246
 AliHLTTask.h:247
 AliHLTTask.h:248
 AliHLTTask.h:249
 AliHLTTask.h:250
 AliHLTTask.h:251
 AliHLTTask.h:252
 AliHLTTask.h:253
 AliHLTTask.h:254
 AliHLTTask.h:255
 AliHLTTask.h:256
 AliHLTTask.h:257
 AliHLTTask.h:258
 AliHLTTask.h:259
 AliHLTTask.h:260
 AliHLTTask.h:261
 AliHLTTask.h:262
 AliHLTTask.h:263
 AliHLTTask.h:264
 AliHLTTask.h:265
 AliHLTTask.h:266
 AliHLTTask.h:267
 AliHLTTask.h:268
 AliHLTTask.h:269
 AliHLTTask.h:270
 AliHLTTask.h:271
 AliHLTTask.h:272
 AliHLTTask.h:273
 AliHLTTask.h:274
 AliHLTTask.h:275
 AliHLTTask.h:276
 AliHLTTask.h:277
 AliHLTTask.h:278
 AliHLTTask.h:279
 AliHLTTask.h:280
 AliHLTTask.h:281
 AliHLTTask.h:282
 AliHLTTask.h:283
 AliHLTTask.h:284
 AliHLTTask.h:285
 AliHLTTask.h:286
 AliHLTTask.h:287
 AliHLTTask.h:288
 AliHLTTask.h:289
 AliHLTTask.h:290
 AliHLTTask.h:291
 AliHLTTask.h:292
 AliHLTTask.h:293
 AliHLTTask.h:294
 AliHLTTask.h:295
 AliHLTTask.h:296
 AliHLTTask.h:297
 AliHLTTask.h:298
 AliHLTTask.h:299
 AliHLTTask.h:300
 AliHLTTask.h:301
 AliHLTTask.h:302
 AliHLTTask.h:303
 AliHLTTask.h:304
 AliHLTTask.h:305
 AliHLTTask.h:306
 AliHLTTask.h:307
 AliHLTTask.h:308
 AliHLTTask.h:309
 AliHLTTask.h:310
 AliHLTTask.h:311
 AliHLTTask.h:312
 AliHLTTask.h:313
 AliHLTTask.h:314
 AliHLTTask.h:315
 AliHLTTask.h:316
 AliHLTTask.h:317
 AliHLTTask.h:318
 AliHLTTask.h:319
 AliHLTTask.h:320
 AliHLTTask.h:321
 AliHLTTask.h:322
 AliHLTTask.h:323
 AliHLTTask.h:324
 AliHLTTask.h:325
 AliHLTTask.h:326
 AliHLTTask.h:327
 AliHLTTask.h:328
 AliHLTTask.h:329
 AliHLTTask.h:330
 AliHLTTask.h:331
 AliHLTTask.h:332
 AliHLTTask.h:333
 AliHLTTask.h:334
 AliHLTTask.h:335
 AliHLTTask.h:336
 AliHLTTask.h:337
 AliHLTTask.h:338
 AliHLTTask.h:339
 AliHLTTask.h:340
 AliHLTTask.h:341
 AliHLTTask.h:342
 AliHLTTask.h:343
 AliHLTTask.h:344
 AliHLTTask.h:345
 AliHLTTask.h:346
 AliHLTTask.h:347
 AliHLTTask.h:348