Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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//_____________________________________________________________________________
25ProofStdVect::ProofStdVect()
26{
27 // Constructor
28
29 fCreate = kFALSE;
30 fTree = nullptr;
31 fFile = nullptr;
32 fProofFile = nullptr;
33 fRandom = nullptr;
34 fHgood = nullptr;
35 fHbad = nullptr;
36}
37
38//_____________________________________________________________________________
39ProofStdVect::~ProofStdVect()
40{
41 // Destructor
42
43 SafeDelete(fTree);
44 SafeDelete(fFile);
45 SafeDelete(fRandom);
46}
47
48//_____________________________________________________________________________
49void 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//_____________________________________________________________________________
66void 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//_____________________________________________________________________________
120Bool_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) ? false : true;
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//_____________________________________________________________________________
209void 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(nullptr);
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//_____________________________________________________________________________
247void 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//_____________________________________________________________________________
270void ProofStdVect::Init(TTree *tree)
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 = nullptr;
285 fVfxr = nullptr;
286 fVfyr = nullptr;
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//_____________________________________________________________________________
299Bool_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}
Selector for generic processing with stdlib collections.
#define SafeDelete(p)
Definition RConfig.hxx:542
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:218
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
The Canvas class.
Definition TCanvas.h:23
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
The most important graphics class in the ROOT system.
Definition TPad.h:28
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:640
TVirtualPad * GetPad(Int_t subpadnumber) const override
Get a pointer to subpadnumber of this pad.
Definition TPad.cxx:2947
Class to steer the merging of files produced on the workers.
Random number generator class based on M.
Definition TRandom3.h:27
Basic string class.
Definition TString.h:139
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1381
A TTree represents a columnar dataset.
Definition TTree.h:79
This class represents a WWW compatible URL.
Definition TUrl.h:33
void Draw(Option_t *option="") override=0
Default Draw method for all objects.
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
return c2
Definition legend2.C:14
Double_t Erfc(Double_t x)
Computes the complementary error function erfc(x).
Definition TMath.cxx:199
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123