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