Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TRootSnifferFull.cxx
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#include "TRootSnifferFull.h"
13
14#include "TH1.h"
15#include "TGraph.h"
16#include "TProfile.h"
17#include "TCanvas.h"
18#include "TFile.h"
19#include "TKey.h"
20#include "TList.h"
21#include "TMemFile.h"
22#include "TBufferFile.h"
23#include "TBufferJSON.h"
24#include "TBufferXML.h"
25#include "TROOT.h"
26#include "TFolder.h"
27#include "TTree.h"
28#include "TBranch.h"
29#include "TLeaf.h"
30#include "TClass.h"
31#include "TMethod.h"
32#include "TFunction.h"
33#include "TMethodArg.h"
34#include "TMethodCall.h"
35#include "TUrl.h"
36#include "TImage.h"
37#include "TVirtualMutex.h"
38#include "TRootSnifferStore.h"
39#include "THttpCallArg.h"
40
41#include <cstdlib>
42#include <cstring>
43
44/** \class TRootSnifferFull
45
46Extends TRootSniffer for many ROOT classes
47
48Provides access to different ROOT collections and containers
49like TTree, TCanvas, TFile, ...
50*/
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// constructor
55
56TRootSnifferFull::TRootSnifferFull(const char *name, const char *objpath) : TRootSniffer(name, objpath)
57{
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// destructor
62
64{
65 delete fSinfo;
66
67 delete fMemFile;
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// return true if given class can be drawn in JSROOT
72
74{
75 if (!cl)
76 return kFALSE;
77 if (cl->InheritsFrom(TH1::Class()))
78 return kTRUE;
79 if (cl->InheritsFrom(TGraph::Class()))
80 return kTRUE;
82 return kTRUE;
84 return kTRUE;
85 return kFALSE;
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// Scans object properties
90///
91/// here such fields as `_autoload` or `_icon` properties depending on class or object name could be assigned
92/// By default properties, coded in the Class title are scanned. Example:
93///
94/// ClassDef(UserClassName, 1) // class comments *SNIFF* _field1=value _field2="string value"
95///
96/// Here *SNIFF* mark is important. After it all expressions like field=value are parsed
97/// One could use double quotes to code string values with spaces.
98/// Fields separated from each other with spaces
99
101{
102 if (obj && obj->InheritsFrom(TLeaf::Class())) {
103 rec.SetField("_more", "false", kFALSE);
104 rec.SetField("_can_draw", "false", kFALSE);
105 rec.SetField("_player", "drawLeafPlayer");
106 rec.SetField("_module", "draw_tree");
107 return;
108 }
109
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Scans TKey properties
115///
116/// in special cases load objects from the file
117
119{
120 if (strcmp(key->GetClassName(), "TDirectoryFile") == 0) {
121 TRootSniffer::ScanKeyProperties(rec, key, obj, obj_class);
122 } else {
123 obj_class = TClass::GetClass(key->GetClassName());
124 if (obj_class && obj_class->InheritsFrom(TTree::Class())) {
125 if (rec.CanExpandItem()) {
126 // it is requested to expand tree element - read it
127 obj = key->ReadObj();
128 if (obj)
129 obj_class = obj->IsA();
130 } else {
131 rec.SetField("_ttree", "true", kFALSE); // indicate ROOT TTree
132 rec.SetField("_player", "drawTreePlayerKey");
133 rec.SetField("_module", "draw_tree");
134 // rec.SetField("_more", "true", kFALSE); // one could allow to extend
135 }
136 }
137 }
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Scans object childs (if any)
142///
143/// here one scans collection, branches, trees and so on
144
146{
147 if (obj->InheritsFrom(TTree::Class())) {
148 if (!rec.IsReadOnly(fReadOnly)) {
149 rec.SetField("_ttree", "true", kFALSE); // indicate ROOT TTree
150 rec.SetField("_player", "drawTreePlayer");
151 rec.SetField("_module", "draw_tree");
152 }
153 ScanCollection(rec, ((TTree *)obj)->GetListOfLeaves());
154 } else if (obj->InheritsFrom(TBranch::Class())) {
155 ScanCollection(rec, ((TBranch *)obj)->GetListOfLeaves());
156 } else {
158 }
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Returns hash value for streamer infos
163///
164/// At the moment - just number of items in streamer infos list.
165
167{
168 return fSinfo ? fSinfo->GetSize() : 0;
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Return true if it is streamer info item name
173
175{
176 if (!itemname || (*itemname == 0))
177 return kFALSE;
178
179 return (strcmp(itemname, "StreamerInfo") == 0) || (strcmp(itemname, "StreamerInfo/") == 0);
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Get hash function for specified item
184///
185/// Used to detect any changes in the specified object
186
188{
189 if (IsStreamerInfoItem(itemname))
190 return GetStreamerInfoHash();
191
192 return TRootSniffer::GetItemHash(itemname);
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Creates TMemFile instance, which used for objects streaming
197///
198/// One could not use TBufferFile directly,
199/// while one also require streamer infos list
200
202{
203 if (fMemFile)
204 return;
205
206 TDirectory::TContext dirCtx{nullptr};
207 TFile::TContext fileCtx{nullptr};
208
209 fMemFile = new TMemFile("dummy.file", "RECREATE");
210 gROOT->GetListOfFiles()->Remove(fMemFile);
211
212 TH1F *d = new TH1F("d", "d", 10, 0, 10);
213 fMemFile->WriteObject(d, "h1");
214 delete d;
215
216 TGraph *gr = new TGraph(10);
217 gr->SetName("abc");
218 // // gr->SetDrawOptions("AC*");
219 fMemFile->WriteObject(gr, "gr1");
220 delete gr;
221
222 fMemFile->WriteStreamerInfo();
223
224 // make primary list of streamer infos
225 TList *l = new TList();
226
227 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TGraph"));
228 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TH1F"));
229 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TH1"));
230 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TNamed"));
231 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TObject"));
232
233 fMemFile->WriteObject(l, "ll");
234 delete l;
235
236 fMemFile->WriteStreamerInfo();
237
238 fSinfo = fMemFile->GetStreamerInfoList();
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Search element with specified path
243///
244/// Returns pointer on element
245///
246/// Optionally one could obtain element class, member description
247/// and number of childs. When chld!=0, not only element is searched,
248/// but also number of childs are counted. When member!=0, any object
249/// will be scanned for its data members (disregard of extra options)
250
251void *TRootSnifferFull::FindInHierarchy(const char *path, TClass **cl, TDataMember **member, Int_t *chld)
252{
253 if (IsStreamerInfoItem(path)) {
254 // special handling for streamer info
256 if (cl && fSinfo)
257 *cl = fSinfo->IsA();
258 return fSinfo;
259 }
260
261 return TRootSniffer::FindInHierarchy(path, cl, member, chld);
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// Produce binary data for specified item
266///
267/// if "zipped" option specified in query, buffer will be compressed
268
269Bool_t TRootSnifferFull::ProduceBinary(const std::string &path, const std::string & /*query*/, std::string &res)
270{
271 if (path.empty())
272 return kFALSE;
273
274 const char *path_ = path.c_str();
275 if (*path_ == '/')
276 path_++;
277
278 TClass *obj_cl = nullptr;
279 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
280 if (!obj_ptr || !obj_cl)
281 return kFALSE;
282
283 if (obj_cl->GetBaseClassOffset(TObject::Class()) != 0) {
284 Info("ProduceBinary", "Non-TObject class not supported");
285 return kFALSE;
286 }
287
288 // ensure that memfile exists
290
291 TDirectory::TContext dirCtx{nullptr};
292 TFile::TContext fileCtx{nullptr};
293
294 TObject *obj = (TObject *)obj_ptr;
295
296 TBufferFile *sbuf = new TBufferFile(TBuffer::kWrite, 100000);
297 sbuf->SetParent(fMemFile);
298 sbuf->MapObject(obj);
299 obj->Streamer(*sbuf);
300 if (fCurrentArg)
301 fCurrentArg->SetExtraHeader("RootClassName", obj_cl->GetName());
302
303 // produce actual version of streamer info
304 delete fSinfo;
305 fMemFile->WriteStreamerInfo();
306 fSinfo = fMemFile->GetStreamerInfoList();
307
308 res.resize(sbuf->Length());
309 std::copy((const char *)sbuf->Buffer(), (const char *)sbuf->Buffer() + sbuf->Length(), res.begin());
310
311 delete sbuf;
312
313 return kTRUE;
314}
315
316////////////////////////////////////////////////////////////////////////////////
317/// Produce ROOT file for specified item
318///
319/// File created in memory using TMemFile class.
320
321Bool_t TRootSnifferFull::ProduceRootFile(const std::string &path, const std::string & /*query*/, std::string &res)
322{
323 if (path.empty())
324 return kFALSE;
325
326 const char *path_ = path.c_str();
327 if (*path_ == '/')
328 path_++;
329
330 TClass *obj_cl = nullptr;
331 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
332 if (!obj_ptr || !obj_cl)
333 return kFALSE;
334
335 const char *store_name = "object";
336
337 if (obj_cl->GetBaseClassOffset(TNamed::Class()) == 0) {
338 const char *obj_name = ((TNamed *) obj_ptr)->GetName();
339 if (obj_name && *obj_name)
340 store_name = obj_name;
341 }
342
343 TDirectory::TContext dirCtx{nullptr};
344 struct RestoreGFile {
345 TFile *oldFile{gFile};
346 ~RestoreGFile() { gFile = oldFile; }
347 } restoreGFile;
348
349 {
350 TMemFile memfile("dummy.file", "RECREATE");
351 gROOT->GetListOfFiles()->Remove(&memfile);
352
353 memfile.WriteObjectAny(obj_ptr, obj_cl, store_name);
354 memfile.Close();
355
356 res.resize(memfile.GetSize());
357 memfile.CopyTo(res.data(), memfile.GetSize());
358 }
359
360 return kTRUE;
361}
362
363
364////////////////////////////////////////////////////////////////////////////////
365/// Method to produce image from specified object
366///
367/// @param kind image kind TImage::kPng, TImage::kJpeg, TImage::kGif
368/// @param path path to object
369/// @param options extra options
370/// @param res std::string with binary data
371///
372/// By default, image 300x200 is produced
373/// In options string one could provide following parameters:
374///
375/// * w - image width
376/// * h - image height
377/// * opt - draw options
378///
379/// For instance:
380///
381/// http://localhost:8080/Files/hsimple.root/hpx/get.png?w=500&h=500&opt=lego1
382
383Bool_t TRootSnifferFull::ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res)
384{
385 if (path.empty())
386 return kFALSE;
387
388 const char *path_ = path.c_str();
389 if (*path_ == '/')
390 path_++;
391
392 TClass *obj_cl(nullptr);
393 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
394 if (!obj_ptr || !obj_cl)
395 return kFALSE;
396
397 if (obj_cl->GetBaseClassOffset(TObject::Class()) != 0) {
398 Error("TRootSniffer", "Only derived from TObject classes can be drawn");
399 return kFALSE;
400 }
401
402 TObject *obj = (TObject *)obj_ptr;
403
404 TImage *img = TImage::Create();
405 if (!img)
406 return kFALSE;
407
408 if (obj->InheritsFrom(TPad::Class())) {
409
410 if (gDebug > 1)
411 Info("TRootSniffer", "Crate IMAGE directly from pad");
412 img->FromPad((TPad *)obj);
413 } else if (CanDrawClass(obj->IsA())) {
414
415 if (gDebug > 1)
416 Info("TRootSniffer", "Crate IMAGE from object %s", obj->GetName());
417
418 Int_t width(300), height(200);
419 TString drawopt;
420
421 if (!options.empty()) {
422 TUrl url;
423 url.SetOptions(options.c_str());
424 url.ParseOptions();
425 Int_t w = url.GetIntValueFromOptions("w");
426 if (w > 10)
427 width = w;
428 Int_t h = url.GetIntValueFromOptions("h");
429 if (h > 10)
430 height = h;
431 const char *opt = url.GetValueFromOptions("opt");
432 if (opt)
433 drawopt = opt;
434 }
435
436 Bool_t isbatch = gROOT->IsBatch();
437 TVirtualPad::TContext ctxt(false);
438
439 if (!isbatch)
440 gROOT->SetBatch(kTRUE);
441
442 TCanvas *c1 = new TCanvas("__online_draw_canvas__", "title", width, height);
443 obj->Draw(drawopt.Data());
444 img->FromPad(c1);
445 delete c1;
446
447 if (!isbatch)
448 gROOT->SetBatch(kFALSE);
449
450 } else {
451 delete img;
452 return kFALSE;
453 }
454
455 TImage *im = TImage::Create();
456 im->Append(img);
457
458 char *png_buffer = nullptr;
459 int size(0);
460
461 im->GetImageBuffer(&png_buffer, &size, (TImage::EImageFileTypes)kind);
462
463 if (png_buffer && (size > 0)) {
464 res.resize(size);
465 memcpy((void *)res.data(), png_buffer, size);
466 }
467
468 free(png_buffer);
469 delete im;
470
471 return !res.empty();
472}
473
474////////////////////////////////////////////////////////////////////////////////
475/// Invokes TRootSniffer::ProduceIamge, converting kind into TImage::EImageFileTypes type
476
477Bool_t TRootSnifferFull::CallProduceImage(const std::string &kind, const std::string &path, const std::string &options, std::string &res)
478{
479 if (kind == "png")
480 return ProduceImage(TImage::kPng, path, options, res);
481
482 if (kind == "jpeg")
483 return ProduceImage(TImage::kJpeg, path, options, res);
484
485 if (kind == "gif")
486 return ProduceImage(TImage::kGif, path, options, res);
487
488 return kFALSE;
489}
490
491////////////////////////////////////////////////////////////////////////////////
492/// Produce XML data for specified item
493///
494/// For object conversion TBufferXML is used
495
496Bool_t TRootSnifferFull::ProduceXml(const std::string &path, const std::string & /*options*/, std::string &res)
497{
498 if (path.empty())
499 return kFALSE;
500 const char *path_ = path.c_str();
501 if (*path_ == '/')
502 path_++;
503
504 TClass *obj_cl = nullptr;
505 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
506 if (!obj_ptr || !obj_cl)
507 return kFALSE;
508
509 // TODO: support std::string in TBufferXML
510 res = TBufferXML::ConvertToXML(obj_ptr, obj_cl).Data();
511
512 return !res.empty();
513}
514
515////////////////////////////////////////////////////////////////////////////////
516/// Execute command for specified object
517///
518/// @param path the object path
519/// @param options include method and extra list of parameters
520/// sniffer should be not-readonly to allow execution of the commands
521/// @param reskind defines kind of result 0 - debug, 1 - json, 2 - binary
522/// @param res_str result string
523
524Bool_t TRootSnifferFull::ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res_str)
525{
526 std::string *debug = (reskind == 0) ? &res_str : nullptr;
527
528 if (path.empty()) {
529 if (debug)
530 debug->append("Item name not specified\n");
531 return debug != nullptr;
532 }
533
534 const char *path_ = path.c_str();
535 if (*path_ == '/')
536 path_++;
537
538 TClass *obj_cl = nullptr;
539 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
540 if (debug)
541 debug->append(TString::Format("Item:%s found:%s\n", path_, obj_ptr ? "true" : "false").Data());
542 if (!obj_ptr || !obj_cl)
543 return debug != nullptr;
544
545 TUrl url;
546 url.SetOptions(options.c_str());
547
548 const char *method_name = url.GetValueFromOptions("method");
549 TString prototype = DecodeUrlOptionValue(url.GetValueFromOptions("prototype"), kTRUE);
550 TString funcname = DecodeUrlOptionValue(url.GetValueFromOptions("func"), kTRUE);
551 TMethod *method = nullptr;
552 TFunction *func = nullptr;
553 if (method_name) {
554 if (prototype.Length() == 0) {
555 if (debug)
556 debug->append(TString::Format("Search for any method with name \'%s\'\n", method_name).Data());
557 method = obj_cl->GetMethodAllAny(method_name);
558 } else {
559 if (debug)
560 debug->append(
561 TString::Format("Search for method \'%s\' with prototype \'%s\'\n", method_name, prototype.Data())
562 .Data());
563 method = obj_cl->GetMethodWithPrototype(method_name, prototype);
564 }
565 }
566
567 if (method) {
568 if (debug)
569 debug->append(TString::Format("Method: %s\n", method->GetPrototype()).Data());
570 } else {
571 if (funcname.Length() > 0) {
572 if (prototype.Length() == 0) {
573 if (debug)
574 debug->append(TString::Format("Search for any function with name \'%s\'\n", funcname.Data()).Data());
575 func = gROOT->GetGlobalFunction(funcname);
576 } else {
577 if (debug)
578 debug->append(TString::Format("Search for function \'%s\' with prototype \'%s\'\n", funcname.Data(),
579 prototype.Data())
580 .Data());
581 func = gROOT->GetGlobalFunctionWithPrototype(funcname, prototype);
582 }
583 }
584
585 if (func && debug)
586 debug->append(TString::Format("Function: %s\n", func->GetPrototype()).Data());
587 }
588
589 if (!method && !func) {
590 if (debug)
591 debug->append("Method not found\n");
592 return debug != nullptr;
593 }
594
595 if ((fReadOnly && (fCurrentRestrict == 0)) || (fCurrentRestrict == 1)) {
596 if ((method != nullptr) && (fCurrentAllowedMethods.Index(method_name) == kNPOS)) {
597 if (debug)
598 debug->append("Server runs in read-only mode, method cannot be executed\n");
599 return debug != nullptr;
600 } else if ((func != nullptr) && (fCurrentAllowedMethods.Index(funcname) == kNPOS)) {
601 if (debug)
602 debug->append("Server runs in read-only mode, function cannot be executed\n");
603 return debug != nullptr;
604 } else {
605 if (debug)
606 debug->append("For that special method server allows access even read-only mode is specified\n");
607 }
608 }
609
610 TList *args = method ? method->GetListOfMethodArgs() : func->GetListOfMethodArgs();
611
612 TList garbage;
613 garbage.SetOwner(kTRUE); // use as garbage collection
614 TObject *post_obj = nullptr; // object reconstructed from post request
615 TString call_args;
616
617 TIter next(args);
618 while (auto arg = static_cast<TMethodArg *>(next())) {
619
620 if ((strcmp(arg->GetName(), "rest_url_opt") == 0) && (strcmp(arg->GetFullTypeName(), "const char*") == 0) &&
621 (args->GetSize() == 1)) {
622 // very special case - function requires list of options after method=argument
623
624 const char *pos = strstr(options.c_str(), "method=");
625 if (!pos || (strlen(pos) < strlen(method_name) + 7))
626 return debug != nullptr;
627 const char *rest_url = pos + strlen(method_name) + 7;
628 if (*rest_url == '&') ++rest_url;
629 call_args.Form("\"%s\"", rest_url);
630 break;
631 }
632
633 TString sval;
634 const char *val = url.GetValueFromOptions(arg->GetName());
635 if (val) {
636 sval = DecodeUrlOptionValue(val, kFALSE);
637 val = sval.Data();
638 }
639
640 if ((val != nullptr) && (strcmp(val, "_this_") == 0)) {
641 // special case - object itself is used as argument
642 sval.Form("(%s*)0x%zx", obj_cl->GetName(), (size_t)obj_ptr);
643 val = sval.Data();
644 } else if ((val != nullptr) && (fCurrentArg != nullptr) && (fCurrentArg->GetPostData() != nullptr)) {
645 // process several arguments which are specific for post requests
646 if (strcmp(val, "_post_object_xml_") == 0) {
647 // post data has extra 0 at the end and can be used as null-terminated string
648 post_obj = TBufferXML::ConvertFromXML((const char *)fCurrentArg->GetPostData());
649 if (!post_obj) {
650 sval = "0";
651 } else {
652 sval.Form("(%s*)0x%zx", post_obj->ClassName(), (size_t)post_obj);
653 if (url.HasOption("_destroy_post_"))
654 garbage.Add(post_obj);
655 }
656 val = sval.Data();
657 } else if (strcmp(val, "_post_object_json_") == 0) {
658 // post data has extra 0 at the end and can be used as null-terminated string
659 post_obj = TBufferJSON::ConvertFromJSON((const char *)fCurrentArg->GetPostData());
660 if (!post_obj) {
661 sval = "0";
662 } else {
663 sval.Form("(%s*)0x%zx", post_obj->ClassName(), (size_t)post_obj);
664 if (url.HasOption("_destroy_post_"))
665 garbage.Add(post_obj);
666 }
667 val = sval.Data();
668 } else if ((strcmp(val, "_post_object_") == 0) && url.HasOption("_post_class_")) {
669 TString clname = url.GetValueFromOptions("_post_class_");
670 TClass *arg_cl = gROOT->GetClass(clname, kTRUE, kTRUE);
671 if ((arg_cl != nullptr) && (arg_cl->GetBaseClassOffset(TObject::Class()) == 0) && (post_obj == nullptr)) {
672 post_obj = (TObject *)arg_cl->New();
673 if (post_obj == nullptr) {
674 if (debug)
675 debug->append(TString::Format("Fail to create object of class %s\n", clname.Data()).Data());
676 } else {
677 if (debug)
678 debug->append(TString::Format("Reconstruct object of class %s from POST data\n", clname.Data()).Data());
679 TBufferFile buf(TBuffer::kRead, fCurrentArg->GetPostDataLength(), (void *)fCurrentArg->GetPostData(), kFALSE);
680 buf.MapObject(post_obj, arg_cl);
681 post_obj->Streamer(buf);
682 if (url.HasOption("_destroy_post_"))
683 garbage.Add(post_obj);
684 }
685 }
686 sval.Form("(%s*)0x%zx", clname.Data(), (size_t)post_obj);
687 val = sval.Data();
688 } else if (strcmp(val, "_post_data_") == 0) {
689 sval.Form("(void*)0x%zx", (size_t)fCurrentArg->GetPostData());
690 val = sval.Data();
691 } else if (strcmp(val, "_post_length_") == 0) {
692 sval.Form("%ld", (long)fCurrentArg->GetPostDataLength());
693 val = sval.Data();
694 }
695 }
696
697 if (!val)
698 val = arg->GetDefault();
699
700 if (debug)
701 debug->append(TString::Format(" Argument:%s Type:%s Value:%s \n", arg->GetName(), arg->GetFullTypeName(),
702 val ? val : "<missed>")
703 .Data());
704 if (!val)
705 return debug != nullptr;
706
707 if (call_args.Length() > 0)
708 call_args += ", ";
709
710 if ((strcmp(arg->GetFullTypeName(), "const char*") == 0) || (strcmp(arg->GetFullTypeName(), "Option_t*") == 0)) {
711 int len = strlen(val);
712 if ((strlen(val) < 2) || (*val != '\"') || (val[len - 1] != '\"'))
713 call_args.Append(TString::Format("\"%s\"", val));
714 else
715 call_args.Append(val);
716 } else {
717 call_args.Append(val);
718 }
719 }
720
721 TMethodCall *call = nullptr;
722
723 if (method != nullptr) {
724 call = new TMethodCall(obj_cl, method_name, call_args.Data());
725 if (debug)
726 debug->append(TString::Format("Calling obj->%s(%s);\n", method_name, call_args.Data()).Data());
727 } else {
728 call = new TMethodCall(funcname.Data(), call_args.Data());
729 if (debug)
730 debug->append(TString::Format("Calling %s(%s);\n", funcname.Data(), call_args.Data()).Data());
731 }
732
733 garbage.Add(call);
734
735 if (!call->IsValid()) {
736 if (debug)
737 debug->append("Fail: invalid TMethodCall\n");
738 return debug != nullptr;
739 }
740
741 Int_t compact = 0;
742 if (url.GetValueFromOptions("compact"))
743 compact = url.GetIntValueFromOptions("compact");
744
745 TString res = "null";
746 void *ret_obj = nullptr;
747 TClass *ret_cl = nullptr;
748 TBufferFile *resbuf = nullptr;
749 if (reskind == 2) {
750 resbuf = new TBufferFile(TBuffer::kWrite, 10000);
751 garbage.Add(resbuf);
752 }
753
754 switch (call->ReturnType()) {
755 case TMethodCall::kLong: {
756 Longptr_t l(0);
757 if (method)
758 call->Execute(obj_ptr, l);
759 else
760 call->Execute(l);
761 if (resbuf)
762 resbuf->WriteLong(l);
763 else
764 res.Form("%zd", (size_t)l);
765 break;
766 }
768 Double_t d(0.);
769 if (method)
770 call->Execute(obj_ptr, d);
771 else
772 call->Execute(d);
773 if (resbuf)
774 resbuf->WriteDouble(d);
775 else
777 break;
778 }
780 char *txt = nullptr;
781 if (method)
782 call->Execute(obj_ptr, &txt);
783 else
784 call->Execute(&txt);
785 if (txt != nullptr) {
786 if (resbuf)
787 resbuf->WriteString(txt);
788 else
789 res.Form("\"%s\"", txt);
790 }
791 break;
792 }
793 case TMethodCall::kOther: {
794 std::string ret_kind = func ? func->GetReturnTypeNormalizedName() : method->GetReturnTypeNormalizedName();
795 if ((ret_kind.length() > 0) && (ret_kind[ret_kind.length() - 1] == '*')) {
796 ret_kind.resize(ret_kind.length() - 1);
797 ret_cl = gROOT->GetClass(ret_kind.c_str(), kTRUE, kTRUE);
798 }
799
800 if (ret_cl != nullptr) {
801 Longptr_t l(0);
802 if (method)
803 call->Execute(obj_ptr, l);
804 else
805 call->Execute(l);
806 if (l != 0)
807 ret_obj = (void *)l;
808 } else {
809 if (method)
810 call->Execute(obj_ptr);
811 else
812 call->Execute();
813 }
814
815 break;
816 }
817 case TMethodCall::kNone: {
818 if (method)
819 call->Execute(obj_ptr);
820 else
821 call->Execute();
822 break;
823 }
824 }
825
826 const char *_ret_object_ = url.GetValueFromOptions("_ret_object_");
827 if (_ret_object_ != nullptr) {
828 TObject *obj = nullptr;
829 if (gDirectory)
830 obj = gDirectory->Get(_ret_object_);
831 if (debug)
832 debug->append(TString::Format("Return object %s found %s\n", _ret_object_, obj ? "true" : "false").Data());
833
834 if (obj == nullptr) {
835 res = "null";
836 } else {
837 ret_obj = obj;
838 ret_cl = obj->IsA();
839 }
840 }
841
842 if (ret_obj && ret_cl) {
843 if ((resbuf != nullptr) && (ret_cl->GetBaseClassOffset(TObject::Class()) == 0)) {
844 TObject *obj = (TObject *)ret_obj;
845 resbuf->MapObject(obj);
846 obj->Streamer(*resbuf);
847 if (fCurrentArg)
848 fCurrentArg->SetExtraHeader("RootClassName", ret_cl->GetName());
849 } else {
850 res = TBufferJSON::ConvertToJSON(ret_obj, ret_cl, compact);
851 }
852 }
853
854 if ((resbuf != nullptr) && (resbuf->Length() > 0)) {
855 res_str.resize(resbuf->Length());
856 std::copy((const char *)resbuf->Buffer(), (const char *)resbuf->Buffer() + resbuf->Length(), res_str.begin());
857 }
858
859 if (debug)
860 debug->append(TString::Format("Result = %s\n", res.Data()).Data());
861
862 if (reskind == 1)
863 res_str = res.Data();
864
865 if (url.HasOption("_destroy_result_") && ret_obj && ret_cl) {
866 ret_cl->Destructor(ret_obj);
867 if (debug)
868 debug->append("Destroy result object at the end\n");
869 }
870
871 // delete all garbage objects, but should be also done with any return
872 garbage.Delete();
873
874 return kTRUE;
875}
#define d(i)
Definition RSha256.hxx:102
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
long Longptr_t
Integer large enough to hold a pointer (platform-dependent).
Definition RtypesCore.h:89
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:131
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define gDirectory
Definition TDirectory.h:385
#define gFile
Definition TFile.h:439
char name[80]
Definition TGX11.cxx:148
Int_t gDebug
Definition TROOT.cxx:777
#define gROOT
Definition TROOT.h:417
#define free
Definition civetweb.c:1578
A TTree is a list of TBranches.
Definition TBranch.h:93
static TClass * Class()
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
void WriteLong(Long_t l) override
void WriteString(const char *s) override
Write string to I/O buffer.
void WriteDouble(Double_t d) override
void MapObject(const TObject *obj, UInt_t offset=1) override
Add object to the fMap container.
static TObject * ConvertFromJSON(const char *str)
Read TObject-based class from JSON, produced by ConvertToJSON() method.
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=nullptr)
Converts object, inherited from TObject class, to JSON string Lower digit of compact parameter define...
static const char * GetFloatFormat()
return current printf format for float members, default "%e"
static TString ConvertToXML(const TObject *obj, Bool_t GenericLayout=kFALSE, Bool_t UseNamespaces=kFALSE)
Converts object, inherited from TObject class, to XML string GenericLayout defines layout choice for ...
static TObject * ConvertFromXML(const char *str, Bool_t GenericLayout=kFALSE, Bool_t UseNamespaces=kFALSE)
Read object from XML, produced by ConvertToXML() method.
void SetParent(TObject *parent)
Set parent owning this buffer.
Definition TBuffer.cxx:269
@ kWrite
Definition TBuffer.h:73
@ kRead
Definition TBuffer.h:73
Int_t Length() const
Definition TBuffer.h:100
char * Buffer() const
Definition TBuffer.h:96
The Canvas class.
Definition TCanvas.h:23
static TClass * Class()
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:5048
TMethod * GetMethodWithPrototype(const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Find the method with a given prototype.
Definition TClass.cxx:4514
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5470
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4932
Int_t GetBaseClassOffset(const TClass *toBase, void *address=nullptr, bool isDerivedObject=true)
Definition TClass.cxx:2812
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition TClass.cxx:4442
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2994
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
Int_t WriteObjectAny(const void *obj, const char *classname, const char *name, Option_t *option="", Int_t bufsize=0) override
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:981
Global functions class (global functions are obtained from CINT).
Definition TFunction.h:30
virtual const char * GetPrototype() const
Returns the prototype of a function as defined by CINT, or 0 in case of error.
TList * GetListOfMethodArgs()
Return list containing the TMethodArgs of a TFunction.
std::string GetReturnTypeNormalizedName() const
Get the normalized name of the return type.
static TClass * Class()
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
static TClass * Class()
An abstract interface to image processing library.
Definition TImage.h:29
virtual void FromPad(TVirtualPad *, Int_t=0, Int_t=0, UInt_t=0, UInt_t=0)
Definition TImage.h:122
EImageFileTypes
Definition TImage.h:36
@ kPng
Definition TImage.h:40
@ kJpeg
Definition TImage.h:41
@ kGif
Definition TImage.h:48
static TImage * Create()
Create an image.
Definition TImage.cxx:34
virtual void Append(const TImage *, const char *="+", const char *="#00000000")
Definition TImage.h:175
virtual void GetImageBuffer(char **, int *, EImageFileTypes=TImage::kPng)
Definition TImage.h:242
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:77
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:792
static TClass * Class()
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:600
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:27
Long64_t GetSize() const override
Return the current size of the memory file.
Definition TMemFile.cxx:289
virtual Long64_t CopyTo(void *to, Long64_t maxsize) const
Copy the binary representation of the TMemFile into the memory area starting at 'to' and of length at...
Definition TMemFile.cxx:254
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
Method or function calling interface.
Definition TMethodCall.h:37
EReturnType ReturnType()
Returns the return type of the method.
static const EReturnType kLong
Definition TMethodCall.h:43
static const EReturnType kString
Definition TMethodCall.h:45
static const EReturnType kOther
Definition TMethodCall.h:46
static const EReturnType kNone
Definition TMethodCall.h:49
void Execute(const char *, const char *, int *=nullptr) override
Execute method on this object with the given parameter string, e.g.
Definition TMethodCall.h:64
Bool_t IsValid() const
Return true if the method call has been properly initialized and is usable.
static const EReturnType kDouble
Definition TMethodCall.h:44
Each ROOT class (see TClass) has a linked list of methods.
Definition TMethod.h:38
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();.
Definition TMethod.cxx:306
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
TNamed()
Definition TNamed.h:38
static TClass * Class()
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:462
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:997
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:227
static TClass * Class()
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:549
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
virtual TClass * IsA() const
Definition TObject.h:248
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
TObject()
TObject constructor.
Definition TObject.h:259
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1072
The most important graphics class in the ROOT system.
Definition TPad.h:28
static TClass * Class()
static TClass * Class()
void * FindInHierarchy(const char *path, TClass **cl=nullptr, TDataMember **member=nullptr, Int_t *chld=nullptr) override
Search element with specified path.
Bool_t IsStreamerInfoItem(const char *itemname) override
Return true if it is streamer info item name.
static Bool_t IsDrawableClass(TClass *cl)
return true if given class can be drawn in JSROOT
Bool_t ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res) override
Execute command for specified object.
TList * fSinfo
! last produced streamer info
TRootSnifferFull(const char *name="sniff", const char *objpath="Objects")
constructor
Bool_t CanDrawClass(TClass *cl) override
void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj) override
Scans object properties.
virtual ~TRootSnifferFull()
destructor
void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj) override
Scans object childs (if any).
Bool_t ProduceRootFile(const std::string &path, const std::string &options, std::string &res) override
Produce ROOT file for specified item.
ULong_t GetItemHash(const char *itemname) override
Get hash function for specified item.
Bool_t ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res) override
Method to produce image from specified object.
Bool_t CallProduceImage(const std::string &kind, const std::string &path, const std::string &options, std::string &res) override
Invokes TRootSniffer::ProduceIamge, converting kind into TImage::EImageFileTypes type.
Bool_t ProduceXml(const std::string &path, const std::string &options, std::string &res) override
Produce XML data for specified item.
void CreateMemFile()
Creates TMemFile instance, which used for objects streaming.
Bool_t ProduceBinary(const std::string &path, const std::string &options, std::string &res) override
Produce binary data for specified item.
TMemFile * fMemFile
! file used to manage streamer infos
ULong_t GetStreamerInfoHash() override
Returns hash value for streamer infos.
void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class) override
Scans TKey properties.
Structure used to scan hierarchies of ROOT objects.
Bool_t CanExpandItem()
Returns true when item can be expanded.
void SetField(const char *name, const char *value, Bool_t with_quotes=kTRUE)
Set item field only when creating is specified.
Bool_t IsReadOnly(Bool_t dflt=kTRUE)
Returns read-only flag for current item.
TRootSniffer(const char *name="sniff", const char *objpath="Objects")
constructor
virtual void ScanObjectChilds(TRootSnifferScanRec &rec, TObject *obj)
scans object childs (if any) here one scans collection, branches, trees and so on
TString fCurrentAllowedMethods
! list of allowed methods, extracted when analyzed object restrictions
virtual void ScanKeyProperties(TRootSnifferScanRec &rec, TKey *key, TObject *&obj, TClass *&obj_class)
Scans TKey properties in special cases load objects from the file.
virtual void ScanObjectProperties(TRootSnifferScanRec &rec, TObject *obj)
Scans object properties here such fields as _autoload or _icon properties depending on class or objec...
TString DecodeUrlOptionValue(const char *value, Bool_t remove_quotes=kTRUE)
Method replaces all kind of special symbols, which could appear in URL options.
virtual ULong_t GetItemHash(const char *itemname)
Get hash function for specified item used to detect any changes in the specified object.
Bool_t fReadOnly
! indicate if sniffer allowed to change ROOT structures - like read objects from file
THttpCallArg * fCurrentArg
! current http arguments (if any)
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...
Int_t fCurrentRestrict
! current restriction for last-found object
void ScanCollection(TRootSnifferScanRec &rec, TCollection *lst, const char *foldername=nullptr, TCollection *keys_lst=nullptr)
Scan collection content.
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
const char * Data() const
Definition TString.h:384
TString & Append(const char *cs)
Definition TString.h:581
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
A TTree represents a columnar dataset.
Definition TTree.h:89
static TClass * Class()
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetValueFromOptions(const char *key) const
Return a value for a given key from the URL options.
Definition TUrl.cxx:662
Int_t GetIntValueFromOptions(const char *key) const
Return a value for a given key from the URL options as an Int_t, a missing key returns -1.
Definition TUrl.cxx:674
void SetOptions(const char *opt)
Definition TUrl.h:87
void ParseOptions() const
Parse URL options into a key/value map.
Definition TUrl.cxx:628
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
Definition TUrl.cxx:685
small helper class to store/restore gPad context in TPad methods
Definition TVirtualPad.h:61
return c1
Definition legend1.C:41
TGraphErrors * gr
Definition legend1.C:25
TLine l
Definition textangle.C:4