00001
00002
00003
00004
00005 #include "Riostream.h"
00006 #include "TProof.h"
00007 #include "TString.h"
00008
00009
00010 Bool_t make_event_trees(const char *basedir, Int_t events_per_file,
00011 Int_t files_per_node)
00012 {
00013
00014
00015
00016
00017 if (!gProof) {
00018 cout << "Must Start PROOF before using make_event_trees.C" << endl;
00019 return kFALSE;
00020 }
00021
00022 if (!basedir) {
00023 cout << "'basedir' must not be empty" << endl;
00024 return kFALSE;
00025 }
00026
00027 if (events_per_file <= 0) {
00028 cout << "events_per_file must be > 0" << endl;
00029 return kFALSE;
00030 }
00031
00032 if (files_per_node <= 0) {
00033 cout << "files_per_node must be > 0" << endl;
00034 return kFALSE;
00035 }
00036
00037 if (gProof->UploadPackage("event.par")) return kFALSE;
00038 if (gProof->EnablePackage("event")) return kFALSE;
00039
00040 ofstream slavemacro("build_trees.C");
00041
00042 slavemacro << "#include \"TSystem.h\"" << endl;
00043 slavemacro << "#include \"TProof.h\"" << endl;
00044 slavemacro << "#include \"TProofServ.h\"" << endl;
00045 slavemacro << "#include \"TRandom.h\"" << endl;
00046 slavemacro << "#include \"TFile.h\"" << endl;
00047 slavemacro << "#include \"TTree.h\"" << endl;
00048 slavemacro << "#include \"Riostream.h\"" << endl;
00049 slavemacro << "#include \"event/Event.h\"" << endl;
00050 slavemacro << "void build_trees(const char *basedir, Int_t nevents, Int_t nfiles) {" << endl;
00051 slavemacro << " Int_t slave_number = -1;" << endl;
00052 slavemacro << " Int_t nslaves = 0;" << endl;
00053 if (!strncmp(gProof->GetMaster(), "localhost", 9))
00054 slavemacro << " TString hn = \"localhost\";" << endl;
00055 else
00056 slavemacro << " TString hn = gSystem->HostName();" << endl;
00057 slavemacro << " TString ord = gProofServ->GetOrdinal();" << endl;
00058 slavemacro << endl;
00059 TList* l = gProof->GetListOfSlaveInfos();
00060 for (Int_t i=0; i<l->GetSize(); i++) {
00061 TSlaveInfo* si = dynamic_cast<TSlaveInfo*>(l->At(i));
00062 if (si->fStatus != TSlaveInfo::kActive) continue;
00063 slavemacro << " if (hn == \"";
00064 slavemacro << si->fHostName;
00065 slavemacro << "\") { nslaves++; if (ord == \"";
00066 slavemacro << si->fOrdinal;
00067 slavemacro << "\") slave_number = nslaves; }" << endl;
00068 }
00069
00070 slavemacro << " if (gSystem->AccessPathName(basedir)) {" << endl;
00071 slavemacro << " Printf(\"No such file or directory: %s\", basedir);" << endl;
00072 slavemacro << " return;" << endl;
00073 slavemacro << " }" << endl;
00074 slavemacro << endl;
00075 slavemacro << " if (slave_number >= 0) {" << endl;
00076 slavemacro << " for(Int_t i=slave_number; i<=nfiles; i+=nslaves) {" << endl;
00077 slavemacro << endl;
00078 slavemacro << " TString seed = hn;" << endl;
00079 slavemacro << " seed += \"_\";" << endl;
00080 slavemacro << " seed += i;" << endl;
00081 slavemacro << " gRandom->SetSeed(static_cast<UInt_t>(TMath::Hash(seed)));" << endl;
00082 slavemacro << endl;
00083 slavemacro << " TString filename = basedir;" << endl;
00084 slavemacro << " filename += \"/event_tree_\";" << endl;
00085 slavemacro << " filename += seed;" << endl;
00086 slavemacro << " filename += \".root\";" << endl;
00087 slavemacro << " TDirectory* savedir = gDirectory;" << endl;
00088 slavemacro << " TFile *f = TFile::Open(filename, \"RECREATE\");" << endl;
00089 slavemacro << " savedir->cd();" << endl;
00090 slavemacro << endl;
00091 slavemacro << " if (!f || f->IsZombie()) break;" << endl;
00092 slavemacro << " Event event;" << endl;
00093 slavemacro << " Event *ep = &event;" << endl;
00094 slavemacro << " TTree eventtree(\"EventTree\", \"Event Tree\");" << endl;
00095 slavemacro << " eventtree.SetDirectory(f);" << endl;
00096 slavemacro << " eventtree.Branch(\"event\", \"Event\", &ep, 32000, 1);" << endl;
00097 slavemacro << " eventtree.AutoSave();" << endl;
00098 slavemacro << endl;
00099 slavemacro << " for(Int_t j=0; j<nevents; j++) {" << endl;
00100 slavemacro << " event.Build(j,3,0);" << endl;
00101 slavemacro << " eventtree.Fill();" << endl;
00102 slavemacro << " }" << endl;
00103 slavemacro << endl;
00104 slavemacro << " savedir = gDirectory;" << endl;
00105 slavemacro << " f->cd();" << endl;
00106 slavemacro << " eventtree.Write();" << endl;
00107 slavemacro << " eventtree.SetDirectory(0);" << endl;
00108 slavemacro << " f->Close();" << endl;
00109 slavemacro << " delete f;" << endl;
00110 slavemacro << " f = 0;" << endl;
00111 slavemacro << " savedir->cd();" << endl;
00112 slavemacro << endl;
00113 slavemacro << " }" << endl;
00114 slavemacro << " } else {" << endl;
00115 slavemacro << " cout << \"Could not find slave hostname=\";" << endl;
00116 slavemacro << " cout << hn << \", ordinal=\" << ord;" << endl;
00117 slavemacro << " cout << \" in file production list.\" << endl;" << endl;
00118 slavemacro << " cout << \"Make sure the proof.conf contains the \";" << endl;
00119 slavemacro << " cout << \"correct slave hostnames.\" << endl;" << endl;
00120 slavemacro << " }" << endl;
00121 slavemacro << "}" << endl;
00122
00123 slavemacro.close();
00124
00125 gProof->Load("build_trees.C+");
00126
00127 TString cmd = "build_trees(\"";
00128 cmd += basedir;
00129 cmd += "\",";
00130 cmd += events_per_file;
00131 cmd += ",";
00132 cmd += files_per_node;
00133 cmd += ")";
00134
00135 if (gProof->Exec(cmd)<0) return kFALSE;
00136
00137 return kTRUE;
00138 }