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