GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ADF_to_Tree.C
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) IPNL, IN2P3, CNRS *
3  * Contibutor(s) : *
4  * Olivier Stezowski stezow(AT)ipnl.in2p3.fr [2014] *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20  ***************************************************************************/
21 
22 
24 void ADF_to_Tree(const Char_t *rootafpconf = "");
26 void ADF_to_Tree(Int_t Run,
27  const char *which_experiment = "/agatadisks/eagata/238u_comm_1/",
28  const char *which_adf_data = "/Out/Global/",
29  const char *run_name_pattern = "run",
30  const char *opt_adf_pattern = "*.adf",
31  const char *which_output_dir="");
32 
33 #if !defined(__CINT__)
34 
35 // gammaware/watchers includes
36 #include "WatchersConfig.h"
37 #include "FrameDispatcher.h"
38 #include "Trigger.h"
39 #include "TTreeWatchers.h"
40 #include "GANILWatchers.h"
41 #include "TSystemDirectory.h"
42 #include "ADTree.h"
43 #include "EmulatorControl.h"
44 #include "Emulator.h"
45 #include "RootAFP.h"
46 
47 #include "BashColor.h"
48 
49 // root/system includes
50 #include "TRegexp.h"
51 #include "TSystem.h"
52 
54 namespace {
55  TString PathToOut = "./Out"; // path where to store TTrees
56  TString PatternOfROOTFiles = "AGATA_VAMOS_Tree_"; // basename for ROOT files
57  TString TTreeName = "TreeMaster" ; // name of the Tree
58  TString TTeeTilte = "GANIL Tree" ; // Title
59 
60  TString Run = "" ; // used to add run number by hand or automatically
61  Int_t RunNr = -1;
62  Bool_t OverWriteFile = false; // used to avoid to increment the output file name if already existing
63  BashColor *fBashColor = new BashColor();
64 }
65 
66 void PrintStats(BaseEmulatorChain *emulator);
67 
68 //
70 {
71  // Trigger means event:data with inside a data:tracked AND an event:data:pas AND an ancillary
72  AgataFrameTrigger *trig = AgataFrameTrigger::Build("Event", "event:data data:tracked event:data:psa data:ranc0");
73  fd->SetTrigger(trig);
74 
75  // ROOT Tree with Raw Branch and VAMOS one
76  TTreeMaster *rootmaster = fd->AddTreeMaster(Form("%s%s",PatternOfROOTFiles.Data(),Run.Data()) , TTeeTilte.Data(), OverWriteFile);
77  // This adds a VAMOS Watcher. The data containt depemds on the MODE
78  VAMOSWatcher *vamosw = rootmaster->Add<VAMOSWatcher>("GVWInterface","Branch managed by the VAMOS librarie");
79  vamosw->InitVAMOS(2,Form("%s/VAMOS_Spectra_%s.root",PathToOut.Data(),Run.Data()),RunNr,PathToOut.Data());
80  // this compute from VAMOS the Doppler correction used in the branches/watcher following this
81  rootmaster->AddWatcher<VAMOSVertexBuilder>("VAMOSVertexBuilder","To Compute a vertex from VAMOS library");
82  // // Tracked Part
83  ADTrackTree *trackw = rootmaster->Add<ADTrackTree>("TrackTree","Branch on track");
84  trackw->SetAgataOffset(0.);
85  // Hit part
86  rootmaster->Add<ADHitTree>("HitTree","Branch on hits");
87 }
88 
89 void ADF_to_Tree(const Char_t *rootafpconf)
90 {
92  if ( ! emulator->Init(rootafpconf) ) {
93  return;
94  }
95  // Setup i.e. attached the Watchers
96  TTreeMaster::SetDefaultPathAndName(PathToOut.Data(),TTreeName.Data());
98 
99  // loop
100  emulator->Run();
101 
102  PrintStats(emulator);
103 
104  emulator->GetBaseConsumer()->GetFrameIO().Print();
105 }
106 
107 void ADF_to_Tree(Int_t which_run, const char *which_experiment, const char *which_adf_data, const char *run_name_pattern, const char *opt_adf_pattern, const char *which_output_dir)
108 {
110  if ( ! emulator->Init(which_run,which_experiment,which_adf_data,run_name_pattern,opt_adf_pattern) ) {
111  return;
112  }
113  Run = emulator->GetRunDir();
114  RunNr = which_run;
115  if(which_output_dir != "")
116  PathToOut = which_output_dir ;
117 
118  if(!gSystem->OpenDirectory(PathToOut.Data()))
119  if(gSystem->mkdir(PathToOut.Data()) == -1)
120  {
121  fBashColor->SetErrorOut();
122  cout << "ERROR : Could not create Output directory : " << PathToOut << endl;
123  fBashColor->ResetColor();
124  return ;
125  }
126  else
127  {
128  fBashColor->SetInfoOut();
129  cout << "INFO : Creating new output directory : " << PathToOut << endl;
130  fBashColor->ResetColor();
131  }
132  else
133  {
134  fBashColor->SetInfoOut();
135  cout << "INFO : Writing in output directory : " << PathToOut << endl;
136  fBashColor->ResetColor();
137  }
138 
139  TTreeMaster::SetDefaultPathAndName(PathToOut.Data(),TTreeName.Data());
140 
141 
142  ADF_to_Tree_UserSetup(emulator->GetConsumer());
143 
144  // loop
145  emulator->Run();
146 
147  PrintStats(emulator);
148 
149  emulator->GetBaseConsumer()->GetFrameIO().Print();
150 }
151 
153 {
154  fBashColor->SetInfoOut();
155 
156  std::cout<<"Watchers properties:"<<std::endl;
157 
158  FrameDispatcher *fd = dynamic_cast<FrameDispatcher *> (emulator->GetBaseConsumer()) ;
159 
160  const TList *ListOfWatchers = fd->GetListOfWatchers();
161  TIter iter(ListOfWatchers);
162  TObject *o;
163 
164  while( (o = iter.Next()) )
165  {
166  o->Print();
167  }
168 
169  fBashColor->ResetColor();
170 }
171 
172 /*
174  void ADF_to_Tree(const Char_t *rootafpconf)
175  {
176  TString RootAFPConf = rootafpconf; Bool_t is_default_rootafp = false;
177  //
178  if ( RootAFPConf == "" ) { // just use standart RootAFP.conf file
179  is_default_rootafp = true;
180  }
181  else { // use the one given in argument ... test if it exists !
182  if ( gSystem->AccessPathName(rootafpconf) ) {
183  std::cout << " File " << rootafpconf << " does not exist ! - Cannot proceed " << std::endl; return;
184  }
185  // then keep current RootAFP and copy this one into RootAFP.conf just the time to initiate the emulator
186  gSystem->Rename("RootAFP.conf",".RootAFP.conf.keep"); gSystem->CopyFile(rootafpconf,"RootAFP.conf");
187  }
188 
189  // chain a producer and a FrameDispacher to which Watchers are attached
190  EmulatorPC<RootAFP,FrameDispatcher> *emulator = new EmulatorPC<RootAFP,FrameDispatcher>("./");
191  if ( ! emulator->Init() ) {
192  if ( !is_default_rootafp )
193  gSystem->Rename(".RootAFP.conf.keep","RootAFP.conf"); std::cout << "Cannot Init emulator" << std::endl;
194  return;
195  }
196  else {
197  if ( !is_default_rootafp )
198  gSystem->Rename(".RootAFP.conf.keep","RootAFP.conf");
199  // emulator->GetBaseConsumer()->SetName();
200  }
201 
202  // Setup i.e. attached the Watchers
203  TTreeMaster::SetDefaultPathAndName(PathToOut.Data(),TTreeName.Data());
204  ADF_to_Tree_UserSetup(emulator->GetConsumer());
205 
206  // loop
207  emulator->Run();
208  emulator->GetBaseConsumer()->GetFrameIO().Print();
209  }
210 
212  TString 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)
213  {
214  TString result = "", t_path = path, path_in_exp = which_adf_data, opt = opt_adf_pattern, run_pattern=run_pat; ; Bool_t ok_dir = false;
215  if ( !t_path.EndsWith("/") )
216  t_path += "/";
217 
218  // First it looks at the list of sub-directory in path to search from one containing the run number
219  TString dir_name = ""; TSystemDirectory dir; dir.SetDirectory(t_path.Data());
220  //
221  TList *list = dir.GetListOfFiles();
222  TIter iter(list);
223  TObject *entry;
224  //
225  while ( (entry = iter()) )
226  {
227  if ( entry->InheritsFrom("TSystemDirectory") )
228  {
229  TString fname = entry->GetName();
230  if ( fname.Contains( Form("%s%4.4d",run_pattern,which_run) ) ) {
231  dir_name = t_path.Data();
232  dir_name += entry->GetName();
233  if ( path_in_exp.Sizeof() ) {
234  if ( !path_in_exp.BeginsWith("/") )
235  dir_name += "/";
236  dir_name += which_adf_data;
237  if ( !path_in_exp.EndsWith("/") )
238  dir_name += "/";
239  }
240  result = entry->GetName();
241  ok_dir = true;
242 
243  break;
244  }
245  }
246  }
247  if ( !ok_dir ) {
248  std::cout << " **** !! **** Run # " << which_run << " has not been found in path " << path << std::endl;
249  return result;
250  }
251  else {
252  if ( !dir_name.EndsWith("/") )
253  dir_name += "/";
254  std::cout << " Run # " << which_run << " has been found in path " << path << " - full directory is " << dir_name << std::endl;
255  }
256 
257  TSystemDirectory dir_run; dir_run.SetDirectory(dir_name); TList list_of_final_file;
258  //
259  list = dir_run.GetListOfFiles();
260  if ( list == 0x0 ) {
261  result = "";
262  std::cout << dir_name << " Is Empty " << std::endl;
263  return result; // no files with such a run number
264  }
265 
266  TIter iter_file(list); // start collecting .adf file
267  //
268  while ( (entry = iter_file()) )
269  {
270  TString fname = entry->GetName();
271 
272  Bool_t wildcard = false;
273  if ( opt.Contains("*") )
274  wildcard = true;
275  TRegexp all("*",kTRUE), pattern(opt.Data(),wildcard);
276 
277  if ( pattern.Status() != TRegexp::kOK )
278  {
279  if ( gDebug > 0 )
280  std::cerr << "pattern [*] intead of " << opt.Data()<< std::endl;
281  pattern = all;
282  }
283  else
284  {
285  if ( gDebug > 0 ) std::cerr << "pattern " << opt.Data() << std::endl ;
286  }
287  if ( entry->InheritsFrom("TSystemFile") )
288  {
289  if ( fname.Contains( pattern ) ) {
290  list_of_final_file.Add( new TNamed(dir_name.Data(),fname.Data()) );
291  std::cout << " + ADD ADF " << fname.Data() << " file to the list of files to be read " << std::endl;
292  }
293  }
294  }
295  if ( list_of_final_file.GetEntries() ) {
296  std::ofstream outputfile(tmp_rootafp_filename);
297  std::cout << " A new file, " << tmp_rootafp_filename << ", suitable to configure the RootAFP producer, has been created " << std::endl;
298  for ( Int_t i = 0; i < list_of_final_file.GetSize(); i++ )
299  {
300  outputfile << "l " << list_of_final_file.At(i)->GetName() << list_of_final_file.At(i)->GetTitle() << std::endl;
301  }
302  outputfile.close();
303  }
304  else result = "";
305 
306  return result;
307  }
308 
310  void ADF_to_Tree(Int_t which_run, const char *which_experiment, const char *which_adf_data, Option_t *opt_adf_pattern)
311  {
312  TString result = DoROOTAFPConf(which_run,which_experiment,which_adf_data,opt_adf_pattern,".RootAFP.conf.tmp");
313  if ( result == "" ) {
314  return;
315  }
316  Run = result;
317 
318  ADF_to_Tree(".RootAFP.conf.tmp");
319  }
320  */
321 #endif
322 
323 
TString GetRunDir() const
to get back the RunDirectory
virtual Bool_t Run(UInt_t max_loop=kMaxUInt_t)
loop
Watcher_T * AddWatcher(const Char_t *name, const Char_t *title)
Allocate a standard watcher i.e. not a tree builder.
Process a buffer, trigg and dispachs the work to sub-watchers.
Watcher_T * Add(const Char_t *name, const Char_t *title, Short_t do_spectra, Short_t do_tree)
Allocate a watcher in charge of filling one part of the tree ==> Should inherits from TreeBuilder...
virtual Bool_t SetTrigger(ADF::DFTrigger *)
This is the default trigger for any added watcher.
Cons_actor * GetConsumer()
header file for Trigger.cpp
AD ROOT TTree branches for the hit part.
Definition: ADTree.h:129
header file for GANILWatchers.C
virtual ADF::NarvalConsumer * GetBaseConsumer()
To be used for Agata Data Flow.
Definition: Trigger.h:462
to have a common base class to be able to control the emulator through a TTask
static const TList * GetListOfWatchers()
Definition: Watchers.h:63
virtual void SetAgataOffset(Double_t offset=0.0)
set the agata offset in case it is not @ the optimal position
TTreeMaster * AddTreeMaster(const Char_t *name, const Char_t *title, Bool_t Overwrite=false, ADF::DFTrigger *t=0x0)
NarvalConsumer * GetBaseConsumer()
virtual Bool_t Init(const Char_t *rootafp)
Init the emulator using a different root afp file for the producer.
header file for RootAFP.cpp
Watcher to build a tree with branches processed in several watchers.
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
Definition: FrameIO.cpp:571
AD ROOT TTree branches for the tracked part.
Definition: ADTree.h:77
void ADF_to_Tree_UserSetup(Gw::FrameDispatcher *fd)
Definition: ADF_to_Tree.C:69
void ADF_to_Tree(const Char_t *rootafpconf="")
Transform ADF files into a ROOT Tree using the RootAFP.conf given as argument. No Arguments means Roo...
Definition: ADF_to_Tree.C:89
void PrintStats(BaseEmulatorChain *emulator)
Definition: ADF_to_Tree.C:152