00001 #include <TFile.h>
00002 #include <TNtuple.h>
00003 #include <TH2.h>
00004 #include <TProfile.h>
00005 #include <TCanvas.h>
00006 #include <TFrame.h>
00007 #include <TROOT.h>
00008 #include <TSystem.h>
00009 #include <TRandom.h>
00010 #include <TBenchmark.h>
00011 #include <TInterpreter.h>
00012 
00013 TFile *hsimple(Int_t get=0)
00014 {
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027    TString filename = "hsimple.root";
00028    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
00029    dir.ReplaceAll("hsimple.C","");
00030    dir.ReplaceAll("/./","/");
00031    TFile *hfile = 0;
00032    if (get) {
00033       
00034       
00035       TString fullPath = dir+"hsimple.root";
00036       if (!gSystem->AccessPathName(fullPath,kFileExists)) {
00037          hfile = TFile::Open(fullPath); 
00038          if (hfile) return hfile;
00039       }
00040       
00041       if (!gSystem->AccessPathName("hsimple.root",kFileExists)) {
00042          hfile = TFile::Open("hsimple.root"); 
00043          if (hfile) return hfile;
00044       }
00045    }
00046    
00047    
00048    if (!gSystem->AccessPathName(dir,kWritePermission)) {
00049       filename = dir+"hsimple.root";
00050    } else if (!gSystem->AccessPathName(".",kWritePermission)) {
00051       
00052    } else {
00053       printf("you must run the script in a directory with write access\n");
00054       return 0;
00055    }
00056    hfile = (TFile*)gROOT->FindObject(filename); if (hfile) hfile->Close();
00057    hfile = new TFile(filename,"RECREATE","Demo ROOT file with histograms");
00058 
00059    
00060    TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
00061    hpx->SetFillColor(48);
00062    TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
00063    TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
00064    TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
00065 
00066    gBenchmark->Start("hsimple");
00067   
00068    
00069    TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
00070    c1->SetFillColor(42);
00071    c1->GetFrame()->SetFillColor(21);
00072    c1->GetFrame()->SetBorderSize(6);
00073    c1->GetFrame()->SetBorderMode(-1);
00074 
00075 
00076    
00077    gRandom->SetSeed();
00078    Float_t px, py, pz;
00079    const Int_t kUPDATE = 1000;
00080    for (Int_t i = 0; i < 25000; i++) {
00081       gRandom->Rannor(px,py);
00082       pz = px*px + py*py;
00083       Float_t random = gRandom->Rndm(1);
00084       hpx->Fill(px);
00085       hpxpy->Fill(px,py);
00086       hprof->Fill(px,pz);
00087       ntuple->Fill(px,py,pz,random,i);
00088       if (i && (i%kUPDATE) == 0) {
00089          if (i == kUPDATE) hpx->Draw();
00090          c1->Modified();
00091          c1->Update();
00092          if (gSystem->ProcessEvents())
00093             break;
00094       }
00095    }
00096    gBenchmark->Show("hsimple");
00097 
00098    
00099    hpx->SetFillColor(0);
00100    hfile->Write();
00101    hpx->SetFillColor(48);
00102    c1->Modified();
00103    return hfile;
00104   
00105 
00106 
00107 }