TQueryResult.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TQueryResult.cxx 36262 2010-10-11 07:11:18Z brun $
00002 // Author: G Ganis Sep 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 // TQueryResult                                                         //
00015 //                                                                      //
00016 // A container class for query results.                                 //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 #include <string.h>
00020 
00021 #include "TBrowser.h"
00022 #include "TError.h"
00023 #include "TEventList.h"
00024 #include "TQueryResult.h"
00025 #include "TRegexp.h"
00026 #include "TROOT.h"
00027 #include "TMath.h"
00028 #include "TSelector.h"
00029 #include "TSystem.h"
00030 #include "TTimeStamp.h"
00031 
00032 
00033 ClassImp(TQueryResult)
00034 
00035 //______________________________________________________________________________
00036 TQueryResult::TQueryResult(Int_t seqnum, const char *opt, TList *inlist,
00037                            Long64_t entries, Long64_t first, const char *selec)
00038              : fSeqNum(seqnum), fStatus(kSubmitted), fUsedCPU(0.), fOptions(opt),
00039                fEntries(entries), fFirst(first),
00040                fBytes(0), fParList("-"), fOutputList(0),
00041                fFinalized(kFALSE), fArchived(kFALSE), fResultFile("-"),
00042                fInitTime(0.), fProcTime(0.), fNumWrks(-1)
00043 {
00044    // Main constructor.
00045 
00046    // Name and unique title
00047    SetName(Form("q%d", fSeqNum));
00048    SetTitle(Form("session-localhost-%ld-%d",
00049                  (Long_t)TTimeStamp().GetSec(), gSystem->GetPid()));
00050 
00051    // Start time
00052    fStart.Set();
00053    fEnd.Set(fStart.Convert()-1);
00054 
00055    // Save input list
00056    fInputList = 0;
00057    if (inlist) {
00058       fInputList = (TList *) (inlist->Clone());
00059       fInputList->SetOwner();
00060    }
00061 
00062    // Log file
00063    fLogFile = new TMacro("LogFile");
00064 
00065    // Selector files
00066    fDraw = selec ? TSelector::IsStandardDraw(selec) : kFALSE;
00067    if (fDraw) {
00068       // The input list should contain info about the variables and
00069       // selection cuts: save them into the macro title
00070       TString varsel;
00071       if (fInputList) {
00072          TIter nxo(fInputList);
00073          TObject *o = 0;
00074          while ((o = nxo())) {
00075             if (!strcmp(o->GetName(),"varexp")) {
00076                varsel = o->GetTitle();
00077                Int_t iht = varsel.Index(">>htemp");
00078                if (iht > -1)
00079                   varsel.Remove(iht);
00080                varsel = Form("\"%s\";", varsel.Data());
00081             }
00082             if (!strcmp(o->GetName(),"selection"))
00083                varsel += Form("\"%s\"", o->GetTitle());
00084          }
00085          if (gDebug > 0)
00086             Info("TQueryResult","selec: %s, varsel: %s", selec, varsel.Data());
00087          // Log notification also in the instance
00088          fLogFile->AddLine(Form("TQueryResult: selec: %s, varsel: %s",
00089                                 selec, varsel.Data()));
00090       }
00091       // Standard draw action: save only the name
00092       fSelecImp = new TMacro(selec, varsel);
00093       fSelecHdr = 0;
00094    } else {
00095       // Save selector file
00096       fSelecHdr = new TMacro;
00097       fSelecImp = new TMacro;
00098       SaveSelector(selec);
00099    }
00100 
00101    // List of libraries loaded at creation
00102    const char *pl = gSystem->GetLibraries();
00103    fLibList = (pl && (strlen(pl) > 0)) ? pl : "-";
00104 }
00105 
00106 //______________________________________________________________________________
00107 TQueryResult::~TQueryResult()
00108 {
00109    // Destructor.
00110 
00111    SafeDelete(fInputList);
00112    SafeDelete(fOutputList);
00113    SafeDelete(fLogFile);
00114    SafeDelete(fSelecImp);
00115    SafeDelete(fSelecHdr);
00116 }
00117 
00118 //______________________________________________________________________________
00119 TQueryResult *TQueryResult::CloneInfo()
00120 {
00121    // Return an instance of TQueryResult containing only the local
00122    // info fields, i.e. no outputlist, liblist, dset, selectors, etc..
00123    // Used for fast retrieve of information about existing queries
00124    // and their status.
00125 
00126    // Create instance
00127    TQueryResult *qr = new TQueryResult(fSeqNum, fOptions, 0, fEntries,
00128                                        fFirst, 0);
00129 
00130    // Correct fields
00131    qr->fStatus = fStatus;
00132    qr->fStart.Set(fStart.Convert());
00133    qr->fEnd.Set(fEnd.Convert());
00134    qr->fUsedCPU = fUsedCPU;
00135    qr->fEntries = fEntries;
00136    qr->fFirst = fFirst;
00137    qr->fBytes = fBytes;
00138    qr->fParList = fParList;
00139    qr->fResultFile = fResultFile;
00140    qr->fArchived = fArchived;
00141    qr->fInitTime = fInitTime;
00142    qr->fProcTime = fProcTime;
00143    qr->fNumWrks = fNumWrks;
00144 
00145    qr->fSelecHdr = 0;
00146    if (GetSelecHdr()) {
00147       qr->fSelecHdr = new TMacro();
00148       qr->fSelecHdr->SetName(GetSelecHdr()->GetName());
00149       qr->fSelecHdr->SetTitle(GetSelecHdr()->GetTitle());
00150    }
00151    qr->fSelecImp = 0;
00152    if (GetSelecImp()) {
00153       qr->fSelecImp = new TMacro();
00154       qr->fSelecImp->SetName(GetSelecImp()->GetName());
00155       qr->fSelecImp->SetTitle(GetSelecImp()->GetTitle());
00156    }
00157 
00158    // Name and title
00159    qr->SetName(GetName());
00160    qr->SetTitle(GetTitle());
00161 
00162    return qr;
00163 }
00164 
00165 //_____________________________________________________________________________
00166 void TQueryResult::SaveSelector(const char *selector)
00167 {
00168    // Save the selector header and implementation into the dedicated
00169    // TMacro instances. The header is searched for in the same directory
00170    // of the implementation file.
00171 
00172    if (!selector)
00173       return;
00174 
00175    // Separate out aclic chars
00176    TString selec = selector;
00177    TString aclicMode;
00178    TString arguments;
00179    TString io;
00180    selec = gSystem->SplitAclicMode(selec, aclicMode, arguments, io);
00181 
00182    // Store aclic options, if any
00183    if (aclicMode.Length() > 0)
00184       fOptions += Form("#%s", aclicMode.Data());
00185 
00186    // If the selector is in a precompiled shared lib (e.g. in a PAR)
00187    // we just save the name
00188    TString selname = gSystem->BaseName(selec);
00189    Int_t idx = selname.Index(".");
00190    if (idx < 0) {
00191       // Notify
00192       if (gDebug > 0)
00193          Info("SaveSelector", "precompiled selector: just save the name");
00194       fSelecImp->SetName(selname);
00195       fSelecImp->SetTitle(selname);
00196       fSelecHdr->SetName(selname);
00197       fSelecHdr->SetTitle(selname);
00198    } else {
00199       // We locate the file and save it in compressed form
00200       if (idx > -1)
00201          selname.Remove(idx);
00202 
00203       // Locate the implementation file
00204       char *selc = gSystem->Which(TROOT::GetMacroPath(), selec, kReadPermission);
00205       if (!selc) {
00206          Warning("SaveSelector",
00207                  "could not locate selector implementation file (%s)", selec.Data());
00208          return;
00209       }
00210 
00211       // Fill the TMacro instance
00212       fSelecImp->ReadFile(selc);
00213       fSelecImp->SetName(gSystem->BaseName(selc));
00214       fSelecImp->SetTitle(selname);
00215 
00216       // Locate the included header file
00217       char *p = (char *) strrchr(selc,'.');
00218       if (p) {
00219          strlcpy(p+1,"h",strlen(p));
00220       } else {
00221          Warning("SaveSelector",
00222                  "bad formatted name (%s): could not build header file name", selc);
00223       }
00224       if (!(gSystem->AccessPathName(selc, kReadPermission))) {
00225          fSelecHdr->ReadFile(selc);
00226          fSelecHdr->SetName(gSystem->BaseName(selc));
00227          fSelecHdr->SetTitle(selname);
00228       } else {
00229          Warning("SaveSelector",
00230                  "could not locate selector header file (%s)", selc);
00231       }
00232 
00233       delete[] selc;
00234    }
00235 }
00236 
00237 //______________________________________________________________________________
00238 void TQueryResult::RecordEnd(EQueryStatus status, TList *outlist)
00239 {
00240    // End of query settings.
00241 
00242    // End time
00243    fEnd.Set();
00244 
00245    // Status
00246    fStatus = (status < kAborted || status > kCompleted) ? kAborted : status;
00247 
00248    // Clone the results
00249    if (outlist && fOutputList != outlist) {
00250       if (fOutputList) {
00251          fOutputList->Delete();
00252          SafeDelete(fOutputList);
00253       }
00254       fOutputList = (TList *) (outlist->Clone());
00255       fOutputList->SetOwner();
00256    }
00257 }
00258 
00259 //______________________________________________________________________________
00260 void TQueryResult::SetProcessInfo(Long64_t ent, Float_t cpu, Long64_t bytes,
00261                                   Float_t init, Float_t proc)
00262 {
00263    // Set processing info.
00264 
00265    fEntries = (ent > 0) ? ent : fEntries;
00266    fUsedCPU = (cpu > 0.) ? cpu : fUsedCPU;
00267    fBytes = (bytes > 0.) ? bytes : fBytes;
00268    fInitTime = (init > 0.) ? init : fInitTime;
00269    fProcTime = (proc > 0.) ? proc : fProcTime;
00270 }
00271 
00272 //______________________________________________________________________________
00273 void TQueryResult::AddLogLine(const char *logline)
00274 {
00275    // Fill log file.
00276 
00277    if (logline)
00278       fLogFile->AddLine(logline);
00279 }
00280 
00281 //______________________________________________________________________________
00282 void TQueryResult::AddInput(TObject *obj)
00283 {
00284    // Add obj to the input list
00285 
00286    if (fInputList && obj)
00287       fInputList->Add(obj);
00288 }
00289 
00290 //______________________________________________________________________________
00291 void TQueryResult::SetArchived(const char *archfile)
00292 {
00293    // Set (or update) query in archived state.
00294 
00295    if (IsDone()) {
00296       fArchived = kTRUE;
00297       if (archfile && (strlen(archfile) > 0))
00298          fResultFile = archfile;
00299    }
00300 }
00301 
00302 //______________________________________________________________________________
00303 void TQueryResult::Print(Option_t *opt) const
00304 {
00305    // Print query content. Use opt = "F" for a full listing.
00306 
00307    // Attention: the list must match EQueryStatus
00308    const char *qst[] = {
00309       "aborted  ", "submitted", "running  ", "stopped  ", "completed"
00310    };
00311 
00312    // Status label
00313    Int_t st = (fStatus > 0 && fStatus <= kCompleted) ? fStatus : 0;
00314 
00315    // Range label
00316    Long64_t last = (fEntries > -1) ? fFirst+fEntries-1 : -1;
00317 
00318    // Option
00319    Bool_t full = ((strchr(opt,'F') || strchr(opt,'f'))) ? kTRUE : kFALSE;
00320 
00321    // Query number to be printed
00322    Int_t qry = fSeqNum;
00323    TString qn = opt;
00324    TRegexp re("N.*N");
00325    Int_t i1 = qn.Index(re);
00326    if (i1 != kNPOS) {
00327       qn.Remove(0, i1+1);
00328       qn.Remove(qn.Index("N"));
00329       qry = qn.Atoi();
00330    }
00331 
00332    // Print separator if full dump
00333    if (full) Printf("+++");
00334 
00335    TString range;
00336    if (!full)
00337       range = (last > -1) ? Form("evts:%lld-%lld", fFirst, last) : "";
00338 
00339    // Print header
00340    if (!fDraw) {
00341       const char *fin = fFinalized ? "finalized" : qst[st];
00342       const char *arc = fArchived ? "(A)" : "";
00343       Printf("+++ #:%d ref:\"%s:%s\" sel:%s %9s%s %s",
00344              qry, GetTitle(), GetName(), fSelecImp->GetTitle(), fin, arc,
00345              range.Data());
00346    } else {
00347       Printf("+++ #:%d ref:\"%s:%s\" varsel:%s %s",
00348              qry, GetTitle(), GetName(), fSelecImp->GetTitle(),
00349              range.Data());
00350    }
00351 
00352    // We are done, if not full dump
00353    if (!full) return;
00354 
00355    // Time information
00356    Float_t elapsed = (fProcTime > 0.) ? fProcTime
00357                                       : (Float_t)(fEnd.Convert() - fStart.Convert());
00358    Printf("+++        started:   %s", fStart.AsString());
00359    Printf("+++        init time: %.3f sec", fInitTime);
00360    Printf("+++        proc time: %.3f sec (CPU time: %.1f sec)", elapsed, fUsedCPU);
00361 
00362    // Number of events processed, rate, size
00363    Double_t rate = 0.0;
00364    if (fEntries > -1 && elapsed > 0)
00365       rate = fEntries / (Double_t)elapsed ;
00366    Float_t size = ((Float_t)fBytes) / TMath::Power(2.,20.);
00367    Printf("+++        processed: %lld events (size: %.3f MBs)", fEntries, size);
00368    Printf("+++        rate:      %.1f evts/sec", rate);
00369 
00370    Printf("+++        # workers: %d ", fNumWrks);
00371 
00372    // Package information
00373    if (fParList.Length() > 1)
00374       Printf("+++        packages:  %s", fParList.Data());
00375 
00376    // Result information
00377    TString res = fResultFile;
00378    if (!fArchived) {
00379       Int_t dq = res.Index("queries");
00380       if (dq > -1) {
00381          res.Remove(0,res.Index("queries"));
00382          res.Insert(0,"<PROOF_SandBox>/");
00383       }
00384       if (res.BeginsWith("-")) {
00385          res = (fStatus == kAborted) ? "not available" : "sent to client";
00386       }
00387    }
00388    if (res.Length() > 1)
00389       Printf("+++        results:   %s", res.Data());
00390 
00391    if (fOutputList && fOutputList->GetSize() > 0)
00392       Printf("+++        outlist:   %d objects", fOutputList->GetSize());
00393 }
00394 
00395 //______________________________________________________________________________
00396 void TQueryResult::Browse(TBrowser *b)
00397 {
00398    // To support browsing of the results.
00399 
00400    if (fOutputList)
00401       b->Add(fOutputList, fOutputList->Class(), "OutputList");
00402 }
00403 
00404 //______________________________________________________________________________
00405 void TQueryResult::SetInputList(TList *in, Bool_t adopt)
00406 {
00407    // Set / change the input list.
00408    // The flag 'adopt' determines whether the list is adopted (default)
00409    // or cloned. If adopted, object ownership is transferred to this object.
00410    // The internal fInputList will always be owner of its objects.
00411 
00412    if (!in || in != fInputList)
00413       SafeDelete(fInputList);
00414 
00415    if (in && in != fInputList) {
00416       if (!adopt) {
00417          fInputList = (TList *) (in->Clone());
00418       } else {
00419          fInputList = new TList;
00420          TIter nxi(in);
00421          TObject *o = 0;
00422          while ((o = nxi()))
00423             fInputList->Add(o);
00424          in->SetOwner(kFALSE);
00425       }
00426       fInputList->SetOwner();
00427    }
00428 }
00429 
00430 //______________________________________________________________________________
00431 void TQueryResult::SetOutputList(TList *out, Bool_t adopt)
00432 {
00433    // Set / change the output list.
00434    // The flag 'adopt' determines whether the list is adopted (default)
00435    // or cloned.  If adopted, object ownership is transferred to this object.
00436    // The internal fOutputList will always be owner of its objects.
00437 
00438    if (!out || out != fOutputList)
00439       SafeDelete(fOutputList);
00440 
00441    if (out && out != fOutputList) {
00442       if (!adopt) {
00443          fOutputList = (TList *) (out->Clone());
00444       } else {
00445          fOutputList = new TList;
00446          TIter nxo(out);
00447          TObject *o = 0;
00448          while ((o = nxo()))
00449             fOutputList->Add(o);
00450          out->SetOwner(kFALSE);
00451       }
00452       fOutputList->SetOwner();
00453    }
00454 }
00455 
00456 //______________________________________________________________________________
00457 Bool_t operator==(const TQueryResult &qr1, const TQueryResult &qr2)
00458 {
00459    // Compare two query result instances for equality.
00460    // Session name and query number are compared.
00461 
00462    if (!strcmp(qr1.GetTitle(), qr2.GetTitle()))
00463       if (qr1.GetSeqNum() == qr2.GetSeqNum())
00464          return kTRUE;
00465    return kFALSE;
00466 }
00467 
00468 //______________________________________________________________________________
00469 Bool_t TQueryResult::Matches(const char *ref)
00470 {
00471    // Return TRUE if reference ref matches.
00472 
00473    TString lref = Form("%s:%s", GetTitle(), GetName());
00474 
00475    if (lref == ref)
00476       return kTRUE;
00477 
00478    return kFALSE;
00479 }
00480 
00481 //______________________________________________________________________________
00482 TObject *TQueryResult::GetInputObject(const char *classname) const
00483 {
00484    // Return first instance of class 'classname' in the input list.
00485    // Usefull to access TDSet, TEventList, ...
00486 
00487    TObject *o = 0;
00488    if (classname && fInputList) {
00489       TIter nxi(fInputList);
00490       while ((o = nxi()))
00491          if (!strncmp(o->ClassName(), classname, strlen(classname)))
00492             return o;
00493    }
00494 
00495    // Not found
00496    return o;
00497 }

Generated on Tue Jul 5 15:34:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1