00001 #define ProofAux_cxx
00002
00003
00004
00005
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
00026
00027 fAction = -1;
00028 fNEvents= -1;
00029 fMainList = 0;
00030 fFriendList = 0;
00031 }
00032
00033
00034 ProofAux::~ProofAux()
00035 {
00036
00037
00038 }
00039
00040
00041 Int_t ProofAux::GetAction(TList *input)
00042 {
00043
00044
00045
00046 Int_t action = -1;
00047
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
00057 return action;
00058 }
00059
00060
00061
00062 void ProofAux::Begin(TTree * )
00063 {
00064
00065
00066
00067
00068 TString option = GetOption();
00069
00070
00071 fAction = GetAction(fInput);
00072 }
00073
00074
00075 void ProofAux::SlaveBegin(TTree * )
00076 {
00077
00078
00079
00080
00081 TString option = GetOption();
00082
00083
00084 fAction = GetAction(fInput);
00085
00086
00087 TParameter<Long64_t> *a = (TParameter<Long64_t> *) fInput->FindObject("ProofAux_NEvents");
00088 if (a) fNEvents = a->GetVal();
00089
00090
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 if (fAction < 0) {
00120 Error("Process", "action not specified!");
00121 return kFALSE;
00122 }
00123
00124
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
00137 if (fAction == 0) {
00138 TString fnt;
00139
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
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
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
00164
00165
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
00175
00176
00177
00178 }
00179
00180
00181 Int_t ProofAux::GenerateTree(const char *fnt, Long64_t ent, TString &fn)
00182 {
00183
00184
00185
00186
00187
00188 Int_t rc = -1;
00189
00190
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
00199 if (!gProofServ ||
00200 !(gProofServ->GetDataDir()) || strlen(gProofServ->GetDataDir()) <= 0) {
00201 Error("GenerateTree", "data directory undefined!");
00202 return rc;
00203 }
00204
00205 fn.Insert(0, TString::Format("%s/", gProofServ->GetDataDir()));
00206
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
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
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
00255 Info("GenerateTree", "file '%s' successfully created", fn.Data());
00256
00257
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
00264 return rc;
00265 }
00266
00267
00268 Int_t ProofAux::GenerateFriend(const char *fnt, const char *fnf)
00269 {
00270
00271
00272
00273
00274
00275
00276
00277
00278 Int_t rc = -1;
00279
00280 TString fin(fnt);
00281 if (fin.IsNull()) {
00282 Error("GenerateFriend", "file name for the main tree undefined!");
00283 return rc;
00284 }
00285
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
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
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
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
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
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
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
00372 Info("GenerateFriend", "friend file '%s' successfully created", fout.Data());
00373
00374
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
00381 return rc;
00382 }