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