]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliXMLCollection.cxx
New raw-data TAGS infrastructure
[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 "TEntryList.h"
31 #include "TObjArray.h"
32
33 #include "AliXMLCollection.h"
34
35 ClassImp(AliXMLCollection)
36
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 //___________________________________________________________________________
50 AliXMLCollection::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 //___________________________________________________________________________
65 AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
66   TGridCollection(collection),
67   fXmlFile(collection.fXmlFile),
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
76 //___________________________________________________________________________
77 AliXMLCollection::~AliXMLCollection() {
78   //Destructor
79   if(fEventList) delete fEventList;
80   if(fEventListIter) delete fEventListIter;
81 }
82
83 //___________________________________________________________________________
84 Bool_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";
99   fout<<"<alien>\n";
100   fout<<"  "<<collectionHeader<<"\n";  
101
102   return kTRUE;
103 }
104
105 //___________________________________________________________________________
106 Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList *list) {
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 += "\" ";
123   line1 += "lfn=\"";
124   line1 += lfn;
125   line1 += "\" ";
126   line1 += "turl=\"";
127   line1 += turl;
128   line1 += "\" ";
129   line1 += "evlist=\"";
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 //___________________________________________________________________________
142 Bool_t AliXMLCollection::Export() {
143   //Closes the stream
144   fout<<"  "<<"</collection>\n";
145   fout<<"</alien>\n";
146
147   fout.close();
148
149   return kTRUE;
150 }
151
152 //___________________________________________________________________________
153 void AliXMLCollection::Reset() {
154   // Reset file iterator.
155   
156   fEventListIter->Reset();
157   fCurrent = 0;
158 }
159
160 //___________________________________________________________________________
161 TMap *AliXMLCollection::Next() {
162   // Return next event file map.
163   
164   fCurrent = (TMap*)fEventListIter->Next();
165   return fCurrent;
166 }
167
168 //___________________________________________________________________________
169 const 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 //___________________________________________________________________________
185 const char *AliXMLCollection::GetGUID(const char* filename) const {
186   // Get a file's transport UID. Returns 0 in case of error.
187   
188   if (fCurrent) {
189     TMap *obj = (TMap*)fCurrent->GetValue(filename);
190     if (obj) {
191       if (obj->GetValue("guid")) {
192         return ( ((TObjString*)obj->GetValue("guid"))->GetName());
193       }
194     }
195   }
196   Error("GetGUID","cannot get GUID of file %s",filename);
197   return 0;
198 }
199
200 //___________________________________________________________________________
201 TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
202   // Get a file's event list. Returns 0 in case of error.
203
204   if (fCurrent) {
205     TMap *obj = (TMap *) fCurrent->GetValue(filename);
206     if (obj) {
207       if (obj->GetValue("evlist")) {
208         return ((TEntryList *) obj->GetValue("evlist"));
209       }
210     }
211   }
212   Error("GetEvList", "cannot get evelist of file %s", filename);
213   return 0;
214 }
215
216 //___________________________________________________________________________
217 Bool_t AliXMLCollection::Remove(TMap * map) {
218   // Return next event file map.
219   if (fEventList->Remove(map)) {
220     return kTRUE;
221   } else {
222     return kFALSE;
223   }
224 }
225
226 //___________________________________________________________________________
227 const char *AliXMLCollection::GetLFN(const char* ) const {
228   // Get a file's LFN. Returns 0 in case of error.
229   
230   if (fCurrent) {
231     TMap *obj = (TMap *) fCurrent->GetValue("");
232     if (obj) {
233       if (obj->GetValue("lfn")) {
234         return (((TObjString *) obj->GetValue("lfn"))->GetName());
235       }
236     }
237   }
238   Error("GetLFN", "cannot get LFN");
239   return 0;
240 }
241
242 //__________________________________________________________________________
243 Bool_t AliXMLCollection::OverlapCollection(AliXMLCollection * comparator) {
244   // return kTRUE if comparator overlaps with this
245   if ((!comparator)) return kFALSE;
246   
247  loopagain:
248   // loop over col1 and try to find it in col2
249   this->Reset();
250   // loop over all elements in reference (=this)
251   TMap *overlapmap;
252   while ((overlapmap = this->Next())) {
253     comparator->Reset();
254     Bool_t found = kFALSE;
255     // try to find in the comparator collection
256     while ((comparator->Next())) {
257       TString s1 = this->GetLFN("");
258       TString s2 = comparator->GetLFN("");
259       if (s1 == s2) {
260         found = kTRUE;
261         break;
262       }
263     }
264     if (!found) {
265       this->Remove(overlapmap);
266       goto loopagain;
267     }
268   }
269   return kTRUE;
270 }
271
272 //___________________________________________________________________________
273 AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
274   // Static method used to create an Alien event collection, by reading
275   // collection for the specified file.
276   
277   AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
278   return collection;
279 }
280
281 //___________________________________________________________________________
282 void AliXMLCollection::ParseXML() {
283   // Parse event file collection XML file.
284   
285   TXMLEngine xml;
286   
287   XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
288   if (!xdoc) {
289     Error("ParseXML","cannot parse the xml file %s",fXmlFile.Data());
290     return;
291   }
292
293   XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
294   if (!xalien) {
295     Error("ParseXML","cannot find the <alien> tag in %s",fXmlFile.Data());
296     return;
297   }
298   
299   XMLNodePointer_t xcollection = xml.GetChild(xalien);
300   if (!xcollection) {
301     Error("ParseXML","cannot find the <collection> tag in %s",fXmlFile.Data());
302     return;
303   }
304   
305   XMLNodePointer_t xevent = xml.GetChild(xcollection);;
306   if (!xevent) {
307     Error("ParseXML","cannot find the <event> tag in %s",fXmlFile.Data());
308     return;
309   }
310   if (!xevent) return;
311   
312   do {
313     if (xml.GetAttr(xevent, "name")) {
314       TMap *files = new TMap();
315             
316       // files
317       XMLNodePointer_t xfile = xml.GetChild(xevent);
318       if (!xfile) continue;
319       
320       Bool_t firstfile=kTRUE;
321       do {
322         // here we have an event file
323         // get the attributes;
324         xml.GetAttr(xfile, "lfn");
325         xml.GetAttr(xfile, "turl");
326         
327         TMap *attributes = new TMap();
328         TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
329         TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
330         TObjString *olfn  = new TObjString(xml.GetAttr(xfile,"lfn"));
331         TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
332         TObjString *oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
333         printf("Collection: %s - The Eventlist is %s\n",fXmlFile.Data(),oevlist->GetName());
334         if (oevlist->GetName() != "") {
335           TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
336           TString stringevlist = oevlist->GetName();
337           TObjArray *evlist = stringevlist.Tokenize(",");
338           for (Int_t n = 0; n < evlist->GetEntries(); n++)  xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
339           attributes->Add(new TObjString("evlist"), xmlevlist);
340         }
341         
342         attributes->Add(new TObjString("name"),oname);
343         attributes->Add(new TObjString("turl"),oturl);
344         attributes->Add(new TObjString("lfn"),olfn);
345         attributes->Add(new TObjString("guid"),oguid);
346         files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
347         
348         // we add the first file always as a file without name to the map
349         if (firstfile) {
350           files->Add(new TObjString(""),attributes);
351           firstfile=kFALSE;
352         }
353       } while ((xfile = xml.GetNext(xfile)));
354       fEventList->Add(files);
355     }
356   } while ((xevent =  xml.GetNext(xevent)));
357 }
358