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