hsimple.C

Go to the documentation of this file.
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 //  This program creates :
00016 //    - a one dimensional histogram
00017 //    - a two dimensional histogram
00018 //    - a profile histogram
00019 //    - a memory-resident ntuple
00020 //
00021 //  These objects are filled with some random numbers and saved on a file.
00022 //  If get=1 the macro returns a pointer to the TFile of "hsimple.root"
00023 //          if this file exists, otherwise it is created.
00024 //  The file "hsimple.root" is created in $ROOTSYS/tutorials if the caller has
00025 //  write access to this directory, otherwise the file is created in $PWD
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       // if the argument get =1 return the file "hsimple.root"
00034       // if the file does not exist, it is created
00035       TString fullPath = dir+"hsimple.root";
00036       if (!gSystem->AccessPathName(fullPath,kFileExists)) {
00037          hfile = TFile::Open(fullPath); //in $ROOTSYS/tutorials
00038          if (hfile) return hfile;
00039       }
00040       //otherwise try $PWD/hsimple.root
00041       if (!gSystem->AccessPathName("hsimple.root",kFileExists)) {
00042          hfile = TFile::Open("hsimple.root"); //in current dir
00043          if (hfile) return hfile;
00044       }
00045    }
00046    //no hsimple.root file found. Must generate it !
00047    //generate hsimple.root in $ROOTSYS/tutorials if we have write access
00048    if (!gSystem->AccessPathName(dir,kWritePermission)) {
00049       filename = dir+"hsimple.root";
00050    } else if (!gSystem->AccessPathName(".",kWritePermission)) {
00051       //otherwise generate hsimple.root in the current directory
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    // Create some histograms, a profile histogram and an ntuple
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    // Create a new canvas.
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    // Fill histograms randomly
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    // Save all objects in this file
00099    hpx->SetFillColor(0);
00100    hfile->Write();
00101    hpx->SetFillColor(48);
00102    c1->Modified();
00103    return hfile;
00104   
00105 // Note that the file is automatically close when application terminates
00106 // or when the file destructor is called.
00107 }

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