]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTOUTHandlerEsdBranch.cxx
automatically added data sink components are now added directly to the internal insta...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTOUTHandlerEsdBranch.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT 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   AliHLTOUTHandlerEsdBranch.cxx
20 /// @author Matthias Richter
21 /// @date   01.07.2010
22 /// @brief  HLTOUT handler of type kEsd to merge objects into the hltEsd.
23
24 #include "AliHLTOUTHandlerEsdBranch.h"
25 #include "AliHLTOUT.h"
26 #include "AliHLTMessage.h"
27 #include "AliHLTErrorGuard.h"
28 #include "AliHLTEsdManager.h"
29 #include "AliHLTComponent.h" // DataType2Text
30 #include "AliHLTMisc.h"
31 #include "TString.h"
32 #include "TObjString.h"
33 #include "TObjArray.h"
34 #include "TArrayC.h"
35 #include <cassert>
36
37 /** ROOT macro for the implementation of ROOT specific class methods */
38 ClassImp(AliHLTOUTHandlerEsdBranch)
39
40 AliHLTOUTHandlerEsdBranch::AliHLTOUTHandlerEsdBranch(const char* branchname)
41   : AliHLTOUTHandler() 
42   , fBranch(branchname)
43   , fESD(NULL)
44   , fpData(NULL)
45   , fSize(0)
46   , fManager(NULL)
47
48   // see header file for class documentation
49   // or
50   // refer to README to build package
51   // or
52   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
53 }
54
55 AliHLTOUTHandlerEsdBranch::~AliHLTOUTHandlerEsdBranch()
56 {
57   // see header file for class documentation
58   if (fESD) fManager->DestroyEsdEvent(fESD);
59   fESD=NULL;
60   if (fpData) delete fpData;
61   fpData=NULL;
62   if (fManager) AliHLTEsdManager::Delete(fManager);
63   fManager=NULL;
64 }
65
66 int AliHLTOUTHandlerEsdBranch::ProcessData(AliHLTOUT* pData)
67 {
68   // see header file for class documentation
69   if (!pData) return -EINVAL;
70   int iResult=0;
71
72   if (CheckStatus(kHandlerError)) {
73     HLTWarning("kEsd handler for ESD branch '%s' in error state, skipping processing of associated HLTOUT blocks", fBranch.Data());
74     return -EPERM;
75   }
76
77   if (!fManager) {
78     fManager=AliHLTMisc::LoadInstance((AliHLTEsdManager*)NULL, "AliHLTEsdManagerImplementation", "libHLTrec.so");
79   }
80
81   if (!fManager) {
82     AliHLTComponentDataType dt=kAliHLTVoidDataType;
83     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
84     if (pData->SelectFirstDataBlock()>=0) {
85       pData->GetDataBlockDescription(dt, spec);
86       ALIHLTERRORGUARD(1, "failed to create AliHLTEsdManagerImplementation object, skipping handling of HLTOUT data %s 0x%80x", AliHLTComponent::DataType2Text(dt).c_str(), spec);
87     }  
88     return -ENOSYS;
89   }
90
91   if (!fESD) {
92     // create the ESD container, but without std content
93     fESD = fManager->CreateEsdEvent();
94   }
95   if (!fpData) fpData=new TArrayC;
96   if (fESD && fpData) {
97     fManager->ResetEsdEvent(fESD);
98     iResult=ExtractAndAddObjects(pData);
99   }
100
101   AliHLTMessage* pMsg=AliHLTMessage::Stream(fESD);
102   if (pMsg) {
103     if (!pMsg->CompBuffer()) {
104       fSize=pMsg->Length();
105       fpData->Set(fSize, pMsg->Buffer());
106     } else {
107       fSize=pMsg->CompLength();
108       fpData->Set(fSize, pMsg->CompBuffer());
109     }
110     delete pMsg;
111     pMsg=NULL;
112   } else {
113     HLTError("streaming of object failed");
114   }
115
116   return iResult;
117 }
118
119 int AliHLTOUTHandlerEsdBranch::ExtractAndAddObjects(AliHLTOUT* pData)
120 {
121   // Default method
122   // Extract streamed object from the HLTOUT and add to ESD
123   // The default method works only for single blocks in the HLTOUT,
124   // A specific child class is required if multiple blocks should be
125   // treated.
126
127   if (!fESD || !fManager) return -ENOSYS;
128   int iResult=0;
129   iResult=pData->SelectFirstDataBlock(); 
130   if (iResult<0) return iResult;
131
132   TObject* pObject=pData->GetDataObject();
133   if (pObject) {
134     TString bname=fBranch;
135     if (bname.IsNull()) {
136       bname=pObject->GetName();
137       if (bname.CompareTo(pObject->ClassName())==0) {
138         ALIHLTERRORGUARD(5, "no branch name specified for unnamed object %s, not added to ESD", bname.Data());
139         bname="";
140       }
141     }
142     if (!bname.IsNull()) {
143       iResult=fManager->AddObject(fESD, pObject, bname.Data());
144     } else {
145       iResult=-EBADF;
146     }
147     pData->ReleaseDataObject(pObject);
148     pObject=NULL;
149   } else {
150     AliHLTComponentDataType dt=kAliHLTVoidDataType;
151     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
152     pData->GetDataBlockDescription(dt, spec);
153     HLTError("Can not get TObject from HLTOUT buffer for block %s 0x%x", AliHLTComponent::DataType2Text(dt).c_str(), spec);
154     iResult=-ENODATA;
155   }
156
157   if (pData->SelectNextDataBlock()>=0) {
158     ALIHLTERRORGUARD(5, "the default function can only handle one single data block/object");
159   }
160
161   return iResult;
162 }
163
164 int AliHLTOUTHandlerEsdBranch::GetProcessedData(const AliHLTUInt8_t* &pData)
165 {
166   // see header file for class documentation
167   if (!fpData) {
168     pData=NULL;
169     return 0;
170   }
171
172   pData=reinterpret_cast<AliHLTUInt8_t*>(fpData->GetArray());
173   return fSize;
174 }
175
176 int AliHLTOUTHandlerEsdBranch::ReleaseProcessedData(const AliHLTUInt8_t* pData, int size)
177 {
178   // see header file for class documentation
179   int iResult=0;
180   if (!fpData || size != fSize ||
181       const_cast<AliHLTUInt8_t*>(pData) != reinterpret_cast<AliHLTUInt8_t*>(fpData->GetArray())) {
182     HLTError("attempt to release to wrong data buffer %p size %d, expected %p size %d", pData, size, fpData?fpData->GetArray():NULL, fSize);
183   }
184   fSize=0;
185   return iResult;
186 }