3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /** @file AliHLTEsdManager.cxx
20 @author Matthias Richter
22 @brief Manager for merging and writing of HLT ESDs
25 #include "AliHLTEsdManager.h"
26 #include "AliHLTComponent.h"
27 #include "AliESDEvent.h"
28 #include "AliHLTMessage.h"
29 #include "AliESDEvent.h"
34 #include "TObjectTable.h"
36 /** ROOT macro for the implementation of ROOT specific class methods */
37 ClassImp(AliHLTEsdManager)
39 AliHLTEsdManager::AliHLTEsdManager()
43 // see header file for class documentation
45 // refer to README to build package
47 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
50 AliHLTEsdManager::~AliHLTEsdManager()
52 // see header file for class documentation
53 for (unsigned int i=0; i<fESDs.size(); i++) {
61 AliHLTEsdManager::AliHLTEsdListEntry* AliHLTEsdManager::Find(AliHLTComponentDataType dt) const
63 // see header file for class documentation
64 AliHLTEsdListEntry* pEntry=NULL;
65 for (unsigned int i=0; i<fESDs.size(); i++) {
66 if (fESDs[i] && *(fESDs[i])==dt) {
67 pEntry=const_cast<AliHLTEsdListEntry*>(fESDs[i]);
73 int AliHLTEsdManager::WriteESD(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
74 AliHLTComponentDataType dt, AliESDEvent* tgtesd, int eventno)
76 // see header file for class documentation
77 if (!pBuffer && size<=0) return -EINVAL;
79 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
80 if (firstWord==size-sizeof(AliHLTUInt32_t)) {
81 HLTDebug("create object from block %s size %d", AliHLTComponent::DataType2Text(dt).c_str(), size);
82 AliHLTMessage msg(const_cast<AliHLTUInt8_t*>(pBuffer), size);
83 TClass* objclass=msg.GetClass();
84 TObject* pObj=msg.ReadObject(objclass);
85 if (pObj && objclass) {
86 HLTDebug("object %p type %s created", pObj, objclass->GetName());
87 AliESDEvent* pESD=dynamic_cast<AliESDEvent*>(pObj);
90 pTree=dynamic_cast<TTree*>(pObj);
93 pESD->CreateStdContent();
94 if (pTree->GetEntries()>0) {
95 if (pTree->GetEntries()>1) {
96 HLTWarning("only one entry allowed for ESD embedded into tree, data block %s contains tree with %d entires, taking first entry",
97 AliHLTComponent::DataType2Text(dt).c_str(), pTree->GetEntries());
99 pESD->ReadFromTree(pTree);
103 HLTWarning("tree of data block %s has no events, skipping data block", AliHLTComponent::DataType2Text(dt).c_str());
107 AliHLTEsdListEntry* entry=Find(dt);
109 AliHLTEsdListEntry* newEntry=new AliHLTEsdListEntry(dt);
110 fESDs.push_back(newEntry);
116 entry->WriteESD(pESD, eventno);
118 HLTError("internal mismatch, can not create list entry");
122 HLTWarning("data block %s is not of class type AliESDEvent, ignoring ...", AliHLTComponent::DataType2Text(dt).c_str());
125 // ESD has been created and must be cleaned up
137 AliHLTEsdManager::AliHLTEsdListEntry::AliHLTEsdListEntry(AliHLTComponentDataType dt)
145 // see header file for class documentation
148 AliHLTEsdManager::AliHLTEsdListEntry::~AliHLTEsdListEntry()
150 // see header file for class documentation
152 fpTree->GetUserInfo()->Clear();
157 // due to the Root garbage collection the ESD object might already be
158 // deleted since the pTree->GetUserInfo()->Add(pESD) adds the ESD object to
159 // an internal list which is cleaned when the tree is deleted
160 if (fpEsd && gObjectTable->PtrIsValid(fpEsd)) {
172 bool AliHLTEsdManager::AliHLTEsdListEntry::operator==(AliHLTComponentDataType dt) const
174 // see header file for class documentation
178 int AliHLTEsdManager::AliHLTEsdListEntry::WriteESD(AliESDEvent* pESD, int eventno)
180 // see header file for class documentation
181 if (!pESD) return -EINVAL;
185 origin.Insert(0, fDt.fOrigin, kAliHLTComponentDataTypefOriginSize);
186 origin.Remove(TString::kTrailing, ' ');
188 fName="AliHLT"; fName+=origin;
189 if (fDt!=kAliHLTDataTypeESDObject &&
190 fDt!=kAliHLTDataTypeESDTree) {
192 HLTWarning("non-standard ESD type %s", AliHLTComponent::DataType2Text(fDt).c_str());
194 id.Insert(0, fDt.fID, kAliHLTComponentDataTypefIDsize);
195 id.Remove(TString::kTrailing, ' ');
197 fName+="_"; fName+=id; fName+=".root";
200 fpFile=new TFile(fName, "RECREATE");
203 if (fpFile && !fpFile->IsZombie() && iResult>=0) {
205 fpTree=new TTree("esdTree", "Tree with HLT ESD objects");
209 fpTree->SetDirectory(0);
212 if (fpTree && iResult>=0) {
214 // create the ESD structure for filling into the tree
215 fpEsd=new AliESDEvent;
217 fpEsd->CreateStdContent();
218 fpTree->GetUserInfo()->Add(fpEsd);
223 fpEsd->ResetStdContent();
226 // synchronize and add empty events
227 for (int i=fpTree->GetEntries(); i<eventno; i++) {
230 if (fpTree->GetEntries()>eventno) {
231 HLTWarning("event %d ESD of type %s already written, skipping additional data block", eventno, AliHLTComponent::DataType2Text(fDt).c_str());
235 // we need to copy the ESD, I did not find an approptiate
236 // method, the workaround is to save the ESD in a temporary
237 // tree, read the content back into the ESD structure
239 TTree* dummy=new TTree("dummy","dummy");
242 dummy->SetDirectory(0);
243 pESD->WriteToTree(dummy);
245 dummy->GetUserInfo()->Add(pESD);
246 fpEsd->ReadFromTree(dummy);
249 fpEsd->WriteToTree(fpTree);
251 dummy->GetUserInfo()->Clear();
260 fpTree->Write("",TObject::kOverwrite);