]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliXMLCollection.cxx
The ANALYSIS one is a modification of the AliXMLCollection which adds
[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     fTotalEvents(0),
48     fAcceptedEvents(0),
49     fRejectedRun(0),
50     fRejectedLHC(0),
51     fRejectedDet(0),
52     fRejectedEvt(0)
53 {
54   //Default constructor
55 }
56
57 //___________________________________________________________________________
58 AliXMLCollection::AliXMLCollection(const char *localcollectionfile) :
59     TGridCollection(),
60     fXmlFile(localcollectionfile),
61     fEventList(0),
62     fEventListIter(0),
63     fCurrent(0),
64     fCollectionName(),
65     fout(),
66     fTotalEvents(0),
67     fAcceptedEvents(0),
68     fRejectedRun(0),
69     fRejectedLHC(0),
70     fRejectedDet(0),
71     fRejectedEvt(0)
72  {
73    // Create Alien event collection, by reading collection for the specified
74    // file.
75
76    fEventList = new TList();
77    fEventList->SetOwner(kTRUE);
78    fEventListIter = new TIter(fEventList);
79
80    if (localcollectionfile!=0) {
81      ParseXML();
82    }
83 }
84
85 //___________________________________________________________________________
86 AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
87   TGridCollection(collection),
88   fXmlFile(collection.fXmlFile),
89   fEventList(0),
90   fEventListIter(0),
91   fCurrent(0),
92   fCollectionName(collection.fCollectionName),
93   fout(),
94   fTotalEvents(0),
95   fAcceptedEvents(0),
96   fRejectedRun(0),
97   fRejectedLHC(0),
98   fRejectedDet(0),
99   fRejectedEvt(0)
100 {
101   //copy constructor
102
103   if (collection.fEventList) fEventList = new TList();
104   if (collection.fEventListIter) fEventListIter = new TIter(fEventList);
105   if (collection.fCurrent) fCurrent = 0;
106 }
107
108 //___________________________________________________________________________
109 AliXMLCollection::~AliXMLCollection() {
110   //Destructor
111   if(fEventList) delete fEventList;
112   if(fEventListIter) delete fEventListIter;
113 }
114
115 //___________________________________________________________________________
116 Bool_t AliXMLCollection::WriteHeader() {
117   //Creates the xml output file
118
119   TString xmlName = fCollectionName;
120   xmlName += ".xml";
121
122   TString collectionHeader = "<collection name=";
123   collectionHeader += "\"";
124   collectionHeader += fCollectionName;
125   collectionHeader += "\"";
126   collectionHeader += ">";
127   
128   // Open the output stream
129   fout.open(xmlName);
130   fout<<"<?xml version=\"1.0\"?>\n";
131   fout<<"<alien>\n";
132   fout<<"  "<<collectionHeader<<"\n";  
133
134   return kTRUE;
135 }
136
137 //___________________________________________________________________________
138 Bool_t AliXMLCollection::WriteSummary(Int_t aTotal, Int_t aAccepted, Int_t aRejRun, Int_t aRejLHC, Int_t aRejDet, Int_t aRejEvt)
139 {
140   // Write selection summary
141
142   TString collectionSummary = "<summary";
143   collectionSummary += " acceptedEvents=\"";
144   collectionSummary += aAccepted;
145   collectionSummary += "\" totalEvent=\"";
146   collectionSummary += aTotal;
147   collectionSummary += "\" rejectedRun=\"";
148   collectionSummary += aRejRun;
149   collectionSummary += "\" rejectedLHC=\"";
150   collectionSummary += aRejLHC;
151   collectionSummary += "\" rejectedDet=\"";
152   collectionSummary += aRejDet;
153   collectionSummary += "\" rejectedEvt=\"";
154   collectionSummary += aRejEvt;
155   collectionSummary += "\" />";
156   
157   // Open the output stream
158   fout<<"  "<<collectionSummary<<"\n";  
159
160   return kTRUE;
161 }
162
163 //___________________________________________________________________________
164 Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList *list) {
165   //Writes the body of the xml collection
166   TString listline;
167   for(Int_t i = 0; i < list->GetN(); i++) {
168     listline += list->GetEntry(i);
169     listline += ",";
170   }  
171   listline = listline(0,listline.Length()-1);
172
173   TString line0 = "<event name=\"";
174   line0 += counter;
175   line0 += "\">";
176   
177   TString line1 = "<file name=\"AliESDs.root\" ";
178   line1 += "guid=\"";
179   line1 += guid;
180   line1 += "\" ";
181   line1 += "lfn=\"";
182   line1 += lfn;
183   line1 += "\" ";
184   line1 += "turl=\"";
185   line1 += turl;
186   line1 += "\" ";
187   line1 += "evlist=\"";
188   line1 += listline;
189   line1 += "\"";
190   line1 += " />";
191   
192   fout<<"    "<<line0<<"\n";
193   fout<<"      "<<line1<<"\n";
194   fout<<"    </event>\n";
195   
196   return kTRUE;
197 }
198 //___________________________________________________________________________
199 Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char *lfn, const char *turl, TEntryList *list, Int_t accSum, Int_t rejSum)
200 {
201   //Writes the body of the xml collection with tag cuts summary
202   TString listline;
203   for(Int_t i = 0; i < list->GetN(); i++) {
204     listline += list->GetEntry(i);
205     listline += ",";
206   }  
207   listline = listline(0,listline.Length()-1);
208
209   TString line0 = "<event name=\"";
210   line0 += counter;
211   line0 += "\">";
212   
213   TString line1 = "<file name=\"AliESDs.root\" ";
214   line1 += "guid=\"";
215   line1 += guid;
216   line1 += "\" ";
217   line1 += "lfn=\"";
218   line1 += lfn;
219   line1 += "\" ";
220   line1 += "turl=\"";
221   line1 += turl;
222   line1 += "\" ";
223   line1 += "evlist=\"";
224   line1 += listline;
225   line1 += "\" ";
226   line1 += "cutsumm=\"";
227   line1 += accSum;
228   line1 += ",";
229   line1 += rejSum;
230   line1 += "\"";
231   line1 += " />";
232   
233   fout<<"    "<<line0<<"\n";
234   fout<<"      "<<line1<<"\n";
235   fout<<"    </event>\n";
236   
237   return kTRUE;
238   
239 }
240 //___________________________________________________________________________
241 Bool_t AliXMLCollection::Export() {
242   //Closes the stream
243   fout<<"  "<<"</collection>\n";
244   fout<<"</alien>\n";
245
246   fout.close();
247
248   return kTRUE;
249 }
250
251 //___________________________________________________________________________
252 void AliXMLCollection::Reset() {
253   // Reset file iterator.
254   
255   fEventListIter->Reset();
256   fCurrent = 0;
257 }
258
259 //___________________________________________________________________________
260 TMap *AliXMLCollection::Next() {
261   // Return next event file map.
262   
263   fCurrent = (TMap*)fEventListIter->Next();
264   return fCurrent;
265 }
266
267 //___________________________________________________________________________
268 const char *AliXMLCollection::GetTURL(const char* filename) {
269   // Get a file's transport URL (TURL). Returns 0 in case of error.
270   
271   if (fCurrent) {
272     TMap *obj = (TMap*)fCurrent->GetValue(filename);
273     if (obj) {
274       if (obj->GetValue("turl")) {
275         return ( ((TObjString*)obj->GetValue("turl"))->GetName());
276       }
277     }
278   }
279   Error("GetTURL","cannot get TURL of file %s",filename);
280   return 0;
281 }
282
283 //___________________________________________________________________________
284 const char *AliXMLCollection::GetGUID(const char* filename) {
285   // Get a file's transport UID. Returns 0 in case of error.
286   
287   if (fCurrent) {
288     TMap *obj = (TMap*)fCurrent->GetValue(filename);
289     if (obj) {
290       if (obj->GetValue("guid")) {
291         return ( ((TObjString*)obj->GetValue("guid"))->GetName());
292       }
293     }
294   }
295   Error("GetGUID","cannot get GUID of file %s",filename);
296   return 0;
297 }
298
299 //___________________________________________________________________________
300 TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
301   // Get a file's event list. Returns 0 in case of error.
302
303   if (fCurrent) {
304     TMap *obj = (TMap *) fCurrent->GetValue(filename);
305     if (obj) {
306       if (obj->GetValue("evlist")) {
307         return ((TEntryList *) obj->GetValue("evlist"));
308       }
309     }
310   }
311   Error("GetEvList", "cannot get evelist of file %s", filename);
312   return 0;
313 }
314
315 //___________________________________________________________________________
316 Bool_t AliXMLCollection::Remove(TMap * map) {
317   // Return next event file map.
318   if (fEventList->Remove(map)) {
319     return kTRUE;
320   } else {
321     return kFALSE;
322   }
323 }
324
325 //___________________________________________________________________________
326 const char *AliXMLCollection::GetLFN(const char* ) {
327   // Get a file's LFN. Returns 0 in case of error.
328   
329   if (fCurrent) {
330     TMap *obj = (TMap *) fCurrent->GetValue("");
331     if (obj) {
332       if (obj->GetValue("lfn")) {
333         return (((TObjString *) obj->GetValue("lfn"))->GetName());
334       }
335     }
336   }
337   Error("GetLFN", "cannot get LFN");
338   return 0;
339 }
340
341 //__________________________________________________________________________
342 const char *AliXMLCollection::GetCutSumm() {
343   // Get a file's tag cuts summary. Returns 0 in case of error.
344   
345   if (fCurrent) {
346     TMap *obj = (TMap *) fCurrent->GetValue("");
347     if (obj) {
348       if (obj->GetValue("cutsumm")) {
349         return (((TObjString *) obj->GetValue("cutsumm"))->GetName());
350       }
351     }
352   }
353   Error("GetTagSumm", "cannot get Tag Cut Summary");
354   return 0;
355
356 }
357 //__________________________________________________________________________
358 Bool_t AliXMLCollection::OverlapCollection(TGridCollection * comparator) {
359   // return kTRUE if comparator overlaps with this
360   if ((!comparator)) return kFALSE;
361   
362  loopagain:
363   // loop over col1 and try to find it in col2
364   this->Reset();
365   // loop over all elements in reference (=this)
366   TMap *overlapmap;
367   while ((overlapmap = this->Next())) {
368     comparator->Reset();
369     Bool_t found = kFALSE;
370     // try to find in the comparator collection
371     while ((comparator->Next())) {
372       TString s1 = this->GetLFN("");
373       TString s2 = comparator->GetLFN("");
374       if (s1 == s2) {
375         found = kTRUE;
376         break;
377       }
378     }
379     if (!found) {
380       this->Remove(overlapmap);
381       goto loopagain;
382     }
383   }
384   return kTRUE;
385 }
386
387 //___________________________________________________________________________
388 AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
389   // Static method used to create an Alien event collection, by reading
390   // collection for the specified file.
391   
392   AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
393   return collection;
394 }
395
396 //___________________________________________________________________________
397 void AliXMLCollection::ParseXML() {
398   // Parse event file collection XML file.
399   
400   TXMLEngine xml;
401   
402   XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
403   if (!xdoc) {
404     Error("ParseXML","cannot parse the xml file %s",fXmlFile.Data());
405     return;
406   }
407
408   XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
409   if (!xalien) {
410     Error("ParseXML","cannot find the <alien> tag in %s",fXmlFile.Data());
411     return;
412   }
413   
414   XMLNodePointer_t xcollection = xml.GetChild(xalien);
415   if (!xcollection) {
416     Error("ParseXML","cannot find the <collection> tag in %s",fXmlFile.Data());
417     return;
418   }
419   
420   XMLNodePointer_t xevent = xml.GetChild(xcollection);;
421   if (!xevent) {
422     Error("ParseXML","cannot find the <event> tag in %s",fXmlFile.Data());
423     return;
424   }
425   if (!xevent) return;
426   
427   do {
428     if (xml.GetAttr(xevent, "name")) {
429       TMap *files = new TMap();
430             
431       // files
432       XMLNodePointer_t xfile = xml.GetChild(xevent);
433       if (!xfile) continue;
434
435       Bool_t firstfile=kTRUE;
436       do {
437         // here we have an event file
438         // get the attributes;
439         xml.GetAttr(xfile, "lfn");
440         xml.GetAttr(xfile, "turl");
441         
442         TMap *attributes = new TMap();
443         TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
444         TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
445         TObjString *olfn  = new TObjString(xml.GetAttr(xfile,"lfn"));
446         TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
447
448         TObjString *oevlist;
449         if (xml.GetAttr(xfile, "evlist"))
450           oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
451         else
452           oevlist = 0;
453
454         TObjString *otagsumm;
455         if (xml.GetAttr(xfile, "cutsumm"))
456           otagsumm = new TObjString(xml.GetAttr(xfile, "cutsumm"));
457         else 
458           otagsumm = 0;
459
460         if (oevlist) {
461           Info("ParseXML","Collection: %s - turl: %s eventlist: %s",
462                fXmlFile.Data(),oturl->GetName(),oevlist->GetName());
463           TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
464           if (strcmp(oevlist->GetName(),"") != 0) {
465             TString stringevlist = oevlist->GetName();
466             TObjArray *evlist = stringevlist.Tokenize(",");
467             for (Int_t n = 0; n < evlist->GetEntries(); n++)  xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
468           }
469           attributes->Add(new TObjString("evlist"), xmlevlist);
470         }
471         
472         attributes->Add(new TObjString("name"),oname);
473         attributes->Add(new TObjString("turl"),oturl);
474         attributes->Add(new TObjString("lfn"),olfn);
475         attributes->Add(new TObjString("guid"),oguid);
476         if (otagsumm)
477           attributes->Add(new TObjString("cutsumm"),otagsumm);
478         files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
479         
480         // we add the first file always as a file without name to the map
481         if (firstfile) {
482           files->Add(new TObjString(""),attributes);
483           firstfile=kFALSE;
484         }
485       } while ((xfile = xml.GetNext(xfile)));
486       fEventList->Add(files);
487     }
488     else {
489       if (xml.GetAttr(xevent, "acceptedEvents")) {
490         // Read list summary
491         fAcceptedEvents = atoi(xml.GetAttr(xevent, "acceptedEvents"));
492         fTotalEvents = atoi(xml.GetAttr(xevent, "totalEvent"));
493         fRejectedRun = atoi(xml.GetAttr(xevent, "rejectedRun"));
494         fRejectedLHC = atoi(xml.GetAttr(xevent, "rejectedLHC"));
495         fRejectedDet = atoi(xml.GetAttr(xevent, "rejectedDet"));
496         fRejectedEvt = atoi(xml.GetAttr(xevent, "rejectedEvt"));
497       }
498     }
499   } while ((xevent =  xml.GetNext(xevent)));
500 }
501
502 Bool_t      AliXMLCollection::GetCollectionSummary(Int_t *aTot, Int_t *aAcc, Int_t *aRejRun, Int_t *aRejLHC, Int_t *aRejDet, Int_t *aRejEvt)
503 {
504   // Return read list summary
505   *aTot = fTotalEvents;
506   *aAcc = fAcceptedEvents;
507   *aRejRun = fRejectedRun;
508   *aRejLHC = fRejectedLHC;
509   *aRejDet = fRejectedDet;
510   *aRejEvt = fRejectedEvt;
511   return kTRUE;
512 }