From $ROOTSYS/tutorials/proof/ProofStdVect.C

#define ProofStdVect_cxx

//////////////////////////////////////////////////////////
//
// Example of TSelector implementation to do generic
// processing with stdlib collections.
// See tutorials/proof/runProof.C, option "stdlib", for an
// example of how to run this selector.
//
//////////////////////////////////////////////////////////

#include "ProofStdVect.h"
#include <TMath.h>
#include <TTree.h>
#include <TRandom3.h>
#include <TROOT.h>
#include <TString.h>
#include <TSystem.h>
#include <TFile.h>
#include <TProofOutputFile.h>
#include <TCanvas.h>
#include <TH1F.h>

//_____________________________________________________________________________
ProofStdVect::ProofStdVect()
{
   // Constructor

   fCreate = kFALSE;
   fTree = 0;
   fFile = 0;
   fProofFile = 0;
   fRandom = 0;
   fHgood = 0;
   fHbad = 0;
}

//_____________________________________________________________________________
ProofStdVect::~ProofStdVect()
{
   // Destructor

   SafeDelete(fTree);
   SafeDelete(fFile);
   SafeDelete(fRandom);
}

//_____________________________________________________________________________
void ProofStdVect::Begin(TTree * /*tree*/)
{
   // The Begin() function is called at the start of the query.
   // When running with PROOF Begin() is only called on the client.
   // The tree argument is deprecated (on PROOF 0 is passed).

   TString option = GetOption();

   // Dataset creation run?
   if (fInput && fInput->FindObject("ProofStdVect_Create")) {
      fCreate = kTRUE;
   } else if (option.Contains("create")) {
      fCreate = kTRUE;
   }
}

//_____________________________________________________________________________
void ProofStdVect::SlaveBegin(TTree * /*tree*/)
{
   // The SlaveBegin() function is called after the Begin() function.
   // When running with PROOF SlaveBegin() is called on each slave server.
   // The tree argument is deprecated (on PROOF 0 is passed).

   TString option = GetOption();

   // Dataset creation run?
   if (fInput && fInput->FindObject("ProofStdVect_Create")) {
      fCreate = kTRUE;
   } else if (option.Contains("create")) {
      fCreate = kTRUE;
   }

   // If yes, create the output file ...
   if (fCreate) {
      // Just create the object
      UInt_t opt = TProofOutputFile::kRegister | TProofOutputFile::kOverwrite | TProofOutputFile::kVerify;
      fProofFile = new TProofOutputFile("ProofStdVect.root",
                                        TProofOutputFile::kDataset, opt, "TestStdVect");

      // Open the file
      fFile = fProofFile->OpenFile("RECREATE");
      if (fFile && fFile->IsZombie()) SafeDelete(fFile);

      // Cannot continue
      if (!fFile) {
         Info("SlaveBegin", "could not create '%s': instance is invalid!", fProofFile->GetName());
         return;
      }

      // Create a TTree
      fTree = new TTree("stdvec", "Tree with std vector");
      fTree->Branch("Vb",&fVb);
      fTree->Branch("Vfx",&fVfx);
      fTree->Branch("Vfy",&fVfy);
      // File resident
      fTree->SetDirectory(fFile);
      fTree->AutoSave();

      // Init the random generator
      fRandom = new TRandom3(0);
      
   } else {
      // Create two histograms
      fHgood = new TH1F("Hgood", "Good hits", 100., -2.5, 2.5);
      fHbad = new TH1F("Hbad", "Bad hits", 100., -6., 6.);
      fOutput->Add(fHgood);
      fOutput->Add(fHbad);
   }
}

//_____________________________________________________________________________
Bool_t ProofStdVect::Process(Long64_t entry)
{
   // The Process() function is called for each entry in the tree (or possibly
   // keyed object in the case of PROOF) to be processed. The entry argument
   // specifies which entry in the currently loaded tree is to be processed.
   // It can be passed to either ProofStdVect::GetEntry() or TBranch::GetEntry()
   // to read either all or the required parts of the data. When processing
   // keyed objects with PROOF, the object is already loaded and is available
   // via the fObject pointer.
   //
   // This function should contain the "body" of the analysis. It can contain
   // simple or elaborate selection criteria, run algorithms on the data
   // of the event and typically fill histograms.
   //
   // The processing can be stopped by calling Abort().
   //
   // Use fStatus to set the return value of TTree::Process().
   //
   // The return value is currently not used.

   if (fCreate) {
      if (!fTree) return kTRUE;
   
      // Number of vectors
      Int_t nv =  (Int_t) (entry % 10);
      if (nv < 1) nv = 1;

      // Create vectors
      for (Int_t i = 0; i < nv; i++) {
         std::vector<bool> vb;
         std::vector<float> vfx, vfy;
         Int_t np =  (Int_t) (entry % 100);
         if (np < 1) np = 1;
         for (Int_t j = 0; j < np; j++) {
            float x = (float)j;
            float y = 5.*x;
            Double_t sy = (Double_t) (0.1*y);
            Double_t ym = fRandom->Gaus((Double_t)y, sy);
            Double_t c2 = TMath::Abs((ym - y) / sy);
            bool xb = (1. - TMath::Erfc(c2/TMath::Sqrt(2.)) > .95) ? 0 : 1;
            vb.push_back(xb);
            vfx.push_back(x);
            vfy.push_back(float(ym));
         }
         fVb.push_back(vb);
         fVfx.push_back(vfx);
         fVfy.push_back(vfy);
      }

      // Fill the tree
      fTree->Fill();
      
      // Clear the vectors
      std::vector<std::vector<bool> >::iterator ivb;
      for (ivb = fVb.begin(); ivb != fVb.end(); ivb++) {
         (*ivb).clear();
      }
      fVb.clear();
      std::vector<std::vector<float> >::iterator ivf;
      for (ivf = fVfx.begin(); ivf != fVfx.end(); ivf++) {
         (*ivf).clear();
      }
      fVfx.clear();
      for (ivf = fVfy.begin(); ivf != fVfy.end(); ivf++) {
         (*ivf).clear();
      }
      fVfy.clear();
   } else {
      // Read the entry
      GetEntry(entry);
      // Plot normalized values for bad and good hits
      for (UInt_t i = 0; i < fVfyr->size(); i++) {
         std::vector<bool> &vb = fVbr->at(i);
         std::vector<float> &vfx = fVfxr->at(i);
         std::vector<float> &vfy = fVfyr->at(i);
         for (UInt_t j = 0; j < vfy.size(); j++) {
            Double_t ny = (vfy.at(j) - 5*vfx.at(j)) / (0.1 * 5 * vfx.at(j));
            if (vb.at(j) < 0.5)
               fHbad->Fill(ny);
            else
               fHgood->Fill(ny);            
         }
      }
   }

   return kTRUE;
}

//_____________________________________________________________________________
void ProofStdVect::SlaveTerminate()
{
   // The SlaveTerminate() function is called after all entries or objects
   // have been processed. When running with PROOF SlaveTerminate() is called
   // on each slave server.

   // Nothing to do in read mode
   if (!fCreate) return;
   
   // Write the ntuple to the file
   if (fFile) {
      if (!fTree) {
         Error("SlaveTerminate", "'tree' is undefined!");
         return;
      }
      Bool_t cleanup = kFALSE;
      TDirectory::TContext ctx(0);
      if (fTree->GetEntries() > 0) {
         fFile->cd();
         fTree->Write();
         fProofFile->Print();
         fOutput->Add(fProofFile);
      } else {
         cleanup = kTRUE;
      }
      fTree->SetDirectory(0);
      fFile->Close();
      // Cleanup, if needed
      if (cleanup) {
         TUrl uf(*(fFile->GetEndpointUrl()));
         SafeDelete(fFile);
         gSystem->Unlink(uf.GetFile());
         SafeDelete(fProofFile);
      }
   }
}

//_____________________________________________________________________________
void ProofStdVect::Terminate()
{
   // The Terminate() function is the last function to be called during
   // a query. It always runs on the client, it can be used to present
   // the results graphically or save the results to file.

   // Nothing to do in create mode
   if (fCreate) return;

   // Create a canvas, with 2 pads
   TCanvas *c1 = new TCanvas("cvstdvec", "Test StdVec", 800,10,700,780);
   c1->Divide(1,2);
   TPad *pad1 = (TPad *) c1->GetPad(1);
   TPad *pad2 = (TPad *) c1->GetPad(2);
   pad2->cd();
   if (fHbad) fHbad->Draw();
   pad1->cd();
   if (fHgood) fHgood->Draw();
   c1->cd();
   c1->Update();
}

//_____________________________________________________________________________
void ProofStdVect::Init(TTree *tree)
{
   // The Init() function is called when the selector needs to initialize
   // a new tree or chain. Typically here the branch addresses and branch
   // pointers of the tree will be set.
   // It is normally not necessary to make changes to the generated
   // code, but the routine can be extended by the user if needed.
   // Init() will be called many times when running on PROOF
   // (once per file to be processed).

   // Nothing to do in create mode
   if (fCreate) return;

   // Set object pointer
   fVbr = 0;
   fVfxr = 0;
   fVfyr = 0;
   // Set branch addresses and branch pointers
   if (!tree) return;
   fChain = tree;
   fChain->SetMakeClass(1);

   fChain->SetBranchAddress("Vb", &fVbr, &b_Vb);
   fChain->SetBranchAddress("Vfx", &fVfxr, &b_Vfx);
   fChain->SetBranchAddress("Vfy", &fVfyr, &b_Vfy);
}


//_____________________________________________________________________________
Bool_t ProofStdVect::Notify()
{
   // The Notify() function is called when a new file is opened. This
   // can be either for a new TTree in a TChain or when when a new TTree
   // is started when using PROOF. It is normally not necessary to make changes
   // to the generated code, but the routine can be extended by the
   // user if needed. The return value is currently not used.

   // Nothing to do in create mode
   if (fCreate) return kTRUE;
   Info("Notify","processing file: %s",fChain->GetCurrentFile()->GetName());

   return kTRUE;
}
 ProofStdVect.C:1
 ProofStdVect.C:2
 ProofStdVect.C:3
 ProofStdVect.C:4
 ProofStdVect.C:5
 ProofStdVect.C:6
 ProofStdVect.C:7
 ProofStdVect.C:8
 ProofStdVect.C:9
 ProofStdVect.C:10
 ProofStdVect.C:11
 ProofStdVect.C:12
 ProofStdVect.C:13
 ProofStdVect.C:14
 ProofStdVect.C:15
 ProofStdVect.C:16
 ProofStdVect.C:17
 ProofStdVect.C:18
 ProofStdVect.C:19
 ProofStdVect.C:20
 ProofStdVect.C:21
 ProofStdVect.C:22
 ProofStdVect.C:23
 ProofStdVect.C:24
 ProofStdVect.C:25
 ProofStdVect.C:26
 ProofStdVect.C:27
 ProofStdVect.C:28
 ProofStdVect.C:29
 ProofStdVect.C:30
 ProofStdVect.C:31
 ProofStdVect.C:32
 ProofStdVect.C:33
 ProofStdVect.C:34
 ProofStdVect.C:35
 ProofStdVect.C:36
 ProofStdVect.C:37
 ProofStdVect.C:38
 ProofStdVect.C:39
 ProofStdVect.C:40
 ProofStdVect.C:41
 ProofStdVect.C:42
 ProofStdVect.C:43
 ProofStdVect.C:44
 ProofStdVect.C:45
 ProofStdVect.C:46
 ProofStdVect.C:47
 ProofStdVect.C:48
 ProofStdVect.C:49
 ProofStdVect.C:50
 ProofStdVect.C:51
 ProofStdVect.C:52
 ProofStdVect.C:53
 ProofStdVect.C:54
 ProofStdVect.C:55
 ProofStdVect.C:56
 ProofStdVect.C:57
 ProofStdVect.C:58
 ProofStdVect.C:59
 ProofStdVect.C:60
 ProofStdVect.C:61
 ProofStdVect.C:62
 ProofStdVect.C:63
 ProofStdVect.C:64
 ProofStdVect.C:65
 ProofStdVect.C:66
 ProofStdVect.C:67
 ProofStdVect.C:68
 ProofStdVect.C:69
 ProofStdVect.C:70
 ProofStdVect.C:71
 ProofStdVect.C:72
 ProofStdVect.C:73
 ProofStdVect.C:74
 ProofStdVect.C:75
 ProofStdVect.C:76
 ProofStdVect.C:77
 ProofStdVect.C:78
 ProofStdVect.C:79
 ProofStdVect.C:80
 ProofStdVect.C:81
 ProofStdVect.C:82
 ProofStdVect.C:83
 ProofStdVect.C:84
 ProofStdVect.C:85
 ProofStdVect.C:86
 ProofStdVect.C:87
 ProofStdVect.C:88
 ProofStdVect.C:89
 ProofStdVect.C:90
 ProofStdVect.C:91
 ProofStdVect.C:92
 ProofStdVect.C:93
 ProofStdVect.C:94
 ProofStdVect.C:95
 ProofStdVect.C:96
 ProofStdVect.C:97
 ProofStdVect.C:98
 ProofStdVect.C:99
 ProofStdVect.C:100
 ProofStdVect.C:101
 ProofStdVect.C:102
 ProofStdVect.C:103
 ProofStdVect.C:104
 ProofStdVect.C:105
 ProofStdVect.C:106
 ProofStdVect.C:107
 ProofStdVect.C:108
 ProofStdVect.C:109
 ProofStdVect.C:110
 ProofStdVect.C:111
 ProofStdVect.C:112
 ProofStdVect.C:113
 ProofStdVect.C:114
 ProofStdVect.C:115
 ProofStdVect.C:116
 ProofStdVect.C:117
 ProofStdVect.C:118
 ProofStdVect.C:119
 ProofStdVect.C:120
 ProofStdVect.C:121
 ProofStdVect.C:122
 ProofStdVect.C:123
 ProofStdVect.C:124
 ProofStdVect.C:125
 ProofStdVect.C:126
 ProofStdVect.C:127
 ProofStdVect.C:128
 ProofStdVect.C:129
 ProofStdVect.C:130
 ProofStdVect.C:131
 ProofStdVect.C:132
 ProofStdVect.C:133
 ProofStdVect.C:134
 ProofStdVect.C:135
 ProofStdVect.C:136
 ProofStdVect.C:137
 ProofStdVect.C:138
 ProofStdVect.C:139
 ProofStdVect.C:140
 ProofStdVect.C:141
 ProofStdVect.C:142
 ProofStdVect.C:143
 ProofStdVect.C:144
 ProofStdVect.C:145
 ProofStdVect.C:146
 ProofStdVect.C:147
 ProofStdVect.C:148
 ProofStdVect.C:149
 ProofStdVect.C:150
 ProofStdVect.C:151
 ProofStdVect.C:152
 ProofStdVect.C:153
 ProofStdVect.C:154
 ProofStdVect.C:155
 ProofStdVect.C:156
 ProofStdVect.C:157
 ProofStdVect.C:158
 ProofStdVect.C:159
 ProofStdVect.C:160
 ProofStdVect.C:161
 ProofStdVect.C:162
 ProofStdVect.C:163
 ProofStdVect.C:164
 ProofStdVect.C:165
 ProofStdVect.C:166
 ProofStdVect.C:167
 ProofStdVect.C:168
 ProofStdVect.C:169
 ProofStdVect.C:170
 ProofStdVect.C:171
 ProofStdVect.C:172
 ProofStdVect.C:173
 ProofStdVect.C:174
 ProofStdVect.C:175
 ProofStdVect.C:176
 ProofStdVect.C:177
 ProofStdVect.C:178
 ProofStdVect.C:179
 ProofStdVect.C:180
 ProofStdVect.C:181
 ProofStdVect.C:182
 ProofStdVect.C:183
 ProofStdVect.C:184
 ProofStdVect.C:185
 ProofStdVect.C:186
 ProofStdVect.C:187
 ProofStdVect.C:188
 ProofStdVect.C:189
 ProofStdVect.C:190
 ProofStdVect.C:191
 ProofStdVect.C:192
 ProofStdVect.C:193
 ProofStdVect.C:194
 ProofStdVect.C:195
 ProofStdVect.C:196
 ProofStdVect.C:197
 ProofStdVect.C:198
 ProofStdVect.C:199
 ProofStdVect.C:200
 ProofStdVect.C:201
 ProofStdVect.C:202
 ProofStdVect.C:203
 ProofStdVect.C:204
 ProofStdVect.C:205
 ProofStdVect.C:206
 ProofStdVect.C:207
 ProofStdVect.C:208
 ProofStdVect.C:209
 ProofStdVect.C:210
 ProofStdVect.C:211
 ProofStdVect.C:212
 ProofStdVect.C:213
 ProofStdVect.C:214
 ProofStdVect.C:215
 ProofStdVect.C:216
 ProofStdVect.C:217
 ProofStdVect.C:218
 ProofStdVect.C:219
 ProofStdVect.C:220
 ProofStdVect.C:221
 ProofStdVect.C:222
 ProofStdVect.C:223
 ProofStdVect.C:224
 ProofStdVect.C:225
 ProofStdVect.C:226
 ProofStdVect.C:227
 ProofStdVect.C:228
 ProofStdVect.C:229
 ProofStdVect.C:230
 ProofStdVect.C:231
 ProofStdVect.C:232
 ProofStdVect.C:233
 ProofStdVect.C:234
 ProofStdVect.C:235
 ProofStdVect.C:236
 ProofStdVect.C:237
 ProofStdVect.C:238
 ProofStdVect.C:239
 ProofStdVect.C:240
 ProofStdVect.C:241
 ProofStdVect.C:242
 ProofStdVect.C:243
 ProofStdVect.C:244
 ProofStdVect.C:245
 ProofStdVect.C:246
 ProofStdVect.C:247
 ProofStdVect.C:248
 ProofStdVect.C:249
 ProofStdVect.C:250
 ProofStdVect.C:251
 ProofStdVect.C:252
 ProofStdVect.C:253
 ProofStdVect.C:254
 ProofStdVect.C:255
 ProofStdVect.C:256
 ProofStdVect.C:257
 ProofStdVect.C:258
 ProofStdVect.C:259
 ProofStdVect.C:260
 ProofStdVect.C:261
 ProofStdVect.C:262
 ProofStdVect.C:263
 ProofStdVect.C:264
 ProofStdVect.C:265
 ProofStdVect.C:266
 ProofStdVect.C:267
 ProofStdVect.C:268
 ProofStdVect.C:269
 ProofStdVect.C:270
 ProofStdVect.C:271
 ProofStdVect.C:272
 ProofStdVect.C:273
 ProofStdVect.C:274
 ProofStdVect.C:275
 ProofStdVect.C:276
 ProofStdVect.C:277
 ProofStdVect.C:278
 ProofStdVect.C:279
 ProofStdVect.C:280
 ProofStdVect.C:281
 ProofStdVect.C:282
 ProofStdVect.C:283
 ProofStdVect.C:284
 ProofStdVect.C:285
 ProofStdVect.C:286
 ProofStdVect.C:287
 ProofStdVect.C:288
 ProofStdVect.C:289
 ProofStdVect.C:290
 ProofStdVect.C:291
 ProofStdVect.C:292
 ProofStdVect.C:293
 ProofStdVect.C:294
 ProofStdVect.C:295
 ProofStdVect.C:296
 ProofStdVect.C:297
 ProofStdVect.C:298
 ProofStdVect.C:299
 ProofStdVect.C:300
 ProofStdVect.C:301
 ProofStdVect.C:302
 ProofStdVect.C:303
 ProofStdVect.C:304
 ProofStdVect.C:305
 ProofStdVect.C:306
 ProofStdVect.C:307
 ProofStdVect.C:308
 ProofStdVect.C:309
 ProofStdVect.C:310
 ProofStdVect.C:311
 ProofStdVect.C:312
 ProofStdVect.C:313