TAlienCollection.cxx

Go to the documentation of this file.
00001 // @(#)root/alien:$Id: TAlienCollection.cxx 35952 2010-09-30 16:20:22Z brun $
00002 // Author: Andreas-Joachim Peters 9/5/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TAlienCollection                                                     //
00015 //                                                                      //
00016 // Class which manages collection of files on AliEn middleware.         //
00017 // The file collection is in the form of an XML file.                   //
00018 //                                                                      //
00019 // The internal list is managed as follows:                             //
00020 // TList* ===> TMap*(file) ===> TMap*(attributes)                       //
00021 //                                                                      //
00022 //////////////////////////////////////////////////////////////////////////
00023 
00024 #include "TAlienCollection.h"
00025 #include "TAlienResult.h"
00026 #include "TAlienFile.h"
00027 #include "TDSet.h"
00028 #include "TMap.h"
00029 #include "TFile.h"
00030 #include "TXMLEngine.h"
00031 #include "TObjString.h"
00032 #include "TEntryList.h"
00033 #include "TObjArray.h"
00034 #include "TROOT.h"
00035 #include "TError.h"
00036 #include "TFileCollection.h"
00037 #include "TFileInfo.h"
00038 #include <cstdlib>
00039 
00040 ClassImp(TAlienCollection)
00041 
00042 //______________________________________________________________________________
00043 TAlienCollection::TAlienCollection(const char *localcollectionfile,
00044                                    UInt_t maxentries)
00045 {
00046    // Create Alien event collection by reading collection from the specified XML file.
00047    // You can restrict the number of importet entries using the maxentries value.
00048 
00049    fXmlFile = localcollectionfile;
00050    fFileGroupList = new TList();
00051    fFileGroupList->SetOwner(kTRUE);
00052    fFileGroupListIter = new TIter(fFileGroupList);
00053    fCurrent = 0;
00054    fNofGroups = 0;
00055    fNofGroupfiles = 0;
00056    fHasSUrls = kFALSE;
00057    fHasSelection = kFALSE;
00058    fFileStager = 0;
00059    fExportUrl = "";
00060    fInfoComment = "";
00061    fCollectionName = "unnamed";
00062    fTagFilterList = 0;
00063    if (localcollectionfile != 0) {
00064       ParseXML(maxentries);
00065    }
00066 }
00067 
00068 //______________________________________________________________________________
00069 TAlienCollection::TAlienCollection(TList *eventlist, UInt_t nofgroups,
00070                                    UInt_t nofgroupfiles)
00071 {
00072    // Create Alien event collection using an event list.
00073 
00074    fFileGroupList = eventlist;
00075    fFileGroupList->SetOwner(kTRUE);
00076    fFileGroupListIter = new TIter(fFileGroupList);
00077    fCurrent = 0;
00078    fNofGroups = nofgroups;
00079    fNofGroupfiles = nofgroupfiles;
00080    fHasSUrls = kFALSE;
00081    fHasSelection = kFALSE;
00082    fFileStager = 0;
00083    fExportUrl = "";
00084    fInfoComment = "";
00085    fCollectionName = "unnamed";
00086    fTagFilterList = 0;
00087 }
00088 
00089 //______________________________________________________________________________
00090 TAlienCollection::~TAlienCollection()
00091 {
00092    // Clean up event file collection.
00093 
00094    if (fFileGroupList)
00095       delete fFileGroupList;
00096 
00097    if (fFileGroupListIter)
00098       delete fFileGroupListIter;
00099 
00100    if (fTagFilterList)
00101       delete fTagFilterList;
00102 }
00103 
00104 //______________________________________________________________________________
00105 TGridCollection *TAlienCollection::Open(const char *collectionurl,
00106                                         UInt_t maxentries)
00107 {
00108    // Static method used to create an Alien event collection, by reading
00109    // an XML collection from the specified url. All ROOT URLs are supported.
00110    // You can restrict the number of importet entries using the maxentries value
00111 
00112    //! stage the url to /tmp/
00113 
00114    TUUID uuid;
00115    TString rndname = "/tmp/";
00116    rndname += "aliencollection.";
00117    rndname += uuid.AsString();
00118    if (!TFile::Cp(collectionurl, rndname.Data())) {
00119       ::Error("TAlienCollection::Open", "Cannot make a local copy of collection with url %s",
00120               collectionurl);
00121       return 0;
00122    }
00123 
00124    TAlienCollection *collection =
00125        new TAlienCollection(rndname, maxentries);
00126 
00127    if (gSystem->Unlink(rndname.Data())) {
00128       ::Error("TAlienCollection::Open", "Cannot remove the local copy of the collection %s",
00129               rndname.Data());
00130    }
00131 
00132    return dynamic_cast <TAlienCollection * > (collection);
00133 }
00134 
00135 //______________________________________________________________________________
00136 TFile *TAlienCollection::OpenFile(const char *filename)
00137 {
00138    // Open the file specified by <filename> from the currently active file group in the collection via its TURL.
00139 
00140    const char *turl = GetTURL(filename);
00141    if (turl) {
00142       return TFile::Open(turl);
00143    }
00144    return 0;
00145 }
00146 
00147 //______________________________________________________________________________
00148 TAlienCollection *TAlienCollection::OpenAlienCollection(TGridResult * queryresult,
00149                                              Option_t* /*option*/)
00150 {
00151    // Static method used to create an Alien event collection, by creating
00152    // collection from a TGridResult GetCollection result (TAlien::GetCollection)
00153 
00154    if (!queryresult)
00155       return 0;
00156 
00157    TList *filelist = new TList();
00158 
00159    TIterator* fileiter = queryresult->MakeIterator();
00160    TMap *filemap = 0;
00161 
00162    while ((filemap = ((TMap *) fileiter->Next()))) {
00163       if (!filemap->GetValue("origLFN"))
00164          continue;
00165 
00166       filemap->Add(new TObjString("lfn"), new TObjString(filemap->GetValue("origLFN")->GetName()));
00167       filemap->Add(new TObjString("turl"), new TObjString(Form("alien://%s", filemap->GetValue("origLFN")->GetName())));
00168       TString bname = gSystem->BaseName(filemap->GetValue("origLFN")->GetName());
00169       filemap->Add(new TObjString("name"), new TObjString(bname.Data()));
00170 
00171       TMap* filegroup = new TMap();
00172       filegroup->Add(new TObjString(bname.Data()), filemap);
00173       filegroup->Add(new TObjString(""), filemap);
00174 
00175       // store the filegroup
00176       filelist->Add(filegroup);
00177    }
00178    delete fileiter;
00179 
00180    return new TAlienCollection(filelist, filelist->GetEntries(), 1);;
00181 }
00182 
00183 //______________________________________________________________________________
00184 TGridCollection *TAlienCollection::OpenQuery(TGridResult * queryresult,
00185                                              Bool_t nogrouping)
00186 {
00187    // Static method used to create an Alien event collection, by creating
00188    // collection from a TGridResult Query result (TAlien::Query)
00189    // nogrouping means that files in the same directory are treated as not belonging to a file group
00190 
00191    UInt_t nofgroups = 0;
00192    UInt_t nofgroupfiles = 0;
00193 
00194    if (!queryresult) {
00195       return 0;
00196    }
00197 
00198    TIterator *fileiter = queryresult->MakeIterator();
00199    TMap *filemap = 0;
00200    TString prev_bname = "";
00201    TString prev_dname = "";
00202    TMap *filegroup = 0;
00203 
00204    TList *filelist = new TList();
00205 
00206    while ((filemap = ((TMap *) fileiter->Next()))) {
00207       if (!filemap->GetValue("lfn"))
00208          continue;
00209       TString dname =
00210           gSystem->DirName(filemap->GetValue("lfn")->GetName());
00211       TString bname =
00212           gSystem->BaseName(filemap->GetValue("lfn")->GetName());
00213       filemap->Add(new TObjString("name"), new TObjString(bname.Data()));
00214       if ((!nogrouping)
00215           && ((bname != prev_bname) && (dname == prev_dname)
00216               && (filegroup))) {
00217          // add to group
00218          filegroup->Add(new TObjString(bname.Data()), filemap);
00219          if (nofgroups == 0)
00220             nofgroupfiles++;
00221       } else {
00222          // new group
00223          if (filegroup) {
00224             // store the old filegroup
00225             filelist->Add(filegroup);
00226             nofgroups++;
00227          }
00228          if (nofgroups == 0)
00229             nofgroupfiles++;
00230          filegroup = new TMap();
00231          filegroup->Add(new TObjString(bname.Data()), filemap);
00232          filegroup->Add(new TObjString(""), filemap);
00233       }
00234 
00235       prev_bname = bname;
00236       prev_dname = dname;
00237    }
00238    if (filegroup) {
00239       nofgroups++;
00240       filelist->Add(filegroup);
00241    }
00242    // set tag filter list
00243    TList *filterlist = new TList();
00244    filterlist->Add(new TObjString("type"));
00245    filterlist->Add(new TObjString("dir"));
00246    filterlist->Add(new TObjString("perm"));
00247    filterlist->Add(new TObjString("owner"));
00248    filterlist->Add(new TObjString("ctime"));
00249    filterlist->Add(new TObjString("seStringlist"));
00250    filterlist->Add(new TObjString("aclId"));
00251    filterlist->Add(new TObjString("expiretime"));
00252    filterlist->Add(new TObjString("replicated"));
00253    filterlist->Add(new TObjString("entryId"));
00254    filterlist->Add(new TObjString("gowner"));
00255    filterlist->Add(new TObjString("selist"));
00256    filterlist->Add(new TObjString("select"));
00257    filterlist->Add(new TObjString("online"));
00258 
00259 
00260    TAlienCollection *newcollection =
00261        new TAlienCollection(filelist, nofgroups, nofgroupfiles);
00262    if (newcollection) {
00263       newcollection->SetTagFilterList(filterlist);
00264    }
00265    return  dynamic_cast <TAlienCollection *> (newcollection);
00266 }
00267 
00268 //______________________________________________________________________________
00269 void TAlienCollection::Reset()
00270 {
00271    // Reset file iterator.
00272 
00273    fFileGroupListIter->Reset();
00274    fCurrent = 0;
00275 }
00276 
00277 //______________________________________________________________________________
00278 void TAlienCollection::ParseXML(UInt_t maxentries)
00279 {
00280    // Parse event file collection XML file.
00281    // <maxentries> stops the parsing after <maxentries>.
00282 
00283    TXMLEngine xml;
00284    UInt_t parsedentries = 0;
00285 
00286    XMLDocPointer_t xdoc = xml.ParseFile(fXmlFile);
00287    if (!xdoc) {
00288       Error("ParseXML", "cannot parse the xml file %s", fXmlFile.Data());
00289       return;
00290    }
00291 
00292    XMLNodePointer_t xalien = xml.DocGetRootElement(xdoc);
00293    if (!xalien) {
00294       Error("ParseXML", "cannot find the <alien> tag in %s",
00295             fXmlFile.Data());
00296       return;
00297    }
00298 
00299    XMLNodePointer_t xcollection = xml.GetChild(xalien);
00300    if (!xcollection) {
00301       Error("ParseXML", "cannot find the <collection> tag in %s",
00302             fXmlFile.Data());
00303       return;
00304    }
00305 
00306    if (xml.GetAttr(xcollection,"name")) {
00307       fCollectionName = TString(xml.GetAttr(xcollection,"name"));
00308    } else {
00309       fCollectionName = ("unnamed");
00310    }
00311 
00312    XMLNodePointer_t xevent = xml.GetChild(xcollection);;
00313    if (!xevent) {
00314       Error("ParseXML", "cannot find the <event> tag in %s",
00315             fXmlFile.Data());
00316       return;
00317    }
00318    if (!xevent)
00319       return;
00320 
00321    fNofGroups = 0;
00322    fNofGroupfiles = 0;
00323    fHasSUrls = kFALSE;
00324    fHasOnline = kFALSE;
00325 
00326    do {
00327       if (TString(xml.GetNodeName(xevent)) == "event") {
00328          parsedentries++;
00329          fNofGroups++;
00330          TMap *files = new TMap();
00331 
00332          // here is our event
00333          //      printf("Found event: %s\n",xml.GetAttr(xevent,"name"));
00334 
00335          // files
00336          XMLNodePointer_t xfile = xml.GetChild(xevent);
00337          if (!xfile)
00338             continue;
00339 
00340          Bool_t firstfile = kTRUE;
00341          do {
00342             // here we have an event file
00343             // get the attributes;
00344             xml.GetAttr(xfile, "lfn");
00345             xml.GetAttr(xfile, "turl");
00346 
00347             TMap *attributes = new TMap();
00348             TObjString *oname = new TObjString(xml.GetAttr(xfile, "name"));
00349             TObjString *oturl = new TObjString(xml.GetAttr(xfile, "turl"));
00350             TObjString *olfn = new TObjString(xml.GetAttr(xfile, "lfn"));
00351             TObjString *omd5 = new TObjString(xml.GetAttr(xfile, "md5"));
00352             TObjString *osize = new TObjString(xml.GetAttr(xfile, "size"));
00353             TObjString *oguid = new TObjString(xml.GetAttr(xfile, "guid"));
00354             TObjString *osurl = new TObjString(xml.GetAttr(xfile, "surl"));
00355             TObjString *osselect =
00356                 new TObjString(xml.GetAttr(xfile, "select"));
00357             TObjString *ossexporturl =
00358                 new TObjString(xml.GetAttr(xfile, "exporturl"));
00359             TObjString *osonline =
00360                 new TObjString(xml.GetAttr(xfile, "online"));
00361 
00362             TObjString *oseStringlist =
00363                 new TObjString(xml.GetAttr(xfile, "seStringlist"));
00364             TObjString *oevlist =
00365                 new TObjString(xml.GetAttr(xfile, "evlist"));
00366             // if oevlist is defined, we parse it and fill a TEntyList
00367             if (oevlist && strlen(oevlist->GetName())) {
00368                TEntryList *xmlentrylist =
00369                    new TEntryList(oturl->GetName(), oguid->GetName());
00370                TString stringevlist = oevlist->GetName();
00371                TObjArray *evlist = stringevlist.Tokenize(",");
00372                for (Int_t n = 0; n < evlist->GetEntries(); n++) {
00373                   xmlentrylist->
00374                       Enter(atol
00375                             (((TObjString *) evlist->At(n))->GetName()));
00376                }
00377                attributes->Add(new TObjString("evlist"), xmlentrylist);
00378             }
00379             attributes->Add(new TObjString("name"), oname);
00380             attributes->Add(new TObjString("turl"), oturl);
00381             attributes->Add(new TObjString("lfn"), olfn);
00382             attributes->Add(new TObjString("md5"), omd5);
00383             attributes->Add(new TObjString("size"), osize);
00384             attributes->Add(new TObjString("guid"), oguid);
00385             attributes->Add(new TObjString("seStringlist"), oseStringlist);
00386 
00387             if (osurl && strlen(osurl->GetName())) {
00388                attributes->Add(new TObjString("surl"), osurl);
00389                fHasSUrls = kTRUE;
00390             }
00391             if (osselect && strlen(osselect->GetName())) {
00392                attributes->Add(new TObjString("select"), osselect);
00393                fHasSelection = kTRUE;
00394             }
00395             if (osonline && strlen(osonline->GetName())) {
00396                attributes->Add(new TObjString("online"), osonline);
00397                fHasOnline = kTRUE;
00398             }
00399 
00400             if (ossexporturl && strlen(ossexporturl->GetName())) {
00401                attributes->Add(new TObjString("exporturl"), ossexporturl);
00402                fExportUrl = ossexporturl->GetName();
00403             }
00404 
00405             files->Add(new TObjString(xml.GetAttr(xfile, "name")),
00406                        attributes);
00407 
00408             // we add the first file always as a file without name to the map
00409             if (firstfile) {
00410                files->Add(new TObjString(""), attributes);
00411                firstfile = kFALSE;
00412             }
00413             if (fNofGroups == 1)
00414                fNofGroupfiles++;
00415          } while ((xfile = xml.GetNext(xfile)));
00416          fFileGroupList->Add(files);
00417       }
00418 
00419       if (TString(xml.GetNodeName(xevent)) == "info") {
00420          if (xml.GetAttr(xevent,"comment")) {
00421             fInfoComment = TString(xml.GetAttr(xevent,"comment"));
00422          } else {
00423             fInfoComment = "";
00424          }
00425       }
00426 
00427       if (TString(xml.GetNodeName(xevent)) == "export") {
00428          if (xml.GetAttr(xevent,"url")) {
00429             SetExportUrl(xml.GetAttr(xevent,"url"));
00430          } else {
00431             fExportUrl = "";
00432          }
00433       }
00434       if (parsedentries >= maxentries)
00435          return;
00436    } while ((xevent = xml.GetNext(xevent)));
00437 }
00438 
00439 //______________________________________________________________________________
00440 TMap *TAlienCollection::Next()
00441 {
00442    // Return next event file map.
00443 
00444    fCurrent = (TMap *) fFileGroupListIter->Next();
00445    return fCurrent;
00446 }
00447 
00448 //______________________________________________________________________________
00449 void TAlienCollection::Status()
00450 {
00451    // Prints statistics, selection and status about the loaded collection.
00452 
00453    TIter *statuslist = new TIter(fFileGroupList);
00454    statuslist->Reset();
00455    TMap *oldcurrent = fCurrent;
00456    TMap *nextgroup = 0;
00457    UInt_t gc;
00458    UInt_t fc;
00459    UInt_t onlinegroups;
00460    UInt_t offlinegroups;
00461    UInt_t onlinefiles;
00462    UInt_t offlinefiles;
00463    UInt_t totalfiles;
00464    Long64_t totalfilesize;
00465    Long64_t onlinefilesize;
00466    Long64_t offlinefilesize;
00467 
00468    gc = 0;
00469    fc = 0;
00470    onlinegroups = offlinegroups = onlinefiles = offlinefiles = totalfiles =
00471        totalfilesize = onlinefilesize = offlinefilesize = 0;
00472    while ((nextgroup = (TMap *) statuslist->Next())) {
00473       gc++;
00474       fc = 0;
00475       Bool_t online;
00476       Bool_t selected;
00477       online = kTRUE;
00478       selected = kFALSE;
00479       TMap *attributes;
00480       TIterator *nextfile = nextgroup->MakeIterator();
00481       nextfile->Reset();
00482       while ((attributes = (TMap *) nextfile->Next())) {
00483          fCurrent = nextgroup;
00484          if (TString(attributes->GetName()) != "") {
00485             totalfiles++;
00486             totalfilesize += GetSize(attributes->GetName());
00487          }
00488          if (IsSelected(attributes->GetName())) {
00489             selected = kTRUE;
00490             if (TString(attributes->GetName()) != "") {
00491                fc++;
00492                fCurrent = nextgroup;
00493                if (!IsOnline(attributes->GetName())) {
00494                   online = kFALSE;
00495                   offlinefiles++;
00496                   offlinefilesize += GetSize(attributes->GetName());
00497                } else {
00498                   onlinefiles++;
00499                   onlinefilesize += GetSize(attributes->GetName());
00500                }
00501             }
00502          }
00503       }
00504       if (selected) {
00505          if (online)
00506             onlinegroups++;
00507          else
00508             offlinegroups++;
00509       }
00510    }
00511    fCurrent = oldcurrent;
00512    Info("Status", "=========================================");
00513    Info("Status", "     Tot. Number of files: %u", totalfiles);
00514    Info("Status", "                Tot. Size: %0.2f GB",
00515         totalfilesize / 1024.0 / 1024.0 / 1024.0);
00516    Info("Status", "    Number of file groups: %u", gc);
00517    Info("Status", "Number of files per group: %u", fc);
00518    Info("Status", "-----------------------------------------");
00519    Info("Status", "Online (staged [selected]):");
00520    Info("Status", "    Number of file groups: %u", onlinegroups);
00521    Info("Status", "          Number of files: %u", onlinefiles);
00522    Info("Status", "                     Size: %0.2f GB",
00523         onlinefilesize / 1024.0 / 1024.0 / 1024.0);
00524    Info("Status", "           Fraction avail: %0.2f %%",
00525         100.0 * onlinefilesize / (onlinefilesize + offlinefilesize +
00526                                   0.0000001));
00527    Info("Status", "-----------------------------------------");
00528    Info("Status", "Offline (to be staged [selected]):");
00529    Info("Status", "    Number of file groups: %u", offlinegroups);
00530    Info("Status", "          Number of files: %u", offlinefiles);
00531    Info("Status", "                     Size: %0.2f GB",
00532         offlinefilesize / 1024.0 / 1024.0 / 1024.0);
00533    Info("Status", "            Fraction miss: %0.2f %%",
00534         100.0 * offlinefilesize / (onlinefilesize + offlinefilesize +
00535                                    0.0000001));
00536    Info("Status", "=========================================\n");
00537 
00538 }
00539 
00540 //______________________________________________________________________________
00541 void TAlienCollection::SetTag(const char *tag, const char *value,
00542                               TMap * tagmap)
00543 {
00544    // Set's a key value pair in a tagmap. If it is existing, the existing tag is overwritten. If not, it is created.
00545 
00546    if ((!tag) || (!value) || (!tagmap)) {
00547       return;
00548    }
00549    TObject *delobject = tagmap->FindObject(tag);
00550    if (delobject) {
00551       TObject *keyobject = ((TPair *) delobject)->Key();
00552       tagmap->Remove(keyobject);;
00553    }
00554    tagmap->Add(new TObjString(tag), new TObjString(value));
00555 }
00556 
00557 
00558 //______________________________________________________________________________
00559 Bool_t TAlienCollection::SelectFile(const char *filename, Int_t nstart, Int_t nstop)
00560 {
00561    // Selects all files with name <filename> in the collection
00562    // All files can be selected using "*" as filename
00563    Int_t cnt=0;
00564    fHasSelection = kTRUE;
00565    Reset();
00566    TMap *nextgroup;
00567    while ((nextgroup = (TMap *) Next())) {
00568       cnt++;
00569       TMap *attributes;
00570       TIterator *nextfile = nextgroup->MakeIterator();
00571       nextfile->Reset();
00572       if ( ((nstart == -1 ) && (nstop == -1)) ||
00573            ((nstart != -1 ) && (cnt >= nstart) && (nstop == -1)) ||
00574            ((nstart != -1 ) && (cnt >= nstart) && (nstop != -1) && (cnt <= nstop)) ||
00575            ((nstop  != -1 ) && (cnt <= nstop)  && (nstart == -1))) {
00576          while ((attributes = (TMap *) nextfile->Next())) {
00577             if (TString(attributes->GetName()) != "") {
00578                if ((TString(attributes->GetName()) == TString(filename)) ||
00579                    (TString(filename) == TString("*"))) {
00580                   SetTag("select", "1",
00581                   ((TMap *) nextgroup->GetValue(attributes->GetName())));
00582                }
00583             }
00584          }
00585       }
00586    }
00587    return kTRUE;
00588 }
00589 
00590 //______________________________________________________________________________
00591 Bool_t TAlienCollection::DeselectFile(const char *filename, Int_t nstart, Int_t nstop)
00592 {
00593    // Deselects the file <filename> from the loaded collection
00594    // All files can be deselected using "*" as <filename>
00595    Int_t cnt=0;
00596    Reset();
00597    TMap *nextgroup;
00598    while ((nextgroup = (TMap *) Next())) {
00599       cnt++;
00600       TMap *attributes;
00601       TIterator *nextfile = nextgroup->MakeIterator();
00602       nextfile->Reset();
00603       if ( ((nstart == -1 ) && (nstop == -1)) ||
00604            ((nstart != -1 ) && (cnt >= nstart) && (nstop == -1)) ||
00605            ((nstart != -1 ) && (cnt >= nstart) && (nstop != -1) && (cnt <= nstop)) ||
00606            ((nstop  != -1 ) && (cnt <= nstop)  && (nstart == -1))) {
00607          while ((attributes = (TMap *) nextfile->Next())) {
00608             if (TString(attributes->GetName()) != "") {
00609                if ((TString(attributes->GetName()) == TString(filename)) ||
00610                    (TString(filename) == TString("*"))) {
00611                   SetTag("select", "0",
00612                   ((TMap *) nextgroup->GetValue(attributes->GetName())));
00613                }
00614             }
00615          }
00616       }
00617    }
00618 
00619    return kTRUE;
00620 }
00621 
00622 //______________________________________________________________________________
00623 Bool_t TAlienCollection::InvertSelection()
00624 {
00625    // Invert the selection.
00626 
00627    Int_t cnt=0;
00628    fHasSelection = kTRUE;
00629    Reset();
00630    TMap *nextgroup;
00631    while ((nextgroup = (TMap *) Next())) {
00632       cnt++;
00633       TMap *attributes;
00634       TIterator *nextfile = nextgroup->MakeIterator();
00635       nextfile->Reset();
00636       while ((attributes = (TMap *) nextfile->Next())) {
00637          if (IsSelected(attributes->GetName())) {
00638             SetTag("select", "0",
00639                ((TMap *) nextgroup->GetValue(attributes->GetName())));
00640          } else {
00641             SetTag("select", "1",
00642                ((TMap *) nextgroup->GetValue(attributes->GetName())));
00643          }
00644       }
00645    }
00646    return kTRUE;
00647 }
00648 
00649 //______________________________________________________________________________
00650 Bool_t TAlienCollection::DownscaleSelection(UInt_t scaler)
00651 {
00652    // downscales the selection with scaler
00653    Int_t cnt = 0;
00654 
00655    Reset();
00656    TMap *nextgroup;
00657    while ((nextgroup = (TMap *) Next())) {
00658       cnt++;
00659       TMap *attributes;
00660       TIterator *nextfile = nextgroup->MakeIterator();
00661       nextfile->Reset();
00662       if (cnt%scaler) {
00663          while ((attributes = (TMap *) nextfile->Next())) {
00664             if (TString(attributes->GetName()) != "") {
00665                SetTag("select", "0",
00666                   ((TMap *) nextgroup->GetValue(attributes->GetName())));
00667             }
00668          }
00669       }
00670    }
00671    return kTRUE;
00672 }
00673 
00674 
00675 //______________________________________________________________________________
00676 Bool_t TAlienCollection::Remove(TMap * map)
00677 {
00678    // Return next event file map.
00679    if (fFileGroupList->Remove(map)) {
00680       return kTRUE;
00681    } else {
00682       return kFALSE;
00683    }
00684 }
00685 
00686 //______________________________________________________________________________
00687 const char *TAlienCollection::GetTURL(const char *filename)
00688 {
00689    // Get a file's transport URL (TURL). Returns 0 in case of error.
00690 
00691    if (fCurrent) {
00692       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00693       if (obj) {
00694          if (obj->GetValue("turl")) {
00695             return (((TObjString *) obj->GetValue("turl"))->GetName());
00696          }
00697       }
00698    }
00699    Error("GetTURL", "cannot get TURL of file %s", filename);
00700    return 0;
00701 }
00702 
00703 //______________________________________________________________________________
00704 const char *TAlienCollection::GetSURL(const char *filename)
00705 {
00706    // Get a file's storage URL (SURL). Returns 0 in case of error.
00707 
00708    if (fCurrent) {
00709       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00710       if (obj) {
00711          if (obj->GetValue("surl")) {
00712             return (((TObjString *) obj->GetValue("surl"))->GetName());
00713          }
00714       }
00715    }
00716    Error("GetSURL", "cannot get SURL of file %s", filename);
00717    return 0;
00718 }
00719 
00720 //______________________________________________________________________________
00721 Bool_t TAlienCollection::IsOnline(const char *filename)
00722 {
00723    // Get a file's online tag. Returns false if not online or CheckIfOnline was never executed, true if online
00724 
00725    if (fCurrent) {
00726       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00727       if (obj) {
00728          if (obj->GetValue("online")) {
00729             TString online =
00730                 (((TObjString *) obj->GetValue("online"))->GetName());
00731             if (online == "1") {
00732                return kTRUE;
00733             } else {
00734                return kFALSE;
00735             }
00736          }
00737       }
00738    }
00739    //   Error("IsOnline", "cannot get online tag of file %s", filename);
00740    return kFALSE;
00741 }
00742 
00743 //______________________________________________________________________________
00744 Bool_t TAlienCollection::IsSelected(const char *filename)
00745 {
00746    // Get a file's online tag. Returns false if not online or CheckIfOnline was never executed, true if online
00747 
00748    if (fCurrent) {
00749       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00750       if (obj) {
00751          if (obj->GetValue("select")) {
00752             TString selected;
00753             selected =
00754                 (((TObjString *) obj->GetValue("select"))->GetName());
00755             if (selected == TString("1")) {
00756                return kTRUE;
00757             } else {
00758                return kFALSE;
00759             }
00760          }
00761       }
00762    }
00763    return kFALSE;
00764 }
00765 
00766 //______________________________________________________________________________
00767 TEntryList *TAlienCollection::GetEntryList(const char *filename)
00768 {
00769    // Get a file's event list. Returns 0 in case of error.
00770 
00771    if (fCurrent) {
00772       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00773       if (obj) {
00774          if (obj->GetValue("evlist")) {
00775             return ((TEntryList *) obj->GetValue("evlist"));
00776          }
00777       }
00778    }
00779    Error("GetEntryList", "cannot get evelist of file %s", filename);
00780    return 0;
00781 }
00782 
00783 //______________________________________________________________________________
00784 const char *TAlienCollection::GetLFN(const char *filename)
00785 {
00786    // Get a file's LFN. Returns 0 in case of error.
00787 
00788    if (fCurrent) {
00789       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00790       if (obj) {
00791          if (obj->GetValue("lfn")) {
00792             return (((TObjString *) obj->GetValue("lfn"))->GetName());
00793          }
00794       }
00795    }
00796    Error("GetLFN", "cannot get LFN");
00797    return 0;
00798 }
00799 
00800 //______________________________________________________________________________
00801 Long64_t TAlienCollection::GetSize(const char *filename)
00802 {
00803    // Get a file's LFN. Returns 0 in case of error.
00804 
00805    if (fCurrent) {
00806       TMap *obj = (TMap *) fCurrent->GetValue(filename);
00807       if (obj) {
00808          if (obj->GetValue("size")) {
00809             TString ssize =
00810                 (((TObjString *) obj->GetValue("size"))->GetName());
00811             return ssize.Atoll();
00812          }
00813       }
00814    }
00815    Error("GetSize", "cannot get size of %s", filename);
00816    return 0;
00817 }
00818 
00819 //______________________________________________________________________________
00820 void TAlienCollection::Print(Option_t *) const
00821 {
00822    // Print event file collection.
00823 
00824    Info("Print", "dumping %d elements", fFileGroupList->GetSize());
00825    TIter next(fFileGroupList);
00826    TMap *filemap;
00827    Int_t count = 0;
00828    while ((filemap = (TMap *) next())) {
00829       count++;
00830       Info("Print", "printing element %d", count);
00831       filemap->Print();
00832    }
00833 }
00834 
00835 //______________________________________________________________________________
00836 TDSet *TAlienCollection::GetDataset(const char *type, const char *objname,
00837                                     const char *dir)
00838 {
00839    // Return a TDSet from a collection. Filter, Selections and online tags are not
00840    // taken into account.
00841 
00842    Reset();
00843    TMap *mapp;
00844    TDSet *dset = new TDSet(type, objname, dir);
00845    if (!dset) {
00846       return 0;
00847    }
00848 
00849    while ((mapp = Next())) {
00850       if (((TObjString *) fCurrent->GetValue("")))
00851          dset->Add(((TMap *) (fCurrent->GetValue("")))->GetValue("turl")->
00852                    GetName());;
00853    }
00854    return dset;
00855 }
00856 
00857 //______________________________________________________________________________
00858 TGridResult *TAlienCollection::GetGridResult(const char *filename,
00859                                              Bool_t onlyonline,
00860                                              Bool_t publicaccess)
00861 {
00862    // Return a TGridResult.
00863    // If files have been selected in this collection, only the selected files
00864    // are returned. If <onlyonline> is set to kTRUE, only files which are
00865    // 'online' (staged) are included. If no online check was done, TGridResult
00866    // will be empty. <publicaccess> adds the publicaccess option to the TGridResult entries
00867 
00868    Reset();
00869    TMap *mapp;
00870    TGridResult *result = new TAlienResult();
00871 
00872    while ((mapp = Next())) {
00873       if (((TObjString *) fCurrent->GetValue(filename))) {
00874          TMap *attributes = (TMap *) fCurrent->GetValue(filename)->Clone();
00875          if (publicaccess) {
00876             attributes->Add(new TObjString("options"),
00877                             new TObjString("&publicaccess=1"));
00878          }
00879          if ((!fHasSelection) || (IsSelected(filename))) {
00880             if ((!onlyonline) || (fHasOnline && IsOnline(filename))) {
00881                result->Add(attributes);
00882             }
00883          }
00884       }
00885    }
00886    return dynamic_cast < TGridResult * >(result);
00887 }
00888 
00889 //______________________________________________________________________________
00890 Bool_t TAlienCollection::OverlapCollection(TGridCollection * comparator)
00891 {
00892    // return kTRUE if comparator overlaps with this
00893    // all objects in this collection, which are not defined in the <comparator> collection are removed.
00894 
00895    if ((!comparator)) {
00896       return kFALSE;
00897    }
00898 
00899 loopagain:
00900    // loop over col1 and try to find it in col2
00901    this->Reset();
00902    // loop over all elements in reference (=this)
00903    TMap *overlapmap;
00904    while ((overlapmap = this->Next())) {
00905       comparator->Reset();
00906       Bool_t found = kFALSE;
00907       // try to find in the comparator collection
00908       while ((comparator->Next())) {
00909          TString s1 = this->GetLFN();
00910          TString s2 = comparator->GetLFN();
00911          if (s1 == s2) {
00912             found = kTRUE;
00913             break;
00914          }
00915       }
00916       if (!found) {
00917          this->Remove(overlapmap);
00918          goto loopagain;
00919       }
00920    }
00921    return kTRUE;
00922 }
00923 
00924 //______________________________________________________________________________
00925 void TAlienCollection::Add(TGridCollection * addcollection)
00926 {
00927    // adds <addcollection> to this collection - equal elements are skipped
00928 
00929    if ((!addcollection)) {
00930       return;
00931    }
00932    // loop over col1 and try to find it in col2
00933    addcollection->Reset();
00934    // loop over all elements in reference (=this)
00935    TMap *addmap;
00936    TMap *thismap;
00937    while ((addmap = addcollection->Next())) {
00938       Reset();
00939       // try to find in the comparator collection
00940       TString s2 = addcollection->GetLFN();
00941       while ((thismap = Next())) {
00942          TString s1 = GetLFN();
00943          // printf("%s = %s\n", s1.Data(), s2.Data());
00944          if (s1 == s2) {
00945             Error("Add",
00946                   "File group with lfn %s exists already in this collection - skipping",
00947                   GetLFN());
00948             goto leaveloop;
00949          }
00950       }
00951       TMap *clonemap;
00952       clonemap = (TMap *) addmap->Clone();
00953       fFileGroupList->Add(clonemap);
00954 leaveloop:
00955       ;
00956    }
00957 }
00958 
00959 //______________________________________________________________________________
00960 Bool_t TAlienCollection::LookupSUrls(Bool_t verbose)
00961 {
00962    // retrieves all the SURLS for the LFNS
00963 
00964    Bool_t ok = kTRUE;
00965    UInt_t lc = 0;
00966    Reset();
00967    TMap *filemap;
00968    while ((filemap = Next())) {
00969       // loop over all files in this map
00970       TIterator *nextfile = filemap->MakeIterator();
00971       TMap *attributes;
00972       while ((attributes = (TMap *) nextfile->Next())) {
00973          if (TString(attributes->GetName()) != "") {
00974             lc++;
00975             if (fHasSelection && (!IsSelected(attributes->GetName())))
00976                continue;
00977             // there is always an "" entry in the map to point to the first file of a file group
00978             if (verbose)
00979                Info("LookupSUrls", "Lookup SURL for %s [%u/%u]",
00980                     GetTURL(attributes->GetName()), lc,
00981                     fNofGroups * fNofGroupfiles);
00982             TString surl =
00983                 TAlienFile::SUrl(GetTURL(attributes->GetName()));
00984             if (!surl.Data()) {
00985                ok = kFALSE;
00986             } else {
00987                // delete the surl map entry, if it exists already
00988                TObject *delobject =
00989                    ((TMap *) filemap->GetValue(attributes->GetName()))->
00990                    FindObject("surl");
00991                if (delobject) {
00992                   TObject *keyobject = ((TPair *) delobject)->Key();
00993                   ((TMap *) filemap->GetValue(attributes->GetName()))->
00994                       Remove(keyobject);;
00995                }
00996 
00997                ((TMap *) filemap->GetValue(attributes->GetName()))->
00998                    Add(new TObjString("surl"),
00999                        new TObjString(surl.Data()));
01000                if (verbose)
01001                   Info("LookupSUrls", "SURL = %s", surl.Data());
01002 
01003             }
01004 
01005          }
01006       }
01007    }
01008    if (ok) {
01009       fHasSUrls = kTRUE;
01010    }
01011    return ok;
01012 }
01013 
01014 //_____________________________________________________________________________
01015 Bool_t TAlienCollection::Stage(Bool_t bulk,Option_t* option)
01016 {
01017    // Execute the 'stage' method for all files in this collection (trigger staging).
01018    // The <bulk> stage method is currently not working.
01019 
01020    if (!fHasSUrls) {
01021       Error("Stage",
01022             "You have to execute LookupSUrls() before you can stage this collection");
01023       return kFALSE;
01024    }
01025    UInt_t fc = 0;
01026    Reset();
01027    TMap *filemap;
01028 
01029    if (!bulk) {
01030       while ((filemap = Next())) {
01031          // loop over all files in this map
01032          TIterator *nextfile = filemap->MakeIterator();
01033          TMap *attributes;
01034          while ((attributes = (TMap *) nextfile->Next())) {
01035             if (TString(attributes->GetName()) != "") {
01036                fc++;
01037                if (fHasSelection && (!IsSelected(attributes->GetName())))
01038                   continue;
01039 
01040                if (!fFileStager) {
01041                   fFileStager =
01042                       TFileStager::Open(GetSURL(attributes->GetName()));
01043                }
01044                if ((fFileStager)->Stage(GetSURL(attributes->GetName()),option)) {
01045                   // file staged
01046                   Info("Stage", "[%05u/%05u] <Staged> : %s", fc,
01047                        GetNofGroups() * GetNofGroupfiles(),
01048                        GetLFN(attributes->GetName()));
01049                } else {
01050                   // file stage failed
01051                   Error("Stage", "[%05u/%05u] <Failed to stage> : %s",
01052                         fc, GetNofGroups() * GetNofGroupfiles(),
01053                         GetLFN(attributes->GetName()));
01054                }
01055             }
01056          }
01057       }
01058    } else {
01059       // bulk request
01060       TList* stagelist = new TList();
01061       stagelist->SetOwner(kTRUE);
01062       Bool_t stageresult=kFALSE;
01063       Reset();
01064       while ((filemap = Next())) {
01065          TIterator *nextfile = filemap->MakeIterator();
01066          TMap *attributes;
01067          while ((attributes = (TMap *) nextfile->Next())) {
01068             if (TString(attributes->GetName()) != "") {
01069                fc++;
01070                stagelist->Add( new TUrl((GetSURL(attributes->GetName()))));
01071             }
01072          }
01073       }
01074 
01075       if (fc) {
01076          if (!fFileStager) {
01077             fFileStager = TFileStager::Open(stagelist->First()->GetName());
01078          }
01079 
01080          stageresult = (fFileStager)->Stage(stagelist,option);
01081       }
01082       delete stagelist;
01083       return stageresult;
01084    }
01085    return kTRUE;
01086 }
01087 
01088 //_____________________________________________________________________________
01089 Bool_t TAlienCollection::CheckIfOnline(Bool_t bulk)
01090 {
01091    // Run an online check if files are currently accessible (staged) or offline (to be staged).
01092    // The <bulk> check is currently not working.
01093 
01094    if (!fHasSUrls) {
01095       Error("CheckIfOnline",
01096             "You have to execute LookupSUrls() before you can prepare this collection");
01097       return kFALSE;
01098    }
01099 
01100    Reset();
01101    TMap *filemap;
01102    UInt_t fc=0;
01103 
01104    if (!bulk) {
01105       while ((filemap = Next())) {
01106          // loop over all files in this map
01107          TIterator *nextfile = filemap->MakeIterator();
01108          TMap *attributes;
01109          while ((attributes = (TMap *) nextfile->Next())) {
01110             if (fHasSelection && (!IsSelected(attributes->GetName())))
01111                continue;
01112             if (TString(attributes->GetName()) != "") {
01113                fc++;
01114                // check if we have a fFileStager
01115                if (!fFileStager) {
01116                   fFileStager =
01117                       TFileStager::Open(GetSURL(attributes->GetName()));
01118                }
01119                Bool_t online = kFALSE;
01120                if ((fFileStager)->IsStaged(GetSURL(attributes->GetName()))) {
01121                   // file is online
01122                   Info("CheckIfOnline", "[%05u/%05u] <Online> : %s", fc,
01123                        GetNofGroups() * GetNofGroupfiles(),
01124                        GetLFN(attributes->GetName()));
01125                   online = kTRUE;
01126                } else {
01127                   // file is offline
01128                   Info("CheckIfOnline", "[%05u/%05u]<Offline> : %s", fc,
01129                        GetNofGroups() * GetNofGroupfiles(),
01130                        GetLFN(attributes->GetName()));
01131                   online = kFALSE;
01132                }
01133 
01134                {
01135                   // set the online tag in the collection
01136                   // delete the online tag entry, if it exists already
01137                   TObject *delobject =
01138                       ((TMap *) filemap->GetValue(attributes->GetName()))->
01139                       FindObject("online");
01140                   if (delobject) {
01141                      TObject *keyobject = ((TPair *) delobject)->Key();
01142                      ((TMap *) filemap->GetValue(attributes->GetName()))->
01143                          Remove(keyobject);;
01144                   }
01145                   if (online)
01146                      ((TMap *) filemap->GetValue(attributes->GetName()))->
01147                          Add(new TObjString("online"),
01148                              new TObjString("1"));
01149                   else
01150                      ((TMap *) filemap->GetValue(attributes->GetName()))->
01151                          Add(new TObjString("online"),
01152                              new TObjString("0"));
01153                }
01154             }
01155          }
01156       }
01157    } else {
01158       // bulk lookup
01159       TList *lookuplist = new TList();
01160       if (lookuplist) {
01161          lookuplist->SetOwner(kTRUE);
01162          while ((filemap = Next())) {
01163             // loop over all files in this map
01164             TIterator *nextfile = filemap->MakeIterator();
01165             TMap *attributes;
01166             while ((attributes = (TMap *) nextfile->Next())) {
01167                if (TString(attributes->GetName()) != "") {
01168                   fc++;
01169                   // check if we have a fFileStager
01170                   if (!fFileStager) {
01171                      fFileStager =
01172                        TFileStager::Open(GetSURL(attributes->GetName()));
01173                   }
01174                   lookuplist->
01175                       Add(new TObjString(GetSURL(attributes->GetName())));
01176                }
01177             }
01178          }
01179 
01180          TList *onlinelist = fFileStager->GetStaged(lookuplist);
01181          if (!onlinelist)
01182             return kFALSE;
01183 
01184          Reset();
01185          fc=0;
01186          while ((filemap = Next())) {
01187             // loop over all files in this map
01188             TIterator *nextfile = filemap->MakeIterator();
01189             TMap *attributes;
01190             while ((attributes = (TMap *) nextfile->Next())) {
01191                if (TString(attributes->GetName()) != "") {
01192                   fc++;
01193                   Bool_t online;
01194                   // check if it is in the online list
01195                   if (onlinelist->
01196                       FindObject(GetSURL(attributes->GetName()))) {
01197                      // this file is online
01198                      Info("CheckIfOnline", "[%05u/%05u] <Online> : %s", fc,
01199                           GetNofGroups() * GetNofGroupfiles(),
01200                           GetLFN(attributes->GetName()));
01201                      online = kTRUE;
01202                   } else {
01203                      // this file is offline
01204                      Info("CheckIfOnline", "[%05u/%05u]<Offline> : %s", fc,
01205                           GetNofGroups() * GetNofGroupfiles(),
01206                           GetLFN(attributes->GetName()));
01207                      online = kFALSE;
01208                   }
01209 
01210                   {
01211                      // set the online tag in the collection
01212                      // delete the online tag entry, if it exists already
01213                      TObject *delobject =
01214                          ((TMap *) filemap->
01215                          GetValue(attributes->GetName()))->
01216                          FindObject("online");
01217                      if (delobject) {
01218                         TObject *keyobject = ((TPair *) delobject)->Key();
01219                         ((TMap *) filemap->
01220                          GetValue(attributes->GetName()))->
01221                          Remove(keyobject);
01222                      }
01223                      if (online)
01224                         ((TMap *) filemap->
01225                          GetValue(attributes->GetName()))->
01226                          Add(new TObjString("online"), new TObjString("1"));
01227                      else
01228                         ((TMap *) filemap->
01229                          GetValue(attributes->GetName()))->
01230                          Add(new TObjString("online"), new TObjString("0"));
01231                   }
01232                }
01233             }
01234          }
01235 
01236          SafeDelete(onlinelist);
01237          SafeDelete(lookuplist);
01238       } else {
01239          fHasOnline = kFALSE;
01240          return kFALSE;
01241       }
01242    }
01243 
01244    fHasOnline = kTRUE;
01245    return kTRUE;
01246 }
01247 
01248 //_____________________________________________________________________________
01249 Bool_t TAlienCollection::ExportXML(const char *exporturl, Bool_t selected, Bool_t online,
01250                                    const char *name , const char *comment)
01251 {
01252    // Exports the contens of the TAlienCollection into an XML formatted file.
01253    // By default exports only selected files. By default exports only accessible (online) files.
01254    // You can change this behaviour by specifying online=kFALSE or selected=kFALSE.
01255    // <name> specifies a name you want to assign to this collection.
01256    // <comment> can be a user comment to this collection.
01257    // If <exporturl>="" the collection is exported to the URL which was stored inside the collection or
01258    // was specified by the ExportUrl(const char* url) method.
01259 
01260 
01261    TFile *exportfile;
01262 
01263    if ((exporturl == 0) || (TString(exporturl) == "")) {
01264       if (!(exporturl = GetExportUrl())) {
01265          Error("ExportXML",
01266                "You used the option to store to the collection export url, but this is not defined!");
01267          return kFALSE;
01268       }
01269    }
01270    TUrl expfn(exporturl);
01271    TString options = expfn.GetOptions();
01272    if (options.Length()) {
01273       options += ",filetype=raw";
01274    } else {
01275       options = "filetype=raw";
01276    }
01277    expfn.SetOptions(options.Data());
01278 
01279    if (!(exportfile = TFile::Open(expfn.GetUrl(), "RECREATE"))) {
01280       Error("ExportXML", "Cannot open export URL %s", expfn.GetUrl());
01281       return kFALSE;
01282    }
01283 
01284    Bool_t expret = ExportXML(exportfile, selected, online, name, comment);
01285    exportfile->Close();
01286    return expret;
01287 }
01288 
01289 //_____________________________________________________________________________
01290 Bool_t TAlienCollection::ExportXML(TFile * exportfile, Bool_t selected,
01291                                    Bool_t online, const char *name,
01292                                    const char *comment)
01293 {
01294    // Internal Export function to write a collection as an XML file. See above.
01295 
01296    char outline[4096];
01297 
01298    // write headers
01299    snprintf(outline,4096,
01300            "<?xml version=\"1.0\"?>\n<alien>\n  <collection name=\"%s\">\n",
01301            name);
01302    if ((exportfile->WriteBuffer(outline, strlen(outline)))) {
01303       Error("ExportXML", "Error writing XML to export file");
01304       exportfile->Close();
01305       return kFALSE;
01306    }
01307    // write events
01308    TMap *oldcurrent = fCurrent;
01309    TMap *nextgroup;
01310    TIter *filegroups = new TIter(fFileGroupList);
01311    filegroups->Reset();
01312    UInt_t groupcnt = 0;
01313    while ((nextgroup = (TMap *) filegroups->Next())) {
01314       Bool_t isonline;
01315       Bool_t isselected;
01316       isonline = kTRUE;
01317       isselected = kFALSE;
01318       TMap *attributes;
01319       TIterator *nextfile = nextgroup->MakeIterator();
01320       nextfile->Reset();
01321       // check if something is selected
01322       while ((attributes = (TMap *) nextfile->Next())) {
01323          if (TString(attributes->GetName()) != "") {
01324             fCurrent = nextgroup;
01325             if (IsSelected(attributes->GetName())) {
01326                isselected = kTRUE;
01327             }
01328 
01329             if (!IsOnline(attributes->GetName())) {
01330                isonline = kFALSE;
01331             }
01332          }
01333       }
01334       if ((!selected) || isselected) {
01335          // loop again and export files
01336          nextfile->Reset();
01337          groupcnt++;
01338          // open new event header
01339          snprintf(outline,4096, "    <event name=\"%d\">\n", groupcnt);
01340          if ((exportfile->WriteBuffer(outline, strlen(outline)))) {
01341             Error("ExportXML", "Error writing XML to export file");
01342             exportfile->Close();
01343             return kFALSE;
01344          }
01345          while ((attributes = (TMap *) nextfile->Next())) {
01346             if (TString(attributes->GetName()) != "") {
01347                fCurrent = nextgroup;
01348                if ((!selected) || (IsSelected(attributes->GetName()))) {
01349                   if ((!online) || (IsOnline(attributes->GetName()))) {
01350                      // export this file
01351                      /////////////////////////////////////////////////////////////
01352                      // open file tag
01353                      snprintf(outline,4096, "      <file ");
01354                      if ((exportfile->
01355                           WriteBuffer(outline, strlen(outline)))) {
01356                         Error("ExportXML",
01357                               "Error writing XML to export file");
01358                         exportfile->Close();
01359                         return kFALSE;
01360                      }
01361                      // loop over map keys
01362                      TIterator *mkeys =
01363                          ((TMap *) nextgroup->
01364                           GetValue(attributes->GetName()))->MakeIterator();
01365                      mkeys->Reset();
01366                      TObjString *tagname = 0;
01367                      TObjString *tagval = 0;
01368                      while ((tagname = (TObjString *) mkeys->Next())) {
01369                         Bool_t filtered = kFALSE;
01370                         // check for filtered tags from the filter list
01371                         if (fTagFilterList) {
01372                            TIter next(fTagFilterList);
01373                            TObjString *filtertag;
01374                            while ((filtertag = (TObjString *) next())) {
01375                               if (TString(tagname->GetName()) ==
01376                                   TString(filtertag->GetName())) {
01377                                  filtered = kTRUE;
01378                                  break;
01379                               }
01380                            }
01381                         }
01382                         if (!filtered) {
01383                            tagval =
01384                                (TObjString *) ((TMap *) nextgroup->
01385                                                GetValue(attributes->
01386                                                         GetName()))->
01387                                GetValue(tagname);
01388                            if (TString(tagname->GetName()) != "evlist") {
01389                               snprintf(outline,4096, "%s=\"%s\" ",
01390                                       tagname->GetName(),
01391                                       tagval->GetName());
01392                            } else {
01393                               // the eventlist has to be converted from TEventList to a string list with komma separation
01394                               TEntryList *xmlentrylist =
01395                                   (TEntryList *) tagval;
01396                               if (!xmlentrylist)
01397                                  continue;
01398                               TString slist = "";
01399                               for (Int_t i = 0; i < xmlentrylist->GetN(); i++) {
01400                                  if (i > 0)
01401                                     slist += ",";
01402                                  slist += xmlentrylist->GetEntry(i);
01403                               }
01404                               snprintf(outline,4096, "%s=\"%s\" ",
01405                                       tagname->GetName(), slist.Data());
01406                            }
01407 
01408                            if ((exportfile->
01409                                 WriteBuffer(outline, strlen(outline)))) {
01410                               Error("ExportXML",
01411                                     "Error writing XML to export file");
01412                               exportfile->Close();
01413                               return kFALSE;
01414                            }
01415                         }
01416                      }
01417 
01418                      // close file tag
01419                      snprintf(outline,4096, "/>\n");
01420                      if ((exportfile->
01421                           WriteBuffer(outline, strlen(outline)))) {
01422                         Error("ExportXML",
01423                               "Error writing XML to export file");
01424                         exportfile->Close();
01425                         return kFALSE;
01426                      }
01427                   }
01428                }
01429             }
01430          }
01431 
01432          // close event
01433          snprintf(outline,4096, "    </event>\n");
01434          if ((exportfile->WriteBuffer(outline, strlen(outline)))) {
01435             Error("ExportXML", "Error writing XML to export file");
01436             exportfile->Close();
01437             return kFALSE;
01438          }
01439       }
01440    }
01441 
01442    fCurrent = oldcurrent;
01443 
01444    // write export url if present
01445    if (GetExportUrl()) {
01446       snprintf(outline,4096, "    <export url=\"%s\">\n",GetExportUrl());
01447       if ((exportfile->WriteBuffer(outline, strlen(outline)))) {
01448          Error("ExportXML", "Error writing XML to export file");
01449          exportfile->Close();
01450          return kFALSE;
01451       }
01452    }
01453 
01454 
01455    // write trailer
01456    snprintf(outline,4096,
01457            "    <info comment=\"%s\" />\n</collection>\n</alien>\n",
01458            comment);
01459    if ((exportfile->WriteBuffer(outline, strlen(outline)))) {
01460       Error("ExportXML", "Error writing XML to export file");
01461       exportfile->Close();
01462       return kFALSE;
01463    }
01464 
01465    return kTRUE;
01466 }
01467 
01468 //_____________________________________________________________________________
01469 Bool_t TAlienCollection::SetExportUrl(const char *exporturl)
01470 {
01471    // Set the 'default' export URL for an XML collection. A collection can be restored to the export URL using ExportXML("");
01472 
01473    if (exporturl)
01474       fExportUrl = exporturl;
01475 
01476    if (fExportUrl == "") {
01477       Info("ExportUrl",
01478            "There is no remote url defined in this collection");
01479       return kFALSE;
01480    }
01481    return kTRUE;
01482 }
01483 
01484 //_____________________________________________________________________________
01485 const char *TAlienCollection::GetOutputFileName(const char *infile,
01486                                                 Bool_t rename, const char* suffix)
01487 {
01488    // Adds to a file given by infile the collection identification , f.e.
01489    // for collection files sitting in directories like 100/1/AliESD.root
01490    //                                                  ...
01491    //                                                   110/1/AliESD.root
01492    // "./histo.root" will be converted to "./histo.100_1-110_1.230.root
01493    //
01494    // The name syntax is <oldname>.<first run>_<first event>-<last run>.<last event>.<nevents>.root
01495    //
01496    // By default the found files are renamed on the local disk
01497    // example:
01498    // - you specify f.e. as arguments GetOutputFileName("*.root",kTRUE);
01499    //  --->> this renames all ROOT files corresponding to the collection contents
01500 
01501    Bool_t first = kTRUE;
01502    TString firstrun;
01503    TString firstevent;
01504    TString lastrun;
01505    TString lastevent;
01506    UInt_t nevents = 0;
01507 
01508    Reset();
01509    while (Next()) {
01510       TString s1(gSystem->DirName(GetLFN("")));
01511       TString s2(gSystem->BaseName(s1.Data()));
01512       TString s3(gSystem->DirName(s1.Data()));
01513       TString s4(gSystem->BaseName(s3.Data()));
01514       nevents++;
01515       if (first) {
01516          first = kFALSE;
01517          firstevent = s2;
01518          firstrun = s4;
01519       }
01520       lastevent = s2;
01521       lastrun = s4;
01522    }
01523 
01524    // list the matching files
01525    TString pcmd("ls ");
01526    pcmd += infile;
01527    //printf("Pipe is %s\n",pcmd.Data());
01528    FILE *fp = gSystem->OpenPipe(pcmd.Data(), "r");
01529    if (fp) {
01530       char rootfile[4096];
01531       Int_t item;
01532       while ((item = fscanf(fp, "%s", rootfile)) == 1) {
01533          TString rootdir(gSystem->DirName(rootfile));
01534          TString rootbase(gSystem->BaseName(rootfile));
01535          TString rootbasenosuffix;
01536          rootbasenosuffix = rootbase(0, rootbase.First('.'));;
01537          // build new name like <oldname>.<firstrun>_<firstevent>-<lastrun>_<lastevent>.root
01538          TString newrootname;
01539          if (rootdir.Length()) {
01540             newrootname += rootdir;
01541             newrootname += "/";
01542          }
01543          newrootname += rootbasenosuffix;
01544          newrootname += ".";
01545          newrootname += firstrun;
01546          newrootname += "_";
01547          newrootname += firstevent;
01548          newrootname += "-";
01549          newrootname += lastrun;
01550          newrootname += "_";
01551          newrootname += lastevent;
01552          newrootname += ".";
01553          newrootname += nevents;
01554          newrootname += ".";
01555          newrootname += suffix;
01556          Info("GetOutputFilename", "Renaming File %s to %s", rootfile,
01557               newrootname.Data());
01558          fLastOutFileName = newrootname;
01559          if (rename) {
01560             gSystem->Rename(rootfile, newrootname.Data());
01561          }
01562       }
01563       gSystem->ClosePipe(fp);
01564    }
01565    return fLastOutFileName.Data();
01566 }
01567 
01568 //_____________________________________________________________________________
01569 TFileCollection *TAlienCollection::GetFileCollection(const char* name, const char* title) const
01570 {
01571    // creates a TFileCollection objects and fills it with the information from this collection
01572    // note that TFileCollection has a flat structure and no groups --> all files are filles on a flat level
01573    // the TFileInfo of each file in the TFileCollection is filled with turl, size, md5, guid
01574    //
01575    // the object has to be deleted by the user
01576 
01577    TFileCollection* collection = new TFileCollection(name, title);
01578 
01579    TIter next(fFileGroupList);
01580    TMap* group = 0;
01581    while ((group = dynamic_cast<TMap*>(next()))) {
01582       TIter next2(group);
01583       TObjString* key = 0;
01584       while ((key = dynamic_cast<TObjString*> (next2()))) {
01585          if (key->String().Length() == 0)
01586             continue;
01587 
01588          TMap* file = dynamic_cast<TMap*> (group->GetValue(key));
01589          if (!file)
01590             continue;
01591 
01592          TObjString* turl = dynamic_cast<TObjString*> (file->GetValue("turl"));
01593          TObjString* size = dynamic_cast<TObjString*> (file->GetValue("size"));
01594          TObjString* md5 = dynamic_cast<TObjString*> (file->GetValue("md5"));
01595          TObjString* guid = dynamic_cast<TObjString*> (file->GetValue("guid"));
01596 
01597          if (!turl || turl->String().Length() == 0)
01598             continue;
01599 
01600          TFileInfo* fileInfo = new TFileInfo(turl->String(), size->String().Atoi(), guid->String(), md5->String());
01601          collection->Add(fileInfo);
01602       }
01603    }
01604 
01605    collection->Update();
01606 
01607    return collection;
01608 }

Generated on Tue Jul 5 14:45:29 2011 for ROOT_528-00b_version by  doxygen 1.5.1