]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTRootFileWriterComponent.cxx
New Component:
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTRootFileWriterComponent.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   AliHLTRootFileWriterComponent.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Base class for writer components to store data in a ROOT file
23
24                                                                           */
25
26 #include "AliHLTRootFileWriterComponent.h"
27 #include "TFile.h"
28 #include "TString.h"
29 #include "TObjectTable.h" // for root object validity
30
31 /** ROOT macro for the implementation of ROOT specific class methods */
32 ClassImp(AliHLTRootFileWriterComponent)
33
34 AliHLTRootFileWriterComponent::AliHLTRootFileWriterComponent()
35   :
36   AliHLTFileWriter(),
37   fEventID(kAliHLTVoidEventID),
38   fCurrentFile(NULL),
39   fOptions(0)
40 {
41   // see header file for class documentation
42   // or
43   // refer to README to build package
44   // or
45   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
46
47   // all blocks of one event go into the same file
48   SetMode(kConcatenateBlocks);
49 }
50
51 AliHLTRootFileWriterComponent::~AliHLTRootFileWriterComponent()
52 {
53   // see header file for class documentation
54 }
55
56 int AliHLTRootFileWriterComponent::InitWriter()
57 {
58   // see header file for class documentation
59
60   // choose .root as default extension
61   if (GetExtension().IsNull()) SetExtension("root");
62   return 0;
63 }
64
65 int AliHLTRootFileWriterComponent::CloseWriter()
66 {
67   // see header file for class documentation
68   if (fCurrentFile!=NULL) {
69     HLTDebug("close root file");
70     TFile* pFile=fCurrentFile; fCurrentFile=NULL;
71     pFile->Close(); delete pFile;
72   }
73   return 0;
74 }
75
76 int AliHLTRootFileWriterComponent::DumpEvent( const AliHLTComponentEventData& evtData,
77                                               const AliHLTComponentBlockData* /*blocks*/, 
78                                               AliHLTComponentTriggerData& /*trigData*/ )
79 {
80   // see header file for class documentation
81   int iResult=0;
82   if (!IsDataEvent() && !CheckMode(kWriteAllEvents)) return 0;
83
84   int count=0;
85   for (const TObject* pObj=GetFirstInputObject();
86        pObj && iResult>=0;
87        pObj=GetNextInputObject()) {
88     iResult=WriteObject(evtData.fEventID, pObj);
89     if (iResult == 0) {
90       count++;
91       HLTDebug("wrote object of class %s, data type %s", pObj->ClassName(), (DataType2Text(GetDataType(pObj)).c_str())); 
92     }
93   }
94   HLTDebug("wrote %d object(s) from %d input blocks to file", count, GetNumberOfInputBlocks());
95   return iResult;
96 }
97
98 int AliHLTRootFileWriterComponent::ScanArgument(int argc, const char** argv)
99 {
100   // see header file for class documentation
101   int iResult=0;
102   if (argc<=0) return 0;
103   TString argument=argv[0];
104   int bMissingParam=0;
105
106   // -overwrite
107   if (argument.CompareTo("-overwrite")==0) {
108     fOptions|=TObject::kOverwrite;
109     iResult=1;
110     
111   } else {
112     iResult=-EINVAL;
113   }
114
115   if (bMissingParam) {
116     HLTError("missing parameter for argument %s", argument.Data());
117     iResult=-EINVAL;
118   }
119
120   return iResult;
121 }
122
123 int AliHLTRootFileWriterComponent::WriteObject(const AliHLTEventID_t eventID, const TObject *pOb)
124 {
125   // see header file for class documentation
126   int iResult=0;
127   if (pOb) {
128     HLTDebug("write object %p (%s)", pOb, pOb->GetName());
129     if (!CheckMode(kConcatenateEvents) && eventID != fEventID &&
130         fCurrentFile!=NULL && eventID!=kAliHLTVoidEventID) {
131       TFile* pFile=fCurrentFile; fCurrentFile=NULL;
132       pFile->Close(); delete pFile;
133     }
134     if (fCurrentFile==NULL) {
135       fCurrentFile=OpenFile(eventID, 0);
136       if (fCurrentFile) fEventID=eventID;
137     }
138     if (fCurrentFile) {
139       fCurrentFile->cd();
140       pOb->Write("", fOptions);
141     } else {
142       iResult=-EBADF;
143     }
144   }
145   return iResult;
146 }
147
148 TFile* AliHLTRootFileWriterComponent::OpenFile(const AliHLTEventID_t eventID, const int blockID, const char* option)
149 {
150   // see header file for class documentation
151   TFile* pFile=NULL;
152   TString filename("");
153   if ((BuildFileName(eventID, blockID, kAliHLTVoidDataType, 0, filename))>=0 && filename.IsNull()==0) {
154     pFile=new TFile(filename, option);
155     if (pFile) {
156       if (pFile->IsZombie()) {
157         delete pFile;
158         pFile=NULL;
159         HLTError("can not open ROOT file %s", filename.Data());
160       }
161     } else {
162       HLTFatal("memory allocation failed");
163     }
164   } else {
165     HLTError("failed to build a new file name for event %#8x", eventID);
166   }
167   return pFile;
168 }