ProofNtuple.C

Go to the documentation of this file.
00001 #define ProofNtuple_cxx
00002 
00003 //////////////////////////////////////////////////////////
00004 //
00005 // Example of TSelector implementation to do generic
00006 // processing (filling a simple ntuple, in this case).
00007 // See tutorials/proof/runProof.C, option "ntuple", for an
00008 // example of how to run this selector.
00009 //
00010 //////////////////////////////////////////////////////////
00011 
00012 #include "ProofNtuple.h"
00013 #include <TCanvas.h>
00014 #include <TFrame.h>
00015 #include <TPaveText.h>
00016 #include <TMath.h>
00017 #include <TNtuple.h>
00018 #include <TRandom3.h>
00019 #include <TString.h>
00020 #include <TStyle.h>
00021 #include <TSystem.h>
00022 #include <TFile.h>
00023 #include <TProofOutputFile.h>
00024 
00025 ProofNtuple::~ProofNtuple()
00026 {
00027    // Destructor
00028 
00029    SafeDelete(fNtp);
00030    SafeDelete(fFile);
00031    SafeDelete(fRandom);
00032 }
00033 
00034 void ProofNtuple::PlotNtuple(TNtuple *ntp, const char *ntptitle)
00035 {
00036    // Make some plots from the ntuple 'ntp'
00037 
00038    //
00039    // Create a canvas, with 2 pads
00040    //
00041    TCanvas *c1 = new TCanvas(Form("cv-%s", ntp->GetName()), ntptitle,800,10,700,780);
00042    c1->Divide(1,2);
00043    TPad *pad1 = (TPad *) c1->GetPad(1);
00044    TPad *pad2 = (TPad *) c1->GetPad(2);
00045    //
00046    // Display a function of one ntuple column imposing a condition
00047    // on another column.
00048    pad1->cd();
00049    pad1->SetGrid();
00050    pad1->SetLogy();
00051    pad1->GetFrame()->SetFillColor(15);
00052    ntp->SetLineColor(1);
00053    ntp->SetFillStyle(1001);
00054    ntp->SetFillColor(45);
00055    ntp->Draw("3*px+2","px**2+py**2>1");
00056    ntp->SetFillColor(38);
00057    ntp->Draw("2*px+2","pz>2","same");
00058    ntp->SetFillColor(5);
00059    ntp->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
00060    pad1->RedrawAxis();
00061 
00062    //
00063    // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
00064    pad2->cd();
00065    ntp->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
00066    ntp->SetMarkerColor(4);
00067    ntp->Draw("pz:py:px","pz<6 && pz>4","same");
00068    ntp->SetMarkerColor(5);
00069    ntp->Draw("pz:py:px","pz<4 && pz>3","same");
00070    TPaveText *l2 = new TPaveText(0.,0.6,0.9,0.95);
00071    l2->SetFillColor(42);
00072    l2->SetTextAlign(12);
00073    l2->AddText("You can interactively rotate this view in 2 ways:");
00074    l2->AddText("  - With the RotateCube in clicking in this pad");
00075    l2->AddText("  - Selecting View with x3d in the View menu");
00076    l2->Draw();
00077 
00078    // Final update
00079    c1->cd();
00080    c1->Update();
00081 }
00082 
00083 void ProofNtuple::Begin(TTree * /*tree*/)
00084 {
00085    // The Begin() function is called at the start of the query.
00086    // When running with PROOF Begin() is only called on the client.
00087    // The tree argument is deprecated (on PROOF 0 is passed).
00088 
00089    TString option = GetOption();
00090 
00091    TNamed *out = (TNamed *) fInput->FindObject("PROOF_NTUPLE_DONT_PLOT");
00092    if (out) fPlotNtuple = kFALSE;
00093 }
00094 
00095 void ProofNtuple::SlaveBegin(TTree * /*tree*/)
00096 {
00097    // The SlaveBegin() function is called after the Begin() function.
00098    // When running with PROOF SlaveBegin() is called on each slave server.
00099    // The tree argument is deprecated (on PROOF 0 is passed).
00100 
00101    TString option = GetOption();
00102 
00103    // We may be creating a dataset or a merge file: check it
00104    TNamed *nm = dynamic_cast<TNamed *>(fInput->FindObject("SimpleNtuple.root"));
00105    if (nm) {
00106       // Just create the object
00107       UInt_t opt = TProofOutputFile::kRegister | TProofOutputFile::kOverwrite | TProofOutputFile::kVerify;
00108       fProofFile = new TProofOutputFile("SimpleNtuple.root",
00109                                         TProofOutputFile::kDataset, opt, nm->GetTitle());
00110    } else {
00111       // For the ntuple, we use the automatic file merging facility
00112       // Check if an output URL has been given
00113       TNamed *out = (TNamed *) fInput->FindObject("PROOF_OUTPUTFILE_LOCATION");
00114       Info("SlaveBegin", "PROOF_OUTPUTFILE_LOCATION: %s", (out ? out->GetTitle() : "undef"));
00115       fProofFile = new TProofOutputFile("SimpleNtuple.root", (out ? out->GetTitle() : "M"));
00116       out = (TNamed *) fInput->FindObject("PROOF_OUTPUTFILE");
00117       if (out) fProofFile->SetOutputFileName(out->GetTitle());
00118    }
00119 
00120    // Open the file
00121    TDirectory *savedir = gDirectory;
00122    fFile = fProofFile->OpenFile("RECREATE");
00123    if (fFile && fFile->IsZombie()) SafeDelete(fFile);
00124    savedir->cd();
00125 
00126    // Cannot continue
00127    if (!fFile) {
00128       Info("SlaveBegin", "could not create '%s': instance is invalid!", fProofFile->GetName());
00129       return;
00130    }
00131 
00132    // Now we create the ntuple
00133    fNtp = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
00134    // File resident
00135    fNtp->SetDirectory(fFile);
00136    fNtp->AutoSave();
00137 
00138    // Init the random generator
00139    fRandom = new TRandom3(0);
00140 }
00141 
00142 Bool_t ProofNtuple::Process(Long64_t entry)
00143 {
00144    // The Process() function is called for each entry in the tree (or possibly
00145    // keyed object in the case of PROOF) to be processed. The entry argument
00146    // specifies which entry in the currently loaded tree is to be processed.
00147    // It can be passed to either ProofNtuple::GetEntry() or TBranch::GetEntry()
00148    // to read either all or the required parts of the data. When processing
00149    // keyed objects with PROOF, the object is already loaded and is available
00150    // via the fObject pointer.
00151    //
00152    // This function should contain the "body" of the analysis. It can contain
00153    // simple or elaborate selection criteria, run algorithms on the data
00154    // of the event and typically fill histograms.
00155    //
00156    // The processing can be stopped by calling Abort().
00157    //
00158    // Use fStatus to set the return value of TTree::Process().
00159    //
00160    // The return value is currently not used.
00161 
00162    if (!fNtp) return kTRUE;
00163 
00164    // Fill ntuple
00165    Float_t px, py;
00166    fRandom->Rannor(px,py);
00167    Float_t pz = px*px + py*py;
00168    Float_t random = fRandom->Rndm();
00169    Int_t i = (Int_t) entry;
00170    fNtp->Fill(px,py,pz,random,i);
00171 
00172    return kTRUE;
00173 }
00174 
00175 void ProofNtuple::SlaveTerminate()
00176 {
00177    // The SlaveTerminate() function is called after all entries or objects
00178    // have been processed. When running with PROOF SlaveTerminate() is called
00179    // on each slave server.
00180 
00181    // Write the ntuple to the file
00182    if (fFile) {
00183       Bool_t cleanup = kFALSE;
00184       TDirectory *savedir = gDirectory;
00185       if (fNtp->GetEntries() > 0) {
00186          fFile->cd();
00187          fNtp->Write();
00188          fProofFile->Print();
00189          fOutput->Add(fProofFile);
00190       } else {
00191          cleanup = kTRUE;
00192       }
00193       fNtp->SetDirectory(0);
00194       gDirectory = savedir;
00195       fFile->Close();
00196       // Cleanup, if needed
00197       if (cleanup) {
00198          TUrl uf(*(fFile->GetEndpointUrl()));
00199          SafeDelete(fFile);
00200          gSystem->Unlink(uf.GetFile());
00201          SafeDelete(fProofFile);
00202       }
00203    }
00204 }
00205 
00206 void ProofNtuple::Terminate()
00207 {
00208    // The Terminate() function is the last function to be called during
00209    // a query. It always runs on the client, it can be used to present
00210    // the results graphically or save the results to file.
00211 
00212    // Do nothing is not requested (dataset creation run)
00213    if (!fPlotNtuple) return;
00214 
00215    // Get the ntuple form the file
00216    if ((fProofFile =
00217            dynamic_cast<TProofOutputFile*>(fOutput->FindObject("SimpleNtuple.root")))) {
00218 
00219       TString outputFile(fProofFile->GetOutputFileName());
00220       TString outputName(fProofFile->GetName());
00221       outputName += ".root";
00222       Printf("outputFile: %s", outputFile.Data());
00223 
00224       // Read the ntuple from the file
00225       fFile = TFile::Open(outputFile);
00226       if (fFile) {
00227          Printf("Managed to open file: %s", outputFile.Data());
00228          fNtp = (TNtuple *) fFile->Get("ntuple");
00229       } else {
00230          Error("Terminate", "could not open file: %s", outputFile.Data());
00231       }
00232       if (!fFile) return; 
00233 
00234    } else {
00235       Error("Terminate", "TProofOutputFile not found");
00236       return;
00237    }
00238 
00239    // Plot ntuples
00240    if (fNtp) PlotNtuple(fNtp, "proof ntuple");
00241 }

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