Logo ROOT   6.14/05
Reference Guide
ProofStdVect.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_ProofStdVec
3 ///
4 /// Selector for generic processing with stdlib collections
5 ///
6 /// \macro_code
7 ///
8 /// \author Gerardo Ganis (gerardo.ganis@cern.ch)
9 
10 #define ProofStdVect_cxx
11 
12 #include "ProofStdVect.h"
13 #include <TMath.h>
14 #include <TTree.h>
15 #include <TRandom3.h>
16 #include <TROOT.h>
17 #include <TString.h>
18 #include <TSystem.h>
19 #include <TFile.h>
20 #include <TProofOutputFile.h>
21 #include <TCanvas.h>
22 #include <TH1F.h>
23 
24 //_____________________________________________________________________________
25 ProofStdVect::ProofStdVect()
26 {
27  // Constructor
28 
29  fCreate = kFALSE;
30  fTree = 0;
31  fFile = 0;
32  fProofFile = 0;
33  fRandom = 0;
34  fHgood = 0;
35  fHbad = 0;
36 }
37 
38 //_____________________________________________________________________________
39 ProofStdVect::~ProofStdVect()
40 {
41  // Destructor
42 
43  SafeDelete(fTree);
44  SafeDelete(fFile);
45  SafeDelete(fRandom);
46 }
47 
48 //_____________________________________________________________________________
49 void ProofStdVect::Begin(TTree * /*tree*/)
50 {
51  // The Begin() function is called at the start of the query.
52  // When running with PROOF Begin() is only called on the client.
53  // The tree argument is deprecated (on PROOF 0 is passed).
54 
55  TString option = GetOption();
56 
57  // Dataset creation run?
58  if (fInput && fInput->FindObject("ProofStdVect_Create")) {
59  fCreate = kTRUE;
60  } else if (option.Contains("create")) {
61  fCreate = kTRUE;
62  }
63 }
64 
65 //_____________________________________________________________________________
66 void ProofStdVect::SlaveBegin(TTree * /*tree*/)
67 {
68  // The SlaveBegin() function is called after the Begin() function.
69  // When running with PROOF SlaveBegin() is called on each slave server.
70  // The tree argument is deprecated (on PROOF 0 is passed).
71 
72  TString option = GetOption();
73 
74  // Dataset creation run?
75  if (fInput && fInput->FindObject("ProofStdVect_Create")) {
76  fCreate = kTRUE;
77  } else if (option.Contains("create")) {
78  fCreate = kTRUE;
79  }
80 
81  // If yes, create the output file ...
82  if (fCreate) {
83  // Just create the object
85  fProofFile = new TProofOutputFile("ProofStdVect.root",
86  TProofOutputFile::kDataset, opt, "TestStdVect");
87 
88  // Open the file
89  fFile = fProofFile->OpenFile("RECREATE");
90  if (fFile && fFile->IsZombie()) SafeDelete(fFile);
91 
92  // Cannot continue
93  if (!fFile) {
94  Info("SlaveBegin", "could not create '%s': instance is invalid!", fProofFile->GetName());
95  return;
96  }
97 
98  // Create a TTree
99  fTree = new TTree("stdvec", "Tree with std vector");
100  fTree->Branch("Vb",&fVb);
101  fTree->Branch("Vfx",&fVfx);
102  fTree->Branch("Vfy",&fVfy);
103  // File resident
104  fTree->SetDirectory(fFile);
105  fTree->AutoSave();
106 
107  // Init the random generator
108  fRandom = new TRandom3(0);
109 
110  } else {
111  // Create two histograms
112  fHgood = new TH1F("Hgood", "Good hits", 100., -2.5, 2.5);
113  fHbad = new TH1F("Hbad", "Bad hits", 100., -6., 6.);
114  fOutput->Add(fHgood);
115  fOutput->Add(fHbad);
116  }
117 }
118 
119 //_____________________________________________________________________________
120 Bool_t ProofStdVect::Process(Long64_t entry)
121 {
122  // The Process() function is called for each entry in the tree (or possibly
123  // keyed object in the case of PROOF) to be processed. The entry argument
124  // specifies which entry in the currently loaded tree is to be processed.
125  // It can be passed to either ProofStdVect::GetEntry() or TBranch::GetEntry()
126  // to read either all or the required parts of the data. When processing
127  // keyed objects with PROOF, the object is already loaded and is available
128  // via the fObject pointer.
129  //
130  // This function should contain the "body" of the analysis. It can contain
131  // simple or elaborate selection criteria, run algorithms on the data
132  // of the event and typically fill histograms.
133  //
134  // The processing can be stopped by calling Abort().
135  //
136  // Use fStatus to set the return value of TTree::Process().
137  //
138  // The return value is currently not used.
139 
140  if (fCreate) {
141  if (!fTree) return kTRUE;
142 
143  // Number of vectors
144  Int_t nv = (Int_t) (entry % 10);
145  if (nv < 1) nv = 1;
146 
147  // Create vectors
148  for (Int_t i = 0; i < nv; i++) {
149  std::vector<bool> vb;
150  std::vector<float> vfx, vfy;
151  Int_t np = (Int_t) (entry % 100);
152  if (np < 1) np = 1;
153  for (Int_t j = 0; j < np; j++) {
154  float x = (float)j;
155  float y = 5.*x;
156  Double_t sy = (Double_t) (0.1*y);
157  Double_t ym = fRandom->Gaus((Double_t)y, sy);
158  Double_t c2 = TMath::Abs((ym - y) / sy);
159  bool xb = (1. - TMath::Erfc(c2/TMath::Sqrt(2.)) > .95) ? 0 : 1;
160  vb.push_back(xb);
161  vfx.push_back(x);
162  vfy.push_back(float(ym));
163  }
164  fVb.push_back(vb);
165  fVfx.push_back(vfx);
166  fVfy.push_back(vfy);
167  }
168 
169  // Fill the tree
170  fTree->Fill();
171 
172  // Clear the vectors
173  std::vector<std::vector<bool> >::iterator ivb;
174  for (ivb = fVb.begin(); ivb != fVb.end(); ivb++) {
175  (*ivb).clear();
176  }
177  fVb.clear();
178  std::vector<std::vector<float> >::iterator ivf;
179  for (ivf = fVfx.begin(); ivf != fVfx.end(); ivf++) {
180  (*ivf).clear();
181  }
182  fVfx.clear();
183  for (ivf = fVfy.begin(); ivf != fVfy.end(); ivf++) {
184  (*ivf).clear();
185  }
186  fVfy.clear();
187  } else {
188  // Read the entry
189  GetEntry(entry);
190  // Plot normalized values for bad and good hits
191  for (UInt_t i = 0; i < fVfyr->size(); i++) {
192  std::vector<bool> &vb = fVbr->at(i);
193  std::vector<float> &vfx = fVfxr->at(i);
194  std::vector<float> &vfy = fVfyr->at(i);
195  for (UInt_t j = 0; j < vfy.size(); j++) {
196  Double_t ny = (vfy.at(j) - 5*vfx.at(j)) / (0.1 * 5 * vfx.at(j));
197  if (vb.at(j) < 0.5)
198  fHbad->Fill(ny);
199  else
200  fHgood->Fill(ny);
201  }
202  }
203  }
204 
205  return kTRUE;
206 }
207 
208 //_____________________________________________________________________________
209 void ProofStdVect::SlaveTerminate()
210 {
211  // The SlaveTerminate() function is called after all entries or objects
212  // have been processed. When running with PROOF SlaveTerminate() is called
213  // on each slave server.
214 
215  // Nothing to do in read mode
216  if (!fCreate) return;
217 
218  // Write the ntuple to the file
219  if (fFile) {
220  if (!fTree) {
221  Error("SlaveTerminate", "'tree' is undefined!");
222  return;
223  }
224  Bool_t cleanup = kFALSE;
226  if (fTree->GetEntries() > 0) {
227  fFile->cd();
228  fTree->Write();
229  fProofFile->Print();
230  fOutput->Add(fProofFile);
231  } else {
232  cleanup = kTRUE;
233  }
234  fTree->SetDirectory(0);
235  fFile->Close();
236  // Cleanup, if needed
237  if (cleanup) {
238  TUrl uf(*(fFile->GetEndpointUrl()));
239  SafeDelete(fFile);
240  gSystem->Unlink(uf.GetFile());
241  SafeDelete(fProofFile);
242  }
243  }
244 }
245 
246 //_____________________________________________________________________________
247 void ProofStdVect::Terminate()
248 {
249  // The Terminate() function is the last function to be called during
250  // a query. It always runs on the client, it can be used to present
251  // the results graphically or save the results to file.
252 
253  // Nothing to do in create mode
254  if (fCreate) return;
255 
256  // Create a canvas, with 2 pads
257  TCanvas *c1 = new TCanvas("cvstdvec", "Test StdVec", 800,10,700,780);
258  c1->Divide(1,2);
259  TPad *pad1 = (TPad *) c1->GetPad(1);
260  TPad *pad2 = (TPad *) c1->GetPad(2);
261  pad2->cd();
262  if (fHbad) fHbad->Draw();
263  pad1->cd();
264  if (fHgood) fHgood->Draw();
265  c1->cd();
266  c1->Update();
267 }
268 
269 //_____________________________________________________________________________
271 {
272  // The Init() function is called when the selector needs to initialize
273  // a new tree or chain. Typically here the branch addresses and branch
274  // pointers of the tree will be set.
275  // It is normally not necessary to make changes to the generated
276  // code, but the routine can be extended by the user if needed.
277  // Init() will be called many times when running on PROOF
278  // (once per file to be processed).
279 
280  // Nothing to do in create mode
281  if (fCreate) return;
282 
283  // Set object pointer
284  fVbr = 0;
285  fVfxr = 0;
286  fVfyr = 0;
287  // Set branch addresses and branch pointers
288  if (!tree) return;
289  fChain = tree;
290  fChain->SetMakeClass(1);
291 
292  fChain->SetBranchAddress("Vb", &fVbr, &b_Vb);
293  fChain->SetBranchAddress("Vfx", &fVfxr, &b_Vfx);
294  fChain->SetBranchAddress("Vfy", &fVfyr, &b_Vfy);
295 }
296 
297 
298 //_____________________________________________________________________________
299 Bool_t ProofStdVect::Notify()
300 {
301  // The Notify() function is called when a new file is opened. This
302  // can be either for a new TTree in a TChain or when when a new TTree
303  // is started when using PROOF. It is normally not necessary to make changes
304  // to the generated code, but the routine can be extended by the
305  // user if needed. The return value is currently not used.
306 
307  // Nothing to do in create mode
308  if (fCreate) return kTRUE;
309  Info("Notify","processing file: %s",fChain->GetCurrentFile()->GetName());
310 
311  return kTRUE;
312 }
void Begin(Int_t type)
Random number generator class based on M.
Definition: TRandom3.h:27
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:2864
Selector for generic processing with stdlib collections.
virtual void Draw(Option_t *option="")=0
Default Draw method for all objects.
return c1
Definition: legend1.C:41
This class represents a WWW compatible URL.
Definition: TUrl.h:35
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Short_t Abs(Short_t d)
Definition: TMathBase.h:108
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:594
Double_t x[n]
Definition: legend1.C:17
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:121
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
Double_t Erfc(Double_t x)
Compute the complementary error function erfc(x).
Definition: TMath.cxx:194
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
unsigned int UInt_t
Definition: RtypesCore.h:42
The most important graphics class in the ROOT system.
Definition: TPad.h:29
Class to steer the merging of files produced on the workers.
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define SafeDelete(p)
Definition: RConfig.h:529
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
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:1162
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70
Double_t Sqrt(Double_t x)
Definition: TMath.h:690
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
const Bool_t kTRUE
Definition: RtypesCore.h:87