GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
adf/Macros/EmulatorControl.C
Go to the documentation of this file.
1 
2 
3 #include "EmulatorControl.h"
4 #include "FrameDispatcher.h"
5 
6 // root/system includes
7 #include "TRegexp.h"
8 #include "TSystem.h"
9 #include "TSystemDirectory.h"
10 
11 #include <fstream>
12 
13 using namespace Gw;
14 
15 //ClassImp(EmulatorControl);
16 
17 void EmulatorControl::Run(UInt_t max_loop)
18 {
19  if ( fIsRunning ) {// already in loop
20  fBashColor->SetWarningOut();
21  std::cout << "Emulator already running " << std::endl;
22  fBashColor->ResetColor();
23  return;
24  }
25  fIsRunning = true; fNbLoop = max_loop;
26 
27  fTimer->SetObject(this);
28  fTimer->Start(20, kTRUE);
29 }
30 
32 {
33  if ( !fIsRunning ) {// already stopped
34  // fBashColor->SetWarningOut();
35  // std::cout << "Emulator is not running " << std::endl;
36  // fBashColor->ResetColor();
37  return;
38  }
39  else
40  {
41  fIsRunning = false;
42  fTimer->SetObject(this);
43  fTimer->Stop();
44  }
45 }
46 
48 {
49  delete fEmulator;
50 }
51 
52 void EmulatorControl::Print(Option_t * /*option*/) const
53 {
54  fBashColor->SetInfoOut();
55 
56  std::cout<<"Watchers properties:"<<std::endl;
57 
58  FrameDispatcher *fd =
59  dynamic_cast<FrameDispatcher *> (fEmulator->GetBaseConsumer()) ;
60 
61  const TList *ListOfWatchers = fd->GetListOfWatchers();
62  TIter iter(ListOfWatchers);
63  TObject *o;
64 
65  while( (o = iter.Next()) )
66  {
67  o->Print();
68  }
69 
70  fBashColor->ResetColor();
71 }
72 
73 
74 void EmulatorControl::Save(Option_t *opt)
75 {
76  fEmulator->GetBaseConsumer()->GetFrameIO().Print();
77 
78  FrameDispatcher *fd =
79  dynamic_cast<FrameDispatcher *> (fEmulator->GetBaseConsumer()) ;
80  if ( fd )
81  fd->Snapshot(opt);
82 }
83 
84 Bool_t EmulatorControl::HandleTimer(TTimer* /*timer*/)
85 {
86  if ( fEmulator )
87  fEmulator->Run(fNbLoop);
88  return true;
89 }
90 
92 TString BaseEmulatorChain::DoROOTAFPConf(Int_t which_run, const char *path, const char *which_adf_data, const char *run_pat, Option_t *opt_adf_pattern, const char *tmp_rootafp_filename)
93 {
94  TString result = "", t_path = path, path_in_exp = which_adf_data, opt = opt_adf_pattern, run_name_pattern=run_pat; Bool_t ok_dir = false;
95 
96  if ( !t_path.EndsWith("/") )
97  t_path += "/";
98 
99  // First it looks at the list of sub-directory in path to search from one containing the run number
100  TString dir_name = ""; TSystemDirectory dir; dir.SetDirectory(t_path.Data());
101  //
102  TList *list = dir.GetListOfFiles();
103  TIter iter(list);
104  TObject *entry;
105  //
106  while ( (entry = iter()) )
107  {
108  if ( entry->InheritsFrom("TSystemDirectory") )
109  {
110 
111  TString fname = entry->GetName();
112  if ( fname.Contains( Form("%s%4.4d",run_pat,which_run) ) ) {
113  dir_name = t_path.Data();
114  dir_name += entry->GetName();
115  if ( path_in_exp.Sizeof() ) {
116  if ( !path_in_exp.BeginsWith("/") )
117  dir_name += "/";
118  dir_name += which_adf_data;
119  if ( !path_in_exp.EndsWith("/") )
120  dir_name += "/";
121  }
122  result = entry->GetName();
123  ok_dir = true;
124 
125  break;
126  }
127  }
128  }
129  if ( !ok_dir ) {
130  std::cout << " **** !! **** Run # " << which_run << " has not been found in path " << path << std::endl;
131  return result;
132  }
133  else {
134  if ( !dir_name.EndsWith("/") )
135  dir_name += "/";
136  std::cout << " Run # " << which_run << " has been found in path " << path << " - full directory is " << dir_name << std::endl;
137  }
138 
139  TSystemDirectory dir_run; dir_run.SetDirectory(dir_name); TList list_of_final_file;
140  //
141  list = dir_run.GetListOfFiles();
142  if ( list == 0x0 ) {
143  result = "";
144  std::cout << dir_name << " Is Empty " << std::endl;
145  return result; // no files with such a run number
146  }
147  list->Sort();
148  TIter iter_file(list); // start collecting .adf file
149  //
150  while ( (entry = iter_file()) )
151  {
152  TString fname = entry->GetName();
153 
154  Bool_t wildcard = false;
155  if ( opt.Contains("*") )
156  wildcard = true;
157  TRegexp all("*",kTRUE), pattern(opt.Data(),wildcard);
158 
159  if ( pattern.Status() != TRegexp::kOK )
160  {
161  if ( gDebug > 0 )
162  std::cerr << "pattern [*] intead of " << opt.Data()<< std::endl;
163  pattern = all;
164  }
165  else
166  {
167  if ( gDebug > 0 ) std::cerr << "pattern " << opt.Data() << std::endl ;
168  }
169  if ( entry->InheritsFrom("TSystemFile") )
170  {
171  if ( fname.Contains( pattern ) ) {
172  list_of_final_file.Add( new TNamed(dir_name.Data(),fname.Data()) );
173  std::cout << " + ADD ADF " << fname.Data() << " file to the list of files to be read " << std::endl;
174  }
175  }
176  }
177  if ( list_of_final_file.GetEntries() ) {
178  std::ofstream outputfile(tmp_rootafp_filename);
179  std::cout << " A new file, " << tmp_rootafp_filename << ", suitable to configure the RootAFP producer, has been created " << std::endl;
180  for ( Int_t i = 0; i < list_of_final_file.GetSize(); i++ )
181  {
182  outputfile << "l " << list_of_final_file.At(i)->GetName() << list_of_final_file.At(i)->GetTitle() << std::endl;
183  }
184  outputfile.close();
185  }
186  else result = "";
187 
188  RunNumber = which_run; RunDir = result; // keep it
189 
190  return result;
191 }
192 
193 Bool_t BaseEmulatorChain::Init(const Char_t *rootafpconf)
194 {
195  TString RootAFPConf = rootafpconf; Bool_t is_default_rootafp = false;
196  //
197  if ( RootAFPConf == "" ) { // just use standart RootAFP.conf file
198  is_default_rootafp = true;
199  }
200  else { // use the one given in argument ... test if it exists !
201  if ( gSystem->AccessPathName(rootafpconf) ) {
202  std::cout << " File " << rootafpconf << " does not exist ! - Cannot proceed " << std::endl;
203  return false;
204  }
205  // then keep current RootAFP and copy this one into RootAFP.conf just the time to initiate the emulator
206  gSystem->Rename("RootAFP.conf",".RootAFP.conf.keep"); gSystem->CopyFile(rootafpconf,"RootAFP.conf");
207  }
208 
209  // chain a producer and a FrameDispacher to which Watchers are attached
210  if ( ! this->Init() ) {
211  if ( !is_default_rootafp )
212  gSystem->Rename(".RootAFP.conf.keep","RootAFP.conf"); std::cout << "Cannot Init emulator" << std::endl;
213  return false;
214  }
215  else {
216  if ( !is_default_rootafp )
217  gSystem->Rename(".RootAFP.conf.keep","RootAFP.conf");
218  // emulator->GetBaseConsumer()->SetName();
219  }
220 
221  return true;
222 }
223 
224 Bool_t BaseEmulatorChain::Init(Int_t which_run, const char *which_experiment, const char *which_adf_data, const char *run_name_pattern, Option_t *opt_adf_pattern)
225 {
226  TString result = BaseEmulatorChain::DoROOTAFPConf(which_run,which_experiment,which_adf_data,run_name_pattern,opt_adf_pattern,".RootAFP.conf.tmp");
227  if ( result == "" ) {
228  return false;
229  }
230  return Init(".RootAFP.conf.tmp");
231 }
232 
233 
Process a buffer, trigg and dispachs the work to sub-watchers.
void Save(Option_t *opt="*")
Save the spectra.
virtual void Print(Option_t *option="") const
virtual Bool_t Init()
It should be overwritten !
static const TList * GetListOfWatchers()
Definition: Watchers.h:63
void Run(UInt_t max_loop=kMaxUInt)
Run the emulator.
virtual UInt_t Snapshot(Option_t *="*")
change kind of Watcher
Definition: Watchers.cpp:534
ADF::LogMessage & endl(ADF::LogMessage &log)