ROOT  6.06/09
Reference Guide
TSelHandleDataSet.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: Sangsu Ryu 22/06/2010
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TSelHandleDataSet //
15 // //
16 // PROOF selector for file cache release. //
17 // List of files to be cleaned for each node is provided by client. //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 
21 #define TSelHandleDataSet_cxx
22 
23 #include "TSelHandleDataSet.h"
24 
25 #include "TDSet.h"
26 #include "TEnv.h"
27 #include "TFile.h"
28 #include "TMap.h"
29 #include "TParameter.h"
30 #include "TProofBenchTypes.h"
31 #include "TSystem.h"
32 #include "TUrl.h"
33 #include "errno.h"
34 #include <unistd.h>
35 #include <fcntl.h>
36 
37 
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Init the type from the input parameters
42 
43 void TSelHandleDataSet::SlaveBegin(TTree *)
44 {
45 
46  TObject *o = fInput->FindObject("PROOF_Benchmark_HandleDSType");
47  if (o) fType = dynamic_cast<TPBHandleDSType *>(o);
48 
49  TNamed *n = dynamic_cast<TNamed *>(fInput->FindObject("PROOF_Benchmark_DestDir"));
50  if (n) {
51  fDestDir = n->GetTitle();
52  if (gSystem->AccessPathName(fDestDir)) {
53  // Create the directory
54  if (gSystem->mkdir(fDestDir, kTRUE) != 0) {
55  fDestDir = "";
56  Error("SlaveBegin", "could not create dir '%s'", fDestDir.Data());
57  } else {
58  if (gSystem->AccessPathName(fDestDir, kWritePermission)) {
59  fDestDir = "";
60  Error("SlaveBegin", "dir '%s' is not writable by this process", fDestDir.Data());
61  } else {
62  Info("SlaveBegin", "dir '%s' successfully created", fDestDir.Data());
63  }
64  }
65  }
66  }
67 
68  // Use default if nothing found in the input list
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Release the memory cache associated with file 'fn'.
74 
75 void TSelHandleDataSet::ReleaseCache(const char *fn)
76 {
77 #if defined(R__LINUX)
78  TString filename(fn);
79  Int_t fd;
80  fd = open(filename.Data(), O_RDONLY);
81  if (fd > -1) {
82  fdatasync(fd);
83  posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
84  close(fd);
85  Info("ReleaseCache", "file cache for file '%s' cleaned ...", filename.Data());
86  } else {
87  Error("ReleaseCache", "cannot open file '%s' for cache clean up; errno=%d",
88  filename.Data(), errno);
89  }
90 #else
91  Info("ReleaseCache", "dummy function: file '%s' untouched ...", fn);
92 #endif
93  // Done
94  return;
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Check the memory cache associated with file 'fn'.
99 
100 void TSelHandleDataSet::CheckCache(const char * /*fn*/)
101 {
102  Warning("CheckCache", "cache checker not implemented yet");
103  // Done
104  return;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Physically remove the file 'fn'.
109 
110 void TSelHandleDataSet::RemoveFile(const char *fn)
111 {
113  if (gSystem->Unlink(fn) != 0) {
114  Error("RemoveFile", "problems removing file '%s' ...", fn);
115  } else {
116  if (!gSystem->AccessPathName(fn))
117  Warning("RemoveFile", "'unlink' returned success but the file still exists ('%s')", fn);
118  }
119  } else {
120  if (!gSystem->AccessPathName(fn)) {
121  Error("RemoveFile", "file '%s' cannot removed by this process", fn);
122  } else {
123  Error("RemoveFile", "file '%s' does not exists", fn);
124  }
125  }
126  // Done
127  return;
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Copy file 'fn' to fDestDir
132 
133 void TSelHandleDataSet::CopyFile(const char *fn)
134 {
135  // Check if we have a destination dir
136  if (fDestDir.IsNull()) {
137  Error("CopyFile", "destination dir undefined: file '%s' not copied", fn);
138  return;
139  }
140 
141  TString basefn = gSystem->BaseName(TUrl(fn, kTRUE).GetFile());
142  TString dst = TString::Format("%s/%s", fDestDir.Data(), basefn.Data());
143  if (!TFile::Cp(fn, dst.Data())) {
144  Error("CopyFile", "problems copying file '%s' to '%s'", fn, dst.Data());
145  return;
146  }
147  Info("CopyFile", "file '%s' created ...", dst.Data());
148 
149  // Done
150  return;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// The Process() function is called for each entry in the tree (or possibly
155 /// keyed object in the case of PROOF) to be processed. The entry argument
156 /// specifies which entry in the currently loaded tree is to be processed.
157 /// It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
158 /// to read either all or the required parts of the data. When processing
159 /// keyed objects with PROOF, the object is already loaded and is available
160 /// via the fObject pointer.
161 ///
162 /// This function should contain the "body" of the analysis. It can contain
163 /// simple or elaborate selection criteria, run algorithms on the data
164 /// of the event and typically fill histograms.
165 
167 {
168  // WARNING when a selector is used with a TChain, you must use
169  // the pointer to the current TTree to call GetEntry(entry).
170  // The entry is always the local entry number in the current tree.
171  // Assuming that fChain is the pointer to the TChain being processed,
172  // use fChain->GetTree()->GetEntry(entry).
173 
174  TDSetElement *fCurrent = 0;
175  TPair *elemPair = 0;
176  if (fInput && (elemPair = dynamic_cast<TPair *>
177  (fInput->FindObject("PROOF_CurrentElement")))) {
178  if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
179  Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
180  } else {
181  Error("Process", "entry %lld: no file specified!", entry);
182  return kFALSE;
183  }
184  }
185 
186  // Resolve the file type; this also adjusts names for Xrd based systems
187  TUrl url(fCurrent->GetName());
188  url.SetAnchor(0);
189  TString lfname = gEnv->GetValue("Path.Localroot", "");
190  TFile::EFileType type = TFile::GetType(url.GetUrl(), "", &lfname);
191  if (type == TFile::kLocal &&
192  strcmp(url.GetProtocol(),"root") && strcmp(url.GetProtocol(),"xrd"))
193  lfname = url.GetFileAndOptions();
194 
196  // Release the file cache
197  if (type == TFile::kLocal) {
198  ReleaseCache(lfname);
199  } else if (type == TFile::kFile) {
200  ReleaseCache(url.GetFile());
201  } else {
202  Error("Process",
203  "attempt to call ReleaseCache for a non-local file: '%s'", url.GetUrl());
204  }
205  } else if (fType->GetType() == TPBHandleDSType::kCheckCache) {
206  // Check the file cache
207  if (type == TFile::kLocal) {
208  CheckCache(lfname);
209  } else if (type == TFile::kFile) {
210  CheckCache(url.GetFile());
211  } else {
212  Error("Process",
213  "attempt to call CheckCache for a non-local file: '%s'", url.GetUrl());
214  }
215  } else if (fType->GetType() == TPBHandleDSType::kRemoveFiles) {
216  // Remove the file
217  RemoveFile(url.GetFileAndOptions());
218  } else if (fType->GetType() == TPBHandleDSType::kCopyFiles) {
219  // Copy file
220  CopyFile(url.GetFileAndOptions());
221  } else {
222  // Type unknown
223  Warning("Process", "type: %d is unknown", fType->GetType());
224  }
225 
226  return kTRUE;
227 }
228 
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:928
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
long long Long64_t
Definition: RtypesCore.h:69
This class represents a WWW compatible URL.
Definition: TUrl.h:41
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
void CheckCache(const char *fn)
Check the memory cache associated with file 'fn'.
static const char * filename()
Basic string class.
Definition: TString.h:137
EHandleDSType GetType() const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:496
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:900
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
Allows to copy this file to the dst URL.
Definition: TFile.cxx:4749
void ReleaseCache(const char *fn)
Release the memory cache associated with file 'fn'.
const char * Data() const
Definition: TString.h:349
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1346
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2334
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
static EFileType GetType(const char *name, Option_t *option="", TString *prefix=0)
Resolve the file type as a function of the protocol field in 'name'.
Definition: TFile.cxx:4566
void Info(const char *location, const char *msgfmt,...)
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void Error(const char *location, const char *msgfmt,...)
void RemoveFile(const char *fn)
Physically remove the file 'fn'.
EFileType
File type.
Definition: TFile.h:163
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:494
TObject * Value() const
Definition: TMap.h:125
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Bool_t IsNull() const
Definition: TString.h:387
virtual Bool_t Process(Long64_t entry)
The Process() function is called for each entry in the tree (or possibly keyed object in the case of ...
Long64_t entry
PyObject * fType
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:106
void SetAnchor(const char *anchor)
Definition: TUrl.h:95
int type
Definition: TGX11.cxx:120
R__EXTERN TEnv * gEnv
Definition: TEnv.h:174
ClassImp(TSelHandleDataSet) void TSelHandleDataSet
Init the type from the input parameters.
Mother of all ROOT objects.
Definition: TObject.h:58
typedef void((*Func_t)())
TList * fInput
Current object if processing object (vs. TTree)
Definition: TSelector.h:49
A TTree object has a header with a name and a title.
Definition: TTree.h:94
TPBHandleDSType * fType
const Bool_t kTRUE
Definition: Rtypes.h:91
void CopyFile(const char *fn)
Copy file 'fn' to fDestDir.
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:379
const Int_t n
Definition: legend1.C:16
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904