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 AliHLTEsdManagerImplementation.cxx
20 @author Matthias Richter
22 @brief Manager for merging and writing of HLT ESDs
25 #include "AliHLTEsdManagerImplementation.h"
26 #include "AliHLTComponent.h"
27 #include "AliESDEvent.h"
28 #include "AliHLTMessage.h"
29 #include "AliESDEvent.h"
30 #include "AliESDtrack.h"
35 #include "TObjectTable.h"
40 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTEsdManagerImplementation)
43 AliHLTEsdManagerImplementation::AliHLTEsdManagerImplementation()
48 // see header file for class documentation
50 // refer to README to build package
52 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
55 AliHLTEsdManagerImplementation::~AliHLTEsdManagerImplementation()
57 // see header file for class documentation
58 for (unsigned int i=0; i<fESDs.size(); i++) {
66 AliHLTEsdManagerImplementation::AliHLTEsdListEntry* AliHLTEsdManagerImplementation::Find(AliHLTComponentDataType dt) const
68 // see header file for class documentation
69 AliHLTEsdListEntry* pEntry=NULL;
70 for (unsigned int i=0; i<fESDs.size(); i++) {
71 if (fESDs[i] && *(fESDs[i])==dt) {
72 pEntry=const_cast<AliHLTEsdListEntry*>(fESDs[i]);
78 int AliHLTEsdManagerImplementation::WriteESD(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
79 AliHLTComponentDataType dt, AliESDEvent* tgtesd, int eventno)
81 // see header file for class documentation
82 if (!pBuffer && size<=0) return -EINVAL;
84 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
85 if (firstWord==size-sizeof(AliHLTUInt32_t)) {
86 HLTDebug("create object from block %s size %d", AliHLTComponent::DataType2Text(dt).c_str(), size);
87 AliHLTMessage msg(const_cast<AliHLTUInt8_t*>(pBuffer), size);
88 TClass* objclass=msg.GetClass();
89 TObject* pObj=msg.ReadObject(objclass);
90 if (pObj && objclass) {
91 HLTDebug("object %p type %s created", pObj, objclass->GetName());
92 AliESDEvent* pESD=dynamic_cast<AliESDEvent*>(pObj);
95 pTree=dynamic_cast<TTree*>(pObj);
98 pESD->CreateStdContent();
99 if (pTree->GetEntries()>0) {
100 if (pTree->GetEntries()>1) {
101 HLTWarning("only one entry allowed for ESD embedded into tree, data block %s contains tree with %d entires, taking first entry",
102 AliHLTComponent::DataType2Text(dt).c_str(), pTree->GetEntries());
104 pESD->ReadFromTree(pTree);
108 HLTWarning("tree of data block %s has no events, skipping data block", AliHLTComponent::DataType2Text(dt).c_str());
112 AliHLTEsdListEntry* entry=Find(dt);
114 AliHLTEsdListEntry* newEntry=new AliHLTEsdListEntry(dt);
115 if (!fDirectory.IsNull()) {
116 newEntry->SetDirectory(fDirectory);
118 fESDs.push_back(newEntry);
121 #if 0 // later extension !defined(HAVE_NOT_ESD_NONSTD)
124 entry->CopyNonEmptyObjects(tgtesd, pESD);
126 HLTError("internal mismatch, can not create list entry");
129 #elif !defined(HAVE_NOT_ESD_COPY)
131 #else //HAVE_NOT_ESD_COPY
132 static bool warningPrinted=false;
133 if (!warningPrinted) {
134 HLTWarning("old version of AliESDEvent does not provide assignment operator, skip merging to global hltEsd");
137 #endif //HAVE_NOT_ESD_COPY
141 entry->WriteESD(pESD, eventno);
143 HLTError("internal mismatch, can not create list entry");
148 HLTWarning("data block %s is not of class type AliESDEvent, ignoring ...", AliHLTComponent::DataType2Text(dt).c_str());
151 // ESD has been created and must be cleaned up
164 int AliHLTEsdManagerImplementation::PadESDs(int eventno)
166 // see header file for class documentation
168 for (unsigned int i=0; i<fESDs.size(); i++) {
170 int res=fESDs[i]->WriteESD(NULL, eventno);
171 if (res<0 && iResult>=0) iResult=res;
177 void AliHLTEsdManagerImplementation::SetDirectory(const char* directory)
179 // see header file for class documentation
180 if (!directory) return;
181 fDirectory=directory;
182 for (unsigned int i=0; i<fESDs.size(); i++) {
184 fESDs[i]->SetDirectory(directory);
189 TString AliHLTEsdManagerImplementation::GetFileNames(AliHLTComponentDataType dt) const
192 for (unsigned int i=0; i<fESDs.size(); i++) {
193 if (fESDs[i] && *(fESDs[i])==dt) {
194 if (!result.IsNull()) result+=" ";
195 result+=fESDs[i]->GetFileName();
201 TTree* AliHLTEsdManagerImplementation::EmbedIntoTree(AliESDEvent* pESD, const char* name, const char* title)
203 // see header file for class documentation
205 TTree* pTree=new TTree(name, title);
207 pESD->WriteToTree(pTree);
209 pTree->GetUserInfo()->Add(pESD);
215 pTree->GetUserInfo()->Clear();
222 AliHLTEsdManagerImplementation::AliHLTEsdListEntry::AliHLTEsdListEntry(AliHLTComponentDataType dt)
232 // see header file for class documentation
235 AliHLTEsdManagerImplementation::AliHLTEsdListEntry::~AliHLTEsdListEntry()
237 // see header file for class documentation
238 if (fpEsd) delete fpEsd;
241 if (fpTree) delete fpTree;
251 bool AliHLTEsdManagerImplementation::AliHLTEsdListEntry::operator==(AliHLTComponentDataType dt) const
253 // see header file for class documentation
257 int AliHLTEsdManagerImplementation::AliHLTEsdListEntry::WriteESD(AliESDEvent* pSrcESD, int eventno)
259 // see header file for class documentation
261 #ifndef HAVE_NOT_ESD_COPY
262 if (fName.IsNull()) {
263 // this is the first event, create the file name
265 if (!fDirectory.IsNull()) {
266 fName+=fDirectory; fName+="/";
268 fName+="Ali"; fName+=GetPrefix();
269 if (fDt!=kAliHLTDataTypeESDObject &&
270 fDt!=kAliHLTDataTypeESDTree) {
272 HLTWarning("non-standard ESD type %s", AliHLTComponent::DataType2Text(fDt).c_str());
274 id.Insert(0, fDt.fID, kAliHLTComponentDataTypefIDsize);
275 id.Remove(TString::kTrailing, ' ');
277 fName+="_"; fName+=id; fName+=".root";
282 fpFile=new TFile(fName, "RECREATE");
283 fpTree=new TTree("esdTree", "Tree with HLT ESD objects");
284 fpTree->SetDirectory(0);
285 fpEsd=new AliESDEvent;
287 fpEsd->CreateStdContent();
290 fpEsd->WriteToTree(fpTree);
295 if (fpFile && fpTree && fpEsd) {
296 // synchronize and add empty events
298 int nofCurrentEvents=fpTree->GetEntries();
299 if (nofCurrentEvents<eventno) {
300 iResult=1; // indicate tree to be written
301 HLTDebug("adding %d empty events to file %s", eventno-nofCurrentEvents, fName.Data());
302 for (int i=nofCurrentEvents; i<eventno; i++) {
307 if (iResult>=0 && pSrcESD) {
308 int nofObjects=fpEsd->GetList()->GetEntries();
310 if (nofObjects!=fpEsd->GetList()->GetEntries()) {
311 // The source ESD contains object not present in the target ESD
312 // before. Those objects will not be written to the tree since
313 // the branch layout has been created earlier.
314 // Create new tree with the additional branches, copy the entries
315 // of the current tree into the new tree, and continue.
316 TTree* pNewTree=new TTree("esdTree", "Tree with HLT ESD objects");
317 pNewTree->SetDirectory(0);
318 AliESDEvent* readESD=new AliESDEvent;
319 readESD->CreateStdContent();
320 readESD->ReadFromTree(fpTree);
322 fpEsd->WriteToTree(pNewTree);
323 HLTDebug("cloning tree with %d entries", fpTree->GetEntries());
324 for (int event=0; event<fpTree->GetEntries(); event++) {
325 fpTree->GetEntry(event);
334 fpFile=new TFile(fName, "RECREATE");
337 HLTDebug("new ESD with %d objects", fpEsd->GetList()->GetEntries());
340 iResult=1; // indicate tree to be written
345 fpTree->GetUserInfo()->Add(fpEsd);
346 fpTree->Write(fpTree->GetName(),TObject::kOverwrite);
347 fpTree->GetUserInfo()->Clear();
350 #else //HAVE_NOT_ESD_COPY
351 // this is the old workaround, necessary for older AliRoot versions
352 // version<=v4-12-Release
354 // we need to copy the ESD, I did not find an approptiate
355 // method, the workaround is to save the ESD in a temporary
356 // tree, read the content back into the ESD structure
358 // Unfortunately the following code crashes at the second event.
359 // The expert on the ESD (Christian Klein Boesig) does not have
360 // a solution either. It seems to be a problem in ROOT.
361 // TTree* dummy=new TTree("dummy","dummy");
362 // dummy->SetDirectory(0);
363 // pESD->WriteToTree(dummy);
365 // dummy->GetUserInfo()->Add(pESD);
366 // fpEsd->ReadFromTree(dummy);
367 // dummy->GetEvent(0);
368 // fpEsd->WriteToTree(fpTree);
370 // dummy->GetUserInfo()->Clear();
373 // The only way is via TChain, which is working on files only at the
375 // We use temporary files for the new event to be copied into the
378 if (fName.IsNull()) {
379 // this is the first event, create the file on disk and write ESD
381 origin.Insert(0, fDt.fOrigin, kAliHLTComponentDataTypefOriginSize);
382 origin.Remove(TString::kTrailing, ' ');
385 if (!fDirectory.IsNull()) {
386 fName+=fDirectory; fName+="/";
388 fName+="AliHLT"; fName+=origin;
389 if (fDt!=kAliHLTDataTypeESDObject &&
390 fDt!=kAliHLTDataTypeESDTree) {
392 HLTWarning("non-standard ESD type %s", AliHLTComponent::DataType2Text(fDt).c_str());
394 id.Insert(0, fDt.fID, kAliHLTComponentDataTypefIDsize);
395 id.Remove(TString::kTrailing, ' ');
397 fName+="_"; fName+=id; fName+=".root";
402 if (!gSystem->AccessPathName(fName)) {
403 // file exists, delete
404 TString shellcmd="rm -f ";
406 gSystem->Exec(shellcmd);
410 TChain chain("esdTree");
414 int nofCurrentEvents=0;
416 if (!gSystem->AccessPathName(fName)) {
417 // these are the other events, use the target file and temporary files to merge
423 if (!file.IsZombie()) {
425 file.GetObject("esdTree", pSrcTree);
427 nofCurrentEvents=pSrcTree->GetEntries();
435 // synchronize and add empty events
436 if (nofCurrentEvents<eventno) {
437 iResult=1; // indicate files to merge
438 TTree* pTgtTree=new TTree("esdTree", "Tree with HLT ESD objects");
440 pTgtTree->SetDirectory(0);
441 AliESDEvent* pTmpESD=new AliESDEvent;
444 FILE* pTmpFile=gSystem->TempFileName(tmpfilename);
448 cleanup.Add(new TObjString(tmpfilename));
449 TFile emptyevents(tmpfilename, "RECREATE");
450 if (!emptyevents.IsZombie()) {
451 pTmpESD->CreateStdContent();
452 pTmpESD->WriteToTree(pTgtTree);
453 HLTDebug("adding %d empty events to file %s", eventno-nofCurrentEvents, fName.Data());
454 for (int i=nofCurrentEvents; i<eventno; i++) {
457 pTgtTree->GetUserInfo()->Add(pTmpESD);
461 chain.Add(tmpfilename);
462 pTgtTree->GetUserInfo()->Clear();
475 if (iResult>=0 && pSrcESD) {
476 // add the new event to the chain
477 iResult=1; // indicate files to merge
478 TString tmpfilename=WriteTempFile(pSrcESD);
479 if (!tmpfilename.IsNull()) {
480 chain.Add(tmpfilename);
481 cleanup.Add(new TObjString(tmpfilename));
486 // build temporary file name for chain output
488 FILE* pTmpFile=gSystem->TempFileName(tgtName);
493 // there have been problems with the memory consumption when using
495 // but using a separate loop soemtimes crashes in AliESDEvent::ReadFromTree
496 // since this is for backward compatiblity only, we take the TChain::Merge
497 chain.Merge(tgtName);
498 // TFile tgtFile(tgtName, "RECREATE");
499 // TTree* pTgtTree=new TTree("esdTree", "Tree with HLT ESD objects");
500 // AliESDEvent* pTgtESD=new AliESDEvent;
501 // if (pTgtTree && pTgtESD) {
502 // pTgtESD->ReadFromTree(&chain);
503 // pTgtESD->WriteToTree(pTgtTree);
505 // int nofEvents=chain.GetEntries();
506 // for (int event=0; event<nofEvents; event++) {
507 // chain.GetEntry(event);
511 // pTgtTree->GetUserInfo()->Add(pTgtESD);
513 // pTgtTree->Write();
514 // pTgtTree->GetUserInfo()->Clear();
519 // if (pTgtTree) delete pTgtTree;
520 // if (pTgtESD) delete pTgtESD;
523 // rename the merged file to the original file
524 TString shellcmd="mv ";
525 shellcmd+=tgtName + " " + fName;
526 if (gSystem->Exec(shellcmd)==0) {
527 HLTDebug("renaming %s to %s", tgtName.Data(), fName.Data());
529 HLTError("can not rename temporary file %s to %s", tgtName.Data(), fName.Data());
532 HLTError("can not get temporary file name from system");
537 // delete temporary files
538 // the list objects are cleaned up by the TList destructor as the
540 TIter entry(&cleanup);
541 while (TObject* pObj=entry.Next()) {
542 if (dynamic_cast<TObjString*>(pObj)) {
543 TString shellcmd="rm -f ";
544 shellcmd+=(dynamic_cast<TObjString*>(pObj))->GetString();
545 gSystem->Exec(shellcmd);
548 #endif //HAVE_NOT_ESD_COPY
553 TString AliHLTEsdManagerImplementation::AliHLTEsdListEntry::WriteTempFile(AliESDEvent* pESD) const
555 // see header file for class documentation
558 FILE* pTmpFile=gSystem->TempFileName(tmpfilename);
563 TFile file(tmpfilename, "RECREATE");
564 if (!file.IsZombie()) {
565 TTree* pTree=AliHLTEsdManagerImplementation::EmbedIntoTree(pESD);
568 if (pTree->Write()>0) {
570 HLTError("can not write esd tree to temporary file %s", tmpfilename.Data());
573 pTree->GetUserInfo()->Clear();
580 HLTError("can not open file %s", tmpfilename.Data());
584 HLTError("can not get temporary file name from system");
589 if (gSystem->AccessPathName(tmpfilename)==0) {
590 TString shellcmd="rm -f ";
591 shellcmd+=tmpfilename;
592 gSystem->Exec(shellcmd);
599 void AliHLTEsdManagerImplementation::AliHLTEsdListEntry::SetDirectory(const char* directory)
601 // see header file for class documentation
602 if (!directory) return;
603 if (!fName.IsNull()) {
604 HLTWarning("ESD entry already in writing mode (%s), ignoring directory", fName.Data());
607 fDirectory=directory;
610 void AliHLTEsdManagerImplementation::AliHLTEsdListEntry::Delete()
612 // see header file for class documentation
613 if (fName.IsNull()) return;
614 if (gSystem->AccessPathName(fName)!=0) return;
616 TString shellcmd="rm -f ";
618 gSystem->Exec(shellcmd);
622 const char* AliHLTEsdManagerImplementation::AliHLTEsdListEntry::GetFileName() const
624 // see header file for class documentation
628 const char* AliHLTEsdManagerImplementation::AliHLTEsdListEntry::GetPrefix()
630 // see header file for class documentation
631 if (fPrefix.IsNull()) {
632 fPrefix.Insert(0, fDt.fOrigin, kAliHLTComponentDataTypefOriginSize);
633 fPrefix.Remove(TString::kTrailing, ' ');
635 if (!fPrefix.Contains("HLT")) {
636 fPrefix.Insert(0, "HLT");
639 return fPrefix.Data();
642 int AliHLTEsdManagerImplementation::AliHLTEsdListEntry::CopyNonEmptyObjects(AliESDEvent* pTgt, AliESDEvent* pSrc)
644 // see header file for class documentation
646 if (!pTgt || !pSrc) return -EINVAL;
648 const char* defaultPrefix="HLT";
649 TIter next(pSrc->GetList());
650 TObject* pSrcObject=NULL;
652 while ((pSrcObject=next())) {
653 if(!pSrcObject->InheritsFrom("TCollection")){
655 } else if(pSrcObject->InheritsFrom("TClonesArray")){
656 TClonesArray* pTClA=dynamic_cast<TClonesArray*>(pSrcObject);
657 if (pTClA!=NULL && pTClA->GetEntriesFast()>0) {
658 bool bMakeNewName=true;
660 name+=pTClA->GetName();
661 TObject* pTgtObject=pTgt->GetList()->FindObject(name);
662 TClonesArray* pTgtArray=NULL;
663 if (bMakeNewName=(pTgtObject!=NULL) && pTgtObject->InheritsFrom("TClonesArray")){
664 pTgtArray=dynamic_cast<TClonesArray*>(pTgtObject);
666 TString classType=pTClA->Class()->GetName();
667 if (classType.CompareTo(pTgtArray->Class()->GetName())==0) {
668 if (pTgtArray->GetEntries()==0) {
671 HLTWarning("TClonesArray \"%s\" in target ESD %p is already filled with %d entries",
672 name.Data(), pTgt, pTgtArray->GetEntries());
675 HLTWarning("TClonesArray \"%s\" exists in target ESD %p, but describes incompatible class type %s instead of %s",
676 name.Data(), pTgt, pTgtArray->GetClass()->GetName(), pTClA->GetClass()->GetName());
679 HLTError("internal error: dynamic cast failed for object %s %p", pTgtObject->GetName(), pTgtObject);
681 } else if (pTgtObject) {
682 HLTWarning("object \"%s\" does already exist in target ESD %p, but is %s rather than TClonesArray",
683 name.Data(), pTgt, pTgtObject->Class()->GetName());
684 // TODO: temporary solution, think about a general naming scheme and add
685 // the name as property of AliHLTEsdListEntry
691 while (pTgt->GetList()->FindObject(name)) {
692 name.Form("%sESD_%s", GetPrefix(), pTClA->GetName());
694 name+=Form("%d", count);
697 HLTWarning("adding new TClonesArray \"%s\" because of conflicts", name.Data());
701 pTgtArray->ExpandCreate(pTClA->GetEntries());
702 HLTInfo("Expanding TClonesArray \"%s\" to %d elements", pTgtArray->GetClass()->GetName(), pTClA->GetEntries());
704 pTgtArray=new TClonesArray(pTClA->GetClass(), pTClA->GetEntries());
705 pTgtArray->ExpandCreate(pTClA->GetEntries());
706 pTgtArray->SetName(name);
707 pTgt->AddObject(pTgtArray);
708 HLTInfo("Adding TClonesArray \"%s\" with %d elements to ESD %p", name.Data(), pTClA->GetEntries(), pTgt);
712 for(int i=0; i<pTClA->GetEntriesFast(); ++i){
713 (*pTClA)[i]->Copy(*((*pTgtArray)[i]));