ProofAux.C

Go to the documentation of this file.
00001 #define ProofAux_cxx
00002 
00003 //////////////////////////////////////////////////////////////
00004 //
00005 // Selector used for auxilliary actions in the PROOF tutorials
00006 //
00007 //////////////////////////////////////////////////////////////
00008 
00009 #include "ProofAux.h"
00010 #include "TDSet.h"
00011 #include "TProofServ.h"
00012 #include "TMap.h"
00013 #include "TString.h"
00014 #include "TSystem.h"
00015 #include "TParameter.h"
00016 #include "TFile.h"
00017 #include "TUrl.h"
00018 #include "TTree.h"
00019 #include "TRandom.h"
00020 #include "TMath.h"
00021 
00022 //_____________________________________________________________________________
00023 ProofAux::ProofAux()
00024 {
00025    // Constructor
00026 
00027    fAction = -1;
00028    fNEvents= -1;
00029    fMainList = 0;
00030    fFriendList = 0;
00031 }
00032 
00033 //_____________________________________________________________________________
00034 ProofAux::~ProofAux()
00035 {
00036    // Destructor
00037 
00038 }
00039 
00040 //_____________________________________________________________________________
00041 Int_t ProofAux::GetAction(TList *input)
00042 {
00043    // Get the required action.
00044    // Returns -1 if unknown.
00045 
00046    Int_t action = -1;
00047    // Determine the test type
00048    TNamed *ntype = dynamic_cast<TNamed*>(input->FindObject("ProofAux_Action"));
00049    if (ntype) {
00050       if (!strcmp(ntype->GetTitle(), "GenerateTrees")) {
00051          action = 0;
00052       } else {
00053          Warning("GetAction", "unknown action: '%s'", ntype->GetTitle());
00054       }
00055    }
00056    // Done
00057    return action;
00058 }
00059 
00060 
00061 //_____________________________________________________________________________
00062 void ProofAux::Begin(TTree * /*tree*/)
00063 {
00064    // The Begin() function is called at the start of the query.
00065    // When running with PROOF Begin() is only called on the client.
00066    // The tree argument is deprecated (on PROOF 0 is passed).
00067 
00068    TString option = GetOption();
00069 
00070    // Determine the action type
00071    fAction = GetAction(fInput);
00072 }
00073 
00074 //_____________________________________________________________________________
00075 void ProofAux::SlaveBegin(TTree * /*tree*/)
00076 {
00077    // The SlaveBegin() function is called after the Begin() function.
00078    // When running with PROOF SlaveBegin() is called on each slave server.
00079    // The tree argument is deprecated (on PROOF 0 is passed).
00080 
00081    TString option = GetOption();
00082 
00083    // Determine the action type
00084    fAction = GetAction(fInput);
00085 
00086    // Get the number of events
00087    TParameter<Long64_t> *a = (TParameter<Long64_t> *) fInput->FindObject("ProofAux_NEvents");
00088    if (a) fNEvents = a->GetVal();
00089 
00090    // Create lists
00091    fMainList = new TList;
00092    if (gProofServ) fMainList->SetName(TString::Format("MainList-%s", gProofServ->GetOrdinal()));
00093    fFriendList = new TList;
00094    if (gProofServ) fFriendList->SetName(TString::Format("FriendList-%s", gProofServ->GetOrdinal()));
00095 }
00096 
00097 //_____________________________________________________________________________
00098 Bool_t ProofAux::Process(Long64_t entry)
00099 {
00100    // The Process() function is called for each entry in the tree (or possibly
00101    // keyed object in the case of PROOF) to be processed. The entry argument
00102    // specifies which entry in the currently loaded tree is to be processed.
00103    // It can be passed to either ProofAux::GetEntry() or TBranch::GetEntry()
00104    // to read either all or the required parts of the data. When processing
00105    // keyed objects with PROOF, the object is already loaded and is available
00106    // via the fObject pointer.
00107    //
00108    // This function should contain the "body" of the analysis. It can contain
00109    // simple or elaborate selection criteria, run algorithms on the data
00110    // of the event and typically fill histograms.
00111    //
00112    // The processing can be stopped by calling Abort().
00113    //
00114    // Use fStatus to set the return value of TTree::Process().
00115    //
00116    // The return value is currently not used.
00117 
00118    // Nothing to do if the action if not defined
00119    if (fAction < 0) {
00120       Error("Process", "action not specified!");
00121       return kFALSE;
00122    }
00123 
00124    // Link to current element, if any
00125    TDSetElement *fCurrent = 0;
00126    TPair *elemPair = 0;
00127    if (fInput && (elemPair = dynamic_cast<TPair *>(fInput->FindObject("PROOF_CurrentElement")))) {
00128       if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
00129          Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
00130       } else {
00131          Error("Process", "entry %lld: no file specified!", entry);
00132          return kFALSE;
00133       }
00134    }
00135 
00136    // Act now
00137    if (fAction == 0) {
00138       TString fnt;
00139       // Generate the TTree and save it in the specified file
00140       if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
00141          Error("Process", "problems generating tree (%lld, %s, %lld)",
00142                           entry, fCurrent->GetName(), fNEvents);
00143          return kFALSE;
00144       }
00145       // Generate the TTree friend and save it in the specified file
00146       if (GenerateFriend(fnt) != 0) {
00147          Error("Process", "problems generating friend tree for %s (%s)",
00148                           fCurrent->GetName(), fnt.Data());
00149          return kFALSE;
00150       }
00151    } else {
00152       // Unknown action
00153       Warning("Process", "do not know how to process action %d - do nothing", fAction);
00154       return kFALSE;
00155    }
00156 
00157    return kTRUE;
00158 }
00159 
00160 //_____________________________________________________________________________
00161 void ProofAux::SlaveTerminate()
00162 {
00163    // The SlaveTerminate() function is called after all entries or objects
00164    // have been processed. When running with PROOF SlaveTerminate() is called
00165    // on each slave server.
00166 
00167    if (fMainList && fMainList->GetSize() > 0) fOutput->Add(fMainList);
00168    if (fFriendList && fFriendList->GetSize() > 0) fOutput->Add(fFriendList);
00169 }
00170 
00171 //_____________________________________________________________________________
00172 void ProofAux::Terminate()
00173 {
00174    // The Terminate() function is the last function to be called during
00175    // a query. It always runs on the client, it can be used to present
00176    // the results graphically or save the results to file.
00177 
00178 }
00179 
00180 //_____________________________________________________________________________
00181 Int_t ProofAux::GenerateTree(const char *fnt, Long64_t ent, TString &fn)
00182 {
00183    // Generate the main tree for the 'friends' tutorial; the tree is called
00184    // 'Tmain', has 'ent' entries and is saved to file 'fnt'.
00185    // The full file path is returned in 'fn'.
00186    // Return 0 on success, -1 on error.
00187 
00188    Int_t rc = -1;
00189 
00190    // Check the filename
00191    fn = fnt;
00192    if (fn.IsNull()) {
00193       Error("GenerateTree", "file name undefined!");
00194       return rc;
00195    }
00196    TUrl uu(fn, kTRUE);
00197    if (!strcmp(uu.GetProtocol(), "file") && !fn.BeginsWith("/")) {
00198       // Local file with relative path: create under the data directory
00199       if (!gProofServ ||
00200           !(gProofServ->GetDataDir()) || strlen(gProofServ->GetDataDir()) <= 0) {
00201          Error("GenerateTree", "data directory undefined!");
00202          return rc;
00203       }
00204       // Insert data directory
00205       fn.Insert(0, TString::Format("%s/", gProofServ->GetDataDir()));
00206       // Make sure the directory exists
00207       TString dir = gSystem->DirName(fn);
00208       if (gSystem->AccessPathName(dir, kWritePermission)) {
00209          if (gSystem->mkdir(dir, kTRUE) != 0) {
00210             Error("GenerateTree", "problems creating directory %s to store the file", dir.Data());
00211             return rc;
00212          }
00213       }
00214    }
00215 
00216    // Create the file
00217    TDirectory* savedir = gDirectory;
00218    TFile *f = new TFile(fn, "RECREATE");
00219    if (!f || f->IsZombie()) {
00220       Error("GenerateTree", "problems opening file %s", fn.Data());
00221       return rc;
00222    }
00223    savedir->cd();
00224    rc = 0;
00225 
00226    // Create the tree
00227    TTree *T = new TTree("Tmain","Main tree for tutorial friends");
00228    T->SetDirectory(f);
00229    Int_t Run = 1;
00230    T->Branch("Run",&Run,"Run/I");
00231    Long64_t Event = 0;
00232    T->Branch("Event",&Event,"Event/L");
00233    Float_t x = 0., y = 0., z = 0.;
00234    T->Branch("x",&x,"x/F");
00235    T->Branch("y",&y,"y/F");
00236    T->Branch("z",&z,"z/F");
00237    TRandom r;
00238    for (Long64_t i = 0; i < ent; i++) {
00239       if (i > 0 && i%1000 == 0) Run++;
00240       Event = i;
00241       x = r.Gaus(10,1);
00242       y = r.Gaus(20,2);
00243       z = r.Landau(2,1);
00244       T->Fill();
00245    }
00246    T->Print();
00247    f->cd();
00248    T->Write();
00249    T->SetDirectory(0);
00250    f->Close();
00251    delete f;
00252    delete T;
00253 
00254    // Notify success
00255    Info("GenerateTree", "file '%s' successfully created", fn.Data());
00256 
00257    // Add to the list
00258    TString fds(fn);
00259    if (!strcmp(uu.GetProtocol(), "file"))
00260       fds.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
00261    fMainList->Add(new TObjString(fds));
00262 
00263    // Done
00264    return rc;
00265 }
00266 
00267 //_____________________________________________________________________________
00268 Int_t ProofAux::GenerateFriend(const char *fnt, const char *fnf)
00269 {
00270    // Generate the friend tree for the main tree in the 'friends' tutorial fetched
00271    // from 'fnt'.
00272    // the tree is called 'Tfriend', has the same number of entries as the main
00273    // tree and is saved to file 'fnf'. If 'fnf' is not defined the filename is
00274    // derived from 'fnt' either replacing 'tree' with 'friend', or adding '_friend'
00275    // before the '.root' extension.
00276    // Return 0 on success, -1 on error.
00277 
00278    Int_t rc = -1;
00279    // Check the input filename
00280    TString fin(fnt);
00281    if (fin.IsNull()) {
00282       Error("GenerateFriend", "file name for the main tree undefined!");
00283       return rc;
00284    }
00285    // Make sure that the file can be read
00286    if (gSystem->AccessPathName(fin, kReadPermission)) {
00287       Error("GenerateFriend", "input file does not exist or cannot be read: %s", fin.Data());
00288       return rc;
00289    }
00290    // The output filename
00291    TString fout(fnf);
00292    if (fout.IsNull()) {
00293       fout = fin;
00294       TString xf = gSystem->BaseName(fout);
00295       fout = gSystem->DirName(fout);
00296       if (xf.Contains("tree")) {
00297          xf.ReplaceAll("tree", "friend");
00298       } else {
00299          if (xf.EndsWith(".root")) {
00300             xf.ReplaceAll(".root", "_friend.root");
00301          } else {
00302             xf += "_friend";
00303          }
00304       }
00305       fout += TString::Format("/%s", xf.Data());
00306    }
00307    // Make sure the directory exists
00308    TString dir = gSystem->DirName(fout);
00309    if (gSystem->AccessPathName(dir, kWritePermission)) {
00310       if (gSystem->mkdir(dir, kTRUE) != 0) {
00311          Error("GenerateFriend", "problems creating directory %s to store the file", dir.Data());
00312          return rc;
00313       }
00314    }
00315 
00316    // Get main tree
00317    TFile *fi = TFile::Open(fin);
00318    if (!fi || fi->IsZombie()) {
00319       Error("GenerateFriend", "problems opening input file %s", fin.Data());
00320       return rc;
00321    }
00322    TTree *Tin = (TTree *) fi->Get("Tmain");
00323    if (!Tin) {
00324       Error("GenerateFriend", "problems getting tree 'Tmain' from file %s", fin.Data());
00325       delete fi;
00326       return rc;
00327    }
00328    // Set branches
00329    Float_t x, y, z;
00330    Tin->SetBranchAddress("x", &x);
00331    Tin->SetBranchAddress("y", &y);
00332    Tin->SetBranchAddress("z", &z);
00333    TBranch *b_x = Tin->GetBranch("x");
00334    TBranch *b_y = Tin->GetBranch("y");
00335    TBranch *b_z = Tin->GetBranch("z");
00336 
00337    TDirectory* savedir = gDirectory;
00338    // Create output file
00339    TFile *fo = new TFile(fout, "RECREATE");
00340    if (!fo || fo->IsZombie()) {
00341       Error("GenerateFriend", "problems opening file %s", fout.Data());
00342       delete fi;
00343       return rc;
00344    }
00345    savedir->cd();
00346    rc = 0;
00347 
00348    // Create the tree
00349    TTree *Tfrnd = new TTree("Tfrnd", "Friend tree for tutorial 'friends'");
00350    Tfrnd->SetDirectory(fo);
00351    Float_t r = 0;
00352    Tfrnd->Branch("r",&x,"r/F");
00353    Long64_t ent = Tin->GetEntries();
00354    for (Long64_t i = 0; i < ent; i++) {
00355       b_x->GetEntry(i);
00356       b_y->GetEntry(i);
00357       b_z->GetEntry(i);
00358       r = TMath::Sqrt(x*x + y*y + z*z);
00359       Tfrnd->Fill();
00360    }
00361    fi->Close();
00362    delete fi;
00363    Tfrnd->Print();
00364    fo->cd();
00365    Tfrnd->Write();
00366    Tfrnd->SetDirectory(0);
00367    fo->Close();
00368    delete fo;
00369    delete Tfrnd;
00370 
00371    // Notify success
00372    Info("GenerateFriend", "friend file '%s' successfully created", fout.Data());
00373 
00374    // Add to the list
00375    TUrl uu(fout);
00376    if (!strcmp(uu.GetProtocol(), "file"))
00377       fout.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
00378    fFriendList->Add(new TObjString(fout));
00379 
00380    // Done
00381    return rc;
00382 }

Generated on Tue Jul 5 15:44:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1