AliPhysics  abafffd (abafffd)
OutputUtilities.C
Go to the documentation of this file.
1 
15 #ifndef TREEOUTPUTHELPER_C
16 #define TREEOUTPUTHELPER_C
17 #ifndef __CINT__
18 # include <TString.h>
19 # include <TError.h>
20 # include <AliAnalysisManager.h>
21 # include <AliAnalysisDataContainer.h>
22 # include <AliVEventHandler.h>
23 # include <TSystem.h>
24 // Below for finding free port number
25 # ifdef R__UNIX
26 # include <sys/types.h>
27 # include <sys/socket.h>
28 # include <netinet/in.h>
29 # include <netinet/ip.h>
30 # include <unistd.h>
31 # endif
32 #else
33 class TString;
34 #endif
35 
36 // ===================================================================
43 {
51  static Bool_t RegisterDataset(const TString& dsname)
52  {
53  // Get the manager
54  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
55 
56  // If we are asked to make a data-set, get the output handler and
57  // common output container.
58  AliVEventHandler* handler = mgr->GetOutputEventHandler();
59  if (!handler) return true;
60 
61  // Get the container
62  AliAnalysisDataContainer* cont = mgr->GetCommonOutputContainer();
63  if (!cont) {
64  Warning("OutputUtilities::RegisterDataset",
65  "No common output container defined");
66  return false;
67  }
68 
69  // Make the name
70  TString nme(dsname);
71  if (nme.IsNull()) nme = mgr->GetName();
72  if (nme.IsNull()) {
73  Error("OutputUtilities::RegisterDataset", "No data set name specified");
74  return false;
75  }
76 
77  // Flag for data-set creation
78  cont->SetRegisterDataset(true);
79 
80  handler->SetOutputFileName(nme);
81  // cont->SetFileName(nme);
82 
83  TString base(handler->GetOutputFileName());
84  base.ReplaceAll(".root","");
85  Info("OutputUtilities::RegisterDataset",
86  "Will register tree output AODs (%s%s) as dataset",
87  base.Data(), cont->GetTitle());
88 
89  return true;
90  }
98  {
99  TString ret;
100 
101  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
102  AliVEventHandler* oh = mgr->GetOutputEventHandler();
103  if (!oh) {
104  Warning("OutputUtilities::GetOutputDataSet",
105  "No outout event handler defined");
106  return ret;
107  }
108  AliAnalysisDataContainer* co = mgr->GetCommonOutputContainer();
109  if (!co) {
110  Warning("OutputUtilities::GetOutputDataSet",
111  "No common output container defined");
112  return ret;
113  }
114  if (!co->IsRegisterDataset()) {
115  Info("OutputUtilities::GetOutputDataSet",
116  "Common output is not registered as dataset");
117  return ret;
118  }
119  ret = oh->GetOutputFileName();
120  // ret.ReplaceAll("TTree", "");
121  ret.ReplaceAll(".root", "");
122  // ret.Append(co->GetTitle());
123 
124  return ret;
125  }
126  static Int_t FindPort()
127  {
128 #ifdef R_UNIX
129  int sd = socket(AF_INET,SOCK_STREAM,0);
130  if (sd < 0) {
131  Warning("FindPort", "Failed to make socket");
132  return -1;
133  }
134 
135  // Binding to port 0 will give us back a free port. The kernel
136  // does not reuse port numbers immediately
137  struct sockaddr_in addr;
138  addr.sin_family = AF_INET;
139  addr.sin_addr.s_addr = INADDR_ANY;
140  addr.sin_port = 0;
141  int bd = bind(sd, (struct sockaddr*)&addr, sizeof(addr));
142  if (bd != 0) {
143  Warning("FindPort", "Failed to bind socket to port 0");
144  close(sd);
145  return -1;
146  }
147 
148  // Get the address on the bound socket
149  struct sockaddr_in radd;
150  socklen_t ladd = sizeof(radd);
151  int nr = getsockname(sd, (struct sockaddr*)&radd, &ladd);
152  if (nr != 0) {
153  Warning("FindPort", "Failed get socket port");
154  close(sd);
155  return -1;
156  }
157 
158  int port = radd.sin_port;
159  close (sd);
160 
161  return port;
162 #else
163  Warning("FindPort", "don't know how to do that on your system");
164  return -1;
165 #endif
166  }
175  {
176  url = "";
177  Int_t port = FindPort();
178  if (port < 0) return false;
179 
180  // Get host, current directory, and user name for unique name
181  TString host(gSystem->HostName());
182  TString dir(gSystem->WorkingDirectory());
183  TString name(gSystem->GetUserInfo()->fUser.Data());
184 
185  // Form the command line. Note, we put the PID file one level up,
186  // so we know where to look for it. Otherwise it would be put in a
187  // sub-directory based on the name of the server. Since we later
188  // on don't know the name of the server we wouldn't now where to
189  // look for the PID file
190  TString exec;
191  exec.Form("xrootd -p %d -l xrd.log -s ../xrd.pid -b -n %s %s",
192  port, name.Data(), dir.Data());
193  Info("StartXrootd", "Starting XRootD to serve %s on port %d",
194  dir.Data(), port);
195  Info("StartXrootd", "%s", exec.Data());
196  int ret = gSystem->Exec(exec);
197  if (ret != 0) {
198  Warning("StartXrootd", "Failed to start XRootd server");
199  return false;
200  }
201 
202  // Form the access URL
203  url = Form("root://%s@%s:%d/%s",
204  name.Data(), host.Data(), port, dir.Data());
205  Info("StartXrootd", "Access URL is \"%s\"", url.Data());
206 
207  return true;
208  }
215  {
216  std::ifstream pidFile("xrd.pid");
217  if (!pidFile) return false;
218 
219  TString s; s.ReadFile(pidFile);
220  pidFile.close();
221  gSystem->Unlink("xrd.pid");
222 
223  if (s.IsNull()) return false;
224 
225  Info("StopXrootd", "Stopping XRootd server (pid: %s)", s.Data());
226  return gSystem->Exec(Form("kill -9 %s", s.Data())) == 0;
227  }
236  {
237  if (url.IsNull()) {
238  Error("OutputUtilities::RegisterStorage", "No storage URI specified");
239  return false;
240  }
241 
242  // Get the manager
243  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
244 
245  // Get the container
246  AliAnalysisDataContainer* cont = mgr->GetCommonOutputContainer();
247  if (!cont) {
248  Warning("OutputUtilities::RegisterStorage",
249  "No common output container defined");
250  return false;
251  }
252 
253  TString u(url);
254  if (u.EqualTo("auto")) {
255  if (!StartXrootd(u)) {
256  Warning("OutputUtilities::RegisterStorage",
257  "Couldn't start the XRootD server");
258  return false;
259  }
260  }
261 
262  cont->SetSpecialOutput();
263  mgr->SetSpecialOutputLocation(u);
264 
265  return true;
266  }
267 };
268 #endif
269 //
270 // EOF
271 //
static Bool_t StartXrootd(TString &url)
const char * url
TSystem * gSystem
static Int_t FindPort()
static Bool_t StopXrootd()
int Int_t
Definition: External.C:63
static TString RegisteredDataset()
bool Bool_t
Definition: External.C:53
static Bool_t RegisterDataset(const TString &dsname)
TDirectoryFile * dir
static Bool_t RegisterStorage(const TString &url)