Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-24 09:25:45

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 fAllowPostObject{kTRUE};    ///<! when true allow to deserialize objects received via POST requests
0124    Bool_t fScanGlobalDir{kTRUE};       ///<! when enabled (default), scan gROOT for histograms, canvases, open files
0125    std::unique_ptr<TFolder> fTopFolder; ///<! own top TFolder object, used for registering objects
0126    THttpCallArg *fCurrentArg{nullptr}; ///<! current http arguments (if any)
0127    Int_t fCurrentRestrict{0};          ///<! current restriction for last-found object
0128    TString fCurrentAllowedMethods;     ///<! list of allowed methods, extracted when analyzed object restrictions
0129    TList fRestrictions;                ///<! list of restrictions for different locations
0130    TString fAutoLoad;                  ///<! scripts names, which are add as _autoload parameter to h.json request
0131 
0132    void ScanObjectMembers(TRootSnifferScanRec &rec, TClass *cl, char *ptr);
0133 
0134    virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj);
0135 
0136    virtual void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class);
0137 
0138    virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj);
0139 
0140    virtual Bool_t CallProduceImage(const std::string &kind, const std::string &path, const std::string &options, std::string &res);
0141 
0142    void
0143    ScanCollection(TRootSnifferScanRec &rec, TCollection *lst, const char *foldername = nullptr, TCollection *keys_lst = nullptr);
0144 
0145    virtual void ScanRoot(TRootSnifferScanRec &rec);
0146 
0147    TString DecodeUrlOptionValue(const char *value, Bool_t remove_quotes = kTRUE, Bool_t escape_special = kTRUE);
0148 
0149    TObject *GetItem(const char *fullname, TFolder *&parent, Bool_t force = kFALSE, Bool_t within_objects = kTRUE);
0150 
0151    TFolder *GetSubFolder(const char *foldername, Bool_t force = kFALSE);
0152 
0153    const char *GetItemField(TFolder *parent, TObject *item, const char *name);
0154 
0155    Bool_t IsItemField(TObject *obj) const;
0156 
0157    Bool_t AccessField(TFolder *parent, TObject *item, const char *name, const char *value, TNamed **only_get = nullptr);
0158 
0159    Int_t WithCurrentUserName(const char *option);
0160 
0161    virtual Bool_t CanDrawClass(TClass *) { return kFALSE; }
0162 
0163    virtual Bool_t HasStreamerInfo() const { return kFALSE; }
0164 
0165    virtual Bool_t ProduceJson(const std::string &path, const std::string &options, std::string &res);
0166 
0167    virtual Bool_t ProduceXml(const std::string &path, const std::string &options, std::string &res);
0168 
0169    virtual Bool_t ProduceBinary(const std::string &path, const std::string &options, std::string &res);
0170 
0171    virtual Bool_t ProduceRootFile(const std::string &path, const std::string &options, std::string &res);
0172 
0173    virtual Bool_t ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res);
0174 
0175    virtual Bool_t ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res);
0176 
0177    virtual Bool_t ExecuteCmd(const std::string &path, const std::string &options, std::string &res);
0178 
0179    virtual Bool_t
0180    ProduceItem(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
0181 
0182    virtual Bool_t
0183    ProduceMulti(const std::string &path, const std::string &options, std::string &res, Bool_t asjson = kTRUE);
0184 
0185 public:
0186    TRootSniffer(const char *name = "sniff", const char *objpath = "Objects");
0187    virtual ~TRootSniffer();
0188 
0189    /** When readonly on (default), sniffer is not allowed to change ROOT structures
0190      * For instance, it is not allowed to read new objects from files */
0191    void SetReadOnly(Bool_t on = kTRUE) { fReadOnly = on; }
0192 
0193    /** Returns readonly mode */
0194    Bool_t IsReadOnly() const { return fReadOnly; }
0195 
0196    /** Allow to deserialize object in POST requests, default off */
0197    void SetAllowPostObject(Bool_t allow_post_obj) { fAllowPostObject = allow_post_obj; }
0198 
0199    /** Is allowed to deserialize object in POST requests, default off */
0200    Bool_t IsAllowPostObject() const { return fAllowPostObject; }
0201 
0202    void Restrict(const char *path, const char *options);
0203 
0204    Bool_t HasRestriction(const char *item_name);
0205 
0206    Int_t CheckRestriction(const char *item_name);
0207 
0208    void CreateOwnTopFolder();
0209 
0210    TFolder *GetTopFolder(Bool_t force = kFALSE);
0211 
0212    /** When enabled (default), sniffer scans gROOT for files, canvases, histograms */
0213    void SetScanGlobalDir(Bool_t on = kTRUE) { fScanGlobalDir = on; }
0214 
0215    void SetAutoLoad(const char *scripts = "");
0216 
0217    const char *GetAutoLoad() const;
0218 
0219    /** Returns true when sniffer allowed to scan global directories */
0220    Bool_t IsScanGlobalDir() const { return fScanGlobalDir; }
0221 
0222    Bool_t RegisterObject(const char *subfolder, TObject *obj);
0223 
0224    Bool_t UnregisterObject(TObject *obj);
0225 
0226    Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon);
0227 
0228    Bool_t CreateItem(const char *fullname, const char *title);
0229 
0230    Bool_t SetItemField(const char *fullname, const char *name, const char *value);
0231 
0232    const char *GetItemField(const char *fullname, const char *name);
0233 
0234    THttpCallArg *SetCurrentCallArg(THttpCallArg *arg);
0235 
0236    /** Method scans normal objects, registered in ROOT */
0237    void ScanHierarchy(const char *topname, const char *path, TRootSnifferStore *store, Bool_t only_fields = kFALSE);
0238 
0239    TObject *FindTObjectInHierarchy(const char *path);
0240 
0241    virtual void *
0242    FindInHierarchy(const char *path, TClass **cl = nullptr, TDataMember **member = nullptr, Int_t *chld = nullptr);
0243 
0244    Bool_t CanDrawItem(const char *path);
0245 
0246    Bool_t CanExploreItem(const char *path);
0247 
0248    virtual Bool_t IsStreamerInfoItem(const char *) { return kFALSE; }
0249 
0250    virtual ULong_t GetStreamerInfoHash() { return 0; }
0251 
0252    virtual ULong_t GetItemHash(const char *itemname);
0253 
0254    Bool_t Produce(const std::string &path, const std::string &file, const std::string &options, std::string &res);
0255 
0256    ClassDefOverride(TRootSniffer, 0) // Sniffer of ROOT objects (basic version)
0257 };
0258 
0259 #endif