Logo ROOT   6.12/07
Reference Guide
ProofNtuple.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_proofntuple
3 ///
4 /// Selector to fill a simple ntuple
5 ///
6 /// \macro_code
7 ///
8 /// \author Gerardo Ganis (gerardo.ganis@cern.ch)
9 
10 #define ProofNtuple_cxx
11 
12 #include "ProofNtuple.h"
13 #include <TCanvas.h>
14 #include <TFrame.h>
15 #include <TPaveText.h>
16 #include <TMath.h>
17 #include <TNtuple.h>
18 #include <TRandom3.h>
19 #include <TROOT.h>
20 #include <TString.h>
21 #include <TStyle.h>
22 #include <TSystem.h>
23 #include <TFile.h>
24 #include <TProofOutputFile.h>
25 
26 //_____________________________________________________________________________
27 ProofNtuple::~ProofNtuple()
28 {
29  // Destructor
30 
31  SafeDelete(fNtp);
32  SafeDelete(fNtp2);
33  SafeDelete(fFile);
34  SafeDelete(fRandom);
35 }
36 
37 //_____________________________________________________________________________
38 void ProofNtuple::PlotNtuple(TNtuple *ntp, const char *ntptitle)
39 {
40  // Make some plots from the ntuple 'ntp'
41 
42  //
43  // Create a canvas, with 2 pads
44  //
45  TCanvas *c1 = new TCanvas(Form("cv-%s", ntp->GetName()), ntptitle,800,10,700,780);
46  c1->Divide(1,2);
47  TPad *pad1 = (TPad *) c1->GetPad(1);
48  TPad *pad2 = (TPad *) c1->GetPad(2);
49  //
50  // Display a function of one ntuple column imposing a condition
51  // on another column.
52  pad1->cd();
53  pad1->SetGrid();
54  pad1->SetLogy();
55  pad1->GetFrame()->SetFillColor(15);
56  ntp->SetLineColor(1);
57  ntp->SetFillStyle(1001);
58  ntp->SetFillColor(45);
59  ntp->Draw("3*px+2","px**2+py**2>1");
60  ntp->SetFillColor(38);
61  ntp->Draw("2*px+2","pz>2","same");
62  ntp->SetFillColor(5);
63  ntp->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
64  pad1->RedrawAxis();
65 
66  //
67  // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
68  pad2->cd();
69  ntp->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
70  ntp->SetMarkerColor(4);
71  ntp->Draw("pz:py:px","pz<6 && pz>4","same");
72  ntp->SetMarkerColor(5);
73  ntp->Draw("pz:py:px","pz<4 && pz>3","same");
74  TPaveText *l2 = new TPaveText(0.,0.6,0.9,0.95);
75  l2->SetFillColor(42);
76  l2->SetTextAlign(12);
77  l2->AddText("You can interactively rotate this view in 2 ways:");
78  l2->AddText(" - With the RotateCube in clicking in this pad");
79  l2->AddText(" - Selecting View with x3d in the View menu");
80  l2->Draw();
81 
82  // Final update
83  c1->cd();
84  c1->Update();
85 }
86 
87 //_____________________________________________________________________________
88 void ProofNtuple::Begin(TTree * /*tree*/)
89 {
90  // The Begin() function is called at the start of the query.
91  // When running with PROOF Begin() is only called on the client.
92  // The tree argument is deprecated (on PROOF 0 is passed).
93 
94  TString option = GetOption();
95 
96  TNamed *out = (TNamed *) fInput->FindObject("PROOF_NTUPLE_DONT_PLOT");
97  if (out) fPlotNtuple = kFALSE;
98 }
99 
100 //_____________________________________________________________________________
101 void ProofNtuple::SlaveBegin(TTree * /*tree*/)
102 {
103  // The SlaveBegin() function is called after the Begin() function.
104  // When running with PROOF SlaveBegin() is called on each slave server.
105  // The tree argument is deprecated (on PROOF 0 is passed).
106 
107  TString option = GetOption();
108 
109  // We may be creating a dataset or a merge file: check it
110  TNamed *nm = dynamic_cast<TNamed *>(fInput->FindObject("SimpleNtuple.root"));
111  if (nm) {
112  // Just create the object
114  fProofFile = new TProofOutputFile("SimpleNtuple.root",
116  } else {
117  // For the ntuple, we use the automatic file merging facility
118  // Check if an output URL has been given
119  TNamed *out = (TNamed *) fInput->FindObject("PROOF_OUTPUTFILE_LOCATION");
120  Info("SlaveBegin", "PROOF_OUTPUTFILE_LOCATION: %s", (out ? out->GetTitle() : "undef"));
121  fProofFile = new TProofOutputFile("SimpleNtuple.root", (out ? out->GetTitle() : "M"));
122  out = (TNamed *) fInput->FindObject("PROOF_OUTPUTFILE");
123  if (out) fProofFile->SetOutputFileName(out->GetTitle());
124  }
125 
126  // Open the file
127  fFile = fProofFile->OpenFile("RECREATE");
128  if (fFile && fFile->IsZombie()) SafeDelete(fFile);
129 
130  // Cannot continue
131  if (!fFile) {
132  Info("SlaveBegin", "could not create '%s': instance is invalid!", fProofFile->GetName());
133  return;
134  }
135 
136  // Now we create the ntuple
137  fNtp = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
138  // File resident
139  fNtp->SetDirectory(fFile);
140  fNtp->AutoSave();
141 
142  // Now we create the second ntuple
143  fNtp2 = new TNtuple("ntuple2","Demo ntuple2","vx:vy:vz");
144  // File resident
145  fNtp2->SetDirectory(fFile);
146  fNtp2->AutoSave();
147 
148  // Should we generate the random numbers or take them from the ntuple ?
149  TNamed *unr = (TNamed *) fInput->FindObject("PROOF_USE_NTP_RNDM");
150  if (unr) {
151  // Get the ntuple from the input list
152  if (!(fNtpRndm = dynamic_cast<TNtuple *>(fInput->FindObject("NtpRndm")))) {
153  Warning("SlaveBegin",
154  "asked to use rndm ntuple but 'NtpRndm' not found in the"
155  " input list! Using the random generator");
156  fInput->Print();
157  } else {
158  Info("SlaveBegin", "taking randoms from input ntuple 'NtpRndm'");
159  }
160  }
161 
162  // Init the random generator, if required
163  if (!fNtpRndm) fRandom = new TRandom3(0);
164 }
165 
166 //_____________________________________________________________________________
167 Bool_t ProofNtuple::Process(Long64_t entry)
168 {
169  // The Process() function is called for each entry in the tree (or possibly
170  // keyed object in the case of PROOF) to be processed. The entry argument
171  // specifies which entry in the currently loaded tree is to be processed.
172  // It can be passed to either ProofNtuple::GetEntry() or TBranch::GetEntry()
173  // to read either all or the required parts of the data. When processing
174  // keyed objects with PROOF, the object is already loaded and is available
175  // via the fObject pointer.
176  //
177  // This function should contain the "body" of the analysis. It can contain
178  // simple or elaborate selection criteria, run algorithms on the data
179  // of the event and typically fill histograms.
180  //
181  // The processing can be stopped by calling Abort().
182  //
183  // Use fStatus to set the return value of TTree::Process().
184  //
185  // The return value is currently not used.
186 
187  if (!fNtp) return kTRUE;
188 
189  // Fill ntuple
190  Float_t px, py, random;
191  if (fNtpRndm) {
192  // Get the entry
193  Float_t *ar = fNtpRndm->GetArgs();
194  Long64_t ent = entry % fNtpRndm->GetEntries();
195  fNtpRndm->GetEntry(ent);
196  random = ar[0];
197  px = (Float_t) TMath::ErfInverse((Double_t)(ar[1]*2 - 1.)) * TMath::Sqrt(2.);
198  py = (Float_t) TMath::ErfInverse((Double_t)(ar[2]*2 - 1.)) * TMath::Sqrt(2.);
199  } else if (fRandom) {
200  fRandom->Rannor(px,py);
201  random = fRandom->Rndm();
202  } else {
203  Abort("no way to get random numbers! Stop processing", kAbortProcess);
204  return kTRUE;
205  }
206  Float_t pz = px*px + py*py;
207  Int_t i = (Int_t) entry;
208  fNtp->Fill(px,py,pz,random,i);
209 
210  if (!fNtp2) return kTRUE;
211 
212  // The second ntuple
213  Float_t vz = random * 2. - 1.;
214  fNtp2->Fill(px,py,vz);
215 
216  return kTRUE;
217 }
218 
219 //_____________________________________________________________________________
220 void ProofNtuple::SlaveTerminate()
221 {
222  // The SlaveTerminate() function is called after all entries or objects
223  // have been processed. When running with PROOF SlaveTerminate() is called
224  // on each slave server.
225 
226  // Write the ntuple to the file
227  if (fFile) {
228  if (!fNtp) {
229  Error("SlaveTerminate", "'ntuple' is undefined!");
230  return;
231  }
232  Bool_t cleanup = kFALSE;
233  TDirectory *savedir = gDirectory;
234  if (fNtp->GetEntries() > 0) {
235  fFile->cd();
236  fNtp->Write(0, TObject::kOverwrite);
237  if (fNtp2 && fNtp2->GetEntries() > 0) fNtp2->Write(0, TObject::kOverwrite);
238  fProofFile->Print();
239  fOutput->Add(fProofFile);
240  } else {
241  cleanup = kTRUE;
242  }
243  fNtp->SetDirectory(0);
244  if (fNtp2) fNtp2->SetDirectory(0);
245  gDirectory = savedir;
246  fFile->Close();
247  // Cleanup, if needed
248  if (cleanup) {
249  TUrl uf(*(fFile->GetEndpointUrl()));
250  SafeDelete(fFile);
251  gSystem->Unlink(uf.GetFile());
252  SafeDelete(fProofFile);
253  }
254  }
255 }
256 
257 //_____________________________________________________________________________
258 void ProofNtuple::Terminate()
259 {
260  // The Terminate() function is the last function to be called during
261  // a query. It always runs on the client, it can be used to present
262  // the results graphically or save the results to file.
263 
264  // Do nothing is not requested (dataset creation run)
265  if (!fPlotNtuple) return;
266 
267  // Get the ntuple from the file
268  if ((fProofFile =
269  dynamic_cast<TProofOutputFile*>(fOutput->FindObject("SimpleNtuple.root")))) {
270 
271  TString outputFile(fProofFile->GetOutputFileName());
272  TString outputName(fProofFile->GetName());
273  outputName += ".root";
274  Printf("outputFile: %s", outputFile.Data());
275 
276  // Read the ntuple from the file
277  fFile = TFile::Open(outputFile);
278  if (fFile) {
279  Printf("Managed to open file: %s", outputFile.Data());
280  fNtp = (TNtuple *) fFile->Get("ntuple");
281  } else {
282  Error("Terminate", "could not open file: %s", outputFile.Data());
283  }
284  if (!fFile) return;
285 
286  } else {
287  Error("Terminate", "TProofOutputFile not found");
288  return;
289  }
290 
291  // Plot ntuples
292  if (fNtp) PlotNtuple(fNtp, "proof ntuple");
293 
294 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void Begin(Int_t type)
Double_t ErfInverse(Double_t x)
returns the inverse error function x must be <-1<x<1
Definition: TMath.cxx:206
Random number generator class based on M.
Definition: TRandom3.h:27
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:234
long long Long64_t
Definition: RtypesCore.h:69
virtual TVirtualPad * GetPad(Int_t subpadnumber) const
Get a pointer to subpadnumber of this pad.
Definition: TPad.cxx:2853
float Float_t
Definition: RtypesCore.h:53
return c1
Definition: legend1.C:41
This class represents a WWW compatible URL.
Definition: TUrl.h:35
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:183
overwrite existing object with same name
Definition: TObject.h:88
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
#define SafeDelete(p)
Definition: RConfig.h:509
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:585
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2812
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:327
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
void Info(const char *location, const char *msgfmt,...)
virtual void Close(Option_t *option="")
Delete all objects from memory and directory structure itself.
Definition: TDirectory.cxx:584
virtual void RedrawAxis(Option_t *option="")
Redraw the frame axis Redrawing axis may be necessary in case of superimposed histograms when one or ...
Definition: TPad.cxx:5185
void Error(const char *location, const char *msgfmt,...)
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:321
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
unsigned int UInt_t
Definition: RtypesCore.h:42
The most important graphics class in the ROOT system.
Definition: TPad.h:29
char * Form(const char *fmt,...)
Class to steer the merging of files produced on the workers.
void Warning(const char *location, const char *msgfmt,...)
static constexpr double nm
#define Printf
Definition: TGeoToOCC.h:18
const Bool_t kFALSE
Definition: RtypesCore.h:88
The Canvas class.
Definition: TCanvas.h:31
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:355
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1153
Selector to fill a simple ntuple.
A TTree object has a header with a name and a title.
Definition: TTree.h:70
#define gDirectory
Definition: TDirectory.h:213
Double_t Sqrt(Double_t x)
Definition: TMath.h:590
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5798