7 |
// |
// |
8 |
#include <sstream> |
#include <sstream> |
9 |
#include <iostream> |
#include <iostream> |
10 |
|
#include <limits.h> |
11 |
// |
// |
12 |
#include <TFile.h> |
#include <TFile.h> |
13 |
#include <TTree.h> |
#include <TTree.h> |
18 |
#include <GLTables.h> |
#include <GLTables.h> |
19 |
#include <sgp4.h> |
#include <sgp4.h> |
20 |
// |
// |
21 |
|
ClassImp(Q2TH); |
22 |
ClassImp(GL_TABLES); |
ClassImp(GL_TABLES); |
23 |
ClassImp(GL_TRK_CALIB); |
ClassImp(GL_TRK_CALIB); |
24 |
ClassImp(GL_RUN); |
ClassImp(GL_RUN); |
32 |
// |
// |
33 |
using namespace std; |
using namespace std; |
34 |
|
|
35 |
|
Q2TH::Q2TH(TString host, TString user, TString psw){ |
36 |
|
this->Open(host,user,psw); |
37 |
|
}; |
38 |
|
|
39 |
|
void Q2TH::Open(TString host, TString user, TString psw){ |
40 |
|
fh = gSystem->ExpandPathName(host.Data()); |
41 |
|
fu = gSystem->ExpandPathName(user.Data()); |
42 |
|
fp = gSystem->ExpandPathName(psw.Data()); |
43 |
|
printf(" Connecting to DB %s \n",fh.Data()); |
44 |
|
dbc = TSQLServer::Connect(fh.Data(),fu.Data(),fp.Data()); |
45 |
|
if ( dbc && dbc->IsConnected() ){ |
46 |
|
printf(" connected! \n"); |
47 |
|
} else { |
48 |
|
printf(" ERROR! not connected... :( \n"); |
49 |
|
}; |
50 |
|
}; |
51 |
|
|
52 |
|
TObject *Q2TH::Draw(TString query, Bool_t verbose, TString hname){ |
53 |
|
// |
54 |
|
if ( !strcmp(query.Data(),"help") ){ |
55 |
|
printf(" USAGE: \n"); |
56 |
|
printf(" 1) start root and create Q2TH object with \n"); |
57 |
|
printf(" Q2TH *qt = new Q2TH() \n"); |
58 |
|
printf(" or \n"); |
59 |
|
printf(" Q2TH *qt = new Q2TH(\"mysql://srvg-g2-01.ts.infn.it/pamelaProcessing9_TS\",\"pamelaprod_ro\",\"mypassword\") \n"); |
60 |
|
printf(" 2) query the DB with \n"); |
61 |
|
printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\"); \n"); |
62 |
|
printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",true); this will print numbers on screen \n"); |
63 |
|
printf(" qt->Draw(\"select REAL_TIME_INIT from ROOT_TABLE_MERGING;\",true,\"myhisto\"); this will print numbers on screen and create histo \"myhisto\"\n"); |
64 |
|
printf(" 3) to use your own THxD create it and then query the DB giving as argument the name of histo: \n"); |
65 |
|
printf(" TH2D *myhisto=new TH2D(\"myhisto\",\"myhisto\",5000,1140000000.,1240000000.,10000,0.,1.) \n"); |
66 |
|
printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",false,\"myhisto\")\n\n\n"); |
67 |
|
|
68 |
|
return NULL; |
69 |
|
}; |
70 |
|
// |
71 |
|
pResult = dbc->Query(query.Data()); |
72 |
|
// |
73 |
|
Row = pResult->Next(); |
74 |
|
// |
75 |
|
Int_t dim = pResult->GetFieldCount(); |
76 |
|
if ( dim < 1 || dim > 2 ){ |
77 |
|
printf(" Dim == %i not supported yet \n",dim); |
78 |
|
return NULL; |
79 |
|
}; |
80 |
|
// |
81 |
|
TH1D *h1 = NULL; |
82 |
|
TH2D *h2 = NULL; |
83 |
|
Double_t f1 = 0.; |
84 |
|
Double_t minf1 = numeric_limits<Double_t>::max(); |
85 |
|
Double_t maxf1 = numeric_limits<Double_t>::min(); |
86 |
|
Double_t f2 = 0.; |
87 |
|
Double_t minf2 = numeric_limits<Double_t>::max(); |
88 |
|
Double_t maxf2 = numeric_limits<Double_t>::min(); |
89 |
|
// |
90 |
|
while ( Row ){ |
91 |
|
f1 = (Double_t)atof(Row->GetField(0)); |
92 |
|
if ( f1 > maxf1 ) maxf1 = f1; |
93 |
|
if ( f1 < minf1 ) minf1 = f1; |
94 |
|
if ( dim == 2 ){ |
95 |
|
f2 = (Double_t)atof(Row->GetField(1)); |
96 |
|
if ( f2 > maxf2 ) maxf2 = f2; |
97 |
|
if ( f2 < minf2 ) minf2 = f2; |
98 |
|
|
99 |
|
}; |
100 |
|
Row = pResult->Next(); |
101 |
|
}; |
102 |
|
pResult->Delete(); |
103 |
|
// |
104 |
|
|
105 |
|
// |
106 |
|
Int_t f1bin = 70; |
107 |
|
Int_t f2bin = 70; |
108 |
|
if ( dim == 1 ){ |
109 |
|
f1bin = int((maxf1-minf1)/1000.); |
110 |
|
if ( f1bin < 70 ) f1bin = 70; |
111 |
|
if ( f1bin > 1000 ) f1bin = 1000; |
112 |
|
if ( !strcmp(hname.Data(),"q2th") ) hname += "1"; |
113 |
|
// h1 = dynamic_cast<TH1D*>(gDirectory->FindObject(hname.Data())); |
114 |
|
h1 = (TH1D*)(gDirectory->FindObject(hname.Data())); |
115 |
|
if ( !strcmp(hname.Data(),"q2th1") ){ |
116 |
|
if ( h1 ) h1->Delete(); |
117 |
|
}; |
118 |
|
if ( !h1 ) h1 = new TH1D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02); |
119 |
|
// h1->SetBit(TH1::kCanRebin); |
120 |
|
if ( verbose ) printf("\n\n Row %s \n",pResult->GetFieldName(0)); |
121 |
|
}; |
122 |
|
if ( dim == 2 ){ |
123 |
|
f2bin = int((maxf2-minf2)/1000.); |
124 |
|
if ( f2bin < 70 ) f2bin = 70; |
125 |
|
if ( f2bin > 1000 ) f2bin = 1000; |
126 |
|
if ( !strcmp(hname.Data(),"q2th") ) hname += "2"; |
127 |
|
// h2 = dynamic_cast<TH2D*>(gDirectory->FindObject(hname.Data())); |
128 |
|
h2 = (TH2D*)(gDirectory->FindObject(hname.Data())); |
129 |
|
if ( !strcmp(hname.Data(),"q2th2") ){ |
130 |
|
if ( h2 ) h2->Delete(); |
131 |
|
}; |
132 |
|
if ( !h2 ) h2 = new TH2D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02,f2bin,minf2*0.98,maxf2*1.02); |
133 |
|
// h2->SetBit(TH2::kCanRebin); |
134 |
|
if ( verbose ) printf("\n\n Row %s %s \n",pResult->GetFieldName(0),pResult->GetFieldName(1)); |
135 |
|
}; |
136 |
|
// |
137 |
|
pResult = dbc->Query(query.Data()); |
138 |
|
// |
139 |
|
Row = pResult->Next(); |
140 |
|
// |
141 |
|
Int_t r = 0; |
142 |
|
// |
143 |
|
while ( Row ){ |
144 |
|
f1 = (Double_t)atof(Row->GetField(0)); |
145 |
|
if ( dim == 1 ){ |
146 |
|
if ( verbose ) printf(" %i %f \n",r,f1); |
147 |
|
h1->Fill(f1); |
148 |
|
} else { |
149 |
|
f2 = (Double_t)atof(Row->GetField(1)); |
150 |
|
if ( verbose ) printf(" %i %f %f \n",r,f1,f2); |
151 |
|
h2->Fill(f1,f2); |
152 |
|
}; |
153 |
|
r++; |
154 |
|
Row = pResult->Next(); |
155 |
|
}; |
156 |
|
// |
157 |
|
TCanvas *c = NULL; |
158 |
|
TString cname = Form("%sc",hname.Data()); |
159 |
|
// c = dynamic_cast<TCanvas*>(gDirectory->FindObject(hname.Data())); |
160 |
|
c = (TCanvas*)(gDirectory->FindObject(cname.Data())); |
161 |
|
if ( !c ) c = new TCanvas(Form("%sc",cname.Data())); |
162 |
|
c->Clear(); |
163 |
|
c->cd(); |
164 |
|
if ( dim == 1 ) h1->Draw(); |
165 |
|
if ( dim == 2 ) h2->Draw(); |
166 |
|
// |
167 |
|
pResult->Delete(); |
168 |
|
if ( dim == 1 ) return h1; |
169 |
|
if ( dim == 2 ) return h2; |
170 |
|
// |
171 |
|
return NULL; |
172 |
|
}; |
173 |
|
|
174 |
GL_TABLES::GL_TABLES(){ |
GL_TABLES::GL_TABLES(){ |
175 |
}; |
}; |
176 |
|
|
197 |
mp = psw.Data(); |
mp = psw.Data(); |
198 |
}; |
}; |
199 |
|
|
200 |
Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ |
//Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ |
201 |
|
Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){ |
202 |
// |
// |
203 |
// |
// |
204 |
// |
// |
234 |
TString host = fHost->Data(); |
TString host = fHost->Data(); |
235 |
TString user = fUser->Data(); |
TString user = fUser->Data(); |
236 |
TString psw = fPsw->Data(); |
TString psw = fPsw->Data(); |
237 |
dbc->Close(); |
if ( dbc ){ |
238 |
delete dbc; |
dbc->Close(); |
239 |
|
delete dbc; |
240 |
|
}; |
241 |
dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
242 |
// |
// |
243 |
myquery.str(""); |
myquery.str(""); |
1733 |
T->GetEntry(0); |
T->GetEntry(0); |
1734 |
ph = eh->GetPscuHeader(); |
ph = eh->GetPscuHeader(); |
1735 |
pktfirst = ph->GetCounter(); |
pktfirst = ph->GetCounter(); |
1736 |
obtfirst = ph->GetOrbitalTime(); |
// obtfirst = ph->GetOrbitalTime(); |
1737 |
|
// |
1738 |
|
}; |
1739 |
|
// |
1740 |
|
// look for Resurs offset |
1741 |
|
// |
1742 |
|
T0 = 0; |
1743 |
|
// |
1744 |
|
stringstream oss; |
1745 |
|
// |
1746 |
|
TString name=rname.str().c_str(); |
1747 |
|
UInt_t dworbit = 0; |
1748 |
|
// Int_t nlength = name.Length(); |
1749 |
|
delete pResult; |
1750 |
|
// |
1751 |
|
// New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset |
1752 |
|
// |
1753 |
|
oss.str(""); |
1754 |
|
oss << "SELECT OBT0,TIMESYNC,TYPE,ID_RESURS_OFFSET from GL_TIMESYNC where ID=" << idtsy <<";"; |
1755 |
|
if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; |
1756 |
|
this->GetGLTABLES()->AddQ(); |
1757 |
|
pResult = dbc->Query(oss.str().c_str()); |
1758 |
|
Bool_t fndit = false; |
1759 |
|
if ( pResult ){ |
1760 |
|
Row = pResult->Next(); |
1761 |
|
if ( Row ){ |
1762 |
|
// |
1763 |
|
OBT0 = (UInt_t)atoll(Row->GetField(0)); |
1764 |
|
obtfirst = OBT0; |
1765 |
|
TIMESYNC = (UInt_t)atoll(Row->GetField(1)); |
1766 |
|
TYPE = (UInt_t)atoll(Row->GetField(2)); |
1767 |
|
// |
1768 |
|
oss.str(""); |
1769 |
|
oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE ID=" |
1770 |
|
<< Row->GetField(3) << ";"; |
1771 |
|
if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; |
1772 |
|
this->GetGLTABLES()->AddQ(); |
1773 |
|
delete pResult; |
1774 |
|
pResult = dbc->Query(oss.str().c_str()); |
1775 |
|
if ( pResult ){ |
1776 |
|
Row = pResult->Next(); |
1777 |
|
if ( Row ){ |
1778 |
|
// printf(" GREAT! the DB structure is the new one! \n"); |
1779 |
|
fndit = true; |
1780 |
|
dworbit = 1; |
1781 |
|
}; |
1782 |
|
}; |
1783 |
|
}; |
1784 |
|
}; |
1785 |
|
if ( !fndit ){ |
1786 |
|
// |
1787 |
|
printf(" ERROR OLD DB! \n"); |
1788 |
|
printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); |
1789 |
|
// |
1790 |
|
}; |
1791 |
|
// |
1792 |
|
TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0); |
1793 |
|
T0 = (UInt_t)tu.GetSec(); |
1794 |
|
// |
1795 |
|
toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0; |
1796 |
|
// |
1797 |
|
// printf(" T0 %u toffset is %u \n",T0,toffset); |
1798 |
|
// |
1799 |
|
if ( file ) file->Close(); |
1800 |
|
delete pResult; |
1801 |
|
}; |
1802 |
|
|
1803 |
|
GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc, Bool_t usel0file){ |
1804 |
|
// MySQL variables |
1805 |
|
TFile *file = 0; |
1806 |
|
UInt_t idtsy = 0; |
1807 |
|
// |
1808 |
|
TSQLResult *pResult; |
1809 |
|
TSQLRow *Row = 0; |
1810 |
|
stringstream myquery; |
1811 |
|
stringstream rname; |
1812 |
|
// pcksList packetsNames; |
1813 |
|
// pcksList::iterator Iter; |
1814 |
|
// getPacketsNames(packetsNames); |
1815 |
|
rname.str(""); |
1816 |
|
// ---------------- |
1817 |
|
myquery.str(""); |
1818 |
|
myquery << "select "; |
1819 |
|
myquery << "PATH"; |
1820 |
|
myquery << ",NAME,ID_TIMESYNC"; |
1821 |
|
myquery << " from GL_ROOT where "; |
1822 |
|
myquery << type.Data(); |
1823 |
|
myquery << "=" << id << ";"; |
1824 |
|
// |
1825 |
|
if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; |
1826 |
|
this->GetGLTABLES()->AddQ(); |
1827 |
|
pResult = dbc->Query(myquery.str().c_str()); |
1828 |
|
if( pResult->GetRowCount() ){ |
1829 |
|
Row = pResult->Next(); |
1830 |
|
if( Row ){ |
1831 |
|
stringstream fname; |
1832 |
|
fname.str(""); |
1833 |
|
fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1); |
1834 |
|
rname << Row->GetField(1); |
1835 |
|
if ( usel0file ) file = new TFile(fname.str().c_str(),"READ"); |
1836 |
|
idtsy = (UInt_t)atoll(Row->GetField(2)); |
1837 |
|
}; |
1838 |
|
}; |
1839 |
|
// |
1840 |
|
if ( usel0file && file && file->IsOpen() ){ |
1841 |
|
TTree *T=(TTree*)file->Get("Physics"); |
1842 |
|
pamela::EventHeader *eh = 0; |
1843 |
|
pamela::PscuHeader *ph = 0; |
1844 |
|
T->SetBranchAddress("Header", &eh); |
1845 |
|
// |
1846 |
|
T->GetEntry(0); |
1847 |
|
ph = eh->GetPscuHeader(); |
1848 |
|
pktfirst = ph->GetCounter(); |
1849 |
|
// obtfirst = ph->GetOrbitalTime(); |
1850 |
// |
// |
1851 |
}; |
}; |
1852 |
|
if ( !usel0file ) pktfirst = 0; |
1853 |
// |
// |
1854 |
// look for Resurs offset |
// look for Resurs offset |
1855 |
// |
// |
1875 |
if ( Row ){ |
if ( Row ){ |
1876 |
// |
// |
1877 |
OBT0 = (UInt_t)atoll(Row->GetField(0)); |
OBT0 = (UInt_t)atoll(Row->GetField(0)); |
1878 |
|
obtfirst = OBT0; |
1879 |
TIMESYNC = (UInt_t)atoll(Row->GetField(1)); |
TIMESYNC = (UInt_t)atoll(Row->GetField(1)); |
1880 |
TYPE = (UInt_t)atoll(Row->GetField(2)); |
TYPE = (UInt_t)atoll(Row->GetField(2)); |
1881 |
// |
// |
1906 |
TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0); |
TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0); |
1907 |
T0 = (UInt_t)tu.GetSec(); |
T0 = (UInt_t)tu.GetSec(); |
1908 |
// |
// |
1909 |
toffset = TIMESYNC - (UInt_t)(this->DBobt(OBT0/1000)) + T0; |
toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0; |
1910 |
// |
// |
1911 |
// printf(" T0 %u toffset is %u \n",T0,toffset); |
// printf(" T0 %u toffset is %u \n",T0,toffset); |
1912 |
// |
// |
1913 |
file->Close(); |
if ( file ) file->Close(); |
1914 |
delete pResult; |
delete pResult; |
1915 |
}; |
}; |
1916 |
|
|