Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
PackageSql.cxx
1/*
2 * HalPackageSql.cxx
3 *
4 * Created on: 28-08-2014
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "PackageSql.h"
11#include "AnaFile.h"
12#include "Cout.h"
13#include "HtmlCore.h"
14#include "Package.h"
15#include "Package2HTML.h"
16#include "Parameter.h"
17
18#include <TCollection.h>
19#include <TDirectory.h>
20#include <TFile.h>
21#include <TKey.h>
22#include <TList.h>
23#include <TNamed.h>
24#include <TObjString.h>
25#include <TRegexp.h>
26#include <TSQLServer.h>
27#include <TSQLStatement.h>
28#include <TSystem.h>
29#include <fstream>
30#include <vector>
31
32#include "Std.h"
33#include "StdString.h"
34
35namespace Hal {
36 void PackageSql::ExportTask(AnaFile* extr, Int_t file_id, Int_t task_id) {
37 // export main objects ??
38 TSQLStatement* stat = fSQL->Statement("INSERT INTO Files (ID, FILE_ID,TASK_ID, Comment, SoftVer, "
39 "Date,DataType,ProcessedEvents,InputFIle,AnaName,Tags,PathFile) VALUES "
40 "(?,?,?,?,?,?,?,?,?,?,?,?)");
41 Package* metadata = (Package*) extr->GetMainObject("HalMetadata");
42 TString comment = metadata->GetComment();
43 ParameterString* anatype = (ParameterString*) metadata->GetObjectByName("Analysis Name");
44 ParameterString* data = (ParameterString*) fRunInfo->GetObjectByName("Date");
45 ParameterString* sofwer = (ParameterString*) fRunInfo->GetObjectByName("Software ver");
46 ParameterString* datatype = (ParameterString*) metadata->GetObjectByName("DataType");
47 ParameterUInt* processed = (ParameterUInt*) fRunInfo->GetObjectByName("Processed_events");
48 ParameterString* inputfile = (ParameterString*) fRunInfo->GetObjectByName("Input file");
49 TString path = GetPath(fFilename);
50 TList* list = (TList*) metadata->GetObjectByName("Tags");
51 TString tags = "";
52 if (list) {
53 for (int j = 0; j < list->GetEntries(); j++) {
54 tags += ((TObjString*) list->At(j))->GetString();
55 tags += " ";
56 }
57 }
58 stat->SetInt(0, fID);
59 stat->SetInt(1, file_id);
60 stat->SetInt(2, task_id);
61 stat->SetString(3, comment, comment.Length());
62 stat->SetString(4, sofwer->GetValue(), sofwer->GetValue().Length());
63 TString data_s = data->GetValue();
64 std::vector<TString> data_arr = Hal::Std::ExplodeString(data_s, '-');
65 Int_t year, month, day;
66 year = data_arr[0].Atoi();
67 month = data_arr[1].Atoi();
68 day = data_arr[2].Atoi();
69
70 stat->SetDate(5, year, month, day);
71 stat->SetString(6, datatype->GetValue(), datatype->GetValue().Length());
72 stat->SetInt(7, processed->GetValue());
73 stat->SetString(8, inputfile->GetValue(), inputfile->GetValue().Length());
74 stat->SetString(9, anatype->GetValue(), anatype->GetValue().Length());
75 stat->SetString(10, tags, tags.Length());
76 stat->SetString(11, path, path.Length());
77 stat->Process();
78 delete stat;
79 }
80
81 PackageSql::PackageSql(TString filename) {
82 fFilename = filename;
83 TString www_path = Hal::Std::GetConfigParameter("www_path");
84 if (www_path.Length() <= 0) {
85 Cout::PrintInfo("www_path not found in hal configruration file!", Hal::EInfo::kError);
86 return;
87 }
88 TString datafile = Form("sqlite://%s/database.sqlite", www_path.Data());
89 TString user = Hal::Std::GetConfigParameter("www_path/database_user");
90 TString pass = Hal::Std::GetConfigParameter("www_path/database_password");
91 if (user.Length() == 0) { user = "user"; }
92 if (pass.Length() == 0) { pass = "password"; }
93 // check if ifle exist
94 TString outPath = Form("%s/database.sqlite", www_path.Data());
95 std::ifstream f(outPath);
97 if (f.good()) {
98 f.close();
99 fSQL = TSQLServer::Connect(datafile, user, pass);
100 } else {
101 TString workdir = Hal::Std::GetHalrootPlus();
102 f.close();
103 gSystem->MakeDirectory(www_path.Data());
104 gSystem->MakeDirectory(Form("%s/html_rep/", www_path.Data()));
105 gSystem->CopyFile(Form("%s/html/database.sqlite", workdir.Data()), Form("%s/database.sqlite", www_path.Data()));
106 gSystem->CopyFile(Form("%s/html/logo_search.gif", workdir.Data()), Form("%s/logo_search.gif", www_path.Data()));
107 gSystem->CopyFile(Form("%s/html/index.php", workdir.Data()), Form("%s/index.php", www_path.Data()));
108 gSystem->CopyFile(Form("%s/html/functions.php", workdir.Data()), Form("%s/functions.php", www_path.Data()));
109 gSystem->CopyFile(Form("%s/html/table.css", workdir.Data()), Form("%s/table.css", www_path.Data()));
110 gSystem->CopyFile(Form("%s/html/search_template.css", workdir.Data()), Form("%s/search_template.css", www_path.Data()));
111 HtmlCore::CopyJs(www_path);
112 HtmlCore::CopyCss(www_path);
113 fSQL = TSQLServer::Connect(datafile, user, pass);
114 }
115 HtmlCore::SetMainDir(www_path.Data());
116 AnaFile* extr = new AnaFile(filename);
117 TSQLStatement* stat = (TSQLStatement*) fSQL->Statement("SELECT COUNT(ID) FROM Files");
118 stat->Process();
119 fID = stat->GetInt(0);
120 delete stat;
121 stat = (TSQLStatement*) fSQL->Statement("SELECT MAX(FILE_ID) FROM Files");
122 stat->Process();
123 Int_t file_id = stat->GetInt(0) + 1;
124
125 delete stat;
126 TFile* file = new TFile(filename);
127 TDirectory* dir = (TDirectory*) file->Get("HalPhysics");
128 TList* keys = dir->GetListOfKeys();
129 Package2HTML* html = new Package2HTML(filename, Form("%s/html_rep/html_%i", www_path.Data(), file_id));
130 delete html;
131 fRunInfo = extr->GetMetaData();
132 for (int i = 0; i < keys->GetEntries(); i++) {
133 // extrack all subtasks
134 TKey* key = (TKey*) keys->At(i);
135 TString key_name = key->GetName();
136 Package* pack = extr->GetPackage(key_name);
137 if (pack == NULL) continue;
138 if (extr->SwitchPackage(key_name)) {
139 fRunInfo = extr->GetMetaData();
140 ExportTask(extr, file_id, i);
141 fID++;
142 }
143 }
144 HtmlCore::ResetMainDir();
145 delete extr;
146 }
147
148 PackageSql::~PackageSql() {}
149
150 TString PackageSql::RemoveDir(TString path) {
151 TRegexp reg(".*/");
152 TString tmp = path(reg);
153 TString res(tmp(0, tmp.Length() - 1));
154 return res;
155 }
156
157 TString PackageSql::RemoveDoubleDot(TString path) {
158 TString res(path(3, path.Length() - 3));
159 return res;
160 }
161
162 Bool_t PackageSql::HasDir(TString path) {
163 if (path.BeginsWith("../")) {
164 return kTRUE;
165 } else {
166 return kFALSE;
167 }
168 }
169
170 TString PackageSql::GetPath(TString filename) {
171 TString pwd = gSystem->Getenv("PWD");
172 while (true) {
173 if (HasDir(filename)) {
174 filename = RemoveDoubleDot(filename);
175 pwd = RemoveDir(pwd);
176 } else {
177 break;
178 }
179 }
180 TString path = pwd;
181 path = path + "/";
182 path = path + filename;
183 return path;
184 }
185} // namespace Hal
Package * GetPackage(TString name) const
Definition AnaFile.cxx:202
Package * GetMetaData() const
Definition AnaFile.cxx:253
Bool_t SwitchPackage(TString name)
Definition AnaFile.cxx:212
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void CopyCss(TString dir, TString css_name="table.css")
Definition HtmlCore.cxx:33
static void SetMainDir(TString name)
Definition HtmlCore.cxx:89
static void CopyJs(TString dir)
Definition HtmlCore.cxx:41
static HtmlCore * Instance()
Definition HtmlCore.cxx:81
PackageSql(TString filename)
TObject * GetObjectByName(TString name, Int_t index=0, Bool_t quited=kFALSE) const
Definition Package.cxx:67