00001 #define ProofNtuple_cxx
00002
00003
00004
00005
00006
00007
00008
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
00028
00029 SafeDelete(fNtp);
00030 SafeDelete(fFile);
00031 SafeDelete(fRandom);
00032 }
00033
00034 void ProofNtuple::PlotNtuple(TNtuple *ntp, const char *ntptitle)
00035 {
00036
00037
00038
00039
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
00047
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
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
00079 c1->cd();
00080 c1->Update();
00081 }
00082
00083 void ProofNtuple::Begin(TTree * )
00084 {
00085
00086
00087
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 * )
00096 {
00097
00098
00099
00100
00101 TString option = GetOption();
00102
00103
00104 TNamed *nm = dynamic_cast<TNamed *>(fInput->FindObject("SimpleNtuple.root"));
00105 if (nm) {
00106
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
00112
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
00121 TDirectory *savedir = gDirectory;
00122 fFile = fProofFile->OpenFile("RECREATE");
00123 if (fFile && fFile->IsZombie()) SafeDelete(fFile);
00124 savedir->cd();
00125
00126
00127 if (!fFile) {
00128 Info("SlaveBegin", "could not create '%s': instance is invalid!", fProofFile->GetName());
00129 return;
00130 }
00131
00132
00133 fNtp = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
00134
00135 fNtp->SetDirectory(fFile);
00136 fNtp->AutoSave();
00137
00138
00139 fRandom = new TRandom3(0);
00140 }
00141
00142 Bool_t ProofNtuple::Process(Long64_t entry)
00143 {
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 if (!fNtp) return kTRUE;
00163
00164
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
00178
00179
00180
00181
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
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
00209
00210
00211
00212
00213 if (!fPlotNtuple) return;
00214
00215
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
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
00240 if (fNtp) PlotNtuple(fNtp, "proof ntuple");
00241 }