AliEn Classes necessary for analysis
[u/mrichter/AliRoot.git] / ANALYSIS / TGliteXmlEventlist.cxx
1 #include "TGliteXmlEventlist.h"
2
3 ///////////////////////////////////////////////////////////////////////////////////////
4 // class to read gLite XML collections
5 // Autor: Dr. A.-J. Peters - CERN 2004/ALICE   Mail-to: Andreas.Joachim.Peters@cern.ch
6 ///////////////////////////////////////////////////////////////////////////////////////
7
8 // example: 
9 //   ---------------------------------------------------------------------------------
10 //   TGliteXmlEventlist* evlist = new TGliteXmlEventlist("/tmp/fileset.rxml");
11 //   evlist->Reset()
12 //   // loop over all events
13 //   while (evlist->Next()) {
14 //     printf("URL for file AliESDs.root is: \n", evlist->GetURL("AliESDs.root"));
15 //   }
16 //   delete evlist;
17 //   ---------------------------------------------------------------------------------
18
19 #include <TCollection.h>
20 #include <TMap.h>
21 #include <TObjString.h>
22 #include <TXMLEngine.h>
23 #include <TList.h>
24
25
26 ClassImp(TGliteXmlEventlist)
27
28
29 TGliteXmlEventlist::TGliteXmlEventlist(const char* localfilename) {
30 //Andi - please put a comment
31   fXmlFile = localfilename;
32   fEventList = new TList();
33   fEventList->SetOwner(kTRUE);
34   fEventListIter = new TIter(fEventList);
35   fCurrent = 0;
36   ReadXML();
37 }
38
39 TGliteXmlEventlist::~TGliteXmlEventlist() {
40 //Andi - please put a comment
41   delete fEventList;
42   delete fEventListIter;
43 }
44
45 void
46 TGliteXmlEventlist::Reset() {
47 //Andi - please put a comment
48   fEventListIter->Reset();
49   fCurrent = 0;
50 }
51
52 TMap* TGliteXmlEventlist::Next() {
53 //Andi - please put a comment
54   fCurrent = (TMap*)fEventListIter->Next();
55   return fCurrent;
56 }
57
58 void TGliteXmlEventlist::ReadXML() {
59 //Andi - please put a comment
60   TXMLEngine* xml = new TXMLEngine();
61   xmlDocPointer xdoc = xml->ParseFile(fXmlFile);
62   xmlNodePointer xglite = xml->DocGetRootElement(xdoc);
63   xmlNodePointer xdtext = xml->GetChild(xglite);
64   xmlNodePointer xcollection = xml->GetNext(xdtext);
65
66   xmlNodePointer xtext = 0;
67   xmlNodePointer xevent = 0;
68   xmlNodePointer xtextnext  = 0;
69   xmlNodePointer xeventnext = 0;
70   Bool_t first_event=kTRUE;
71   do {
72     if (first_event) {
73       xtextnext  = xml->GetChild(xcollection);
74       first_event = kFALSE;
75     } else {
76       xtextnext  = xml->GetNext(xevent);
77     }
78
79     if (xtextnext) {
80       xeventnext = xml->GetNext(xtextnext);
81       xtext = xeventnext;
82       xevent = xeventnext;
83     } else {
84       xtext  = 0;
85     }
86
87     if (xevent) {
88       if (xml->GetAttr(xevent,"name")) {
89         TMap* files = new TMap();
90         
91         // here is our event
92         //      printf("Found xevent: %s\n",xml->GetAttr(xevent,"name"));
93         
94         Bool_t first_file = kTRUE;
95         
96         xmlNodePointer xfile = 0;
97         xmlNodePointer xfiletext = 0;
98         
99         xmlNodePointer xfilenext = 0;
100         xmlNodePointer xfiletextnext = 0;
101         do {
102           if (first_file) {
103             xfiletextnext = xml->GetChild(xevent);
104             first_file = kFALSE;
105           } else {
106             xfiletextnext = xml->GetNext(xfile);
107           }
108           
109           if (xfiletextnext) {
110             xfilenext = xml->GetNext(xfiletextnext);
111             xfiletext = xfilenext;
112             xfile     = xfilenext;
113           } else {
114             xfile     = 0;
115             xfiletext = 0;
116           }
117           if (xfile) {
118             // here we have an event file  
119             //      printf("Found file:   %s\n",xml->GetAttr(xfile,"name"));
120             
121             // get the attributes;
122             //    xml->GetAttr(xfile,"comment");
123             //    xml->GetAttr(xfile,"date");
124             //    xml->GetAttr(xfile,"group");
125             //    xml->GetAttr(xfile,"guid");
126             //    xml->GetAttr(xfile,"path");
127             //    xml->GetAttr(xfile,"permissions");
128             //    xml->GetAttr(xfile,"pfn");
129             //    xml->GetAttr(xfile,"se");
130             //    xml->GetAttr(xfile,"size");
131             //    xml->GetAttr(xfile,"user");
132             
133             Bool_t first_mirror = kTRUE;
134             
135             xmlNodePointer xmirror = 0;
136             xmlNodePointer xmirrortext = 0;
137             
138             xmlNodePointer xmirrornext = 0;
139             xmlNodePointer xmirrortextnext = 0;
140             
141             do {
142               if (first_mirror) {
143                 xmirrortextnext = xml->GetChild(xfile);
144                 first_mirror = kFALSE;
145               } else {
146                 xmirrortextnext = xml->GetNext(xmirror);
147               }
148               
149               if (xmirrortextnext) {
150                 xmirrornext = xml->GetNext(xmirrortextnext);
151                 xmirrortext = xmirrornext;
152                 xmirror     = xmirrornext;
153               } else {
154                 xmirror     = 0;
155                 xmirrortext = 0;
156               }
157               if (xmirror) {
158                 // here we have a file mirror
159                 xml->GetAttr(xmirror,"name");
160                 xml->GetAttr(xmirror,"domain");
161                 xml->GetAttr(xmirror,"latitude");
162                 xml->GetAttr(xmirror,"longitude");
163                 xml->GetAttr(xmirror,"location");
164                 xml->GetAttr(xmirror,"master");
165                 xml->GetAttr(xmirror,"site");
166                 xml->GetAttr(xmirror,"rootd");
167                 const char* master = 0;
168                 if ( (master = xml->GetAttr(xmirror,"master"))) {
169                   if (atoi(master) == 1) {
170                     files->Add(new TObjString(xml->GetAttr(xfile,"name")) , new TObjString(xml->GetAttr(xmirror,"rootd")));
171                   }
172                 }
173                 //              printf("Found File Mirror: %s\n",xml->GetAttr(xmirror,"name"));
174               }
175             } while (xmirror);
176           }
177         } while (xfile);
178         //      printf("Adding files\n");
179         fEventList->Add(files);
180       }
181     }
182   } while ( xtext );
183   delete xml;
184 }    
185
186 const char* TGliteXmlEventlist::GetURL(const char* filename) const {
187 //Andi - please put a comment
188   if (fCurrent) {
189     TObjString* obj = (TObjString*)fCurrent->GetValue(filename);
190     if (obj) {
191       if (strlen(obj->GetName())) 
192         return (obj->GetName());
193       else 
194         return 0;
195     } else {
196       return 0;
197     }
198   } else {
199     return 0;
200   }
201 }
202
203
204 void TGliteXmlEventlist::Print(Option_t */*opt*/) const {
205 //Andi - please put a comment
206   printf("Dumping %d elements\n",fEventList->GetSize());
207   TIter next(fEventList);
208   TMap* filemap;
209   Int_t count=0;
210   while ( (filemap = (TMap*)next()) ) {
211     count++;
212     printf("Printing Element %d\n",count);
213     filemap->Print();
214   }
215 }
216
217
218