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