]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliXMLCollection.cxx
Added const members to contain the IDs of FMD files used by the preprocessor and...
[u/mrichter/AliRoot.git] / ANALYSIS / AliXMLCollection.cxx
CommitLineData
d08133e6 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
25#include <Riostream.h>
26#include <TEntryList.h>
27#include <TList.h>
28#include <TMap.h>
29#include <TObjArray.h>
30#include <TObjString.h>
31#include <TString.h>
32#include <TXMLEngine.h>
33
34#include "AliXMLCollection.h"
35
36ClassImp(AliXMLCollection)
d08133e6 37//___________________________________________________________________________
38 AliXMLCollection::AliXMLCollection() :
39 TGridCollection(),
40 fXmlFile(),
41 fEventList(0),
42 fEventListIter(0),
43 fCurrent(0),
44 fCollectionName(),
45 fout() {
46 //Default constructor
47}
48
49//___________________________________________________________________________
26f071d8 50AliXMLCollection::AliXMLCollection(const char *localcollectionfile) :
51 TGridCollection(),
52 fXmlFile(localcollectionfile),
53 fEventList(0),
54 fEventListIter(0),
55 fCurrent(0),
56 fCollectionName(),
57 fout()
58 {
d08133e6 59 // Create Alien event collection, by reading collection for the specified
60 // file.
61
d08133e6 62 fEventList = new TList();
63 fEventList->SetOwner(kTRUE);
64 fEventListIter = new TIter(fEventList);
26f071d8 65
d08133e6 66 if (localcollectionfile!=0) {
67 ParseXML();
68 }
69}
70
71//___________________________________________________________________________
72AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
73 TGridCollection(collection),
74 fXmlFile(collection.fXmlFile),
26f071d8 75 fEventList(0),
76 fEventListIter(0),
77 fCurrent(0),
78 fCollectionName(collection.fCollectionName),
79 fout()
80{
d08133e6 81 //copy constructor
82
83 if (collection.fEventList) fEventList = new TList();
84 if (collection.fEventListIter) fEventListIter = new TIter(fEventList);
85 if (collection.fCurrent) fCurrent = 0;
86}
87
88//___________________________________________________________________________
89AliXMLCollection::~AliXMLCollection() {
90 //Destructor
91 if(fEventList) delete fEventList;
92 if(fEventListIter) delete fEventListIter;
93}
94
95//___________________________________________________________________________
96Bool_t AliXMLCollection::WriteHeader() {
97 //Creates the xml output file
98
99 TString xmlName = fCollectionName;
100 xmlName += ".xml";
101
102 TString collectionHeader = "<collection name=";
103 collectionHeader += "\"";
104 collectionHeader += fCollectionName;
105 collectionHeader += "\"";
106 collectionHeader += ">";
107
108 // Open the output stream
109 fout.open(xmlName);
110 fout<<"<?xml version=\"1.0\"?>\n";
111 fout<<"<alien>\n";
112 fout<<" "<<collectionHeader<<"\n";
113
114 return kTRUE;
115}
116
117//___________________________________________________________________________
118Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList *list) {
119 //Writes the body of the xml collection
120 TString listline;
121 for(Int_t i = 0; i < list->GetN(); i++) {
122 listline += list->GetEntry(i);
123 listline += ",";
124 }
125 listline = listline(0,listline.Length()-1);
126
127 TString line0 = "<event name=\"";
128 line0 += counter;
129 line0 += "\">";
130
131 TString line1 = "<file name=\"AliESDs.root\" ";
132 line1 += "guid=\"";
133 line1 += guid;
134 line1 += "\" ";
135 line1 += "lfn=\"";
136 line1 += lfn;
137 line1 += "\" ";
138 line1 += "turl=\"";
139 line1 += turl;
140 line1 += "\" ";
141 line1 += "evlist=\"";
142 line1 += listline;
143 line1 += "\"";
144 line1 += " />";
145
146 fout<<" "<<line0<<"\n";
147 fout<<" "<<line1<<"\n";
148 fout<<" </event>\n";
149
150 return kTRUE;
151}
152
153//___________________________________________________________________________
154Bool_t AliXMLCollection::Export() {
155 //Closes the stream
156 fout<<" "<<"</collection>\n";
157 fout<<"</alien>\n";
158
159 fout.close();
160
161 return kTRUE;
162}
163
164//___________________________________________________________________________
165void AliXMLCollection::Reset() {
166 // Reset file iterator.
167
168 fEventListIter->Reset();
169 fCurrent = 0;
170}
171
172//___________________________________________________________________________
173TMap *AliXMLCollection::Next() {
174 // Return next event file map.
175
176 fCurrent = (TMap*)fEventListIter->Next();
177 return fCurrent;
178}
179
180//___________________________________________________________________________
26f071d8 181const char *AliXMLCollection::GetTURL(const char* filename) {
d08133e6 182 // Get a file's transport URL (TURL). Returns 0 in case of error.
183
184 if (fCurrent) {
185 TMap *obj = (TMap*)fCurrent->GetValue(filename);
186 if (obj) {
187 if (obj->GetValue("turl")) {
188 return ( ((TObjString*)obj->GetValue("turl"))->GetName());
189 }
190 }
191 }
192 Error("GetTURL","cannot get TURL of file %s",filename);
193 return 0;
194}
195
196//___________________________________________________________________________
26f071d8 197const char *AliXMLCollection::GetGUID(const char* filename) {
d08133e6 198 // Get a file's transport UID. Returns 0 in case of error.
199
200 if (fCurrent) {
201 TMap *obj = (TMap*)fCurrent->GetValue(filename);
202 if (obj) {
203 if (obj->GetValue("guid")) {
204 return ( ((TObjString*)obj->GetValue("guid"))->GetName());
205 }
206 }
207 }
208 Error("GetGUID","cannot get GUID of file %s",filename);
209 return 0;
210}
211
212//___________________________________________________________________________
213TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
214 // Get a file's event list. Returns 0 in case of error.
215
216 if (fCurrent) {
217 TMap *obj = (TMap *) fCurrent->GetValue(filename);
218 if (obj) {
219 if (obj->GetValue("evlist")) {
220 return ((TEntryList *) obj->GetValue("evlist"));
221 }
222 }
223 }
224 Error("GetEvList", "cannot get evelist of file %s", filename);
225 return 0;
226}
227
228//___________________________________________________________________________
229Bool_t AliXMLCollection::Remove(TMap * map) {
230 // Return next event file map.
231 if (fEventList->Remove(map)) {
232 return kTRUE;
233 } else {
234 return kFALSE;
235 }
236}
237
238//___________________________________________________________________________
26f071d8 239const char *AliXMLCollection::GetLFN(const char* ) {
d08133e6 240 // Get a file's LFN. Returns 0 in case of error.
241
242 if (fCurrent) {
243 TMap *obj = (TMap *) fCurrent->GetValue("");
244 if (obj) {
245 if (obj->GetValue("lfn")) {
246 return (((TObjString *) obj->GetValue("lfn"))->GetName());
247 }
248 }
249 }
250 Error("GetLFN", "cannot get LFN");
251 return 0;
252}
253
254//__________________________________________________________________________
26f071d8 255Bool_t AliXMLCollection::OverlapCollection(TGridCollection * comparator) {
d08133e6 256 // return kTRUE if comparator overlaps with this
257 if ((!comparator)) return kFALSE;
258
259 loopagain:
260 // loop over col1 and try to find it in col2
261 this->Reset();
262 // loop over all elements in reference (=this)
263 TMap *overlapmap;
264 while ((overlapmap = this->Next())) {
265 comparator->Reset();
266 Bool_t found = kFALSE;
267 // try to find in the comparator collection
268 while ((comparator->Next())) {
269 TString s1 = this->GetLFN("");
270 TString s2 = comparator->GetLFN("");
271 if (s1 == s2) {
272 found = kTRUE;
273 break;
274 }
275 }
276 if (!found) {
277 this->Remove(overlapmap);
278 goto loopagain;
279 }
280 }
281 return kTRUE;
282}
283
284//___________________________________________________________________________
285AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
286 // Static method used to create an Alien event collection, by reading
287 // collection for the specified file.
288
289 AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
290 return collection;
291}
292
293//___________________________________________________________________________
294void AliXMLCollection::ParseXML() {
295 // Parse event file collection XML file.
296
297 TXMLEngine xml;
298
299 XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
300 if (!xdoc) {
301 Error("ParseXML","cannot parse the xml file %s",fXmlFile.Data());
302 return;
303 }
304
305 XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
306 if (!xalien) {
307 Error("ParseXML","cannot find the <alien> tag in %s",fXmlFile.Data());
308 return;
309 }
310
311 XMLNodePointer_t xcollection = xml.GetChild(xalien);
312 if (!xcollection) {
313 Error("ParseXML","cannot find the <collection> tag in %s",fXmlFile.Data());
314 return;
315 }
316
317 XMLNodePointer_t xevent = xml.GetChild(xcollection);;
318 if (!xevent) {
319 Error("ParseXML","cannot find the <event> tag in %s",fXmlFile.Data());
320 return;
321 }
322 if (!xevent) return;
323
324 do {
325 if (xml.GetAttr(xevent, "name")) {
326 TMap *files = new TMap();
327
328 // files
329 XMLNodePointer_t xfile = xml.GetChild(xevent);
330 if (!xfile) continue;
331
332 Bool_t firstfile=kTRUE;
333 do {
334 // here we have an event file
335 // get the attributes;
336 xml.GetAttr(xfile, "lfn");
337 xml.GetAttr(xfile, "turl");
338
339 TMap *attributes = new TMap();
340 TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
341 TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
342 TObjString *olfn = new TObjString(xml.GetAttr(xfile,"lfn"));
343 TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
344 TObjString *oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
345 printf("Collection: %s - The Eventlist is %s\n",fXmlFile.Data(),oevlist->GetName());
346 if (oevlist->GetName() != "") {
347 TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
348 TString stringevlist = oevlist->GetName();
349 TObjArray *evlist = stringevlist.Tokenize(",");
350 for (Int_t n = 0; n < evlist->GetEntries(); n++) xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
351 attributes->Add(new TObjString("evlist"), xmlevlist);
352 }
353
354 attributes->Add(new TObjString("name"),oname);
355 attributes->Add(new TObjString("turl"),oturl);
356 attributes->Add(new TObjString("lfn"),olfn);
357 attributes->Add(new TObjString("guid"),oguid);
358 files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
359
360 // we add the first file always as a file without name to the map
361 if (firstfile) {
362 files->Add(new TObjString(""),attributes);
363 firstfile=kFALSE;
364 }
365 } while ((xfile = xml.GetNext(xfile)));
366 fEventList->Add(files);
367 }
368 } while ((xevent = xml.GetNext(xevent)));
369}
370