]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliXMLCollection.cxx
New AddFile method
[u/mrichter/AliRoot.git] / STEER / AliXMLCollection.cxx
CommitLineData
c1b83ab9 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18//-----------------------------------------------------------------
19// Implementation of the AliXMLCollection class
20// This is the class that creates XML collections after querying the tags
21// Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
22//-----------------------------------------------------------------
23
24//ROOT
1ca27a6f 25#include "Riostream.h"
26#include "TString.h"
27#include "TMap.h"
28#include "TObjString.h"
29#include "TXMLEngine.h"
1ca27a6f 30#include "TEntryList.h"
31#include "TObjArray.h"
c1b83ab9 32
33#include "AliXMLCollection.h"
34
35ClassImp(AliXMLCollection)
36
37//___________________________________________________________________________
38 AliXMLCollection::AliXMLCollection() :
1ca27a6f 39 TGridCollection(),
40 fXmlFile(),
41 fEventList(0),
42 fEventListIter(0),
43 fCurrent(0),
fe12e09c 44 fCollectionName(),
1ca27a6f 45 fout() {
c1b83ab9 46 //Default constructor
47}
48
1ca27a6f 49//___________________________________________________________________________
50AliXMLCollection::AliXMLCollection(const char *localcollectionfile) {
51 // Create Alien event collection, by reading collection for the specified
52 // file.
53
54 fXmlFile = localcollectionfile;
55 fEventList = new TList();
56 fEventList->SetOwner(kTRUE);
57 fEventListIter = new TIter(fEventList);
58 fCurrent = 0;
59 if (localcollectionfile!=0) {
60 ParseXML();
61 }
62}
63
64//___________________________________________________________________________
65AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
66 TGridCollection(collection),
67 fXmlFile(collection.fXmlFile),
1ca27a6f 68 fCollectionName(collection.fCollectionName) {
69 //copy constructor
70
71 if (collection.fEventList) fEventList = new TList();
72 if (collection.fEventListIter) fEventListIter = new TIter(fEventList);
73 if (collection.fCurrent) fCurrent = 0;
74}
75
c1b83ab9 76//___________________________________________________________________________
77AliXMLCollection::~AliXMLCollection() {
78 //Destructor
1ca27a6f 79 if(fEventList) delete fEventList;
80 if(fEventListIter) delete fEventListIter;
c1b83ab9 81}
82
83//___________________________________________________________________________
84Bool_t AliXMLCollection::WriteHeader() {
85 //Creates the xml output file
86
87 TString xmlName = fCollectionName;
88 xmlName += ".xml";
89
90 TString collectionHeader = "<collection name=";
91 collectionHeader += "\"";
92 collectionHeader += fCollectionName;
93 collectionHeader += "\"";
94 collectionHeader += ">";
95
96 // Open the output stream
97 fout.open(xmlName);
98 fout<<"<?xml version=\"1.0\"?>\n";
eb771b73 99 fout<<"<alien>\n";
c1b83ab9 100 fout<<" "<<collectionHeader<<"\n";
101
102 return kTRUE;
103}
104
105//___________________________________________________________________________
5b7be856 106Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList *list) {
c1b83ab9 107 //Writes the body of the xml collection
108 TString listline;
109 for(Int_t i = 0; i < list->GetN(); i++) {
110 listline += list->GetEntry(i);
111 listline += ",";
112 }
113 listline = listline(0,listline.Length()-1);
114
115 TString line0 = "<event name=\"";
116 line0 += counter;
117 line0 += "\">";
118
119 TString line1 = "<file name=\"AliESDs.root\" ";
120 line1 += "guid=\"";
121 line1 += guid;
122 line1 += "\" ";
eb771b73 123 line1 += "lfn=\"";
124 line1 += lfn;
125 line1 += "\" ";
ab02b208 126 line1 += "turl=\"";
127 line1 += turl;
128 line1 += "\" ";
129 line1 += "evlist=\"";
c1b83ab9 130 line1 += listline;
131 line1 += "\"";
132 line1 += " />";
133
134 fout<<" "<<line0<<"\n";
135 fout<<" "<<line1<<"\n";
136 fout<<" </event>\n";
137
138 return kTRUE;
139}
140
141//___________________________________________________________________________
142Bool_t AliXMLCollection::Export() {
143 //Closes the stream
144 fout<<" "<<"</collection>\n";
eb771b73 145 fout<<"</alien>\n";
c1b83ab9 146
147 fout.close();
148
149 return kTRUE;
150}
1ca27a6f 151
152//___________________________________________________________________________
153void AliXMLCollection::Reset() {
154 // Reset file iterator.
155
156 fEventListIter->Reset();
157 fCurrent = 0;
158}
159
160//___________________________________________________________________________
161TMap *AliXMLCollection::Next() {
162 // Return next event file map.
163
164 fCurrent = (TMap*)fEventListIter->Next();
165 return fCurrent;
166}
167
168//___________________________________________________________________________
169const char *AliXMLCollection::GetTURL(const char* filename) const {
170 // Get a file's transport URL (TURL). Returns 0 in case of error.
171
172 if (fCurrent) {
173 TMap *obj = (TMap*)fCurrent->GetValue(filename);
174 if (obj) {
175 if (obj->GetValue("turl")) {
176 return ( ((TObjString*)obj->GetValue("turl"))->GetName());
177 }
178 }
179 }
180 Error("GetTURL","cannot get TURL of file %s",filename);
181 return 0;
182}
183
184//___________________________________________________________________________
185TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
186 // Get a file's event list. Returns 0 in case of error.
187
188 if (fCurrent) {
189 TMap *obj = (TMap *) fCurrent->GetValue(filename);
190 if (obj) {
191 if (obj->GetValue("evlist")) {
192 return ((TEntryList *) obj->GetValue("evlist"));
193 }
194 }
195 }
196 Error("GetEvList", "cannot get evelist of file %s", filename);
197 return 0;
198}
199
200//___________________________________________________________________________
201Bool_t AliXMLCollection::Remove(TMap * map) {
202 // Return next event file map.
203 if (fEventList->Remove(map)) {
204 return kTRUE;
205 } else {
206 return kFALSE;
207 }
208}
209
210//___________________________________________________________________________
211const char *AliXMLCollection::GetLFN(const char* filename) const {
212 // Get a file's LFN. Returns 0 in case of error.
213
214 if (fCurrent) {
215 TMap *obj = (TMap *) fCurrent->GetValue("");
216 if (obj) {
217 if (obj->GetValue("lfn")) {
218 return (((TObjString *) obj->GetValue("lfn"))->GetName());
219 }
220 }
221 }
222 Error("GetLFN", "cannot get LFN");
223 return 0;
224}
225
226//__________________________________________________________________________
227Bool_t AliXMLCollection::OverlapCollection(AliXMLCollection * comparator) {
228 // return kTRUE if comparator overlaps with this
229 if ((!comparator)) return kFALSE;
230
231 loopagain:
232 // loop over col1 and try to find it in col2
233 this->Reset();
234 // loop over all elements in reference (=this)
235 TMap *overlapmap;
236 while ((overlapmap = this->Next())) {
237 comparator->Reset();
238 Bool_t found = kFALSE;
239 // try to find in the comparator collection
240 while ((comparator->Next())) {
241 TString s1 = this->GetLFN("");
242 TString s2 = comparator->GetLFN("");
243 if (s1 == s2) {
244 found = kTRUE;
245 break;
246 }
247 }
248 if (!found) {
249 this->Remove(overlapmap);
250 goto loopagain;
251 }
252 }
253 return kTRUE;
254}
255
256//___________________________________________________________________________
257AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
258 // Static method used to create an Alien event collection, by reading
259 // collection for the specified file.
260
261 AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
262 return collection;
263}
264
265//___________________________________________________________________________
266void AliXMLCollection::ParseXML() {
267 // Parse event file collection XML file.
268
269 TXMLEngine xml;
270
271 XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
272 if (!xdoc) {
273 Error("ParseXML","cannot parse the xml file %s",fXmlFile.Data());
274 return;
275 }
276
277 XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
278 if (!xalien) {
279 Error("ParseXML","cannot find the <alien> tag in %s",fXmlFile.Data());
280 return;
281 }
282
283 XMLNodePointer_t xcollection = xml.GetChild(xalien);
284 if (!xcollection) {
285 Error("ParseXML","cannot find the <collection> tag in %s",fXmlFile.Data());
286 return;
287 }
288
289 XMLNodePointer_t xevent = xml.GetChild(xcollection);;
290 if (!xevent) {
291 Error("ParseXML","cannot find the <event> tag in %s",fXmlFile.Data());
292 return;
293 }
294 if (!xevent) return;
295
296 do {
297 if (xml.GetAttr(xevent, "name")) {
298 TMap *files = new TMap();
299
300 // files
301 XMLNodePointer_t xfile = xml.GetChild(xevent);
302 if (!xfile) continue;
303
304 Bool_t firstfile=kTRUE;
305 do {
306 // here we have an event file
307 // get the attributes;
308 xml.GetAttr(xfile, "lfn");
309 xml.GetAttr(xfile, "turl");
310
311 TMap *attributes = new TMap();
312 TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
313 TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
314 TObjString *olfn = new TObjString(xml.GetAttr(xfile,"lfn"));
315 TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
316 TObjString *oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
317 printf("Collection: %s - The Eventlist is %s\n",fXmlFile.Data(),oevlist->GetName());
318 if (oevlist->GetName() != "") {
319 TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
320 TString stringevlist = oevlist->GetName();
321 TObjArray *evlist = stringevlist.Tokenize(",");
322 for (Int_t n = 0; n < evlist->GetEntries(); n++) xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
323 attributes->Add(new TObjString("evlist"), xmlevlist);
324 }
325
326 attributes->Add(new TObjString("name"),oname);
327 attributes->Add(new TObjString("turl"),oturl);
328 attributes->Add(new TObjString("lfn"),olfn);
329 attributes->Add(new TObjString("guid"),oguid);
330 files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
331
332 // we add the first file always as a file without name to the map
333 if (firstfile) {
334 files->Add(new TObjString(""),attributes);
335 firstfile=kFALSE;
336 }
337 } while ((xfile = xml.GetNext(xfile)));
338 fEventList->Add(files);
339 }
340 } while ((xevent = xml.GetNext(xevent)));
341}
342