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