Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:24

0001 // $Id$
0002 // Author: Sergey Linev   22/12/2013
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TRootSniffer
0013 #define ROOT_TRootSniffer
0014 
0015 #include "TNamed.h"
0016 #include "TList.h"
0017 #include <memory>
0018 #include <string>
0019 
0020 class TFolder;
0021 class TKey;
0022 class TBufferFile;
0023 class TDataMember;
0024 class THttpCallArg;
0025 class TRootSnifferStore;
0026 class TRootSniffer;
0027 
0028 class TRootSnifferScanRec {
0029 
0030    friend class TRootSniffer;
0031 
0032 protected:
0033    // different bits used to scan hierarchy
0034    enum {
0035       kScan = 0x0001,        ///< normal scan of hierarchy
0036       kExpand = 0x0002,      ///< expand of specified item - allowed to scan object members
0037       kSearch = 0x0004,      ///< search for specified item (only objects and collections)
0038       kCheckChilds = 0x0008, ///< check if there childs, very similar to search
0039       kOnlyFields = 0x0010,  ///< if set, only fields for specified item will be set (but all fields)
0040       kActions = 0x001F      ///< mask for actions, only actions copied to child rec
0041    };
0042 
0043    TRootSnifferScanRec *fParent{nullptr}; ///<! pointer on parent record
0044    UInt_t fMask{0};                       ///<! defines operation kind
0045    const char *fSearchPath{nullptr};      ///<! current path searched
0046    Int_t fLevel{0};                       ///<! current level of hierarchy
0047    TString fItemName;                     ///<! name of current item
0048    TList fItemsNames;                     ///<! list of created items names, need to avoid duplication
0049    Int_t fRestriction{0};                 ///<! restriction 0 - default, 1 - read-only, 2 - full access
0050 
0051    TRootSnifferStore *fStore{nullptr}; ///<! object to store results
0052    Bool_t fHasMore{kFALSE};            ///<! indicates that potentially there are more items can be found
0053    Bool_t fNodeStarted{kFALSE};        ///<! indicate if node was started
0054    Int_t fNumFields{0};                ///<! number of fields
0055    Int_t fNumChilds{0};                ///<! number of childs
0056 
0057 public:
0058    TRootSnifferScanRec();
0059    virtual ~TRootSnifferScanRec();
0060 
0061    void CloseNode();
0062 
0063    /** return true when fields could be set to the hierarchy item */
0064    Bool_t CanSetFields() const { return (fMask & kScan) && (fStore != nullptr); }
0065 
0066    /** return true when only fields are scanned by the sniffer */
0067    Bool_t ScanOnlyFields() const { return (fMask & kOnlyFields) && (fMask & kScan); }
0068 
0069    /** Starts new node, must be closed at the end */
0070    void CreateNode(const char *_node_name);
0071 
0072    void BeforeNextChild();
0073 
0074    /** Set item field only when creating is specified */
0075    void SetField(const char *name, const char *value, Bool_t with_quotes = kTRUE);
0076 
0077    /** Mark item with ROOT class and correspondent streamer info */
0078    void SetRootClass(TClass *cl);
0079 
0080    /** Returns true when item can be expanded */
0081    Bool_t CanExpandItem();
0082 
0083    /** Checks if result will be accepted. Used to verify if sniffer should read object from the file */
0084    Bool_t IsReadyForResult() const;
0085 
0086    /** Obsolete, use SetFoundResult instead */
0087    Bool_t SetResult(void *obj, TClass *cl, TDataMember *member = nullptr);
0088 
0089    /** Set found element with class and datamember (optional) */
0090    Bool_t SetFoundResult(void *obj, TClass *cl, TDataMember *member = nullptr);
0091 
0092    /** Returns depth of hierarchy */
0093    Int_t Depth() const;
0094 
0095    /** Method indicates that scanning can be interrupted while result is set */
0096    Bool_t Done() const;
0097 
0098    /** Construct item name, using object name as basis */
0099    void MakeItemName(const char *objname, TString &itemname);
0100 
0101    /** Produces full name for the current item */
0102    void BuildFullName(TString &buf, TRootSnifferScanRec *prnt = nullptr);
0103 
0104    /** Returns read-only flag for current item */
0105    Bool_t IsReadOnly(Bool_t dflt = kTRUE);
0106 
0107    Bool_t GoInside(TRootSnifferScanRec &super, TObject *obj, const char *obj_name = nullptr,
0108                    TRootSniffer *sniffer = nullptr);
0109 
0110    ClassDef(TRootSnifferScanRec, 0) // Scan record for objects sniffer
0111 };
0112 
0113 //_______________________________________________________________________
0114 
0115 class TRootSniffer : public TNamed {
0116    enum {
0117       kItemField = BIT(21) // item property stored as TNamed
0118    };
0119 
0120 protected:
0121    TString fObjectsPath;    ///<! default path for registered objects
0122    Bool_t fReadOnly{kTRUE}; ///<! indicate if sniffer allowed to change ROOT structures - like read objects from file
0123    Bool_t fScanGlobalDir{kTRUE};       ///<! when enabled (default), scan gROOT for histograms, canvases, open files
0124    std::unique_ptr<TFolder> fTopFolder; ///<! own top TFolder object, used for registering objects
0125    THttpCallArg *fCurrentArg{nullptr}; ///<! current http arguments (if any)
0126    Int_t fCurrentRestrict{0};          ///<! current restriction for last-found object
0127    TString fCurrentAllowedMethods;     ///<! list of allowed methods, extracted when analyzed object restrictions
0128    TList fRestrictions;                ///<! list of restrictions for different locations
0129    TString fAutoLoad;                  ///<! scripts names, which are add as _autoload parameter to h.json request
0130 
0131    void ScanObjectMembers(TRootSnifferScanRec &rec, TClass *cl, char *ptr);
0132 
0133    virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj);
0134 
0135    virtual void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class);
0136 
0137    virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj);
0138 
0139    virtual Bool_t CallProduceImage(const std::string &kind, const std::string &path, const std::string &options, std::string &res);
0140 
0141    void
0142    ScanCollection(TRootSnifferScanRec &rec, TCollection *lst, const char *foldername = nullptr, TCollection *keys_lst = nullptr);
0143 
0144    virtual void ScanRoot(TRootSnifferScanRec &rec);
0145 
0146    TString DecodeUrlOptionValue(const char *value, Bool_t remove_quotes = kTRUE);
0147 
0148    TObject *GetItem(const char *fullname, TFolder *&parent, Bool_t force = kFALSE, Bool_t within_objects = kTRUE);
0149 
0150    TFolder *GetSubFolder(const char *foldername, Bool_t force = kFALSE);
0151 
0152    const char *GetItemField(TFolder *parent, TObject *item, const char *name);
0153 
0154    Bool_t IsItemField(TObject *obj) const;
0155 
0156    Bool_t AccessField(TFolder *parent, TObject *item, const char *name, const char *value, TNamed **only_get = nullptr);
0157 
0158    Int_t WithCurrentUserName(const char *option);
0159 
0160    virtual Bool_t CanDrawClass(TClass *) { return kFALSE; }
0161 
0162    virtual Bool_t HasStreamerInfo() const { return kFALSE; }
0163 
0164    virtual Bool_t ProduceJson(const std::string &path, const std::string &options, std::string &res);
0165 
0166    virtual Bool_t ProduceXml(const std::string &path, const std::string &options, std::string &res);
0167 
0168    virtual Bool_t ProduceBinary(const std::string &path, const std::string &options, std::string &res);
0169 
0170    virtual Bool_t ProduceRootFile(const std::string &path, const std::string &options, std::string &res);
0171 
0172    virtual Bool_t ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res);
0173 
0174    virtual Bool_t ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res);
0175 
0176    virtual Bool_t ExecuteCmd(const std::string &path, const std::string &options, std::string &res);
0177 
0178    virtual Bool_t
0179    ProduceItem(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
0180 
0181    virtual Bool_t
0182    ProduceMulti(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
0183 
0184 public:
0185    TRootSniffer(const char *name = "sniff", const char *objpath = "Objects");
0186    virtual ~TRootSniffer();
0187 
0188    /** When readonly on (default), sniffer is not allowed to change ROOT structures
0189      * For instance, it is not allowed to read new objects from files */
0190    void SetReadOnly(Bool_t on = kTRUE) { fReadOnly = on; }
0191 
0192    /** Returns readonly mode */
0193    Bool_t IsReadOnly() const { return fReadOnly; }
0194 
0195    void Restrict(const char *path, const char *options);
0196 
0197    Bool_t HasRestriction(const char *item_name);
0198 
0199    Int_t CheckRestriction(const char *item_name);
0200 
0201    void CreateOwnTopFolder();
0202 
0203    TFolder *GetTopFolder(Bool_t force = kFALSE);
0204 
0205    /** When enabled (default), sniffer scans gROOT for files, canvases, histograms */
0206    void SetScanGlobalDir(Bool_t on = kTRUE) { fScanGlobalDir = on; }
0207 
0208    void SetAutoLoad(const char *scripts = "");
0209 
0210    const char *GetAutoLoad() const;
0211 
0212    /** Returns true when sniffer allowed to scan global directories */
0213    Bool_t IsScanGlobalDir() const { return fScanGlobalDir; }
0214 
0215    Bool_t RegisterObject(const char *subfolder, TObject *obj);
0216 
0217    Bool_t UnregisterObject(TObject *obj);
0218 
0219    Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon);
0220 
0221    Bool_t CreateItem(const char *fullname, const char *title);
0222 
0223    Bool_t SetItemField(const char *fullname, const char *name, const char *value);
0224 
0225    const char *GetItemField(const char *fullname, const char *name);
0226 
0227    THttpCallArg *SetCurrentCallArg(THttpCallArg *arg);
0228 
0229    /** Method scans normal objects, registered in ROOT */
0230    void ScanHierarchy(const char *topname, const char *path, TRootSnifferStore *store, Bool_t only_fields = kFALSE);
0231 
0232    TObject *FindTObjectInHierarchy(const char *path);
0233 
0234    virtual void *
0235    FindInHierarchy(const char *path, TClass **cl = nullptr, TDataMember **member = nullptr, Int_t *chld = nullptr);
0236 
0237    Bool_t CanDrawItem(const char *path);
0238 
0239    Bool_t CanExploreItem(const char *path);
0240 
0241    virtual Bool_t IsStreamerInfoItem(const char *) { return kFALSE; }
0242 
0243    virtual ULong_t GetStreamerInfoHash() { return 0; }
0244 
0245    virtual ULong_t GetItemHash(const char *itemname);
0246 
0247    Bool_t Produce(const std::string &path, const std::string &file, const std::string &options, std::string &res);
0248 
0249    ClassDefOverride(TRootSniffer, 0) // Sniffer of ROOT objects (basic version)
0250 };
0251 
0252 #endif