Logo ROOT   6.21/01
Reference Guide
TRootSniffer.h
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 22/12/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TRootSniffer
13 #define ROOT_TRootSniffer
14 
15 #include "TNamed.h"
16 #include "TList.h"
17 #include <memory>
18 
19 class TFolder;
20 class TKey;
21 class TBufferFile;
22 class TDataMember;
23 class THttpCallArg;
24 class TRootSnifferStore;
25 class TRootSniffer;
26 
28 
29  friend class TRootSniffer;
30 
31 protected:
32  // different bits used to scan hierarchy
33  enum {
34  kScan = 0x0001, ///< normal scan of hierarchy
35  kExpand = 0x0002, ///< expand of specified item - allowed to scan object members
36  kSearch = 0x0004, ///< search for specified item (only objects and collections)
37  kCheckChilds = 0x0008, ///< check if there childs, very similar to search
38  kOnlyFields = 0x0010, ///< if set, only fields for specified item will be set (but all fields)
39  kActions = 0x001F ///< mask for actions, only actions copied to child rec
40  };
41 
42  TRootSnifferScanRec *fParent{nullptr}; ///<! pointer on parent record
43  UInt_t fMask{0}; ///<! defines operation kind
44  const char *fSearchPath{nullptr}; ///<! current path searched
45  Int_t fLevel{0}; ///<! current level of hierarchy
46  TString fItemName; ///<! name of current item
47  TList fItemsNames; ///<! list of created items names, need to avoid duplication
48  Int_t fRestriction{0}; ///<! restriction 0 - default, 1 - read-only, 2 - full access
49 
50  TRootSnifferStore *fStore{nullptr}; ///<! object to store results
51  Bool_t fHasMore{kFALSE}; ///<! indicates that potentially there are more items can be found
52  Bool_t fNodeStarted{kFALSE}; ///<! indicate if node was started
53  Int_t fNumFields{0}; ///<! number of fields
54  Int_t fNumChilds{0}; ///<! number of childs
55 
56 public:
58  virtual ~TRootSnifferScanRec();
59 
60  void CloseNode();
61 
62  /** return true when fields could be set to the hierarchy item */
63  Bool_t CanSetFields() const { return (fMask & kScan) && (fStore != nullptr); }
64 
65  /** return true when only fields are scanned by the sniffer */
66  Bool_t ScanOnlyFields() const { return (fMask & kOnlyFields) && (fMask & kScan); }
67 
68  /** Starts new node, must be closed at the end */
69  void CreateNode(const char *_node_name);
70 
71  void BeforeNextChild();
72 
73  /** Set item field only when creating is specified */
74  void SetField(const char *name, const char *value, Bool_t with_quotes = kTRUE);
75 
76  /** Mark item with ROOT class and correspondent streamer info */
77  void SetRootClass(TClass *cl);
78 
79  /** Returns true when item can be expanded */
81 
82  /** Checks if result will be accepted. Used to verify if sniffer should read object from the file */
83  Bool_t IsReadyForResult() const;
84 
85  /** Obsolete, use SetFoundResult instead */
86  Bool_t SetResult(void *obj, TClass *cl, TDataMember *member = nullptr);
87 
88  /** Set found element with class and datamember (optional) */
89  Bool_t SetFoundResult(void *obj, TClass *cl, TDataMember *member = nullptr);
90 
91  /** Returns depth of hierarchy */
92  Int_t Depth() const;
93 
94  /** Method indicates that scanning can be interrupted while result is set */
95  Bool_t Done() const;
96 
97  /** Construct item name, using object name as basis */
98  void MakeItemName(const char *objname, TString &itemname);
99 
100  /** Produces full name for the current item */
101  void BuildFullName(TString &buf, TRootSnifferScanRec *prnt = nullptr);
102 
103  /** Returns read-only flag for current item */
104  Bool_t IsReadOnly(Bool_t dflt = kTRUE);
105 
106  Bool_t GoInside(TRootSnifferScanRec &super, TObject *obj, const char *obj_name = nullptr,
107  TRootSniffer *sniffer = nullptr);
108 
109  ClassDef(TRootSnifferScanRec, 0) // Scan record for objects sniffer
110 };
111 
112 //_______________________________________________________________________
113 
114 class TRootSniffer : public TNamed {
115  enum {
116  kItemField = BIT(21) // item property stored as TNamed
117  };
118 
119 protected:
120  TString fObjectsPath; ///<! default path for registered objects
121  Bool_t fReadOnly{kTRUE}; ///<! indicate if sniffer allowed to change ROOT structures - like read objects from file
122  Bool_t fScanGlobalDir{kTRUE}; ///<! when enabled (default), scan gROOT for histograms, canvases, open files
123  std::unique_ptr<TFolder> fTopFolder; ///<! own top TFolder object, used for registering objects
124  THttpCallArg *fCurrentArg{nullptr}; ///<! current http arguments (if any)
125  Int_t fCurrentRestrict{0}; ///<! current restriction for last-found object
126  TString fCurrentAllowedMethods; ///<! list of allowed methods, extracted when analyzed object restrictions
127  TList fRestrictions; ///<! list of restrictions for different locations
128  TString fAutoLoad; ///<! scripts names, which are add as _autoload parameter to h.json request
129 
130  void ScanObjectMembers(TRootSnifferScanRec &rec, TClass *cl, char *ptr);
131 
132  virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj);
133 
134  virtual void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class);
135 
136  virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj);
137 
138  void
139  ScanCollection(TRootSnifferScanRec &rec, TCollection *lst, const char *foldername = nullptr, TCollection *keys_lst = nullptr);
140 
141  virtual void ScanRoot(TRootSnifferScanRec &rec);
142 
143  TString DecodeUrlOptionValue(const char *value, Bool_t remove_quotes = kTRUE);
144 
145  TObject *GetItem(const char *fullname, TFolder *&parent, Bool_t force = kFALSE, Bool_t within_objects = kTRUE);
146 
147  TFolder *GetSubFolder(const char *foldername, Bool_t force = kFALSE);
148 
149  const char *GetItemField(TFolder *parent, TObject *item, const char *name);
150 
151  Bool_t IsItemField(TObject *obj) const;
152 
153  Bool_t AccessField(TFolder *parent, TObject *item, const char *name, const char *value, TNamed **only_get = nullptr);
154 
155  Int_t WithCurrentUserName(const char *option);
156 
157  virtual Bool_t CanDrawClass(TClass *) { return kFALSE; }
158 
159  virtual Bool_t HasStreamerInfo() const { return kFALSE; }
160 
161  virtual Bool_t ProduceJson(const std::string &path, const std::string &options, std::string &res);
162 
163  virtual Bool_t ProduceXml(const std::string &path, const std::string &options, std::string &res);
164 
165  virtual Bool_t ProduceBinary(const std::string &path, const std::string &options, std::string &res);
166 
167  virtual Bool_t ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res);
168 
169  virtual Bool_t ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res);
170 
171  virtual Bool_t ExecuteCmd(const std::string &path, const std::string &options, std::string &res);
172 
173  virtual Bool_t
174  ProduceItem(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
175 
176  virtual Bool_t
177  ProduceMulti(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
178 
179 public:
180  TRootSniffer(const char *name, const char *objpath = "Objects");
181  virtual ~TRootSniffer();
182 
183  /** When readonly on (default), sniffer is not allowed to change ROOT structures
184  * For instance, it is not allowed to read new objects from files */
185  void SetReadOnly(Bool_t on = kTRUE) { fReadOnly = on; }
186 
187  /** Returns readonly mode */
188  Bool_t IsReadOnly() const { return fReadOnly; }
189 
190  void Restrict(const char *path, const char *options);
191 
192  Bool_t HasRestriction(const char *item_name);
193 
194  Int_t CheckRestriction(const char *item_name);
195 
196  void CreateOwnTopFolder();
197 
198  TFolder *GetTopFolder(Bool_t force = kFALSE);
199 
200  /** When enabled (default), sniffer scans gROOT for files, canvases, histograms */
202 
203  void SetAutoLoad(const char *scripts = "");
204 
205  const char *GetAutoLoad() const;
206 
207  /** Returns true when sniffer allowed to scan global directories */
209 
210  Bool_t RegisterObject(const char *subfolder, TObject *obj);
211 
213 
214  Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon);
215 
216  Bool_t CreateItem(const char *fullname, const char *title);
217 
218  Bool_t SetItemField(const char *fullname, const char *name, const char *value);
219 
220  const char *GetItemField(const char *fullname, const char *name);
221 
222  void SetCurrentCallArg(THttpCallArg *arg);
223 
224  /** Method scans normal objects, registered in ROOT */
225  void ScanHierarchy(const char *topname, const char *path, TRootSnifferStore *store, Bool_t only_fields = kFALSE);
226 
227  TObject *FindTObjectInHierarchy(const char *path);
228 
229  virtual void *
230  FindInHierarchy(const char *path, TClass **cl = nullptr, TDataMember **member = nullptr, Int_t *chld = nullptr);
231 
232  Bool_t CanDrawItem(const char *path);
233 
234  Bool_t CanExploreItem(const char *path);
235 
236  virtual Bool_t IsStreamerInfoItem(const char *) { return kFALSE; }
237 
238  virtual ULong_t GetStreamerInfoHash() { return 0; }
239 
240  virtual ULong_t GetItemHash(const char *itemname);
241 
242  Bool_t Produce(const std::string &path, const std::string &file, const std::string &options, std::string &res);
243 
244  ClassDef(TRootSniffer, 0) // Sniffer of ROOT objects (basic version)
245 };
246 
247 #endif
TString fItemName
! name of current item
Definition: TRootSniffer.h:46
Bool_t RegisterObject(const char *subfolder, TObject *obj)
Register object in subfolder structure subfolder parameter can have many levels like: ...
virtual Bool_t IsStreamerInfoItem(const char *)
Definition: TRootSniffer.h:236
A TFolder object is a collection of objects and folders.
Definition: TFolder.h:30
Bool_t fScanGlobalDir
! when enabled (default), scan gROOT for histograms, canvases, open files
Definition: TRootSniffer.h:122
Bool_t GoInside(TRootSnifferScanRec &super, TObject *obj, const char *obj_name=nullptr, TRootSniffer *sniffer=nullptr)
Method verifies if new level of hierarchy should be started with provided object. ...
void ScanCollection(TRootSnifferScanRec &rec, TCollection *lst, const char *foldername=nullptr, TCollection *keys_lst=nullptr)
scan collection content
Bool_t CanExploreItem(const char *path)
Method returns true when object has childs or one could try to expand item.
virtual ULong_t GetItemHash(const char *itemname)
Get hash function for specified item used to detect any changes in the specified object.
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:46
Bool_t IsItemField(TObject *obj) const
return true when object is TNamed with kItemField bit set such objects used to keep field values for ...
UInt_t fMask
! defines operation kind
Definition: TRootSniffer.h:43
Bool_t IsReadOnly() const
Returns readonly mode.
Definition: TRootSniffer.h:188
virtual Bool_t ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res)
Method to produce image from specified object.
void BeforeNextChild()
indicates that new child for current element will be started
Bool_t IsReadyForResult() const
Checks if result will be accepted.
Bool_t Produce(const std::string &path, const std::string &file, const std::string &options, std::string &res)
Method produce different kind of data out of object Parameter &#39;path&#39; specifies object or object membe...
Int_t CheckRestriction(const char *item_name)
Checked if restriction is applied to the item full_item_name should have full path to the item...
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
normal scan of hierarchy
Definition: TRootSniffer.h:34
virtual Bool_t CanDrawClass(TClass *)
Definition: TRootSniffer.h:157
#define BIT(n)
Definition: Rtypes.h:83
const char * GetAutoLoad() const
return name of configured autoload scripts (or 0)
TString DecodeUrlOptionValue(const char *value, Bool_t remove_quotes=kTRUE)
method replaces all kind of special symbols, which could appear in URL options
Bool_t SetFoundResult(void *obj, TClass *cl, TDataMember *member=nullptr)
Set found element with class and datamember (optional)
Bool_t HasRestriction(const char *item_name)
Made fast check if item with specified name is in restriction list If returns true, requires precise check with CheckRestriction() method.
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
search for specified item (only objects and collections)
Definition: TRootSniffer.h:36
void Restrict(const char *path, const char *options)
Restrict access to the specified location.
Bool_t UnregisterObject(TObject *obj)
unregister (remove) object from folders structures folder itself will remain even when it will be emp...
Int_t fRestriction
! restriction 0 - default, 1 - read-only, 2 - full access
Definition: TRootSniffer.h:48
Abstract interface for storage of hierarchy scan in TRootSniffer.
TString fCurrentAllowedMethods
! list of allowed methods, extracted when analyzed object restrictions
Definition: TRootSniffer.h:126
Bool_t CreateItem(const char *fullname, const char *title)
create item element
expand of specified item - allowed to scan object members
Definition: TRootSniffer.h:35
Bool_t SetResult(void *obj, TClass *cl, TDataMember *member=nullptr)
Obsolete, use SetFoundResult instead.
void CloseNode()
close started node
Bool_t CanExpandItem()
Returns true when item can be expanded.
TFolder * GetTopFolder(Bool_t force=kFALSE)
Returns top TFolder instance for the sniffer.
Bool_t IsReadOnly(Bool_t dflt=kTRUE)
Returns read-only flag for current item.
#define ClassDef(name, id)
Definition: Rtypes.h:326
virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj)
scans object childs (if any) here one scans collection, branches, trees and so on ...
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
set field for specified item
THttpCallArg * fCurrentArg
! current http arguments (if any)
Definition: TRootSniffer.h:124
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Bool_t fReadOnly
! indicate if sniffer allowed to change ROOT structures - like read objects from file ...
Definition: TRootSniffer.h:121
Bool_t ScanOnlyFields() const
return true when only fields are scanned by the sniffer
Definition: TRootSniffer.h:66
Bool_t AccessField(TFolder *parent, TObject *item, const char *name, const char *value, TNamed **only_get=nullptr)
set or get field for the child each field coded as TNamed object, placed after chld in the parent hie...
virtual void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class)
scans key properties in special cases load objects from the file
virtual void * FindInHierarchy(const char *path, TClass **cl=nullptr, TDataMember **member=nullptr, Int_t *chld=nullptr)
Search element with specified path Returns pointer on element Optionally one could obtain element cla...
Bool_t CanDrawItem(const char *path)
Method verifies if object can be drawn.
TFolder * GetSubFolder(const char *foldername, Bool_t force=kFALSE)
creates subfolder where objects can be registered
const char * fSearchPath
! current path searched
Definition: TRootSniffer.h:44
virtual ULong_t GetStreamerInfoHash()
Definition: TRootSniffer.h:238
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
void SetRootClass(TClass *cl)
Mark item with ROOT class and correspondent streamer info.
Int_t fNumChilds
! number of childs
Definition: TRootSniffer.h:54
virtual ~TRootSniffer()
destructor
A doubly linked list.
Definition: TList.h:44
Int_t WithCurrentUserName(const char *option)
return 2 when option match to current user name return 1 when option==all return 0 when option does n...
virtual Bool_t ExecuteCmd(const std::string &path, const std::string &options, std::string &res)
Execute command marked as _kind==&#39;Command&#39;.
virtual Bool_t ProduceXml(const std::string &path, const std::string &options, std::string &res)
Produce XML data for specified item For object conversion TBufferXML is used.
virtual Bool_t ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res)
Execute command for specified object Options include method and extra list of parameters sniffer shou...
TRootSniffer(const char *name, const char *objpath="Objects")
constructor
Int_t fLevel
! current level of hierarchy
Definition: TRootSniffer.h:45
Collection abstract base class.
Definition: TCollection.h:63
virtual Bool_t ProduceJson(const std::string &path, const std::string &options, std::string &res)
Produce JSON data for specified item For object conversion TBufferJSON is used.
unsigned int UInt_t
Definition: RtypesCore.h:42
TString fObjectsPath
! default path for registered objects
Definition: TRootSniffer.h:120
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon)
Register command which can be executed from web interface.
virtual Bool_t ProduceBinary(const std::string &path, const std::string &options, std::string &res)
Produce binary data for specified item if "zipped" option specified in query, buffer will be compress...
Int_t fNumFields
! number of fields
Definition: TRootSniffer.h:53
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:75
virtual void ScanRoot(TRootSnifferScanRec &rec)
scan complete ROOT objects hierarchy For the moment it includes objects in gROOT directory and list o...
mask for actions, only actions copied to child rec
Definition: TRootSniffer.h:39
virtual ~TRootSnifferScanRec()
destructor
void ScanHierarchy(const char *topname, const char *path, TRootSnifferStore *store, Bool_t only_fields=kFALSE)
Method scans normal objects, registered in ROOT.
TString fAutoLoad
! scripts names, which are add as _autoload parameter to h.json request
Definition: TRootSniffer.h:128
void ScanObjectMembers(TRootSnifferScanRec &rec, TClass *cl, char *ptr)
scan object data members some members like enum or static members will be excluded ...
TRootSnifferScanRec()
constructor
const Bool_t kFALSE
Definition: RtypesCore.h:88
TList fItemsNames
! list of created items names, need to avoid duplication
Definition: TRootSniffer.h:47
void BuildFullName(TString &buf, TRootSnifferScanRec *prnt=nullptr)
Produces full name for the current item.
TRootSnifferStore * fStore
! object to store results
Definition: TRootSniffer.h:50
virtual Bool_t ProduceMulti(const std::string &path, const std::string &options, std::string &res, Bool_t asjson=kTRUE)
Process several requests, packing all results into binary or JSON buffer Input parameters should be c...
unsigned long ULong_t
Definition: RtypesCore.h:51
TRootSnifferScanRec * fParent
! pointer on parent record
Definition: TRootSniffer.h:42
Bool_t Done() const
Method indicates that scanning can be interrupted while result is set.
Mother of all ROOT objects.
Definition: TObject.h:37
Int_t Depth() const
Returns depth of hierarchy.
void SetField(const char *name, const char *value, Bool_t with_quotes=kTRUE)
Set item field only when creating is specified.
void SetAutoLoad(const char *scripts="")
When specified, _autoload attribute will be always add to top element of h.json/h.hml requests Used to instruct browser automatically load special code.
void SetReadOnly(Bool_t on=kTRUE)
When readonly on (default), sniffer is not allowed to change ROOT structures For instance, it is not allowed to read new objects from files.
Definition: TRootSniffer.h:185
Definition: file.py:1
Bool_t CanSetFields() const
return true when fields could be set to the hierarchy item
Definition: TRootSniffer.h:63
Bool_t fHasMore
! indicates that potentially there are more items can be found
Definition: TRootSniffer.h:51
virtual Bool_t HasStreamerInfo() const
Definition: TRootSniffer.h:159
void CreateOwnTopFolder()
Create own TFolder structures independent from gROOT This allows to have many independent TRootSniffe...
Bool_t fNodeStarted
! indicate if node was started
Definition: TRootSniffer.h:52
virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj)
scans object properties here such fields as _autoload or _icon properties depending on class or objec...
void MakeItemName(const char *objname, TString &itemname)
Construct item name, using object name as basis.
void SetCurrentCallArg(THttpCallArg *arg)
set current http arguments, which then used in different process methods For instance, if user authorized with some user name, depending from restrictions some objects will be invisible or user get full access to the element
const Bool_t kTRUE
Definition: RtypesCore.h:87
void SetScanGlobalDir(Bool_t on=kTRUE)
When enabled (default), sniffer scans gROOT for files, canvases, histograms.
Definition: TRootSniffer.h:201
if set, only fields for specified item will be set (but all fields)
Definition: TRootSniffer.h:38
virtual Bool_t ProduceItem(const std::string &path, const std::string &options, std::string &res, Bool_t asjson=kTRUE)
Produce JSON/XML for specified item contrary to h.json request, only fields for specified item are st...
Int_t fCurrentRestrict
! current restriction for last-found object
Definition: TRootSniffer.h:125
Bool_t IsScanGlobalDir() const
Returns true when sniffer allowed to scan global directories.
Definition: TRootSniffer.h:208
const char * GetItemField(TFolder *parent, TObject *item, const char *name)
return field for specified item
check if there childs, very similar to search
Definition: TRootSniffer.h:37
char name[80]
Definition: TGX11.cxx:109
void CreateNode(const char *_node_name)
Starts new node, must be closed at the end.
TObject * GetItem(const char *fullname, TFolder *&parent, Bool_t force=kFALSE, Bool_t within_objects=kTRUE)
return item from the subfolders structure
TList fRestrictions
! list of restrictions for different locations
Definition: TRootSniffer.h:127
TObject * FindTObjectInHierarchy(const char *path)
Search element in hierarchy, derived from TObject.
std::unique_ptr< TFolder > fTopFolder
! own top TFolder object, used for registering objects
Definition: TRootSniffer.h:123