#include <string.h>
#include "TBrowser.h"
#include "TError.h"
#include "TEventList.h"
#include "TQueryResult.h"
#include "TRegexp.h"
#include "TROOT.h"
#include "TMath.h"
#include "TSelector.h"
#include "TSystem.h"
#include "TTimeStamp.h"
ClassImp(TQueryResult)
TQueryResult::TQueryResult(Int_t seqnum, const char *opt, TList *inlist,
Long64_t entries, Long64_t first, const char *selec)
: fSeqNum(seqnum), fStatus(kSubmitted), fUsedCPU(0.), fOptions(opt),
fEntries(entries), fFirst(first),
fBytes(0), fParList("-"), fOutputList(0),
fFinalized(kFALSE), fArchived(kFALSE), fResultFile("-"),
fInitTime(0.), fProcTime(0.)
{
SetName(Form("q%d", fSeqNum));
SetTitle(Form("session-localhost-%d-%d",
TTimeStamp().GetSec(),gSystem->GetPid()));
fStart.Set();
fEnd.Set(fStart.Convert()-1);
fInputList = 0;
if (inlist) {
fInputList = (TList *) (inlist->Clone());
fInputList->SetOwner();
}
fLogFile = new TMacro("LogFile");
fDraw = selec ? TSelector::IsStandardDraw(selec) : kFALSE;
if (fDraw) {
TString varsel;
if (fInputList) {
TIter nxo(fInputList);
TObject *o = 0;
while ((o = nxo())) {
if (!strcmp(o->GetName(),"varexp")) {
varsel = o->GetTitle();
Int_t iht = varsel.Index(">>htemp");
if (iht > -1)
varsel.Remove(iht);
varsel = Form("\"%s\";", varsel.Data());
}
if (!strcmp(o->GetName(),"selection"))
varsel += Form("\"%s\"", o->GetTitle());
}
if (gDebug > 0)
Info("TQueryResult","selec: %s, varsel: %s", selec, varsel.Data());
fLogFile->AddLine(Form("TQueryResult: selec: %s, varsel: %s",
selec, varsel.Data()));
}
fSelecImp = new TMacro(selec, varsel);
fSelecHdr = 0;
} else {
fSelecHdr = new TMacro;
fSelecImp = new TMacro;
SaveSelector(selec);
}
const char *pl = gSystem->GetLibraries();
fLibList = (pl && (strlen(pl) > 0)) ? pl : "-";
}
TQueryResult::~TQueryResult()
{
SafeDelete(fInputList);
SafeDelete(fOutputList);
SafeDelete(fLogFile);
SafeDelete(fSelecImp);
SafeDelete(fSelecHdr);
}
TQueryResult *TQueryResult::CloneInfo()
{
TQueryResult *qr = new TQueryResult(fSeqNum, fOptions, 0, fEntries,
fFirst, 0);
qr->fStatus = fStatus;
qr->fStart.Set(fStart.Convert());
qr->fEnd.Set(fEnd.Convert());
qr->fUsedCPU = fUsedCPU;
qr->fEntries = fEntries;
qr->fFirst = fFirst;
qr->fBytes = fBytes;
qr->fParList = fParList;
qr->fResultFile = fResultFile;
qr->fArchived = fArchived;
qr->fInitTime = fInitTime;
qr->fProcTime = fProcTime;
qr->fSelecHdr = 0;
if (GetSelecHdr()) {
qr->fSelecHdr = new TMacro();
qr->fSelecHdr->SetName(GetSelecHdr()->GetName());
qr->fSelecHdr->SetTitle(GetSelecHdr()->GetTitle());
}
qr->fSelecImp = 0;
if (GetSelecImp()) {
qr->fSelecImp = new TMacro();
qr->fSelecImp->SetName(GetSelecImp()->GetName());
qr->fSelecImp->SetTitle(GetSelecImp()->GetTitle());
}
qr->SetName(GetName());
qr->SetTitle(GetTitle());
return qr;
}
void TQueryResult::SaveSelector(const char *selector)
{
if (!selector)
return;
TString selec = selector;
TString aclicMode;
TString arguments;
TString io;
selec = gSystem->SplitAclicMode(selec, aclicMode, arguments, io);
if (aclicMode.Length() > 0)
fOptions += Form("#%s", aclicMode.Data());
TString selname = gSystem->BaseName(selec);
Int_t idx = selname.Index(".");
if (idx < 0) {
if (gDebug > 0)
Info("SaveSelector", "precompiled selector: just save the name");
fSelecImp->SetName(selname);
fSelecImp->SetTitle(selname);
fSelecHdr->SetName(selname);
fSelecHdr->SetTitle(selname);
} else {
if (idx > -1)
selname.Remove(idx);
char *selc = gSystem->Which(TROOT::GetMacroPath(), selec, kReadPermission);
if (!selc) {
Warning("SaveSelector",
"could not locate selector implementation file (%s)", selec.Data());
return;
}
fSelecImp->ReadFile(selc);
fSelecImp->SetName(gSystem->BaseName(selc));
fSelecImp->SetTitle(selname);
char *p = (char *) strrchr(selc,'.');
if (p) {
strcpy(p+1,"h");
} else {
Warning("SaveSelector",
"bad formatted name (%s): could not build header file name", selc);
}
if (!(gSystem->AccessPathName(selc, kReadPermission))) {
fSelecHdr->ReadFile(selc);
fSelecHdr->SetName(gSystem->BaseName(selc));
fSelecHdr->SetTitle(selname);
} else {
Warning("SaveSelector",
"could not locate selector header file (%s)", selc);
}
delete[] selc;
}
}
void TQueryResult::RecordEnd(EQueryStatus status, TList *outlist)
{
fEnd.Set();
fStatus = (status < kAborted || status > kCompleted) ? kAborted : status;
if (outlist && fOutputList != outlist) {
if (fOutputList) {
fOutputList->Delete();
SafeDelete(fOutputList);
}
fOutputList = (TList *) (outlist->Clone());
fOutputList->SetOwner();
}
}
void TQueryResult::SetProcessInfo(Long64_t ent, Float_t cpu, Long64_t bytes,
Float_t init, Float_t proc)
{
fEntries = (ent > 0) ? ent : fEntries;
fUsedCPU = (cpu > 0.) ? cpu : fUsedCPU;
fBytes = (bytes > 0.) ? bytes : fBytes;
fInitTime = (init > 0.) ? init : fInitTime;
fProcTime = (proc > 0.) ? proc : fProcTime;
}
void TQueryResult::AddLogLine(const char *logline)
{
if (logline)
fLogFile->AddLine(logline);
}
void TQueryResult::AddInput(TObject *obj)
{
if (fInputList && obj)
fInputList->Add(obj);
}
void TQueryResult::SetArchived(const char *archfile)
{
if (IsDone()) {
fArchived = kTRUE;
if (archfile && (strlen(archfile) > 0))
fResultFile = archfile;
}
}
void TQueryResult::Print(Option_t *opt) const
{
const char *qst[] = {
"aborted ", "submitted", "running ", "stopped ", "completed"
};
Int_t st = (fStatus > 0 && fStatus <= kCompleted) ? fStatus : 0;
Long64_t last = (fEntries > -1) ? fFirst+fEntries-1 : -1;
Bool_t full = ((strchr(opt,'F') || strchr(opt,'f'))) ? kTRUE : kFALSE;
Int_t qry = fSeqNum;
TString qn = opt;
TRegexp re("N.*N");
Int_t i1 = qn.Index(re);
if (i1 != kNPOS) {
qn.Remove(0, i1+1);
qn.Remove(qn.Index("N"));
qry = qn.Atoi();
}
if (full) Printf("+++");
TString range;
if (!full)
range = (last > -1) ? Form("evts:%lld-%lld", fFirst, last) : "";
if (!fDraw) {
const char *fin = fFinalized ? "finalized" : qst[st];
const char *arc = fArchived ? "(A)" : "";
Printf("+++ #:%d ref:\"%s:%s\" sel:%s %9s%s %s",
qry, GetTitle(), GetName(), fSelecImp->GetTitle(), fin, arc,
range.Data());
} else {
Printf("+++ #:%d ref:\"%s:%s\" varsel:%s %s",
qry, GetTitle(), GetName(), fSelecImp->GetTitle(),
range.Data());
}
if (!full) return;
Float_t elapsed = (fProcTime > 0.) ? fProcTime
: (Float_t)(fEnd.Convert() - fStart.Convert());
Printf("+++ started: %s", fStart.AsString());
Printf("+++ init time: %.3f sec", fInitTime);
Printf("+++ proc time: %.3f sec (CPU time: %.1f sec)", elapsed, fUsedCPU);
Double_t rate = 0.0;
if (fEntries > -1 && elapsed > 0)
rate = fEntries / (Double_t)elapsed ;
Float_t size = ((Float_t)fBytes) / TMath::Power(2.,20.);
Printf("+++ processed: %lld events (size: %.3f MBs)", fEntries, size);
Printf("+++ rate: %.1f evts/sec", rate);
if (fParList.Length() > 1)
Printf("+++ packages: %s", fParList.Data());
TString res = fResultFile;
if (!fArchived) {
Int_t dq = res.Index("queries");
if (dq > -1) {
res.Remove(0,res.Index("queries"));
res.Insert(0,"<PROOF_SandBox>/");
}
if (res.BeginsWith("-")) {
res = (fStatus == kAborted) ? "not available" : "sent to client";
}
}
if (res.Length() > 1)
Printf("+++ results: %s", res.Data());
if (fOutputList && fOutputList->GetSize() > 0)
Printf("+++ outlist: %d objects", fOutputList->GetSize());
}
void TQueryResult::Browse(TBrowser *b)
{
if (fOutputList)
b->Add(fOutputList, fOutputList->Class(), "OutputList");
}
void TQueryResult::SetInputList(TList *in, Bool_t adopt)
{
if (!in || in != fInputList)
SafeDelete(fInputList);
if (in && in != fInputList) {
if (!adopt) {
fInputList = (TList *) (in->Clone());
} else {
fInputList = new TList;
TIter nxi(in);
TObject *o = 0;
while ((o = nxi()))
fInputList->Add(o);
in->SetOwner(kFALSE);
}
fInputList->SetOwner();
}
}
void TQueryResult::SetOutputList(TList *out, Bool_t adopt)
{
if (!out || out != fOutputList)
SafeDelete(fOutputList);
if (out && out != fOutputList) {
if (!adopt) {
fOutputList = (TList *) (out->Clone());
} else {
fOutputList = new TList;
TIter nxo(out);
TObject *o = 0;
while ((o = nxo()))
fOutputList->Add(o);
out->SetOwner(kFALSE);
}
fOutputList->SetOwner();
}
}
Bool_t operator==(const TQueryResult &qr1, const TQueryResult &qr2)
{
if (!strcmp(qr1.GetTitle(), qr2.GetTitle()))
if (qr1.GetSeqNum() == qr2.GetSeqNum())
return kTRUE;
return kFALSE;
}
Bool_t TQueryResult::Matches(const char *ref)
{
TString lref = Form("%s:%s", GetTitle(), GetName());
if (lref == ref)
return kTRUE;
return kFALSE;
}
TObject *TQueryResult::GetInputObject(const char *classname) const
{
TObject *o = 0;
if (classname && fInputList) {
TIter nxi(fInputList);
while ((o = nxi()))
if (!strncmp(o->ClassName(), classname, strlen(classname)))
return o;
}
return o;
}