ANALYSIS and ANALYSIScalib
[u/mrichter/AliRoot.git] / ANALYSIS / AliXMLCollection.cxx
CommitLineData
d08133e6 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
b09247a2 24#include <cstdlib>
d08133e6 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>
0bf3148c 34#include "AliLog.h"
d08133e6 35
36#include "AliXMLCollection.h"
37
38ClassImp(AliXMLCollection)
d08133e6 39//___________________________________________________________________________
40 AliXMLCollection::AliXMLCollection() :
41 TGridCollection(),
42 fXmlFile(),
43 fEventList(0),
44 fEventListIter(0),
45 fCurrent(0),
46 fCollectionName(),
f0f29480 47 fout(),
48 fTotalEvents(0),
49 fAcceptedEvents(0),
50 fRejectedRun(0),
51 fRejectedLHC(0),
52 fRejectedDet(0),
53 fRejectedEvt(0)
54{
d08133e6 55 //Default constructor
56}
57
58//___________________________________________________________________________
26f071d8 59AliXMLCollection::AliXMLCollection(const char *localcollectionfile) :
60 TGridCollection(),
61 fXmlFile(localcollectionfile),
62 fEventList(0),
63 fEventListIter(0),
64 fCurrent(0),
65 fCollectionName(),
f0f29480 66 fout(),
67 fTotalEvents(0),
68 fAcceptedEvents(0),
69 fRejectedRun(0),
70 fRejectedLHC(0),
71 fRejectedDet(0),
72 fRejectedEvt(0)
26f071d8 73 {
d08133e6 74 // Create Alien event collection, by reading collection for the specified
75 // file.
76
d08133e6 77 fEventList = new TList();
78 fEventList->SetOwner(kTRUE);
79 fEventListIter = new TIter(fEventList);
26f071d8 80
d08133e6 81 if (localcollectionfile!=0) {
82 ParseXML();
83 }
84}
85
86//___________________________________________________________________________
87AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
88 TGridCollection(collection),
89 fXmlFile(collection.fXmlFile),
26f071d8 90 fEventList(0),
91 fEventListIter(0),
92 fCurrent(0),
93 fCollectionName(collection.fCollectionName),
f0f29480 94 fout(),
95 fTotalEvents(0),
96 fAcceptedEvents(0),
97 fRejectedRun(0),
98 fRejectedLHC(0),
99 fRejectedDet(0),
100 fRejectedEvt(0)
26f071d8 101{
d08133e6 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//___________________________________________________________________________
0bf3148c 110AliXMLCollection::~AliXMLCollection()
111{
d08133e6 112 //Destructor
0bf3148c 113 delete fEventList;
114 delete fEventListIter;
d08133e6 115}
116
117//___________________________________________________________________________
118Bool_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//___________________________________________________________________________
f0f29480 140Bool_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//___________________________________________________________________________
0bf3148c 166Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList* list)
167{
d08133e6 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}
f0f29480 201//___________________________________________________________________________
0bf3148c 202Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char *lfn, const char *turl, TEntryList* list, Int_t accSum, Int_t rejSum)
f0f29480 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);
d08133e6 211
f0f29480 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}
d08133e6 243//___________________________________________________________________________
244Bool_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//___________________________________________________________________________
255void AliXMLCollection::Reset() {
256 // Reset file iterator.
257
258 fEventListIter->Reset();
259 fCurrent = 0;
260}
261
262//___________________________________________________________________________
263TMap *AliXMLCollection::Next() {
264 // Return next event file map.
265
266 fCurrent = (TMap*)fEventListIter->Next();
267 return fCurrent;
268}
269
270//___________________________________________________________________________
26f071d8 271const char *AliXMLCollection::GetTURL(const char* filename) {
d08133e6 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 }
0bf3148c 282 AliError(Form("cannot get TURL of file %s",filename));
d08133e6 283 return 0;
284}
285
286//___________________________________________________________________________
26f071d8 287const char *AliXMLCollection::GetGUID(const char* filename) {
d08133e6 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 }
0bf3148c 298 AliError(Form("cannot get GUID of file %s",filename));
d08133e6 299 return 0;
300}
301
302//___________________________________________________________________________
303TEntryList *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 }
0bf3148c 314 AliError(Form("cannot get evelist of file %s", filename));
d08133e6 315 return 0;
316}
317
318//___________________________________________________________________________
319Bool_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//___________________________________________________________________________
26f071d8 329const char *AliXMLCollection::GetLFN(const char* ) {
d08133e6 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 }
0bf3148c 340 AliError("cannot get LFN");
d08133e6 341 return 0;
342}
343
344//__________________________________________________________________________
f0f29480 345const 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 }
0bf3148c 356 AliError("cannot get Tag Cut Summary");
f0f29480 357 return 0;
358
359}
360//__________________________________________________________________________
26f071d8 361Bool_t AliXMLCollection::OverlapCollection(TGridCollection * comparator) {
d08133e6 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//___________________________________________________________________________
391AliXMLCollection *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//___________________________________________________________________________
400void AliXMLCollection::ParseXML() {
401 // Parse event file collection XML file.
402
403 TXMLEngine xml;
404
405 XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
406 if (!xdoc) {
0bf3148c 407 AliError(Form("cannot parse the xml file %s",fXmlFile.Data()));
d08133e6 408 return;
409 }
410
411 XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
412 if (!xalien) {
0bf3148c 413 AliError(Form("cannot find the <alien> tag in %s",fXmlFile.Data()));
d08133e6 414 return;
415 }
416
417 XMLNodePointer_t xcollection = xml.GetChild(xalien);
418 if (!xcollection) {
0bf3148c 419 AliError(Form("cannot find the <collection> tag in %s",fXmlFile.Data()));
d08133e6 420 return;
421 }
422
423 XMLNodePointer_t xevent = xml.GetChild(xcollection);;
424 if (!xevent) {
0bf3148c 425 AliError(Form("cannot find the <event> tag in %s",fXmlFile.Data()));
d08133e6 426 return;
427 }
d08133e6 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;
f0f29480 436
d08133e6 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"));
c334d0d3 449
450 TObjString *oevlist;
451 if (xml.GetAttr(xfile, "evlist"))
452 oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
453 else
454 oevlist = 0;
455
f0f29480 456 TObjString *otagsumm;
457 if (xml.GetAttr(xfile, "cutsumm"))
458 otagsumm = new TObjString(xml.GetAttr(xfile, "cutsumm"));
459 else
460 otagsumm = 0;
c334d0d3 461
462 if (oevlist) {
0bf3148c 463 AliDebug(1,Form("Collection: %s - turl: %s eventlist: %s",
464 fXmlFile.Data(),oturl->GetName(),oevlist->GetName()));
d08133e6 465 TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
c334d0d3 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()));
09d5920f 470 delete evlist;
c334d0d3 471 }
d08133e6 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);
f0f29480 479 if (otagsumm)
480 attributes->Add(new TObjString("cutsumm"),otagsumm);
d08133e6 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 }
f0f29480 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 }
d08133e6 502 } while ((xevent = xml.GetNext(xevent)));
503}
504
f690bf48 505Bool_t AliXMLCollection::GetCollectionSummary(Int_t *aTot, Int_t *aAcc, Int_t *aRejRun, Int_t *aRejLHC, Int_t *aRejDet, Int_t *aRejEvt) const
f0f29480 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}