#include <cstdlib>
#include <Riostream.h>
#include <TEntryList.h>
#include <TList.h>
#include <TMap.h>
#include <TObjArray.h>
#include <TObjString.h>
#include <TString.h>
#include <TXMLEngine.h>
#include "AliLog.h"
#include "AliXMLCollection.h"
ClassImp(AliXMLCollection)
AliXMLCollection::AliXMLCollection() :
TGridCollection(),
fXmlFile(),
fEventList(0),
fEventListIter(0),
fCurrent(0),
fCollectionName(),
fout(),
fTotalEvents(0),
fAcceptedEvents(0),
fRejectedRun(0),
fRejectedLHC(0),
fRejectedDet(0),
fRejectedEvt(0)
{
}
AliXMLCollection::AliXMLCollection(const char *localcollectionfile) :
TGridCollection(),
fXmlFile(localcollectionfile),
fEventList(0),
fEventListIter(0),
fCurrent(0),
fCollectionName(),
fout(),
fTotalEvents(0),
fAcceptedEvents(0),
fRejectedRun(0),
fRejectedLHC(0),
fRejectedDet(0),
fRejectedEvt(0)
{
fEventList = new TList();
fEventList->SetOwner(kTRUE);
fEventListIter = new TIter(fEventList);
if (localcollectionfile!=0) {
ParseXML();
}
}
AliXMLCollection::AliXMLCollection(const AliXMLCollection& collection):
TGridCollection(collection),
fXmlFile(collection.fXmlFile),
fEventList(0),
fEventListIter(0),
fCurrent(0),
fCollectionName(collection.fCollectionName),
fout(),
fTotalEvents(0),
fAcceptedEvents(0),
fRejectedRun(0),
fRejectedLHC(0),
fRejectedDet(0),
fRejectedEvt(0)
{
if (collection.fEventList) fEventList = new TList();
if (collection.fEventListIter) fEventListIter = new TIter(fEventList);
if (collection.fCurrent) fCurrent = 0;
}
AliXMLCollection::~AliXMLCollection()
{
delete fEventList;
delete fEventListIter;
}
Bool_t AliXMLCollection::WriteHeader() {
TString xmlName = fCollectionName;
xmlName += ".xml";
TString collectionHeader = "<collection name=";
collectionHeader += "\"";
collectionHeader += fCollectionName;
collectionHeader += "\"";
collectionHeader += ">";
fout.open(xmlName);
fout<<"<?xml version=\"1.0\"?>\n";
fout<<"<alien>\n";
fout<<" "<<collectionHeader<<"\n";
return kTRUE;
}
Bool_t AliXMLCollection::WriteSummary(Int_t aTotal, Int_t aAccepted, Int_t aRejRun, Int_t aRejLHC, Int_t aRejDet, Int_t aRejEvt)
{
TString collectionSummary = "<summary";
collectionSummary += " acceptedEvents=\"";
collectionSummary += aAccepted;
collectionSummary += "\" totalEvent=\"";
collectionSummary += aTotal;
collectionSummary += "\" rejectedRun=\"";
collectionSummary += aRejRun;
collectionSummary += "\" rejectedLHC=\"";
collectionSummary += aRejLHC;
collectionSummary += "\" rejectedDet=\"";
collectionSummary += aRejDet;
collectionSummary += "\" rejectedEvt=\"";
collectionSummary += aRejEvt;
collectionSummary += "\" />";
fout<<" "<<collectionSummary<<"\n";
return kTRUE;
}
Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char* lfn, const char* turl, TEntryList* list)
{
TString listline;
for(Int_t i = 0; i < list->GetN(); i++) {
listline += list->GetEntry(i);
listline += ",";
}
listline = listline(0,listline.Length()-1);
TString line0 = "<event name=\"";
line0 += counter;
line0 += "\">";
TString line1 = "<file name=\"AliESDs.root\" ";
line1 += "guid=\"";
line1 += guid;
line1 += "\" ";
line1 += "lfn=\"";
line1 += lfn;
line1 += "\" ";
line1 += "turl=\"";
line1 += turl;
line1 += "\" ";
line1 += "evlist=\"";
line1 += listline;
line1 += "\"";
line1 += " />";
fout<<" "<<line0<<"\n";
fout<<" "<<line1<<"\n";
fout<<" </event>\n";
return kTRUE;
}
Bool_t AliXMLCollection::WriteBody(Int_t counter, const char* guid, const char *lfn, const char *turl, TEntryList* list, Int_t accSum, Int_t rejSum)
{
TString listline;
for(Int_t i = 0; i < list->GetN(); i++) {
listline += list->GetEntry(i);
listline += ",";
}
listline = listline(0,listline.Length()-1);
TString line0 = "<event name=\"";
line0 += counter;
line0 += "\">";
TString line1 = "<file name=\"AliESDs.root\" ";
line1 += "guid=\"";
line1 += guid;
line1 += "\" ";
line1 += "lfn=\"";
line1 += lfn;
line1 += "\" ";
line1 += "turl=\"";
line1 += turl;
line1 += "\" ";
line1 += "evlist=\"";
line1 += listline;
line1 += "\" ";
line1 += "cutsumm=\"";
line1 += accSum;
line1 += ",";
line1 += rejSum;
line1 += "\"";
line1 += " />";
fout<<" "<<line0<<"\n";
fout<<" "<<line1<<"\n";
fout<<" </event>\n";
return kTRUE;
}
Bool_t AliXMLCollection::Export() {
fout<<" "<<"</collection>\n";
fout<<"</alien>\n";
fout.close();
return kTRUE;
}
void AliXMLCollection::Reset() {
fEventListIter->Reset();
fCurrent = 0;
}
TMap *AliXMLCollection::Next() {
fCurrent = (TMap*)fEventListIter->Next();
return fCurrent;
}
const char *AliXMLCollection::GetTURL(const char* filename) {
if (fCurrent) {
TMap *obj = (TMap*)fCurrent->GetValue(filename);
if (obj) {
if (obj->GetValue("turl")) {
return ( ((TObjString*)obj->GetValue("turl"))->GetName());
}
}
}
AliError(Form("cannot get TURL of file %s",filename));
return 0;
}
const char *AliXMLCollection::GetGUID(const char* filename) {
if (fCurrent) {
TMap *obj = (TMap*)fCurrent->GetValue(filename);
if (obj) {
if (obj->GetValue("guid")) {
return ( ((TObjString*)obj->GetValue("guid"))->GetName());
}
}
}
AliError(Form("cannot get GUID of file %s",filename));
return 0;
}
TEntryList *AliXMLCollection::GetEventList(const char *filename) const {
if (fCurrent) {
TMap *obj = (TMap *) fCurrent->GetValue(filename);
if (obj) {
if (obj->GetValue("evlist")) {
return ((TEntryList *) obj->GetValue("evlist"));
}
}
}
AliError(Form("cannot get evelist of file %s", filename));
return 0;
}
Bool_t AliXMLCollection::Remove(TMap * map) {
if (fEventList->Remove(map)) {
return kTRUE;
} else {
return kFALSE;
}
}
const char *AliXMLCollection::GetLFN(const char* ) {
if (fCurrent) {
TMap *obj = (TMap *) fCurrent->GetValue("");
if (obj) {
if (obj->GetValue("lfn")) {
return (((TObjString *) obj->GetValue("lfn"))->GetName());
}
}
}
AliError("cannot get LFN");
return 0;
}
const char *AliXMLCollection::GetCutSumm() {
if (fCurrent) {
TMap *obj = (TMap *) fCurrent->GetValue("");
if (obj) {
if (obj->GetValue("cutsumm")) {
return (((TObjString *) obj->GetValue("cutsumm"))->GetName());
}
}
}
AliError("cannot get Tag Cut Summary");
return 0;
}
Bool_t AliXMLCollection::OverlapCollection(TGridCollection * comparator) {
if ((!comparator)) return kFALSE;
loopagain:
this->Reset();
TMap *overlapmap;
while ((overlapmap = this->Next())) {
comparator->Reset();
Bool_t found = kFALSE;
while ((comparator->Next())) {
TString s1 = this->GetLFN("");
TString s2 = comparator->GetLFN("");
if (s1 == s2) {
found = kTRUE;
break;
}
}
if (!found) {
this->Remove(overlapmap);
goto loopagain;
}
}
return kTRUE;
}
AliXMLCollection *AliXMLCollection::Open(const char *localcollectionfile) {
AliXMLCollection *collection = new AliXMLCollection(localcollectionfile);
return collection;
}
void AliXMLCollection::ParseXML() {
TXMLEngine xml;
XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
if (!xdoc) {
AliError(Form("cannot parse the xml file %s",fXmlFile.Data()));
return;
}
XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
if (!xalien) {
AliError(Form("cannot find the <alien> tag in %s",fXmlFile.Data()));
return;
}
XMLNodePointer_t xcollection = xml.GetChild(xalien);
if (!xcollection) {
AliError(Form("cannot find the <collection> tag in %s",fXmlFile.Data()));
return;
}
XMLNodePointer_t xevent = xml.GetChild(xcollection);;
if (!xevent) {
AliError(Form("cannot find the <event> tag in %s",fXmlFile.Data()));
return;
}
do {
if (xml.GetAttr(xevent, "name")) {
TMap *files = new TMap();
XMLNodePointer_t xfile = xml.GetChild(xevent);
if (!xfile) continue;
Bool_t firstfile=kTRUE;
do {
xml.GetAttr(xfile, "lfn");
xml.GetAttr(xfile, "turl");
TMap *attributes = new TMap();
TObjString *oname = new TObjString(xml.GetAttr(xfile,"name"));
TObjString *oturl = new TObjString(xml.GetAttr(xfile,"turl"));
TObjString *olfn = new TObjString(xml.GetAttr(xfile,"lfn"));
TObjString *oguid = new TObjString(xml.GetAttr(xfile,"guid"));
TObjString *oevlist;
if (xml.GetAttr(xfile, "evlist"))
oevlist = new TObjString(xml.GetAttr(xfile, "evlist"));
else
oevlist = 0;
TObjString *otagsumm;
if (xml.GetAttr(xfile, "cutsumm"))
otagsumm = new TObjString(xml.GetAttr(xfile, "cutsumm"));
else
otagsumm = 0;
if (oevlist) {
AliDebug(1,Form("Collection: %s - turl: %s eventlist: %s",
fXmlFile.Data(),oturl->GetName(),oevlist->GetName()));
TEntryList *xmlevlist = new TEntryList(oturl->GetName(), oguid->GetName());
if (strcmp(oevlist->GetName(),"") != 0) {
TString stringevlist = oevlist->GetName();
TObjArray *evlist = stringevlist.Tokenize(",");
for (Int_t n = 0; n < evlist->GetEntries(); n++) xmlevlist->Enter(atol(((TObjString *) evlist->At(n))->GetName()));
delete evlist;
}
attributes->Add(new TObjString("evlist"), xmlevlist);
}
attributes->Add(new TObjString("name"),oname);
attributes->Add(new TObjString("turl"),oturl);
attributes->Add(new TObjString("lfn"),olfn);
attributes->Add(new TObjString("guid"),oguid);
if (otagsumm)
attributes->Add(new TObjString("cutsumm"),otagsumm);
files->Add(new TObjString(xml.GetAttr(xfile,"name")) , attributes);
if (firstfile) {
files->Add(new TObjString(""),attributes);
firstfile=kFALSE;
}
} while ((xfile = xml.GetNext(xfile)));
fEventList->Add(files);
}
else {
if (xml.GetAttr(xevent, "acceptedEvents")) {
fAcceptedEvents = atoi(xml.GetAttr(xevent, "acceptedEvents"));
fTotalEvents = atoi(xml.GetAttr(xevent, "totalEvent"));
fRejectedRun = atoi(xml.GetAttr(xevent, "rejectedRun"));
fRejectedLHC = atoi(xml.GetAttr(xevent, "rejectedLHC"));
fRejectedDet = atoi(xml.GetAttr(xevent, "rejectedDet"));
fRejectedEvt = atoi(xml.GetAttr(xevent, "rejectedEvt"));
}
}
} while ((xevent = xml.GetNext(xevent)));
}
Bool_t AliXMLCollection::GetCollectionSummary(Int_t *aTot, Int_t *aAcc, Int_t *aRejRun, Int_t *aRejLHC, Int_t *aRejDet, Int_t *aRejEvt) const
{
*aTot = fTotalEvents;
*aAcc = fAcceptedEvents;
*aRejRun = fRejectedRun;
*aRejLHC = fRejectedLHC;
*aRejDet = fRejectedDet;
*aRejEvt = fRejectedEvt;
return kTRUE;
}