AliRoot Core  edcc906 (edcc906)
FMDUtilda.h
Go to the documentation of this file.
1 #ifndef FMDUTILDA_H
2 #define FMDUTILDA_H
3 #include "AliFMDBaseDA.h"
4 #include "daqDA.h"
5 #include <stdexcept>
6 #include <iostream>
7 #include <fstream>
8 #include <cstdlib>
9 #ifdef ALI_AMORE
10 # include <AmoreDA.h>
11 # include <TH2.h>
12 # include <TSystem.h>
13 #endif
14 
15 Bool_t
16 PostSummaries(AliFMDBaseDA& da, const char* prefix, Int_t runNumber, bool verb=true)
17 {
18 #ifndef ALI_AMORE
19  std::cerr << "AMORE not found - nothing posted" << std::endl;
20  return true;
21 #else
22  if (verb)
23  std::cout << "Posting " << prefix << " data to AMORE" << std::endl;
24  try {
25  TString daName(gSystem->Getenv("AMORE_DA_NAME"));
26  if (daName.IsNull()) {
27  daName = gSystem->Getenv("DATE_ROLE_NAME");
28  if (daName.IsNull())
29  gSystem->Setenv("AMORE_DA_NAME", "FMD");
30  }
31  amore::da::AmoreDA myAmore(amore::da::AmoreDA::kSender);
32 
33  for (UShort_t det = 1; det <= 3; det++) {
34  if (!da.HasSeenDetector(det)) continue;
35  TObject* runNo = new TObject;
36  runNo->SetUniqueID(runNumber);
37  if (verb)
38  std::cout << " Posting " << prefix << "RuNoFMD" << det << " ... "
39  << std::flush;
40  myAmore.Send(Form("%sRunNoFMD%d", prefix, det), runNo);
41  if (verb)
42  std::cout << "done" << std::endl;
43  }
44 
45  TIter next(&da.GetSummaries());
46  if (verb) {
47  std::cout << " Will post summaries: " << std::endl;
48  da.GetSummaries().ls();
49  }
50  TObject* obj = 0;
51  while ((obj = next())) {
52  if (verb)
53  std::cout << " Posting " << obj->GetName()
54  << " ... " << std::flush;
55  myAmore.Send(obj->GetName(), obj);
56  if (verb)
57  std::cout << "done" << std::endl;
58  }
59 
60  }
61  catch (std::exception& e) {
62  std::cerr << "Failed to make AMORE instance: " << e.what() << std::endl;
63  return false;
64  }
65  if (verb)
66  std::cout << "All objects posted to AMORE" << std::endl;
67  return true;
68 #endif
69 }
70 
71 #include <sys/types.h>
72 #include <sys/wait.h>
73 #include <cstring>
74 #include <cerrno>
75 
76 int system_alternative(const char* pgm, char* const argv[])
77 {
78  // See http://stackoverflow.com/questions/3055924/problems-with-system-calls-in-linux
79  pid_t pid = vfork();
80  if (pid > 0) {
81  // We're the parent, so wait for child to finish
82  int status;
83  waitpid(pid, &status, 0);
84  return status;
85  }
86  else if (pid == 0) {
87  // We're the child, so run the specified program. Our exit status will
88  // be that of the child program unless the execv() syscall fails.
89  std::cout << "Execute \"" << pgm << "\"";
90  for (int i = 0; argv[i]; i++)
91  std::cout << " \"" << argv[i] << "\"";
92  std::cout << std::endl;
93  int status = execv(pgm, argv);
94  if (status == -1) {
95  std::cout << "Failed to exec " << errno
96  << " (" << strerror(errno) << ")" << std::endl;
97  }
98  return status;
99  }
100  else {
101  // Something horrible happened, like system out of memory
102  std::cout << "Failed to for fork " << errno
103  << " (" << strerror(errno) << ")" << std::endl;
104  return -1;
105  }
106 }
107 
108 Int_t myFesStore2(const char* file, const char* id, bool verb=true)
109 {
110  TString path(gSystem->Getenv("DAQDALIB_PATH"));
111  if (path.IsNull()) {
112  std::cout << "DAQDALIB_PATH undefined" << std::endl;
113  return -1;
114  }
115  TString cmd(path);
116  cmd.Append("/daqFES_store");
117  std::cout << "Will Execute \"" << cmd
118  << "\",\"" << file << "\",\"" << id
119  << "\"" << std::endl;
120  char* const args[] = {
121  const_cast<char*>(cmd.Data()),
122  const_cast<char*>(file),
123  const_cast<char*>(id),
124  0 };
125  int ret = system_alternative(cmd.Data(),args);
126  if (WIFEXITED(ret)) {
127  ret = WEXITSTATUS(ret);
128  if (ret != 0)
129  std::cout << "Failed to execute \"" << cmd << "\" -> "
130  << ret << std::endl;
131  }
132  else if (WIFSIGNALED(ret)) {
133  int sig = WTERMSIG(ret);
134  std::cout << "Process \"" << cmd << "\" received signal "
135  << sig << " (" << strsignal(sig) << ")" << std::endl;
136  ret = -1;
137  }
138  return ret;
139 }
140 
141 Int_t myFesStore(const char* file, const char* id, bool verb=true)
142 {
143  TString path(gSystem->Getenv("DAQDALIB_PATH"));
144  if (path.IsNull()) {
145  std::cout << "DAQDALIB_PATH undefined" << std::endl;
146  return -1;
147  }
148  // Check if we can get a shell
149  Int_t ret = system(NULL);
150  if (ret == 0) {
151  std::cout << "Cannot spawn a shell" << std::endl;
152  return -2;
153  }
154  // Biuld command
155  TString cmd(path);
156  cmd.Append("/daqDetDB_store ");
157  cmd.Append(file);
158  cmd.Append(" ");
159  cmd.Append(id);
160  // Execute the command
161  std::cout << "Will now execute \"" << cmd << "\"" << std::endl;
162  ret = system(cmd.Data());
163  if (WIFEXITED(ret)) {
164  ret = WEXITSTATUS(ret);
165  if (ret != 0)
166  std::cout << "Failed to execute \"" << cmd << "\" -> "
167  << ret << std::endl;
168  }
169  else if (WIFSIGNALED(ret)) {
170  int sig = WTERMSIG(ret);
171  std::cout << "Process \"" << cmd << "\" received signal "
172  << sig << " (" << strsignal(sig) << ")" << std::endl;
173  ret = -1;
174  }
175  return ret;
176 }
177 
178 Int_t
179 UploadFiles(const char** files, const char** ids, bool useOwn, bool verb=true)
180 {
181  const char** pFile = files;
182  const char** pId = ids;
183 
184  if (verb)
185  std::cout << "Uploading files to FXS" << std::endl;
186  Int_t ret = 0;
187  while (*pFile && *pId) {
188  if (verb)
189  std::cout << " Upload " << *pFile << " (" << *pId << ") ... " << std::flush;
190  Int_t lret = -1;
191  if (useOwn) lret = myFesStore2(*pFile, *pId);
192  else lret = daqDA_FES_storeFile(*pFile, *pId);
193 
194  if (lret != 0) {
195  if (verb)
196  std::cout << "failed (" << lret << ")" << std::endl;
197  std::cerr << "Failed to upload " << *pFile << " to FES id "
198  << *pId << " - exit " << lret << std::endl;
199  ret++;
200  }
201  else {
202  if (verb) std::cout << "done" << std::endl;
203  std::ofstream touch(Form("%s.upload", *pFile));
204  touch << "Done" << std::endl;
205  touch.close();
206  }
207  pFile++;
208  pId++;
209  }
210  if (verb)
211  std::cout << "Done uploading with " << ret
212  << " errors" << std::endl;
213  return ret;
214 }
215 
216 
217 
218 #endif // FMDUTILDA_H
219 // Local Variables:
220 // mode: C++
221 // End:
222 
AliTPCcalibPID * pid
Definition: CalibPID.C:69
Int_t myFesStore2(const char *file, const char *id, bool verb=true)
Definition: FMDUtilda.h:108
const char * path
Int_t myFesStore(const char *file, const char *id, bool verb=true)
Definition: FMDUtilda.h:141
Int_t UploadFiles(const char **files, const char **ids, bool useOwn, bool verb=true)
Definition: FMDUtilda.h:179
Bool_t HasSeenDetector(UShort_t d) const
Definition: AliFMDBaseDA.h:403
int system_alternative(const char *pgm, char *const argv[])
Definition: FMDUtilda.h:76
Bool_t PostSummaries(AliFMDBaseDA &da, const char *prefix, Int_t runNumber, bool verb=true)
Definition: FMDUtilda.h:16
char * prefix
static Int_t runNumber
Definition: pdc06_config.C:126
const Array & GetSummaries() const
Definition: AliFMDBaseDA.h:133