/[PAMELA software]/chewbacca/YodaProfiler/src/GLTables.cpp
ViewVC logotype

Diff of /chewbacca/YodaProfiler/src/GLTables.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by mocchiut, Fri Dec 12 11:07:09 2008 UTC revision 1.12 by mocchiut, Tue Dec 15 09:58:25 2009 UTC
# Line 7  Line 7 
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>
# Line 17  Line 18 
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);
# Line 30  ClassImp(GL_TLE); Line 32  ClassImp(GL_TLE);
32  //  //
33  using namespace std;  using namespace std;
34    
35    Q2TH::Q2TH(TString host, TString user, TString psw){
36      fh = gSystem->ExpandPathName(host.Data());
37      fu = gSystem->ExpandPathName(user.Data());
38      fp = gSystem->ExpandPathName(psw.Data());
39      dbc = TSQLServer::Connect(fh.Data(),fu.Data(),fp.Data());
40    };
41    
42    TObject *Q2TH::Draw(TString query, Bool_t verbose, TString hname){
43      //
44      if ( !strcmp(query.Data(),"help") ){
45        printf(" USAGE: \n");
46        printf(" 1) start root and create Q2TH object with  \n");
47        printf("    Q2TH *qt = new Q2TH()  \n");
48        printf("    or \n");
49        printf("    Q2TH *qt = new Q2TH(\"mysql://srvg-g2-01.ts.infn.it/pamelaProcessing9_TS\",\"pamelaprod_ro\",\"mypassword\")  \n");
50        printf(" 2) query the DB with  \n");
51        printf("    qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\");  \n");
52        printf("    qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",true); this will print numbers on screen \n");
53        printf("    qt->Draw(\"select REAL_TIME_INIT from ROOT_TABLE_MERGING;\",true,\"myhisto\"); this will print numbers on screen and create histo \"myhisto\"\n");
54        printf(" 3) to use your own THxD create it and then query the DB giving as argument the name of histo:   \n");
55        printf("    TH2D *myhisto=new TH2D(\"myhisto\",\"myhisto\",5000,1140000000.,1240000000.,10000,0.,1.) \n");
56        printf("    qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",false,\"myhisto\")\n\n\n");
57    
58        return NULL;
59      };
60      //
61      pResult = dbc->Query(query.Data());
62      //
63      Row = pResult->Next();      
64      //
65      Int_t dim = pResult->GetFieldCount();
66      if ( dim < 1 || dim > 2 ){
67        printf(" Dim == %i not supported yet \n",dim);
68        return NULL;
69      };  
70      //
71      TH1D *h1 = NULL;
72      TH2D *h2 = NULL;
73      Double_t f1 = 0.;
74      Double_t minf1 = numeric_limits<Double_t>::max();
75      Double_t maxf1 = numeric_limits<Double_t>::min();
76      Double_t f2 = 0.;  
77      Double_t minf2 = numeric_limits<Double_t>::max();
78      Double_t maxf2 = numeric_limits<Double_t>::min();
79      //
80      while ( Row ){    
81        f1 = (Double_t)atof(Row->GetField(0));
82        if ( f1 > maxf1 ) maxf1 = f1;
83        if ( f1 < minf1 ) minf1 = f1;
84        if ( dim == 2 ){
85          f2 = (Double_t)atof(Row->GetField(1));
86          if ( f2 > maxf2 ) maxf2 = f2;
87          if ( f2 < minf2 ) minf2 = f2;
88    
89        };
90        Row = pResult->Next();
91      };
92      pResult->Delete();
93      //
94            
95      //
96      Int_t f1bin = 70;
97      Int_t f2bin = 70;
98      if ( dim == 1 ){
99        f1bin = int((maxf1-minf1)/1000.);
100        if ( f1bin < 70 ) f1bin = 70;
101        if ( f1bin > 1000 ) f1bin = 1000;
102            if ( !strcmp(hname.Data(),"q2th") ) hname += "1";
103            //      h1 =  dynamic_cast<TH1D*>(gDirectory->FindObject(hname.Data()));
104            h1 = (TH1D*)(gDirectory->FindObject(hname.Data()));
105            if ( !strcmp(hname.Data(),"q2th1") ){
106             if ( h1 ) h1->Delete();
107            };
108            if ( !h1 ) h1 = new TH1D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02);
109        //    h1->SetBit(TH1::kCanRebin);
110        if ( verbose ) printf("\n\n Row     %s \n",pResult->GetFieldName(0));
111      };
112      if ( dim == 2 ){
113        f2bin = int((maxf2-minf2)/1000.);
114        if ( f2bin < 70 ) f2bin = 70;
115        if ( f2bin > 1000 ) f2bin = 1000;
116            if ( !strcmp(hname.Data(),"q2th") ) hname += "2";
117            //      h2 =  dynamic_cast<TH2D*>(gDirectory->FindObject(hname.Data()));
118            h2 =  (TH2D*)(gDirectory->FindObject(hname.Data()));
119            if ( !strcmp(hname.Data(),"q2th2") ){
120             if ( h2 ) h2->Delete();
121            };
122            if ( !h2 ) h2 = new TH2D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02,f2bin,minf2*0.98,maxf2*1.02);
123        //    h2->SetBit(TH2::kCanRebin);
124        if ( verbose ) printf("\n\n Row     %s     %s \n",pResult->GetFieldName(0),pResult->GetFieldName(1));
125      };
126      //
127      pResult = dbc->Query(query.Data());
128      //
129      Row = pResult->Next();      
130      //
131      Int_t r = 0;
132      //
133      while ( Row ){    
134        f1 = (Double_t)atof(Row->GetField(0));
135        if ( dim == 1 ){
136          if ( verbose ) printf(" %i     %f \n",r,f1);
137          h1->Fill(f1);
138        } else {
139          f2 = (Double_t)atof(Row->GetField(1));
140          if ( verbose ) printf(" %i     %f     %f \n",r,f1,f2);
141          h2->Fill(f1,f2);
142        };
143        r++;
144        Row = pResult->Next();
145      };
146      //
147      TCanvas *c = NULL;
148      TString cname = Form("%sc",hname.Data());
149      //  c =  dynamic_cast<TCanvas*>(gDirectory->FindObject(hname.Data()));
150      c =  (TCanvas*)(gDirectory->FindObject(cname.Data()));
151      if ( !c ) c = new TCanvas(Form("%sc",cname.Data()));
152      c->Clear();
153      c->cd();
154      if ( dim == 1 ) h1->Draw();
155      if ( dim == 2 ) h2->Draw();
156      //
157      pResult->Delete();
158      if ( dim == 1 ) return h1;
159      if ( dim == 2 ) return h2;
160      //
161      return NULL;
162    };
163    
164  GL_TABLES::GL_TABLES(){  GL_TABLES::GL_TABLES(){
165  };  };
166    
# Line 56  void GL_TABLES::Set(TString host, TStrin Line 187  void GL_TABLES::Set(TString host, TStrin
187    mp = psw.Data();    mp = psw.Data();
188  };  };
189    
190  Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){  //Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){
191    Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){
192    //    //
193    //    //
194    //    //
# Line 92  Bool_t GL_TABLES::IsConnected(TSQLServer Line 224  Bool_t GL_TABLES::IsConnected(TSQLServer
224      TString host = fHost->Data();      TString host = fHost->Data();
225      TString user = fUser->Data();      TString user = fUser->Data();
226      TString psw = fPsw->Data();      TString psw = fPsw->Data();
227      dbc->Close();      if ( dbc ){
228      delete dbc;        dbc->Close();
229          delete dbc;
230        };
231      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
232      //      //
233      myquery.str("");      myquery.str("");
# Line 1652  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1786  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1786    //    //
1787    //  printf(" T0 %u toffset is %u \n",T0,toffset);    //  printf(" T0 %u toffset is %u \n",T0,toffset);
1788    //    //
1789    file->Close();    if ( file ) file->Close();
1790      delete pResult;      
1791    };
1792    
1793    GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc, Bool_t usel0file){
1794      // MySQL variables
1795      TFile *file = 0;
1796      UInt_t idtsy = 0;
1797      //
1798      TSQLResult *pResult;
1799      TSQLRow *Row = 0;
1800      stringstream myquery;
1801      stringstream rname;
1802      //  pcksList packetsNames;
1803      //  pcksList::iterator Iter;
1804      //  getPacketsNames(packetsNames);
1805      rname.str("");
1806      // ----------------
1807      myquery.str("");
1808      myquery << "select ";
1809      myquery << "PATH";
1810      myquery << ",NAME,ID_TIMESYNC";
1811      myquery << " from GL_ROOT where ";
1812      myquery << type.Data();
1813      myquery << "=" << id << ";";    
1814      //
1815      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1816      this->GetGLTABLES()->AddQ();
1817      pResult = dbc->Query(myquery.str().c_str());
1818      if( pResult->GetRowCount() ){
1819        Row = pResult->Next();      
1820        if( Row ){
1821          stringstream fname;
1822          fname.str("");
1823          fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1);
1824          rname << Row->GetField(1);
1825          if ( usel0file ) file = new TFile(fname.str().c_str(),"READ");
1826          idtsy = (UInt_t)atoll(Row->GetField(2));
1827        };
1828      };
1829      //
1830      if ( usel0file && file && file->IsOpen() ){
1831        TTree *T=(TTree*)file->Get("Physics");
1832        pamela::EventHeader *eh = 0;
1833        pamela::PscuHeader *ph = 0;
1834        T->SetBranchAddress("Header", &eh);
1835        //
1836        T->GetEntry(0);
1837        ph = eh->GetPscuHeader();
1838        pktfirst = ph->GetCounter();
1839        //    obtfirst = ph->GetOrbitalTime();  
1840        //
1841      };
1842      if ( !usel0file ) pktfirst = 0;
1843      //
1844      // look for Resurs offset
1845      //
1846      T0 = 0;
1847      //
1848      stringstream oss;
1849      //
1850      TString name=rname.str().c_str();
1851      UInt_t dworbit = 0;
1852      //  Int_t nlength = name.Length();
1853      delete pResult;      
1854      //
1855      // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset
1856      //
1857      oss.str("");
1858      oss << "SELECT OBT0,TIMESYNC,TYPE,ID_RESURS_OFFSET from GL_TIMESYNC where ID=" << idtsy <<";";
1859      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1860      this->GetGLTABLES()->AddQ();
1861      pResult = dbc->Query(oss.str().c_str());
1862      Bool_t fndit = false;
1863      if ( pResult ){
1864        Row = pResult->Next();
1865        if ( Row ){
1866          //
1867          OBT0 = (UInt_t)atoll(Row->GetField(0));
1868          obtfirst = OBT0;
1869          TIMESYNC = (UInt_t)atoll(Row->GetField(1));
1870          TYPE = (UInt_t)atoll(Row->GetField(2));      
1871          //
1872          oss.str("");
1873          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="
1874              << Row->GetField(3) << ";";
1875          if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1876          this->GetGLTABLES()->AddQ();
1877          delete pResult;
1878          pResult = dbc->Query(oss.str().c_str());
1879          if ( pResult ){
1880            Row = pResult->Next();
1881            if ( Row ){
1882              //        printf(" GREAT! the DB structure is the new one! \n");
1883              fndit = true;
1884              dworbit = 1;
1885            };
1886          };
1887        };
1888      };
1889      if ( !fndit ){
1890        //
1891        printf(" ERROR OLD DB! \n");
1892        printf(" ERROR FROM GLTables! cannot determine Resurs offset \n");
1893        //
1894      };
1895      //
1896      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);
1897      T0 = (UInt_t)tu.GetSec();
1898      //
1899      toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0;
1900      //
1901      //  printf(" T0 %u toffset is %u \n",T0,toffset);
1902      //
1903      if ( file ) file->Close();
1904    delete pResult;          delete pResult;      
1905  };  };
1906    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.23