ROOT logo

From $ROOTSYS/tutorials/proof/ProofAux.C

#define ProofAux_cxx

//////////////////////////////////////////////////////////////
//
// Selector used for auxilliary actions in the PROOF tutorials
//
//////////////////////////////////////////////////////////////

#include "ProofAux.h"
#include "TDSet.h"
#include "TProofServ.h"
#include "TMap.h"
#include "TString.h"
#include "TSystem.h"
#include "TParameter.h"
#include "TFile.h"
#include "TUrl.h"
#include "TTree.h"
#include "TRandom.h"
#include "TMath.h"

//_____________________________________________________________________________
ProofAux::ProofAux()
{
   // Constructor

   fAction = -1;
   fNEvents= -1;
   fMainList = 0;
   fFriendList = 0;
}

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

}

//_____________________________________________________________________________
Int_t ProofAux::GetAction(TList *input)
{
   // Get the required action.
   // Returns -1 if unknown.

   Int_t action = -1;
   // Determine the test type
   TNamed *ntype = dynamic_cast<TNamed*>(input->FindObject("ProofAux_Action"));
   if (ntype) {
      if (!strcmp(ntype->GetTitle(), "GenerateTrees")) {
         action = 0;
      } else if (!strcmp(ntype->GetTitle(), "GenerateTreesSameFile")) {
         action = 1;
      } else {
         Warning("GetAction", "unknown action: '%s'", ntype->GetTitle());
      }
   }
   // Done
   return action;
}


//_____________________________________________________________________________
void ProofAux::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();

   // Determine the action type
   fAction = GetAction(fInput);
}

//_____________________________________________________________________________
void ProofAux::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();

   // Determine the action type
   fAction = GetAction(fInput);

   // Get the number of events
   TParameter<Long64_t> *a = (TParameter<Long64_t> *) fInput->FindObject("ProofAux_NEvents");
   if (a) fNEvents = a->GetVal();

   // Create lists
   fMainList = new TList;
   if (gProofServ) fMainList->SetName(TString::Format("MainList-%s", gProofServ->GetOrdinal()));
   fFriendList = new TList;
   if (gProofServ) fFriendList->SetName(TString::Format("FriendList-%s", gProofServ->GetOrdinal()));
}

//_____________________________________________________________________________
Bool_t ProofAux::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 ProofAux::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.

   // Nothing to do if the action if not defined
   if (fAction < 0) {
      Error("Process", "action not specified!");
      return kFALSE;
   }

   // Link to current element, if any
   TDSetElement *fCurrent = 0;
   TPair *elemPair = 0;
   if (fInput && (elemPair = dynamic_cast<TPair *>(fInput->FindObject("PROOF_CurrentElement")))) {
      if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
         Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
      } else {
         Error("Process", "entry %lld: no file specified!", entry);
         return kFALSE;
      }
   }

   // Act now
   if (fAction == 0) {
      TString fnt;
      // Generate the TTree and save it in the specified file
      if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
         Error("Process", "problems generating tree (%lld, %s, %lld)",
                          entry, fCurrent->GetName(), fNEvents);
         return kFALSE;
      }
      // The output filename
      TString fnf(fnt);
      TString xf = gSystem->BaseName(fnf);
      fnf = gSystem->DirName(fnf);
      if (xf.Contains("tree")) {
         xf.ReplaceAll("tree", "friend");
      } else {
         if (xf.EndsWith(".root")) {
            xf.ReplaceAll(".root", "_friend.root");
         } else {
            xf += "_friend";
         }
      }
      fnf += TString::Format("/%s", xf.Data());
      // Generate the TTree friend and save it in the specified file
      if (GenerateFriend(fnt, fnf) != 0) {
         Error("Process", "problems generating friend tree for %s (%s)",
                          fCurrent->GetName(), fnt.Data());
         return kFALSE;
      }
   } else if (fAction == 1) {
      TString fnt;
      // Generate the TTree and save it in the specified file
      if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
         Error("Process", "problems generating tree (%lld, %s, %lld)",
                          entry, fCurrent->GetName(), fNEvents);
         return kFALSE;
      }
      // Generate the TTree friend and save it in the specified file
      if (GenerateFriend(fnt) != 0) {
         Error("Process", "problems generating friend tree for %s (%s)",
                          fCurrent->GetName(), fnt.Data());
         return kFALSE;
      }
   } else {
      // Unknown action
      Warning("Process", "do not know how to process action %d - do nothing", fAction);
      return kFALSE;
   }

   return kTRUE;
}

//_____________________________________________________________________________
void ProofAux::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.

   if (fMainList && fMainList->GetSize() > 0) fOutput->Add(fMainList);
   if (fFriendList && fFriendList->GetSize() > 0) fOutput->Add(fFriendList);
}

//_____________________________________________________________________________
void ProofAux::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.

}

//_____________________________________________________________________________
Int_t ProofAux::GenerateTree(const char *fnt, Long64_t ent, TString &fn)
{
   // Generate the main tree for the 'friends' tutorial; the tree is called
   // 'Tmain', has 'ent' entries and is saved to file 'fnt'.
   // The full file path is returned in 'fn'.
   // Return 0 on success, -1 on error.

   Int_t rc = -1;

   // Check the filename
   fn = fnt;
   if (fn.IsNull()) {
      Error("GenerateTree", "file name undefined!");
      return rc;
   }
   TUrl uu(fn, kTRUE);
   if (!strcmp(uu.GetProtocol(), "file") && !fn.BeginsWith("/")) {
      // Local file with relative path: create under the data directory
      if (!gProofServ ||
          !(gProofServ->GetDataDir()) || strlen(gProofServ->GetDataDir()) <= 0) {
         Error("GenerateTree", "data directory undefined!");
         return rc;
      }
      // Insert data directory
      fn.Insert(0, TString::Format("%s/", gProofServ->GetDataDir()));
      // Make sure the directory exists
      TString dir = gSystem->DirName(fn);
      if (gSystem->AccessPathName(dir, kWritePermission)) {
         if (gSystem->mkdir(dir, kTRUE) != 0) {
            Error("GenerateTree", "problems creating directory %s to store the file", dir.Data());
            return rc;
         }
      }
   }

   // Create the file
   TDirectory* savedir = gDirectory;
   TFile *f = new TFile(fn, "RECREATE");
   if (!f || f->IsZombie()) {
      Error("GenerateTree", "problems opening file %s", fn.Data());
      return rc;
   }
   savedir->cd();
   rc = 0;

   // Create the tree
   TTree *T = new TTree("Tmain","Main tree for tutorial friends");
   T->SetDirectory(f);
   Int_t Run = 1;
   T->Branch("Run",&Run,"Run/I");
   Long64_t Event = 0;
   T->Branch("Event",&Event,"Event/L");
   Float_t x = 0., y = 0., z = 0.;
   T->Branch("x",&x,"x/F");
   T->Branch("y",&y,"y/F");
   T->Branch("z",&z,"z/F");
   TRandom r;
   for (Long64_t i = 0; i < ent; i++) {
      if (i > 0 && i%1000 == 0) Run++;
      Event = i;
      x = r.Gaus(10,1);
      y = r.Gaus(20,2);
      z = r.Landau(2,1);
      T->Fill();
   }
   T->Print();
   f->cd();
   T->Write();
   T->SetDirectory(0);
   f->Close();
   delete f;
   delete T;

   // Notify success
   Info("GenerateTree", "file '%s' successfully created", fn.Data());

   // Add to the list
   TString fds(fn);
   if (!strcmp(uu.GetProtocol(), "file"))
      fds.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
   fMainList->Add(new TObjString(fds));

   // Done
   return rc;
}

//_____________________________________________________________________________
Int_t ProofAux::GenerateFriend(const char *fnt, const char *fnf)
{
   // Generate the friend tree for the main tree in the 'friends' tutorial fetched
   // from 'fnt'.
   // the tree is called 'Tfriend', has the same number of entries as the main
   // tree and is saved to file 'fnf'. If 'fnf' is not defined the filename is
   // derived from 'fnt' either replacing 'tree' with 'friend', or adding '_friend'
   // before the '.root' extension.
   // Return 0 on success, -1 on error.

   Int_t rc = -1;
   // Check the input filename
   TString fin(fnt);
   if (fin.IsNull()) {
      Error("GenerateFriend", "file name for the main tree undefined!");
      return rc;
   }
   // Make sure that the file can be read
   if (gSystem->AccessPathName(fin, kReadPermission)) {
      Error("GenerateFriend", "input file does not exist or cannot be read: %s", fin.Data());
      return rc;
   }

   // File handlers
   Bool_t sameFile = kTRUE;
   const char *openMain = "UPDATE";

   // The output filename
   TString fout(fnf);
   if (!fout.IsNull()) {
      sameFile = kFALSE;
      openMain = "READ";
      // Make sure the directory exists
      TString dir = gSystem->DirName(fout);
      if (gSystem->AccessPathName(dir, kWritePermission)) {
         if (gSystem->mkdir(dir, kTRUE) != 0) {
            Error("GenerateFriend", "problems creating directory %s to store the file", dir.Data());
            return rc;
         }
      }
   } else {
      // We set the same name
      fout = fin;
   }

   // Get main tree
   TFile *fi = TFile::Open(fin, openMain);
   if (!fi || fi->IsZombie()) {
      Error("GenerateFriend", "problems opening input file %s", fin.Data());
      return rc;
   }
   TTree *Tin = (TTree *) fi->Get("Tmain");
   if (!Tin) {
      Error("GenerateFriend", "problems getting tree 'Tmain' from file %s", fin.Data());
      delete fi;
      return rc;
   }
   // Set branches
   Float_t x, y, z;
   Tin->SetBranchAddress("x", &x);
   Tin->SetBranchAddress("y", &y);
   Tin->SetBranchAddress("z", &z);
   TBranch *b_x = Tin->GetBranch("x");
   TBranch *b_y = Tin->GetBranch("y");
   TBranch *b_z = Tin->GetBranch("z");

   TDirectory* savedir = gDirectory;
   // Create output file
   TFile *fo = 0;
   if (!sameFile) {
      fo = new TFile(fout, "RECREATE");
      if (!fo || fo->IsZombie()) {
         Error("GenerateFriend", "problems opening file %s", fout.Data());
         delete fi;
         return rc;
      }
      savedir->cd();
   } else {
      // Same file
      fo = fi;
   }
   rc = 0;

   // Create the tree
   TTree *Tfrnd = new TTree("Tfrnd", "Friend tree for tutorial 'friends'");
   Tfrnd->SetDirectory(fo);
   Float_t r = 0;
   Tfrnd->Branch("r",&x,"r/F");
   Long64_t ent = Tin->GetEntries();
   for (Long64_t i = 0; i < ent; i++) {
      b_x->GetEntry(i);
      b_y->GetEntry(i);
      b_z->GetEntry(i);
      r = TMath::Sqrt(x*x + y*y + z*z);
      Tfrnd->Fill();
   }
   if (!sameFile) {
      fi->Close();
      delete fi;
   }
   Tfrnd->Print();
   fo->cd();
   Tfrnd->Write();
   Tfrnd->SetDirectory(0);
   fo->Close();
   delete fo;
   delete Tfrnd;

   // Notify success
   Info("GenerateFriend", "friend file '%s' successfully created", fout.Data());

   // Add to the list
   TUrl uu(fout);
   if (!strcmp(uu.GetProtocol(), "file"))
      fout.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
   fFriendList->Add(new TObjString(fout));

   // Done
   return rc;
}
 ProofAux.C:1
 ProofAux.C:2
 ProofAux.C:3
 ProofAux.C:4
 ProofAux.C:5
 ProofAux.C:6
 ProofAux.C:7
 ProofAux.C:8
 ProofAux.C:9
 ProofAux.C:10
 ProofAux.C:11
 ProofAux.C:12
 ProofAux.C:13
 ProofAux.C:14
 ProofAux.C:15
 ProofAux.C:16
 ProofAux.C:17
 ProofAux.C:18
 ProofAux.C:19
 ProofAux.C:20
 ProofAux.C:21
 ProofAux.C:22
 ProofAux.C:23
 ProofAux.C:24
 ProofAux.C:25
 ProofAux.C:26
 ProofAux.C:27
 ProofAux.C:28
 ProofAux.C:29
 ProofAux.C:30
 ProofAux.C:31
 ProofAux.C:32
 ProofAux.C:33
 ProofAux.C:34
 ProofAux.C:35
 ProofAux.C:36
 ProofAux.C:37
 ProofAux.C:38
 ProofAux.C:39
 ProofAux.C:40
 ProofAux.C:41
 ProofAux.C:42
 ProofAux.C:43
 ProofAux.C:44
 ProofAux.C:45
 ProofAux.C:46
 ProofAux.C:47
 ProofAux.C:48
 ProofAux.C:49
 ProofAux.C:50
 ProofAux.C:51
 ProofAux.C:52
 ProofAux.C:53
 ProofAux.C:54
 ProofAux.C:55
 ProofAux.C:56
 ProofAux.C:57
 ProofAux.C:58
 ProofAux.C:59
 ProofAux.C:60
 ProofAux.C:61
 ProofAux.C:62
 ProofAux.C:63
 ProofAux.C:64
 ProofAux.C:65
 ProofAux.C:66
 ProofAux.C:67
 ProofAux.C:68
 ProofAux.C:69
 ProofAux.C:70
 ProofAux.C:71
 ProofAux.C:72
 ProofAux.C:73
 ProofAux.C:74
 ProofAux.C:75
 ProofAux.C:76
 ProofAux.C:77
 ProofAux.C:78
 ProofAux.C:79
 ProofAux.C:80
 ProofAux.C:81
 ProofAux.C:82
 ProofAux.C:83
 ProofAux.C:84
 ProofAux.C:85
 ProofAux.C:86
 ProofAux.C:87
 ProofAux.C:88
 ProofAux.C:89
 ProofAux.C:90
 ProofAux.C:91
 ProofAux.C:92
 ProofAux.C:93
 ProofAux.C:94
 ProofAux.C:95
 ProofAux.C:96
 ProofAux.C:97
 ProofAux.C:98
 ProofAux.C:99
 ProofAux.C:100
 ProofAux.C:101
 ProofAux.C:102
 ProofAux.C:103
 ProofAux.C:104
 ProofAux.C:105
 ProofAux.C:106
 ProofAux.C:107
 ProofAux.C:108
 ProofAux.C:109
 ProofAux.C:110
 ProofAux.C:111
 ProofAux.C:112
 ProofAux.C:113
 ProofAux.C:114
 ProofAux.C:115
 ProofAux.C:116
 ProofAux.C:117
 ProofAux.C:118
 ProofAux.C:119
 ProofAux.C:120
 ProofAux.C:121
 ProofAux.C:122
 ProofAux.C:123
 ProofAux.C:124
 ProofAux.C:125
 ProofAux.C:126
 ProofAux.C:127
 ProofAux.C:128
 ProofAux.C:129
 ProofAux.C:130
 ProofAux.C:131
 ProofAux.C:132
 ProofAux.C:133
 ProofAux.C:134
 ProofAux.C:135
 ProofAux.C:136
 ProofAux.C:137
 ProofAux.C:138
 ProofAux.C:139
 ProofAux.C:140
 ProofAux.C:141
 ProofAux.C:142
 ProofAux.C:143
 ProofAux.C:144
 ProofAux.C:145
 ProofAux.C:146
 ProofAux.C:147
 ProofAux.C:148
 ProofAux.C:149
 ProofAux.C:150
 ProofAux.C:151
 ProofAux.C:152
 ProofAux.C:153
 ProofAux.C:154
 ProofAux.C:155
 ProofAux.C:156
 ProofAux.C:157
 ProofAux.C:158
 ProofAux.C:159
 ProofAux.C:160
 ProofAux.C:161
 ProofAux.C:162
 ProofAux.C:163
 ProofAux.C:164
 ProofAux.C:165
 ProofAux.C:166
 ProofAux.C:167
 ProofAux.C:168
 ProofAux.C:169
 ProofAux.C:170
 ProofAux.C:171
 ProofAux.C:172
 ProofAux.C:173
 ProofAux.C:174
 ProofAux.C:175
 ProofAux.C:176
 ProofAux.C:177
 ProofAux.C:178
 ProofAux.C:179
 ProofAux.C:180
 ProofAux.C:181
 ProofAux.C:182
 ProofAux.C:183
 ProofAux.C:184
 ProofAux.C:185
 ProofAux.C:186
 ProofAux.C:187
 ProofAux.C:188
 ProofAux.C:189
 ProofAux.C:190
 ProofAux.C:191
 ProofAux.C:192
 ProofAux.C:193
 ProofAux.C:194
 ProofAux.C:195
 ProofAux.C:196
 ProofAux.C:197
 ProofAux.C:198
 ProofAux.C:199
 ProofAux.C:200
 ProofAux.C:201
 ProofAux.C:202
 ProofAux.C:203
 ProofAux.C:204
 ProofAux.C:205
 ProofAux.C:206
 ProofAux.C:207
 ProofAux.C:208
 ProofAux.C:209
 ProofAux.C:210
 ProofAux.C:211
 ProofAux.C:212
 ProofAux.C:213
 ProofAux.C:214
 ProofAux.C:215
 ProofAux.C:216
 ProofAux.C:217
 ProofAux.C:218
 ProofAux.C:219
 ProofAux.C:220
 ProofAux.C:221
 ProofAux.C:222
 ProofAux.C:223
 ProofAux.C:224
 ProofAux.C:225
 ProofAux.C:226
 ProofAux.C:227
 ProofAux.C:228
 ProofAux.C:229
 ProofAux.C:230
 ProofAux.C:231
 ProofAux.C:232
 ProofAux.C:233
 ProofAux.C:234
 ProofAux.C:235
 ProofAux.C:236
 ProofAux.C:237
 ProofAux.C:238
 ProofAux.C:239
 ProofAux.C:240
 ProofAux.C:241
 ProofAux.C:242
 ProofAux.C:243
 ProofAux.C:244
 ProofAux.C:245
 ProofAux.C:246
 ProofAux.C:247
 ProofAux.C:248
 ProofAux.C:249
 ProofAux.C:250
 ProofAux.C:251
 ProofAux.C:252
 ProofAux.C:253
 ProofAux.C:254
 ProofAux.C:255
 ProofAux.C:256
 ProofAux.C:257
 ProofAux.C:258
 ProofAux.C:259
 ProofAux.C:260
 ProofAux.C:261
 ProofAux.C:262
 ProofAux.C:263
 ProofAux.C:264
 ProofAux.C:265
 ProofAux.C:266
 ProofAux.C:267
 ProofAux.C:268
 ProofAux.C:269
 ProofAux.C:270
 ProofAux.C:271
 ProofAux.C:272
 ProofAux.C:273
 ProofAux.C:274
 ProofAux.C:275
 ProofAux.C:276
 ProofAux.C:277
 ProofAux.C:278
 ProofAux.C:279
 ProofAux.C:280
 ProofAux.C:281
 ProofAux.C:282
 ProofAux.C:283
 ProofAux.C:284
 ProofAux.C:285
 ProofAux.C:286
 ProofAux.C:287
 ProofAux.C:288
 ProofAux.C:289
 ProofAux.C:290
 ProofAux.C:291
 ProofAux.C:292
 ProofAux.C:293
 ProofAux.C:294
 ProofAux.C:295
 ProofAux.C:296
 ProofAux.C:297
 ProofAux.C:298
 ProofAux.C:299
 ProofAux.C:300
 ProofAux.C:301
 ProofAux.C:302
 ProofAux.C:303
 ProofAux.C:304
 ProofAux.C:305
 ProofAux.C:306
 ProofAux.C:307
 ProofAux.C:308
 ProofAux.C:309
 ProofAux.C:310
 ProofAux.C:311
 ProofAux.C:312
 ProofAux.C:313
 ProofAux.C:314
 ProofAux.C:315
 ProofAux.C:316
 ProofAux.C:317
 ProofAux.C:318
 ProofAux.C:319
 ProofAux.C:320
 ProofAux.C:321
 ProofAux.C:322
 ProofAux.C:323
 ProofAux.C:324
 ProofAux.C:325
 ProofAux.C:326
 ProofAux.C:327
 ProofAux.C:328
 ProofAux.C:329
 ProofAux.C:330
 ProofAux.C:331
 ProofAux.C:332
 ProofAux.C:333
 ProofAux.C:334
 ProofAux.C:335
 ProofAux.C:336
 ProofAux.C:337
 ProofAux.C:338
 ProofAux.C:339
 ProofAux.C:340
 ProofAux.C:341
 ProofAux.C:342
 ProofAux.C:343
 ProofAux.C:344
 ProofAux.C:345
 ProofAux.C:346
 ProofAux.C:347
 ProofAux.C:348
 ProofAux.C:349
 ProofAux.C:350
 ProofAux.C:351
 ProofAux.C:352
 ProofAux.C:353
 ProofAux.C:354
 ProofAux.C:355
 ProofAux.C:356
 ProofAux.C:357
 ProofAux.C:358
 ProofAux.C:359
 ProofAux.C:360
 ProofAux.C:361
 ProofAux.C:362
 ProofAux.C:363
 ProofAux.C:364
 ProofAux.C:365
 ProofAux.C:366
 ProofAux.C:367
 ProofAux.C:368
 ProofAux.C:369
 ProofAux.C:370
 ProofAux.C:371
 ProofAux.C:372
 ProofAux.C:373
 ProofAux.C:374
 ProofAux.C:375
 ProofAux.C:376
 ProofAux.C:377
 ProofAux.C:378
 ProofAux.C:379
 ProofAux.C:380
 ProofAux.C:381
 ProofAux.C:382
 ProofAux.C:383
 ProofAux.C:384
 ProofAux.C:385
 ProofAux.C:386
 ProofAux.C:387
 ProofAux.C:388
 ProofAux.C:389
 ProofAux.C:390
 ProofAux.C:391
 ProofAux.C:392
 ProofAux.C:393
 ProofAux.C:394
 ProofAux.C:395
 ProofAux.C:396
 ProofAux.C:397
 ProofAux.C:398
 ProofAux.C:399
 ProofAux.C:400
 ProofAux.C:401
 ProofAux.C:402
 ProofAux.C:403
 ProofAux.C:404
 ProofAux.C:405
 ProofAux.C:406
 ProofAux.C:407
 ProofAux.C:408
 ProofAux.C:409
 ProofAux.C:410
 ProofAux.C:411
 ProofAux.C:412
 ProofAux.C:413
 ProofAux.C:414
 ProofAux.C:415
 ProofAux.C:416
 ProofAux.C:417
 ProofAux.C:418