GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCondition.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "GCondition.h"
22 
23 #include "PeakCreator.h"
24 #include "Env.h"
25 #include "Peak1D.h"
26 
27 #include "TRegexp.h"
28 #include "TLeaf.h"
29 #include "TH2.h"
30 #include "TCanvas.h"
31 #include "TCut.h"
32 #include "TCutG.h"
33 #include "TFile.h"
34 #include "TROOT.h"
35 #include "TKey.h"
36 
37 using namespace Gw;
38 
40 
42  TNamed("AGCondition","x"),
43  fListOfConditions(),
44  fEnable(true),
45  fExpectedResult(true),
46  fHasPassed(false),
47  fHasPassed_()
48 {
51  //
52  fListOfConditions.SetName("ListOfGConditions");
53 }
54 
55 GCondition::GCondition(const char * name, const char * title) :
56  TNamed(name,title),
57  fListOfConditions(),
58  fEnable(true),
59  fExpectedResult(true),
60  fHasPassed(false),
61  fHasPassed_()
62 {
65  //
66  fListOfConditions.SetName("ListOfGConditions");
67 }
68 
70  TNamed(p)
71 {
72  TObject *obj; TIter next(&p.fListOfConditions);
73  while ( (obj = next()) ) {
74  fListOfConditions.Add(obj->Clone());
75  }
76 
78 }
79 
80 
82 {
83  fListOfConditions.Clear();
84 }
85 
86 void GCondition::Delete(Option_t* option)
87 {
88  fListOfConditions.Clear(option);
89 }
90 
91 void GCondition::UseAsAnti(Bool_t r)
92 {
93  Style_t hash_style = 1; // 1 for coinc, 9 for anti
94 
95  if ( r == true )
96  hash_style = 9;
97 
98  TObject *obj; TIter next(&fListOfConditions);
99  while ( (obj = next()) ) {
100  if ( obj->InheritsFrom("TAttLine") ) {
101  dynamic_cast<TAttLine *>(obj)->SetLineColor(29);
102  dynamic_cast<TAttLine *>(obj)->SetLineWidth(2);
103  dynamic_cast<TAttLine *>(obj)->SetLineStyle(hash_style);
104  }
105  }
106 
107  fExpectedResult = !r;
108 }
109 
111 {
112  fEnable = !fEnable;
113 }
114 
115 void GCondition::Print(Option_t * option) const
116 {
117  TObject *obj; TIter next(&fListOfConditions);
118  std::cout << "Number of Condition in " << GetName() << " " << fListOfConditions.GetEntries() << std::endl;
119  while ( (obj = next()) ) {
120  obj->Print(option);
121  }
122 }
123 
124 
126 
128  GCondition(),
129  fVarX("X")
130 {
131 
132 }
133 
134 GCond1D::GCond1D(const char * name, const char * title) :
135  GCondition(name,title),
136  fVarX("X")
137 {
138 
139 }
140 
142  GCondition(p),
143  fVarX(p.fVarX)
144 {
145  ;
146 }
147 
149 {
150 
151 }
152 
154 {
155  Gw::PeakCreator *pc = 0x0; TVirtualPad *c_pad = 0x0; TString which_canvas(c_name);
156 
157  if ( which_canvas == "" )
158  c_pad = TVirtualPad::Pad();
159  else
160  c_pad = (TVirtualPad *)gROOT->GetListOfCanvases()->FindObject(c_name);
161 
162  if ( !c_pad )
163  return pc;
164 
165  if ( !Gw::PeakCreator::IsPeakCreator(c_pad) ) {
166  //
167  Style_t hash_style = gCoincStyle; // 3004 for coinc, 3005 for anti
168  if ( fExpectedResult == false )
169  hash_style = gAnticStyle;
170  //
172  if ( pc ) {
173  pc->SetGateMode(true);
174  ((Peak1D *)pc->DefaultPeak())->SetFillStylePeak(hash_style);
175  }
176  }
177 
178  return pc;
179 }
180 
181 void GCond1D::UseAsAnti(Bool_t r)
182 {
183  Style_t hash_style = gCoincStyle; // 3004 for coinc, 3005 for anti
184 
185  if ( r == true )
186  hash_style = gAnticStyle;
187 
188  TObject *obj; TIter next(&fListOfConditions);
189  while ( (obj = next()) ) {
190  Gw::Peak1D *p = dynamic_cast<Gw::Peak1D *> (obj);
191  p->SetFillStylePeak(hash_style);
192  }
193 
194  fExpectedResult = !r;
195 }
196 
197 void GCond1D::Draw(Option_t* /*option*/)
198 {
199  TVirtualPad *c_pad = TVirtualPad::Pad();
200 
201  if ( ! c_pad )
202  return;
203 
204  // it could be fListOfConditions.Draw. However some old version of root (5.24) uses option to select particular object of the collection to be drawn (based on expression)
205  // so appaen current pas by myself
206  TObject *obj; TIter next(&fListOfConditions);
207  while ( (obj = next()) ) {
208  if ( c_pad->GetListOfPrimitives()->FindObject(obj) == 0x0 )
209  c_pad->GetListOfPrimitives()->Add(obj,"lg");
210  c_pad->Modified();
211  }
212  if ( fListOfConditions.GetEntries() ) {
213  WindowGCreator();
214  c_pad->Update();
215  }
216 }
217 
218 Gw::Peak1D *GCond1D::AddWindow(Double_t min, Double_t max, Option_t *opt)
219 {
220  TString option(opt); Double_t xmin = TMath::Min(min,max), xmax = TMath::Max(min,max);
221  //
222  if ( !option.Contains("+") )
223  fListOfConditions.Clear();
224 
225  Gw::Peak1D *p = new Gw::Peak1D( Form("%s_%d",GetName(),fListOfConditions.GetEntries()), GetTitle() );
226  p->SetPeak((xmin+xmax)/2., 1., (xmax-xmin) );
227 
228  if ( !fEnable )
229  fEnable = true;
230 
231  p->SetBit(kCanDelete,false);
232  p->SetBit(kMustCleanup,false);
233 
234  if ( fExpectedResult )
236  else
238 
239  gROOT->GetListOfSpecials()->Add(p);
240 
241  // add to condition
242  fListOfConditions.Add(p); fHasPassed_.push_back(false);
243 
244  return p;
245 }
246 
247 Bool_t GCond1D::IsPassed(Double_t x, Double_t /*y*/) const
248 {
249  if ( !fEnable || fListOfConditions.GetEntries() == 0 ) {
250  fHasPassed = true; return fHasPassed;
251  }
252 
253  Bool_t is_inside = false;
254 
255  for (Int_t i = 0; i < fListOfConditions.GetEntries(); i++) {
257  Double_t p = peak->GetPosition();
258  Double_t w = peak->GetFWHM()/2.;
259  if ( x >= (p-w) && x <= (p+w) ) {
260  is_inside = true;
262  }
263  else fHasPassed_[i] = !fExpectedResult;
264  }
265 
266  return (fHasPassed = (is_inside == fExpectedResult) );
267 
268 }
269 
270 Short_t GCond1D::Import(const char *what, const char *from, Option_t *opt)
271 {
272  TString wh(what), origin(from), option(opt), tmp, obj_name; Short_t result = 0; TDirectory *dir_to_search = gDirectory;
273  //
274  Bool_t wildcard = false;
275  if ( wh.Contains("*") )
276  wildcard = true;
277  TRegexp rexp(Form("%s",what),wildcard);
278 
279  // start with empty container if option + is not there
280  if ( !option.Contains("+") )
281  fListOfConditions.Clear();
282 
283 // Treat root file/dir cases
284  if ( !origin.Contains(":") ) { // means local root
285  if ( origin == "gdir" || origin == "gpad" ) {
286  ;
287  }
288  else origin.Prepend("file:");
289  }
290  if ( origin.BeginsWith("file:") ||
291  origin.BeginsWith("root:") ||
292  origin.BeginsWith("roots:") ||
293  origin.BeginsWith("rootk:") ||
294  origin.BeginsWith("http:") )
295  {
296  TString rname = origin;
297  if ( origin.BeginsWith("file:") )
298  rname.Replace(0,origin.First(":")+1,""); // only if local, remove file: protocol
299 
300  // TODO
301  // if ( "" ) ==> open window
302 
303 
304  // look for similar already open if not open a new one
305  TFile *f = 0x0;
306  if ( !origin.EndsWith(".root") ) {
307  rname.Append(".root"); origin.Append(".root");
308  }
309 
310  if ( gROOT->GetListOfFiles()->FindObject(rname.Data()) ) {// already there
311  std::cout << "Import CUTS from OPEN FILE " << rname.Data() << std::endl ;
312  f = (TFile *)gROOT->GetListOfFiles()->FindObject(rname.Data());
313  }
314  else {
315  std::cout << "Import CUTS from NEW FILE " << origin.Data() << std::endl;
316  f = TFile::Open(origin.Data(),"READ");
317  }
318  if ( f ) {
319  dir_to_search = f; origin = "gdir"; // even if not current dir !
320  }
321  else origin = "FAIL";
322  }
323 
324  if ( origin == "gdir" ) { // root file/directory
325 
326  // useful to browse the TDirectory
327  TString class_name; TObject *obj_in_file = 0x0; TKey *key_in_file; TList new_cuts; TCut *cut; Gw::Peak1D *peak;
328  new_cuts.SetOwner(true);
329 
330  // iterate over the key and keep only TCut and in case a TCut has been saved several time, only the highest cycle
331  TIter it_keys(dir_to_search->GetListOfKeys());
332  while ( (key_in_file = static_cast<TKey *>(it_keys())) ) { // next key
333  class_name = key_in_file->GetClassName();
334  obj_name = key_in_file->GetName();
335  // obj on disk is TCut and with a name not already in the list of Cond
336  if ( class_name == "TCut" && obj_name.Contains(rexp) && fListOfConditions.FindObject(obj_name.Data()) == 0x0 ) {
337  if ( !new_cuts.FindObject(obj_name.Data()) ) { // list of key is ordered so that first for a given name correspond to the highest cycle
338  obj_in_file =
339  key_in_file->ReadObj();
340  new_cuts.Add(obj_in_file);
341  }
342  }
343  }
344  TIter it_cuts(&new_cuts);
345  while ( (cut = static_cast<TCut *>(it_cuts())) ) { // next cut
346  peak = CutToPeak(cut);
347  if ( peak ) {
348 
349  obj_name = peak->GetName();
350  if ( !obj_name.BeginsWith(GetName()) ) {
351  peak->SetName(Form("%s_%s",GetName(),obj_name.Data()));
352  }
353  if ( fListOfConditions.FindObject(peak->GetName()) ) { // already a peak with same name in the list
354  peak->SetName( Form("%s_%d",GetName(), fListOfConditions.GetEntries()) );
355  }
356 
357  peak->SetDrawAs(Peak1D::kGate);
358 
359  if ( !fEnable )
360  fEnable = true;
361 
362  peak->SetBit(kCanDelete,false);
363  peak->SetBit(kMustCleanup,false);
364 
365  // gROOT->GetListOfSpecials()->Add(peak);
366 
367  // add to condition
368  fListOfConditions.Add(peak); fHasPassed_.push_back(false);
369 
370  result++;
371  }
372  else {
373  std::cout << " Cannot convert " << cut->GetName() << " to peak " << std::endl;
374  }
375  }
376  }
377  if ( origin == "gpad" ) { // root current pad
378  TSeqCollection *l_to_seach = 0x0;
379  if ( TVirtualPad::Pad() ) {
380  l_to_seach = TVirtualPad::Pad()->GetListOfPrimitives();
381  }
382  TObject* obj = 0x0;
383  TIter next(l_to_seach);
384  while ( (obj = next()) ) { // iterator skips empty slots
385  if ( obj->InheritsFrom("Gw::Peak1D") ) {
386  tmp = obj->GetName();
387  if ( tmp.Contains(rexp) ) {
388 
389  if ( fListOfConditions.FindObject(obj) ) { // already in the list
390  printf("%s ALREADY IN THE LIST \n", obj->GetName());
391  continue;
392  }
393 
394  // change some attributes
395  ((Gw::Peak1D*)obj)->SetDrawAs(Peak1D::kGate);
396  if ( !tmp.BeginsWith(GetName()) ) {
397  ((Gw::Peak1D*)obj)->SetName(Form("%s_%s",GetName(),tmp.Data()));
398  }
399  if ( fListOfConditions.FindObject(obj->GetName()) ) { // already a peak with same name in the list
400  ((Gw::Peak1D*)obj)->SetName( Form("%s_%d",GetName(), fListOfConditions.GetEntries()) );
401  }
402  ((Gw::Peak1D*)obj)->SetTitle(GetTitle());
403 
404  if ( !fEnable )
405  fEnable = true;
406 
407  obj->SetBit(kCanDelete,false);
408  obj->SetBit(kMustCleanup,false);
409 
410  // gROOT->GetListOfSpecials()->Add(obj);
411 
412  // add to condition
413  fListOfConditions.Add(obj); fHasPassed_.push_back(false);
414  result++;
415  }
416  }
417  }
418  }
419 
420  if ( fListOfConditions.GetEntries() > 0 ) {
421  printf("List of %d 1D Conditions \n",fListOfConditions.GetEntries() );
422  fListOfConditions.ls();
423  }
424 
425  return result;
426 }
427 
429 {
430  Gw::Peak1D *p = 0x0; Double_t min, max; std::string name_leaf1, name_leaf2; TString tmp(cut->GetTitle());
431 
432  // check good format
433  if ( !tmp.Contains("(") || !tmp.Contains(")") || !tmp.Contains("&&") || tmp.CountChar('>') != 1 || tmp.CountChar('<') != 1 || (tmp.CountChar('<') > tmp.First('>')) ) {
434  return p;
435  }
436  else {
437  tmp.ReplaceAll("(","");
438  tmp.ReplaceAll(")","");
439  tmp.ReplaceAll(">","");
440  tmp.ReplaceAll("<","");
441  tmp.ReplaceAll("=","");
442  tmp.ReplaceAll("&&","");
443 
444  tmp += " ";
445  }
446  std::istringstream decode(tmp.Data());
447  decode.clear();
448  decode >> name_leaf1 >> min >> name_leaf2 >> max ;
449  if ( decode.good() ) {
450  if ( name_leaf1 == name_leaf2 ) {
451 
452  p = new Gw::Peak1D(cut->GetName(),name_leaf1.data());
453 
454  Double_t xmin = TMath::Min(min,max), xmax = TMath::Max(min,max);
455  p->SetPeak((xmin+xmax)/2., 1., (xmax-xmin) );
457  }
458  }
459 
460  return p;
461 }
462 
463 TCut *GCond1D::PeakToCut(const Gw::Peak1D *peak) const
464 {
465  TCut *cut = 0x0;
466 
467  Double_t min = peak->GetPosition() - (peak->GetFWHM())/2. ;
468  Double_t max = peak->GetPosition() + (peak->GetFWHM())/2. ;
469 
470  cut = new TCut( peak->GetName(),Form("(%s <= %f && %s >= %f)",fVarX.Data(),min,fVarX.Data(),max) );
471 
472  return cut;
473 }
474 
476 {
478  if ( p ) {
479  if ( fVarX == p->GetTitle() ) {
480  p->SetTitle(GetTitle());
481  }
482  else { delete p; p = 0x0; }
483  }
484 
485  return p;
486 }
487 
488 
489 Short_t GCond1D::Export(const char *what, const char *to, Option_t *opt) const
490 {
491  TString wh(what), destination(to), option(opt), tmp("ROOT"), rname;
492  TDirectory *directory = gDirectory;
493  Short_t result = 0; TObject* obj = 0x0;
494 
495  if ( !destination.Contains(":") ) { // means local root
496  if ( destination != "gdir" ) {
497  destination.Prepend("file:");
498  }
499  }
500  if ( destination.BeginsWith("file:") ||
501  destination.BeginsWith("root:") ||
502  destination.BeginsWith("roots:") ||
503  destination.BeginsWith("rootk:") ||
504  destination.BeginsWith("http:") )
505  {
506  TString rname = destination;
507  if ( destination.BeginsWith("file:") )
508  rname.Replace(0,destination.First(":")+1,""); // only if local, remove file: protocol
509 
510  // TODO
511  // if ( "" ) ==> open window
512 
513 
514  // look for similar already open if not open a new one
515  TFile *f = 0x0;
516  if ( !destination.EndsWith(".root") ) {
517  rname.Append(".root"); destination.Append(".root");
518  }
519 
520  if ( gROOT->GetListOfFiles()->FindObject(rname.Data()) ) {// already there
521  std::cout << "SAVE CUTS in OPEN FILE " << rname.Data() << std::endl ;
522  f = (TFile *)gROOT->GetListOfFiles()->FindObject(rname.Data());
523  }
524  else {
525  std::cout << "SAVE CUTS in OPEN FILE " << destination.Data() << std::endl;
526  f = TFile::Open(destination.Data(),"UPDATE");
527  }
528  if ( f ) {
529  directory = f;
530  }
531  else tmp = "FAIL";
532  }
533 
534  if ( tmp == "ROOT" ) {
535 
536  Bool_t wildcard = false;
537  if ( wh.Contains("*") )
538  wildcard = true;
539  TRegexp rexp(Form("%s",what),wildcard);
540 
541  TIter next(&fListOfConditions);
542  while ( (obj = next()) ) { // iterator skips empty slots
543  tmp = obj->GetName();
544  if ( tmp.Contains(rexp) ) {
545  TCut *cut = PeakToCut((Gw::Peak1D*)obj);
546  if ( cut ) {
547  tmp = cut->GetName(); // make sure the cut start with CONDNAME_
548  if ( !tmp.BeginsWith(GetName()) ) {
549  cut->SetName(Form("%s_%s",GetName(),cut->GetName()));
550  }
551 
552  directory->WriteTObject(cut,"",option.Data());
553  result++;
554  }
555  }
556  }
557  }
558  // other format
559  /*
560  if ( tmp == "GSI" ) {
561 
562  }
563  */
564 
565  return result;
566 }
567 
568 
570 
572  GCondition()
573 {
574 
575 }
576 
577 GCond2D::GCond2D(const char * name, const char * title) :
578  GCondition(name,title)
579 {
580 
581 }
582 
584  GCondition(p),
585  fVarX(p.fVarX),
586  fVarY(p.fVarY)
587 {
588  ;
589 }
590 
592 {
593 
594 }
595 
596 TCutG *GCond2D::AddWindow(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax, Option_t *opt)
597 {
598  TString option(opt);
599 
600  Double_t xxmin = TMath::Min(xmin,xmax), xxmax = TMath::Max(xmin,xmax), yymin = TMath::Min(ymin,ymax), yymax = TMath::Max(ymin,ymax), xx[5], yy[5];
601 
602  xx[0] = xxmin; yy[0] = yymin;
603  xx[1] = xxmax; yy[1] = yymin;
604  xx[2] = xxmax; yy[2] = yymax;
605  xx[3] = xxmin; yy[3] = yymax;
606  xx[4] = xxmin; yy[4] = yymin;
607 
608  //
609  if ( !option.Contains("+") )
610  fListOfConditions.Clear();
611 
612  TCutG *p =
613  new TCutG(Form("%s_%d",GetName(),fListOfConditions.GetEntries()),5,xx,yy);
614  p->SetTitle(GetTitle());
615  p->SetVarX(fVarX.Data());
616  p->SetVarY(fVarY.Data());
617 
618  if ( !fEnable )
619  fEnable = true;
620 
621  if ( fExpectedResult ) {
622  p->SetLineColor(29);
623  p->SetLineWidth(2);
624  p->SetLineStyle(1);
625  }
626  else {
627  p->SetLineColor(29);
628  p->SetLineWidth(2);
629  p->SetLineStyle(9);
630  }
631 
632  p->SetBit(kCanDelete,false);
633  p->SetBit(kMustCleanup,false);
634 
635  gROOT->GetListOfSpecials()->Add(p);
636 
637  // add to condition
638  fListOfConditions.Add(p); fHasPassed_.push_back(false);
639 
640  return p;
641 }
642 
643 void GCond2D::Draw(Option_t* /*option*/)
644 {
645  TVirtualPad *c_pad = TVirtualPad::Pad();
646 
647  if ( !c_pad )
648  return;
649 
650  Style_t hash_style = 1; // 3004 for coinc, 3005 for anti
651  if ( fExpectedResult == false )
652  hash_style = 9;
653 
654  // it could be fListOfConditions.Draw. However some old version of root (5.24) uses option to select particular object of the collection to be drawn (based on expression)
655  // so appaen current pas by myself
656  TObject *obj; TIter next(&fListOfConditions);
657  while ( (obj = next()) ) {
658  if ( c_pad->GetListOfPrimitives()->FindObject(obj) == 0x0 ) {
659 
660  dynamic_cast<TAttLine *>(obj)->SetLineColor(29);
661  dynamic_cast<TAttLine *>(obj)->SetLineWidth(2);
662  dynamic_cast<TAttLine *>(obj)->SetLineStyle(hash_style);
663 
664  c_pad->GetListOfPrimitives()->Add(obj,"LP");
665  c_pad->Modified();
666  }
667  }
668  if ( fListOfConditions.GetEntries() ) {
669  c_pad->Update();
670  }
671 }
672 
673 
674 Bool_t GCond2D::IsPassed(Double_t x, Double_t y) const
675 {
676  if ( !fEnable || fListOfConditions.GetEntries() == 0 ) {
677  fHasPassed = true; return fHasPassed;
678  }
679 
680  Bool_t is_inside = false;
681 
682  for (Int_t i = 0; i < fListOfConditions.GetEntries(); i++) {
683  TCutG *cut = (TCutG *)fListOfConditions[i];
684  if ( cut->IsInside(x,y) ) {
685  is_inside = true;
687  }
688  else fHasPassed_[i] = !fExpectedResult;
689  }
690 
691  return (fHasPassed = (is_inside == fExpectedResult) );
692 }
693 
694 Short_t GCond2D::Import(const char *what, const char *from, Option_t *opt)
695 {
696  TString wh(what), origin(from), option(opt), tmp, obj_name;TDirectory *dir_to_search = gDirectory; Short_t result = 0;
697  //
698  Bool_t wildcard = false;
699  if ( wh.Contains("*") )
700  wildcard = true;
701  TRegexp rexp(Form("%s",what),wildcard);
702 
703  // start with empty container if option + is not there
704  if ( !option.Contains("+") )
705  fListOfConditions.Clear();
706 
707  // Treat root file/dir cases
708  if ( !origin.Contains(":") ) { // means local root
709  if ( origin == "gdir" || origin == "gpad" ) {
710  ;
711  }
712  else origin.Prepend("file:");
713  }
714  if ( origin.BeginsWith("file:") ||
715  origin.BeginsWith("root:") ||
716  origin.BeginsWith("roots:") ||
717  origin.BeginsWith("rootk:") ||
718  origin.BeginsWith("http:") )
719  {
720  TString rname = origin;
721  if ( origin.BeginsWith("file:") )
722  rname.Replace(0,origin.First(":")+1,""); // only if local, remove file: protocol
723 
724  // TODO
725  // if ( "" ) ==> open window
726 
727 
728  // look for similar already open if not open a new one
729  TFile *f = 0x0;
730  if ( !origin.EndsWith(".root") ) {
731  rname.Append(".root"); origin.Append(".root");
732  }
733 
734  if ( gROOT->GetListOfFiles()->FindObject(rname.Data()) ) {// already there
735  std::cout << "Import CUTS from OPEN FILE " << rname.Data() << std::endl ;
736  f = (TFile *)gROOT->GetListOfFiles()->FindObject(rname.Data());
737  }
738  else {
739  std::cout << "Import CUTS from NEW FILE " << origin.Data() << std::endl;
740  f = TFile::Open(origin.Data(),"READ");
741  }
742  if ( f ) {
743  dir_to_search = f; origin = "gdir"; // even if not current dir !
744  }
745  else origin = "FAIL";
746  }
748 
749  if ( origin == "gdir" ) { // root file/directory
750 
751  // useful to browse the TDirectory
752  TString class_name; TObject *obj_in_file = 0x0; TKey *key_in_file; TList new_cuts; TCutG *cut;
753 
754  // iterate over the key and keep only TCutG and in case a TCutG has been saved several time, only the highest cycle
755  TIter it_keys(dir_to_search->GetListOfKeys());
756  while ( (key_in_file = static_cast<TKey *>(it_keys())) ) { // next key
757  class_name = key_in_file->GetClassName();
758  obj_name = key_in_file->GetName();
759  // obj on disk is TCut and with a name not already in the list of Cond
760  if ( class_name == "TCutG" && obj_name.Contains(rexp) && fListOfConditions.FindObject(obj_name.Data()) == 0x0 ) {
761  if ( !new_cuts.FindObject(obj_name.Data()) ) { // list of key is ordered so that first for a given name correspond to the highest cycle
762  obj_in_file =
763  key_in_file->ReadObj();
764  new_cuts.Add(obj_in_file);
765  }
766  }
767  }
768  TIter it_cuts(&new_cuts);
769  while ( (cut = static_cast<TCutG *>(it_cuts())) ) { // next cut
770  if ( cut ) {
771 
772  obj_name = cut->GetName();
773  if ( !obj_name.BeginsWith(GetName()) ) {
774  cut->SetName(Form("%s_%s",GetName(),obj_name.Data()));
775  }
776  if ( fListOfConditions.FindObject(cut->GetName()) ) { // already a peak with same name in the list
777  cut->SetName( Form("%s_%d",GetName(), fListOfConditions.GetEntries()) );
778  }
779 
780  if ( !fEnable )
781  fEnable = true;
782 
783  cut->SetBit(kCanDelete,false);
784  cut->SetBit(kMustCleanup,false);
785 
786  gROOT->GetListOfSpecials()->Remove(cut); // to avoid keeping it in this folder and delete it
787 
788  // add to condition
789  std::cout << "Add TCutG " << cut << std::endl;
790  fListOfConditions.Add(cut); fHasPassed_.push_back(false);
791 
792  result++;
793  }
794  else {
795  std::cout << " Cannot convert " << cut->GetName() << " to peak " << std::endl;
796  }
797  }
798  }
799 
800  if ( origin == "gpad" ) { // root current pad
801  TSeqCollection *l_to_seach = 0x0;
802  if ( TVirtualPad::Pad() ) {
803  l_to_seach = TVirtualPad::Pad()->GetListOfPrimitives();
804  }
805  TObject* obj = 0x0;
806  TIter next(l_to_seach);
807  while ( (obj = next()) ) { // iterator skips empty slots
808  if ( obj->InheritsFrom("TCutG") ) {
809  tmp = obj->GetName();
810  if ( tmp.Contains(rexp) ) {
811 
812  if ( fListOfConditions.FindObject(obj) ) { // already in the list
813  printf("%s ALREADY IN THE LIST \n", obj->GetName());
814  continue;
815  }
816 
817  // change some attributes
818  if ( option.Contains("r") )
819  ((TCutG*)obj)->SetNameTitle( Form("%s_%d",GetName(),fListOfConditions.GetEntries()), GetTitle() );
820  else
821  ((TCutG*)obj)->SetNameTitle( Form("%s_%s",GetName(), obj->GetName()), GetTitle() );
822 
823  if ( !fEnable )
824  fEnable = true;
825 
826  obj->SetBit(kCanDelete,false);
827  obj->SetBit(kMustCleanup,false);
828 
829  gROOT->GetListOfSpecials()->Remove(obj); // to avoid keeping it in this folder and delete it
830 
831  // add to condition
832  fListOfConditions.Add(obj); fHasPassed_.push_back(false);
833  result++;
834  }
835  }
836  }
837  }
838 
839 
840  if ( fListOfConditions.GetEntries() > 0 ) {
841  printf("List of %d 2D Conditions \n",fListOfConditions.GetEntries());
842  fListOfConditions.ls();
843  }
844 
845  return result;
846 
847  /*
848  if ( origin == "gdir" ) { // root file/directory
849  l_to_seach = gROOT->GetListOfSpecials();
850  }
851  if ( origin == "gpad" ) { // root current pad
852  if ( TVirtualPad::Pad() ) {
853  l_to_seach = TVirtualPad::Pad()->GetListOfPrimitives();
854  }
855  }
856  if ( l_to_seach ) {
857  TObject* obj = 0x0;
858  TIter next(l_to_seach);
859  while ( (obj = next()) ) { // iterator skips empty slots
860  if ( obj->InheritsFrom("TCutG") ) {
861  tmp = obj->GetName();
862  if ( tmp.Contains(rexp) ) {
863 
864  if ( fListOfConditions.FindObject(obj) ) { // already in the list
865  printf("%s ALREADY IN THE LIST \n", obj->GetName());
866  continue;
867  }
868 
869  // change some attributes
870  if ( option.Contains("r") )
871  ((TCutG*)obj)->SetNameTitle( Form("%s_%d",GetName(),fListOfConditions.GetEntries()), GetTitle() );
872  else
873  ((TCutG*)obj)->SetNameTitle( Form("%s_%s",GetName(), obj->GetName()), GetTitle() );
874 
875  if ( !fEnable )
876  fEnable = true;
877 
878  ((TCutG*)obj)->SetVarX(fVarX.Data());
879  ((TCutG*)obj)->SetVarY(fVarY.Data());
880 
881  gROOT->GetListOfSpecials()->Add(obj);
882 
883  // add to condition
884  fListOfConditions.Add(obj); fHasPassed_.push_back(false);
885  result++;
886  }
887  }
888  }
889  }
890  */
891 
892 }
893 
894 Short_t GCond2D::Export(const char *what, const char *to, Option_t *opt) const
895 {
896  TString wh(what), destination(to), option(opt), tmp("ROOT"), rname;
897  TDirectory *directory = gDirectory;
898  Short_t result = 0; TObject* obj = 0x0;
899  //
900  Bool_t wildcard = false;
901  if ( wh.Contains("*") )
902  wildcard = true;
903  TRegexp rexp(Form("%s",what),wildcard);
904 
905  if ( !destination.Contains(":") ) { // means local root
906  if ( destination != "gdir" ) {
907  destination.Prepend("file:");
908  }
909  }
910  if ( destination.BeginsWith("gdir") ) {
911  // add path to subdirs of gdir ... something like gdir/sub helps
912  }
913  if ( destination.BeginsWith("file:") ||
914  destination.BeginsWith("root:") ||
915  destination.BeginsWith("roots:") ||
916  destination.BeginsWith("rootk:") ||
917  destination.BeginsWith("http:") )
918  {
919  TString rname = destination;
920  if ( destination.BeginsWith("file:") )
921  rname.Replace(0,destination.First(":")+1,""); // only if local, remove file: protocol
922 
923  // TODO
924  // if ( "" ) ==> open window
925 
926 
927  // look for similar already open if not open a new one
928  TFile *f = 0x0;
929  if ( !destination.EndsWith(".root") ) {
930  rname.Append(".root"); destination.Append(".root");
931  }
932 
933  if ( gROOT->GetListOfFiles()->FindObject(rname.Data()) ) {// already there
934  std::cout << "SAVE CUTS in OPEN FILE " << rname.Data() << std::endl ;
935  f = (TFile *)gROOT->GetListOfFiles()->FindObject(rname.Data());
936  }
937  else {
938  std::cout << "SAVE CUTS in NEW FILE " << destination.Data() << std::endl;
939  f = TFile::Open(destination.Data(),"UPDATE");
940  }
941  if ( f ) {
942  directory = f;
943  }
944  else tmp = "FAIL";
945  }
946 
947  if ( tmp == "ROOT" ) {
948 
949  TIter next(&fListOfConditions);
950  while ( (obj = next()) ) { // iterator skips empty slots
951  tmp = obj->GetName();
952  if ( tmp.Contains(rexp) ) {
953 
954  tmp = obj->GetName(); // make sure the cut start with CONDNAME_
955  if ( !tmp.BeginsWith(GetName()) ) {
956  ((TCutG*)obj)->SetName(Form("%s_%s",GetName(),obj->GetName()));
957  }
958 
959  directory->WriteTObject(obj,"",option.Data());
960  result++;
961  }
962  }
963  }
964 
965  return result;
966 }
967 
968 void GCond2D::WindowGCreator(const char * /*c_name*/)
969 {
970  gROOT->SetEditorMode("CutG");
971 }
972 
973 
974 
975 
976 
977 
978 
979 
980 
BasePeak * DefaultPeak()
access to the peak used to create other peaks (through the Clone method)
Definition: PeakCreator.h:152
TObjArray fListOfConditions
Definition: GCondition.h:56
virtual ~GCond1D()
Definition: GCondition.cpp:148
printf("******************************************************************** \n")
virtual void UseAsAnti(Bool_t r=false)
need to be redefined since Peak does not inherits from TAttFill
Definition: GCondition.cpp:181
header file for PeakCreator.cpp
Base for a Graphical condition 2D.
Definition: GCondition.h:233
void SetPeak(Double_t pos, Double_t height, Double_t fwhm, Double_t intensity=0)
Set peak members.
Definition: Peak1D.cpp:519
static void AddRMimeType()
check mime type and add those proper to gw
Definition: Env.cpp:81
virtual Bool_t IsPassed(Double_t, Double_t) const
returns true if the condition is passed with success
Definition: GCondition.cpp:674
virtual ~GCondition()
Definition: GCondition.cpp:81
static const Style_t gAnticStyle
Definition: GCondition.h:53
Gw::Peak1D * CutToPeak(TCut *)
Definition: GCondition.cpp:475
Bool_t fHasPassed
Definition: GCondition.h:62
virtual void Enable()
toggle on/off the cut. if off returns always passed
Definition: GCondition.cpp:110
virtual Short_t Export(const char *what="*", const char *to="MyCuts", Option_t *opt="") const
Export 1D conditions.
Definition: GCondition.cpp:489
void Draw(Option_t *option="")
Definition: GCondition.cpp:643
virtual Short_t Export(const char *what="*", const char *to="MyCuts", Option_t *opt="") const
Definition: GCondition.cpp:894
virtual Short_t Import(const char *what="*", const char *from="gpad", Option_t *opt="+")
Import 2D conditions.
Definition: GCondition.cpp:694
virtual void SetFillStylePeak(Style_t style)
Definition: Peak1D.cpp:1678
static PeakCreator * AddPeakCreator(TVirtualPad *pad=0x0)
add a peak creator to the given pad. Current pad if argument is 0x0
ClassImp(GCondition)
Bool_t fExpectedResult
Definition: GCondition.h:60
void Draw(Option_t *option="")
Definition: GCondition.cpp:197
Gw::PeakCreator * WindowGCreator(const char *c_name="")
add to the current canvas the way to add graphically 1D gates (type h after in the canvas to get list...
Definition: GCondition.cpp:153
static Gw::Peak1D * TCutToPeak(TCut *)
Definition: GCondition.cpp:428
TCut * PeakToCut(const Gw::Peak1D *) const
Definition: GCondition.cpp:463
TCutG * AddWindow(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax, Option_t *opt="+")
Add a window to the list of conditions.
Definition: GCondition.cpp:596
virtual Bool_t IsPassed(Double_t, Double_t=0.0) const
returns true if the condition is passed with success
Definition: GCondition.cpp:247
void SetGateMode(Bool_t ngate=true)
if true, peak are collected on key board actions (type h for help)
virtual void WindowGCreator(const char *c_name="")
prepare current canvas to draw a TCutG.
Definition: GCondition.cpp:968
virtual Double_t GetFWHM(Option_t *axis="X") const
Get FWHM of peak.
Definition: Peak1D.cpp:962
virtual Short_t Import(const char *what="*", const char *from="gpad", Option_t *opt="+")
Import 1D conditions.
Definition: GCondition.cpp:270
virtual Double_t GetPosition(Option_t *axis="X") const
Get position of peak.
Definition: Peak1D.cpp:955
virtual void Delete(Option_t *option="")
delete all object from fListOfConditions
Definition: GCondition.cpp:86
Base for a Graphical condition 1D.
Definition: GCondition.h:154
header file for a general 1D peak
virtual void Print(Option_t *option="") const
overwritte print method ... should be to gw log system
Definition: GCondition.cpp:115
ADF::LogMessage & endl(ADF::LogMessage &log)
to get information about the gammaware configuration
static PeakCreator * IsPeakCreator(TVirtualPad *pad=0x0)
Check whether or not a PeakCreator is in this canvas.
Gw::Peak1D * AddWindow(Double_t, Double_t, Option_t *opt="+")
Add a window to the list of conditions.
Definition: GCondition.cpp:218
virtual ~GCond2D()
Definition: GCondition.cpp:591
A graphical interface for placing schematic peak onto a 1D histogram with a given position...
Definition: Peak1D.h:79
std::vector< Bool_t > fHasPassed_
Definition: GCondition.h:63
virtual void SetDrawAs(EDrawAs d)
Set drawing flag.
Definition: Peak1D.cpp:1718
Bool_t fEnable
Definition: GCondition.h:59
virtual void UseAsAnti(Bool_t r=false)
to change gate into an anti gate
Definition: GCondition.cpp:91
static const Style_t gCoincStyle
Definition: GCondition.h:52
Base for a Graphical condition on a spectrum (1D, 2D)
Definition: GCondition.h:49