sqlcanvas.C

Go to the documentation of this file.
00001 // This is slight modification of ntuple1.C example.
00002 // A canvas with subpads is produced, stored to an sql data base and read back
00003 // To run this macro, you need the hsimple.root file, produced by the hsimple.C macro
00004 // Author: S.Linev
00005 
00006 // example configuration for MySQL 4.1
00007 const char* dbname = "mysql://host.domain/test";
00008 const char* username = "user";
00009 const char* userpass = "pass";
00010 
00011 // example configuration for Oracle 9i
00012 //const char* dbname = "oracle://host.domain/db-test";
00013 //const char* username = "user";
00014 //const char* userpass = "pass";
00015 
00016 void sqlcanvas() 
00017 {
00018    canvas_write();
00019    canvas_read(); 
00020 }
00021    
00022 void canvas_write() 
00023 {
00024    //just in case this script is executed multiple times
00025    delete gROOT->GetListOfFiles()->FindObject("hsimple.root");
00026    delete gROOT->GetListOfCanvases()->FindObject("c1");
00027 
00028    gBenchmark->Start("ntuple1");
00029    //
00030    // Connect ROOT histogram/ntuple demonstration file
00031    // generated by example hsimple.C.
00032    TFile *f1 = new TFile("hsimple.root");
00033    //
00034    // Create a canvas, with 4 pads
00035    //
00036    TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
00037    TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.48,0.98,21);
00038    TPad *pad2 = new TPad("pad2","This is pad2",0.52,0.52,0.98,0.98,21);
00039    TPad *pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.48,0.48,21);
00040    TPad *pad4 = new TPad("pad4","This is pad4",0.52,0.02,0.98,0.48,1);
00041    pad1->Draw();
00042    pad2->Draw();
00043    pad3->Draw();
00044    pad4->Draw();
00045    //
00046    // Change default style for the statistics box
00047    gStyle->SetStatW(0.30);
00048    gStyle->SetStatH(0.20);
00049    gStyle->SetStatColor(42);
00050    //
00051    // Display a function of one ntuple column imposing a condition
00052    // on another column.
00053    pad1->cd();
00054    pad1->SetGrid();
00055    pad1->SetLogy();
00056    pad1->GetFrame()->SetFillColor(15);
00057    TNtuple *ntuple = (TNtuple*)f1->Get("ntuple");
00058    ntuple->SetLineColor(1);
00059    ntuple->SetFillStyle(1001);
00060    ntuple->SetFillColor(45);
00061    ntuple->Draw("3*px+2","px**2+py**2>1");
00062    ntuple->SetFillColor(38);
00063    ntuple->Draw("2*px+2","pz>2","same");
00064    ntuple->SetFillColor(5);
00065    ntuple->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
00066    pad1->RedrawAxis();
00067    //
00068    // Display the profile of two columns
00069    // The profile histogram produced is saved in the current directory with
00070    // the name hprofs
00071    pad2->cd();
00072    pad2->SetGrid();
00073    pad2->GetFrame()->SetFillColor(32);
00074    ntuple->Draw("pz:px>>hprofs","","goffprofs");
00075    TProfile *hprofs = (TProfile*)gDirectory->Get("hprofs");
00076    hprofs->SetMarkerColor(5);
00077    hprofs->SetMarkerSize(0.7);
00078    hprofs->SetMarkerStyle(21);
00079    hprofs->Fit("pol2");
00080    // Get pointer to fitted function and modify its attributes
00081    TF1 *fpol2 = hprofs->GetFunction("pol2");
00082    fpol2->SetLineWidth(4);
00083    fpol2->SetLineColor(2);
00084    //
00085    // Display a scatter plot of two columns with a selection.
00086    // Superimpose the result of another cut with a different marker color
00087    pad3->cd();
00088    pad3->GetFrame()->SetFillColor(38);
00089    pad3->GetFrame()->SetBorderSize(8);
00090    ntuple->SetMarkerColor(1);
00091    ntuple->Draw("py:px","pz>1");
00092    ntuple->SetMarkerColor(2);
00093    ntuple->Draw("py:px","pz<1","same");
00094    //
00095    // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
00096    pad4->cd();
00097    ntuple->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
00098    ntuple->SetMarkerColor(4);
00099    ntuple->Draw("pz:py:px","pz<6 && pz>4","same");
00100    ntuple->SetMarkerColor(5);
00101    ntuple->Draw("pz:py:px","pz<4 && pz>3","same");
00102    TPaveText *l4 = new TPaveText(-0.9,0.5,0.9,0.95);
00103    l4->SetFillColor(42);
00104    l4->SetTextAlign(12);
00105    l4->AddText("You can interactively rotate this view in 2 ways:");
00106    l4->AddText("  - With the RotateCube in clicking in this pad");
00107    l4->AddText("  - Selecting View with x3d in the View menu");
00108    l4->Draw();
00109    //
00110    c1->cd();
00111    c1->Update();
00112    gStyle->SetStatColor(19);
00113    gBenchmark->Show("ntuple1");
00114    
00115    TSQLFile* fsql1 = new TSQLFile(dbname, "recreate", username, userpass);
00116    if (fsql1->IsZombie()) { delete fsql1; return; }
00117 
00118 //  changing TSQLFile configuration, you may improve speed 
00119 //  of reading or writing object to/from sql database
00120  
00121 //   fsql1->SetUseSuffixes(kFALSE);
00122 //   fsql1->SetArrayLimit(1000);
00123 //   fsql1->SetUseIndexes(1);
00124 //   fsql1->SetTablesType("ISAM");
00125 //   fsql1->SetUseTransactions(kFALSE);
00126 
00127   
00128    //  Unncomment this line to see all SQL commands in log file
00129    //  fsql1->StartLogFile("canvas.log");
00130    
00131    gBenchmark->Start("writeSQL");
00132    c1->Write("Canvas");
00133    gBenchmark->Show("writeSQL");
00134    delete fsql1;
00135 }
00136 
00137 void canvas_read() 
00138 {
00139    TFile* f2 = new TSQLFile(dbname, "open", username, userpass);
00140    if (f2->IsZombie()) { delete f2; return; }
00141    
00142    f2->ls();
00143    gBenchmark->Start("readSQL");
00144    TCanvas* cc = (TCanvas*) f2->Get("Canvas");
00145    gBenchmark->Show("readSQL");
00146    if (cc!=0) cc->Draw();
00147 
00148    delete f2;
00149 }
00150 

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