Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / FMD / DA / FMDUtilda.h
CommitLineData
e0c60e77 1#ifndef FMDUTILDA_H
2#define FMDUTILDA_H
3#include "AliFMDBaseDA.h"
4#include "daqDA.h"
5#include <stdexcept>
6#include <iostream>
32d43296 7#include <fstream>
81276ad2 8#include <cstdlib>
e0c60e77 9#ifdef ALI_AMORE
10# include <AmoreDA.h>
11# include <TH2.h>
12# include <TSystem.h>
13#endif
14
32d43296 15Bool_t
3490bd31 16PostSummaries(AliFMDBaseDA& da, const char* prefix, Int_t runNumber, bool verb=true)
e0c60e77 17{
18#ifndef ALI_AMORE
19 std::cerr << "AMORE not found - nothing posted" << std::endl;
32d43296 20 return true;
e0c60e77 21#else
3490bd31 22 if (verb)
23 std::cout << "Posting " << prefix << " data to AMORE" << std::endl;
e0c60e77 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);
3490bd31 37 if (verb)
38 std::cout << " Posting " << prefix << "RuNoFMD" << det << " ... "
39 << std::flush;
e0c60e77 40 myAmore.Send(Form("%sRunNoFMD%d", prefix, det), runNo);
3490bd31 41 if (verb)
42 std::cout << "done" << std::endl;
e0c60e77 43 }
44
45 TIter next(&da.GetSummaries());
3490bd31 46 if (verb) {
47 std::cout << " Will post summaries: " << std::endl;
48 da.GetSummaries().ls();
49 }
e0c60e77 50 TObject* obj = 0;
3490bd31 51 while ((obj = next())) {
52 if (verb)
53 std::cout << " Posting " << obj->GetName()
54 << " ... " << std::flush;
e0c60e77 55 myAmore.Send(obj->GetName(), obj);
3490bd31 56 if (verb)
57 std::cout << "done" << std::endl;
58 }
e0c60e77 59
60 }
61 catch (std::exception& e) {
62 std::cerr << "Failed to make AMORE instance: " << e.what() << std::endl;
32d43296 63 return false;
e0c60e77 64 }
3490bd31 65 if (verb)
66 std::cout << "All objects posted to AMORE" << std::endl;
32d43296 67 return true;
e0c60e77 68#endif
69}
70
32d43296 71#include <sys/types.h>
72#include <sys/wait.h>
73#include <cstring>
74#include <cerrno>
75
76int 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
108Int_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
141Int_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
e0c60e77 178Int_t
32d43296 179UploadFiles(const char** files, const char** ids, bool useOwn, bool verb=true)
e0c60e77 180{
181 const char** pFile = files;
182 const char** pId = ids;
183
3490bd31 184 if (verb)
185 std::cout << "Uploading files to FXS" << std::endl;
e0c60e77 186 Int_t ret = 0;
3490bd31 187 while (*pFile && *pId) {
188 if (verb)
189 std::cout << " Upload " << *pFile << " (" << *pId << ") ... " << std::flush;
32d43296 190 Int_t lret = -1;
191 if (useOwn) lret = myFesStore2(*pFile, *pId);
192 else lret = daqDA_FES_storeFile(*pFile, *pId);
193
e0c60e77 194 if (lret != 0) {
3490bd31 195 if (verb)
32d43296 196 std::cout << "failed (" << lret << ")" << std::endl;
e0c60e77 197 std::cerr << "Failed to upload " << *pFile << " to FES id "
32d43296 198 << *pId << " - exit " << lret << std::endl;
e0c60e77 199 ret++;
200 }
32d43296 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 }
e0c60e77 207 pFile++;
208 pId++;
209 }
3490bd31 210 if (verb)
211 std::cout << "Done uploading with " << ret
212 << " errors" << std::endl;
e0c60e77 213 return ret;
214}
215
216
217
218#endif // FMDUTILDA_H
219// Local Variables:
220// mode: C++
221// End:
222