1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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 //-----------------------------------------------------------------
25 #include "Riostream.h"
28 #include "TObjString.h"
29 #include "TXMLEngine.h"
30 #include "TEventList.h"
31 #include "TEntryList.h"
32 #include "TObjArray.h"
34 #include "AliXMLCollection.h"
36 ClassImp(AliXMLCollection)
38 //___________________________________________________________________________
39 AliXMLCollection::AliXMLCollection() :
50 //___________________________________________________________________________
51 AliXMLCollection::AliXMLCollection(const char *localcollectionfile) {
52 // Create Alien event collection, by reading collection for the specified
55 fXmlFile = localcollectionfile;
56 fEventList = new TList();
57 fEventList->SetOwner(kTRUE);
58 fEventListIter = new TIter(fEventList);
60 if (localcollectionfile!=0) {
65 //___________________________________________________________________________
66 AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
67 TGridCollection(collection),
68 fXmlFile(collection.fXmlFile),
72 fCollectionName(collection.fCollectionName) {
75 if (collection.fEventList) fEventList = new TList();
76 if (collection.fEventListIter) fEventListIter = new TIter(fEventList);
77 if (collection.fCurrent) fCurrent = 0;
80 //___________________________________________________________________________
81 AliXMLCollection::~AliXMLCollection() {
83 if(fEventList) delete fEventList;
84 if(fEventListIter) delete fEventListIter;
87 //___________________________________________________________________________
88 Bool_t AliXMLCollection::WriteHeader() {
89 //Creates the xml output file
91 TString xmlName = fCollectionName;
94 TString collectionHeader = "<collection name=";
95 collectionHeader += "\"";
96 collectionHeader += fCollectionName;
97 collectionHeader += "\"";
98 collectionHeader += ">";
100 // Open the output stream
102 fout<<"<?xml version=\"1.0\"?>\n";
104 fout<<" "<<collectionHeader<<"\n";
109 //___________________________________________________________________________
110 Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEventList *list) {
111 //Writes the body of the xml collection
113 for(Int_t i = 0; i < list->GetN(); i++) {
114 listline += list->GetEntry(i);
117 listline = listline(0,listline.Length()-1);
119 TString line0 = "<event name=\"";
123 TString line1 = "<file name=\"AliESDs.root\" ";
133 line1 += "evlist=\"";
138 fout<<" "<<line0<<"\n";
139 fout<<" "<<line1<<"\n";
145 //___________________________________________________________________________
146 Bool_t AliXMLCollection::Export() {
148 fout<<" "<<"</collection>\n";
156 //___________________________________________________________________________
157 void AliXMLCollection::Reset() {
158 // Reset file iterator.
160 fEventListIter->Reset();
164 //___________________________________________________________________________
165 TMap *AliXMLCollection::Next() {
166 // Return next event file map.
168 fCurrent = (TMap*)fEventListIter->Next();
172 //___________________________________________________________________________
173 const char *AliXMLCollection::GetTURL(const char* filename) const {
174 // Get a file's transport URL (TURL). Returns 0 in case of error.
177 TMap *obj = (TMap*)fCurrent->GetValue(filename);
179 if (obj->GetValue("turl")) {
180 return ( ((TObjString*)obj->GetValue("turl"))->GetName());
184 Error("GetTURL","cannot get TURL of file %s",filename);
188 //___________________________________________________________________________
189 TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
190 // Get a file's event list. Returns 0 in case of error.
193 TMap *obj = (TMap *) fCurrent->GetValue(filename);
195 if (obj->GetValue("evlist")) {
196 return ((TEntryList *) obj->GetValue("evlist"));
200 Error("GetEvList", "cannot get evelist of file %s", filename);
204 //___________________________________________________________________________
205 Bool_t AliXMLCollection::Remove(TMap * map) {
206 // Return next event file map.
207 if (fEventList->Remove(map)) {
214 //___________________________________________________________________________
215 const char *AliXMLCollection::GetLFN(const char* filename) const {
216 // Get a file's LFN. Returns 0 in case of error.
219 TMap *obj = (TMap *) fCurrent->GetValue("");
221 if (obj->GetValue("lfn")) {
222 return (((TObjString *) obj->GetValue("lfn"))->GetName());
226 Error("GetLFN", "cannot get LFN");
230 //__________________________________________________________________________
231 Bool_t AliXMLCollection::OverlapCollection(AliXMLCollection * comparator) {
232 // return kTRUE if comparator overlaps with this
233 if ((!comparator)) return kFALSE;
236 // loop over col1 and try to find it in col2
238 // loop over all elements in reference (=this)
240 while ((overlapmap = this->Next())) {
242 Bool_t found = kFALSE;
243 // try to find in the comparator collection
244 while ((comparator->Next())) {
245 TString s1 = this->GetLFN("");
246 TString s2 = comparator->GetLFN("");
253 this->Remove(overlapmap);
260 //___________________________________________________________________________
261 AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
262 // Static method used to create an Alien event collection, by reading
263 // collection for the specified file.
265 AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
269 //___________________________________________________________________________
270 void AliXMLCollection::ParseXML() {
271 // Parse event file collection XML file.
275 XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
277 Error("ParseXML","cannot parse the xml file %s",fXmlFile.Data());
281 XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
283 Error("ParseXML","cannot find the <alien> tag in %s",fXmlFile.Data());
287 XMLNodePointer_t xcollection = xml.GetChild(xalien);
289 Error("ParseXML","cannot find the <collection> tag in %s",fXmlFile.Data());
293 XMLNodePointer_t xevent = xml.GetChild(xcollection);;
295 Error("ParseXML","cannot find the <event> tag in %s",fXmlFile.Data());
301 if (xml.GetAttr(xevent, "name")) {
302 TMap *files = new TMap();
305 XMLNodePointer_t xfile = xml.GetChild(xevent);
306 if (!xfile) continue;
308 Bool_t firstfile=kTRUE;
310 // here we have an event file
311 // get the attributes;
312 xml.GetAttr(xfile, "lfn");
313 xml.GetAttr(xfile, "turl");
315 TMap *attributes = new TMap();
316 TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
317 TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
318 TObjString *olfn = new TObjString(xml.GetAttr(xfile,"lfn"));
319 TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
320 TObjString *oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
321 printf("Collection: %s - The Eventlist is %s\n",fXmlFile.Data(),oevlist->GetName());
322 if (oevlist->GetName() != "") {
323 TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
324 TString stringevlist = oevlist->GetName();
325 TObjArray *evlist = stringevlist.Tokenize(",");
326 for (Int_t n = 0; n < evlist->GetEntries(); n++) xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
327 attributes->Add(new TObjString("evlist"), xmlevlist);
330 attributes->Add(new TObjString("name"),oname);
331 attributes->Add(new TObjString("turl"),oturl);
332 attributes->Add(new TObjString("lfn"),olfn);
333 attributes->Add(new TObjString("guid"),oguid);
334 files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
336 // we add the first file always as a file without name to the map
338 files->Add(new TObjString(""),attributes);
341 } while ((xfile = xml.GetNext(xfile)));
342 fEventList->Add(files);
344 } while ((xevent = xml.GetNext(xevent)));