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\ingroup http
46
47Extends TRootSniffer for many ROOT classes
48
49Provides access to different ROOT collections and containers
50like TTree, TCanvas, TFile, ...
51*/
52
54
55////////////////////////////////////////////////////////////////////////////////
56/// constructor
57
58TRootSnifferFull::TRootSnifferFull(const char *name, const char *objpath) : TRootSniffer(name, objpath)
59{
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// destructor
64
66{
67 delete fSinfo;
68
69 delete fMemFile;
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// return true if given class can be drawn in JSROOT
74
76{
77 if (!cl)
78 return kFALSE;
79 if (cl->InheritsFrom(TH1::Class()))
80 return kTRUE;
81 if (cl->InheritsFrom(TGraph::Class()))
82 return kTRUE;
84 return kTRUE;
86 return kTRUE;
87 return kFALSE;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Scans object properties
92///
93/// here such fields as `_autoload` or `_icon` properties depending on class or object name could be assigned
94/// By default properties, coded in the Class title are scanned. Example:
95///
96/// ClassDef(UserClassName, 1) // class comments *SNIFF* _field1=value _field2="string value"
97///
98/// Here *SNIFF* mark is important. After it all expressions like field=value are parsed
99/// One could use double quotes to code string values with spaces.
100/// Fields separated from each other with spaces
101
103{
104 if (obj && obj->InheritsFrom(TLeaf::Class())) {
105 rec.SetField("_more", "false", kFALSE);
106 rec.SetField("_can_draw", "false", kFALSE);
107 rec.SetField("_player", "drawLeafPlayer");
108 rec.SetField("_module", "draw_tree");
109 return;
110 }
111
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Scans TKey properties
117///
118/// in special cases load objects from the file
119
121{
122 if (strcmp(key->GetClassName(), "TDirectoryFile") == 0) {
123 TRootSniffer::ScanKeyProperties(rec, key, obj, obj_class);
124 } else {
125 obj_class = TClass::GetClass(key->GetClassName());
126 if (obj_class && obj_class->InheritsFrom(TTree::Class())) {
127 if (rec.CanExpandItem()) {
128 // it is requested to expand tree element - read it
129 obj = key->ReadObj();
130 if (obj)
131 obj_class = obj->IsA();
132 } else {
133 rec.SetField("_ttree", "true", kFALSE); // indicate ROOT TTree
134 rec.SetField("_player", "drawTreePlayerKey");
135 rec.SetField("_module", "draw_tree");
136 // rec.SetField("_more", "true", kFALSE); // one could allow to extend
137 }
138 }
139 }
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Scans object childs (if any)
144///
145/// here one scans collection, branches, trees and so on
146
148{
149 if (obj->InheritsFrom(TTree::Class())) {
150 if (!rec.IsReadOnly(fReadOnly)) {
151 rec.SetField("_ttree", "true", kFALSE); // indicate ROOT TTree
152 rec.SetField("_player", "drawTreePlayer");
153 rec.SetField("_module", "draw_tree");
154 }
155 ScanCollection(rec, ((TTree *)obj)->GetListOfLeaves());
156 } else if (obj->InheritsFrom(TBranch::Class())) {
157 ScanCollection(rec, ((TBranch *)obj)->GetListOfLeaves());
158 } else {
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Returns hash value for streamer infos
165///
166/// At the moment - just number of items in streamer infos list.
167
169{
170 return fSinfo ? fSinfo->GetSize() : 0;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Return true if it is streamer info item name
175
177{
178 if (!itemname || (*itemname == 0))
179 return kFALSE;
180
181 return (strcmp(itemname, "StreamerInfo") == 0) || (strcmp(itemname, "StreamerInfo/") == 0);
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Get hash function for specified item
186///
187/// Used to detect any changes in the specified object
188
190{
191 if (IsStreamerInfoItem(itemname))
192 return GetStreamerInfoHash();
193
194 return TRootSniffer::GetItemHash(itemname);
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Creates TMemFile instance, which used for objects streaming
199///
200/// One could not use TBufferFile directly,
201/// while one also require streamer infos list
202
204{
205 if (fMemFile)
206 return;
207
208 TDirectory *olddir = gDirectory;
209 gDirectory = nullptr;
210 TFile *oldfile = gFile;
211 gFile = nullptr;
212
213 fMemFile = new TMemFile("dummy.file", "RECREATE");
214 gROOT->GetListOfFiles()->Remove(fMemFile);
215
216 TH1F *d = new TH1F("d", "d", 10, 0, 10);
217 fMemFile->WriteObject(d, "h1");
218 delete d;
219
220 TGraph *gr = new TGraph(10);
221 gr->SetName("abc");
222 // // gr->SetDrawOptions("AC*");
223 fMemFile->WriteObject(gr, "gr1");
224 delete gr;
225
226 fMemFile->WriteStreamerInfo();
227
228 // make primary list of streamer infos
229 TList *l = new TList();
230
231 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TGraph"));
232 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TH1F"));
233 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TH1"));
234 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TNamed"));
235 l->Add(gROOT->GetListOfStreamerInfo()->FindObject("TObject"));
236
237 fMemFile->WriteObject(l, "ll");
238 delete l;
239
240 fMemFile->WriteStreamerInfo();
241
242 fSinfo = fMemFile->GetStreamerInfoList();
243
244 gDirectory = olddir;
245 gFile = oldfile;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Search element with specified path
250///
251/// Returns pointer on element
252///
253/// Optionally one could obtain element class, member description
254/// and number of childs. When chld!=0, not only element is searched,
255/// but also number of childs are counted. When member!=0, any object
256/// will be scanned for its data members (disregard of extra options)
257
258void *TRootSnifferFull::FindInHierarchy(const char *path, TClass **cl, TDataMember **member, Int_t *chld)
259{
260 if (IsStreamerInfoItem(path)) {
261 // special handling for streamer info
263 if (cl && fSinfo)
264 *cl = fSinfo->IsA();
265 return fSinfo;
266 }
267
268 return TRootSniffer::FindInHierarchy(path, cl, member, chld);
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Produce binary data for specified item
273///
274/// if "zipped" option specified in query, buffer will be compressed
275
276Bool_t TRootSnifferFull::ProduceBinary(const std::string &path, const std::string & /*query*/, std::string &res)
277{
278 if (path.empty())
279 return kFALSE;
280
281 const char *path_ = path.c_str();
282 if (*path_ == '/')
283 path_++;
284
285 TClass *obj_cl = nullptr;
286 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
287 if (!obj_ptr || !obj_cl)
288 return kFALSE;
289
290 if (obj_cl->GetBaseClassOffset(TObject::Class()) != 0) {
291 Info("ProduceBinary", "Non-TObject class not supported");
292 return kFALSE;
293 }
294
295 // ensure that memfile exists
297
298 TDirectory *olddir = gDirectory;
299 gDirectory = nullptr;
300 TFile *oldfile = gFile;
301 gFile = nullptr;
302
303 TObject *obj = (TObject *)obj_ptr;
304
305 TBufferFile *sbuf = new TBufferFile(TBuffer::kWrite, 100000);
306 sbuf->SetParent(fMemFile);
307 sbuf->MapObject(obj);
308 obj->Streamer(*sbuf);
309 if (fCurrentArg)
310 fCurrentArg->SetExtraHeader("RootClassName", obj_cl->GetName());
311
312 // produce actual version of streamer info
313 delete fSinfo;
314 fMemFile->WriteStreamerInfo();
315 fSinfo = fMemFile->GetStreamerInfoList();
316
317 gDirectory = olddir;
318 gFile = oldfile;
319
320 res.resize(sbuf->Length());
321 std::copy((const char *)sbuf->Buffer(), (const char *)sbuf->Buffer() + sbuf->Length(), res.begin());
322
323 delete sbuf;
324
325 return kTRUE;
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// Produce ROOT file for specified item
330///
331/// File created in memory using TMemFile class.
332
333Bool_t TRootSnifferFull::ProduceRootFile(const std::string &path, const std::string & /*query*/, std::string &res)
334{
335 if (path.empty())
336 return kFALSE;
337
338 const char *path_ = path.c_str();
339 if (*path_ == '/')
340 path_++;
341
342 TClass *obj_cl = nullptr;
343 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
344 if (!obj_ptr || !obj_cl)
345 return kFALSE;
346
347 const char *store_name = "object";
348
349 if (obj_cl->GetBaseClassOffset(TNamed::Class()) == 0) {
350 const char *obj_name = ((TNamed *) obj_ptr)->GetName();
351 if (obj_name && *obj_name)
352 store_name = obj_name;
353 }
354
355 TDirectory *olddir = gDirectory;
356 gDirectory = nullptr;
357 TFile *oldfile = gFile;
358 gFile = nullptr;
359
360 {
361 TMemFile memfile("dummy.file", "RECREATE");
362 gROOT->GetListOfFiles()->Remove(&memfile);
363
364 memfile.WriteObjectAny(obj_ptr, obj_cl, store_name);
365 memfile.Close();
366
367 res.resize(memfile.GetSize());
368 memfile.CopyTo(res.data(), memfile.GetSize());
369 }
370
371 gDirectory = olddir;
372 gFile = oldfile;
373
374 return kTRUE;
375}
376
377
378////////////////////////////////////////////////////////////////////////////////
379/// Method to produce image from specified object
380///
381/// @param kind image kind TImage::kPng, TImage::kJpeg, TImage::kGif
382/// @param path path to object
383/// @param options extra options
384/// @param res std::string with binary data
385///
386/// By default, image 300x200 is produced
387/// In options string one could provide following parameters:
388///
389/// * w - image width
390/// * h - image height
391/// * opt - draw options
392///
393/// For instance:
394///
395/// http://localhost:8080/Files/hsimple.root/hpx/get.png?w=500&h=500&opt=lego1
396
397Bool_t TRootSnifferFull::ProduceImage(Int_t kind, const std::string &path, const std::string &options, std::string &res)
398{
399 if (path.empty())
400 return kFALSE;
401
402 const char *path_ = path.c_str();
403 if (*path_ == '/')
404 path_++;
405
406 TClass *obj_cl(nullptr);
407 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
408 if (!obj_ptr || !obj_cl)
409 return kFALSE;
410
411 if (obj_cl->GetBaseClassOffset(TObject::Class()) != 0) {
412 Error("TRootSniffer", "Only derived from TObject classes can be drawn");
413 return kFALSE;
414 }
415
416 TObject *obj = (TObject *)obj_ptr;
417
418 TImage *img = TImage::Create();
419 if (!img)
420 return kFALSE;
421
422 if (obj->InheritsFrom(TPad::Class())) {
423
424 if (gDebug > 1)
425 Info("TRootSniffer", "Crate IMAGE directly from pad");
426 img->FromPad((TPad *)obj);
427 } else if (CanDrawClass(obj->IsA())) {
428
429 if (gDebug > 1)
430 Info("TRootSniffer", "Crate IMAGE from object %s", obj->GetName());
431
432 Int_t width = 300, height = 200;
433 TString drawopt;
434
435 if (!options.empty()) {
436 TUrl url;
437 url.SetOptions(options.c_str());
438 url.ParseOptions();
439 Int_t w = url.GetIntValueFromOptions("w");
440 if (w > 10)
441 width = w;
442 Int_t h = url.GetIntValueFromOptions("h");
443 if (h > 10)
444 height = h;
445 drawopt = DecodeUrlOptionValue(url.GetValueFromOptions("opt"), kTRUE);
446 }
447
448 Bool_t isbatch = gROOT->IsBatch();
449 TVirtualPad::TContext ctxt(false);
450
451 if (!isbatch)
452 gROOT->SetBatch(kTRUE);
453
454 TCanvas *c1 = new TCanvas("__online_draw_canvas__", "title", width, height);
455 obj->Draw(drawopt.Data());
456 img->FromPad(c1);
457 delete c1;
458
459 if (!isbatch)
460 gROOT->SetBatch(kFALSE);
461
462 } else {
463 delete img;
464 return kFALSE;
465 }
466
467 TImage *im = TImage::Create();
468 im->Append(img);
469
470 char *png_buffer = nullptr;
471 int size(0);
472
473 im->GetImageBuffer(&png_buffer, &size, (TImage::EImageFileTypes)kind);
474
475 if (png_buffer && (size > 0)) {
476 res.resize(size);
477 memcpy((void *)res.data(), png_buffer, size);
478 }
479
480 free(png_buffer);
481 delete im;
482
483 return !res.empty();
484}
485
486////////////////////////////////////////////////////////////////////////////////
487/// Invokes TRootSniffer::ProduceIamge, converting kind into TImage::EImageFileTypes type
488
489Bool_t TRootSnifferFull::CallProduceImage(const std::string &kind, const std::string &path, const std::string &options, std::string &res)
490{
491 if (kind == "png")
492 return ProduceImage(TImage::kPng, path, options, res);
493
494 if (kind == "jpeg")
495 return ProduceImage(TImage::kJpeg, path, options, res);
496
497 if (kind == "gif")
498 return ProduceImage(TImage::kGif, path, options, res);
499
500 return kFALSE;
501}
502
503////////////////////////////////////////////////////////////////////////////////
504/// Produce XML data for specified item
505///
506/// For object conversion TBufferXML is used
507
508Bool_t TRootSnifferFull::ProduceXml(const std::string &path, const std::string & /*options*/, std::string &res)
509{
510 if (path.empty())
511 return kFALSE;
512 const char *path_ = path.c_str();
513 if (*path_ == '/')
514 path_++;
515
516 TClass *obj_cl = nullptr;
517 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
518 if (!obj_ptr || !obj_cl)
519 return kFALSE;
520
521 // TODO: support std::string in TBufferXML
522 res = TBufferXML::ConvertToXML(obj_ptr, obj_cl).Data();
523
524 return !res.empty();
525}
526
527////////////////////////////////////////////////////////////////////////////////
528/// Execute command for specified object
529///
530/// options include method and extra list of parameters
531/// sniffer should be not-readonly to allow execution of the commands
532/// @param reskind defines kind of result 0 - debug, 1 - json, 2 - binary
533
534Bool_t TRootSnifferFull::ProduceExe(const std::string &path, const std::string &options, Int_t reskind, std::string &res_str)
535{
536 std::string *debug = (reskind == 0) ? &res_str : nullptr;
537
538 if (path.empty()) {
539 if (debug)
540 debug->append("Item name not specified\n");
541 return debug != nullptr;
542 }
543
544 const char *path_ = path.c_str();
545 if (*path_ == '/')
546 path_++;
547
548 TClass *obj_cl = nullptr;
549 void *obj_ptr = FindInHierarchy(path_, &obj_cl);
550 if (debug)
551 debug->append(TString::Format("Item:%s found:%s\n", path_, obj_ptr ? "true" : "false").Data());
552 if (!obj_ptr || !obj_cl)
553 return debug != nullptr;
554
555 TUrl url;
556 url.SetOptions(options.c_str());
557
558 const char *method_name = url.GetValueFromOptions("method");
559 TString prototype = DecodeUrlOptionValue(url.GetValueFromOptions("prototype"), kTRUE);
560 TString funcname = DecodeUrlOptionValue(url.GetValueFromOptions("func"), kTRUE);
561 TMethod *method = nullptr;
562 TFunction *func = nullptr;
563 if (method_name) {
564 if (prototype.Length() == 0) {
565 if (debug)
566 debug->append(TString::Format("Search for any method with name \'%s\'\n", method_name).Data());
567 method = obj_cl->GetMethodAllAny(method_name);
568 } else {
569 if (debug)
570 debug->append(
571 TString::Format("Search for method \'%s\' with prototype \'%s\'\n", method_name, prototype.Data())
572 .Data());
573 method = obj_cl->GetMethodWithPrototype(method_name, prototype);
574 }
575 }
576
577 if (method) {
578 if (debug)
579 debug->append(TString::Format("Method: %s\n", method->GetPrototype()).Data());
580 } else {
581 if (funcname.Length() > 0) {
582 if (prototype.Length() == 0) {
583 if (debug)
584 debug->append(TString::Format("Search for any function with name \'%s\'\n", funcname.Data()).Data());
585 func = gROOT->GetGlobalFunction(funcname);
586 } else {
587 if (debug)
588 debug->append(TString::Format("Search for function \'%s\' with prototype \'%s\'\n", funcname.Data(),
589 prototype.Data())
590 .Data());
591 func = gROOT->GetGlobalFunctionWithPrototype(funcname, prototype);
592 }
593 }
594
595 if (func && debug)
596 debug->append(TString::Format("Function: %s\n", func->GetPrototype()).Data());
597 }
598
599 if (!method && !func) {
600 if (debug)
601 debug->append("Method not found\n");
602 return debug != nullptr;
603 }
604
605 if ((fReadOnly && (fCurrentRestrict == 0)) || (fCurrentRestrict == 1)) {
606 if ((method != nullptr) && (fCurrentAllowedMethods.Index(method_name) == kNPOS)) {
607 if (debug)
608 debug->append("Server runs in read-only mode, method cannot be executed\n");
609 return debug != nullptr;
610 } else if ((func != nullptr) && (fCurrentAllowedMethods.Index(funcname) == kNPOS)) {
611 if (debug)
612 debug->append("Server runs in read-only mode, function cannot be executed\n");
613 return debug != nullptr;
614 } else {
615 if (debug)
616 debug->append("For that special method server allows access even read-only mode is specified\n");
617 }
618 }
619
620 TList *args = method ? method->GetListOfMethodArgs() : func->GetListOfMethodArgs();
621
622 TList garbage;
623 garbage.SetOwner(kTRUE); // use as garbage collection
624 TObject *post_obj = nullptr; // object reconstructed from post request
625 TString call_args;
626
627 TIter next(args);
628 while (auto arg = static_cast<TMethodArg *>(next())) {
629
630 if ((strcmp(arg->GetName(), "rest_url_opt") == 0) && (strcmp(arg->GetFullTypeName(), "const char*") == 0) &&
631 (args->GetSize() == 1)) {
632 // very special case - function requires list of options after method=argument
633
634 const char *pos = strstr(options.c_str(), "method=");
635 if (!pos || (strlen(pos) < strlen(method_name) + 7))
636 return debug != nullptr;
637 const char *rest_url = pos + strlen(method_name) + 7;
638 if (*rest_url == '&') ++rest_url;
639 call_args.Append("\"");
640 call_args.Append(DecodeUrlOptionValue(rest_url, kTRUE));
641 call_args.Append("\"");
642 break;
643 }
644
645 TString sval;
646 const char *val = url.GetValueFromOptions(arg->GetName());
647 if (val)
648 sval = DecodeUrlOptionValue(val, kTRUE);
649
650 Bool_t sanitize_numeric = kFALSE;
651
652 if (sval == "_this_") {
653 // special case - object itself is used as argument
654 sval.Form("(%s*)0x%zx", obj_cl->GetName(), (size_t)obj_ptr);
655 } else if ((fCurrentArg != nullptr) && (fCurrentArg->GetPostData() != nullptr)) {
656 // process several arguments which are specific for post requests
657 if (fAllowPostObject && (sval == "_post_object_xml_")) {
658 // post data has extra 0 at the end and can be used as null-terminated string
659 post_obj = TBufferXML::ConvertFromXML((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 } else if (fAllowPostObject && (sval == "_post_object_json_")) {
668 // post data has extra 0 at the end and can be used as null-terminated string
669 post_obj = TBufferJSON::ConvertFromJSON((const char *)fCurrentArg->GetPostData());
670 if (!post_obj)
671 sval = "0";
672 else {
673 sval.Form("(%s*)0x%zx", post_obj->ClassName(), (size_t)post_obj);
674 if (url.HasOption("_destroy_post_"))
675 garbage.Add(post_obj);
676 }
677 } else if (fAllowPostObject && (sval == "_post_object_") && url.HasOption("_post_class_")) {
678 TString clname = DecodeUrlOptionValue(url.GetValueFromOptions("_post_class_"), kTRUE);
679 TClass *arg_cl = gROOT->GetClass(clname, kTRUE, kTRUE);
680 if ((arg_cl != nullptr) && (arg_cl->GetBaseClassOffset(TObject::Class()) == 0) && (post_obj == nullptr)) {
681 post_obj = (TObject *)arg_cl->New();
682 if (post_obj == nullptr) {
683 if (debug)
684 debug->append(TString::Format("Fail to create object of class %s\n", clname.Data()).Data());
685 } else {
686 if (debug)
687 debug->append(TString::Format("Reconstruct object of class %s from POST data\n", clname.Data()).Data());
688 TBufferFile buf(TBuffer::kRead, fCurrentArg->GetPostDataLength(), (void *)fCurrentArg->GetPostData(), kFALSE);
689 buf.MapObject(post_obj, arg_cl);
690 post_obj->Streamer(buf);
691 if (url.HasOption("_destroy_post_"))
692 garbage.Add(post_obj);
693 }
694 }
695 if (!post_obj)
696 sval = "0";
697 else
698 sval.Form("(%s*)0x%zx", clname.Data(), (size_t)post_obj);
699 } else if (sval == "_post_data_")
700 sval.Form("(void*)0x%zx", (size_t)fCurrentArg->GetPostData());
701 else if (sval == "_post_length_")
702 sval.Form("%ld", (long)fCurrentArg->GetPostDataLength());
703 else
704 sanitize_numeric = kTRUE;
705 } else
706 sanitize_numeric = kTRUE;
707
708 if (sval.IsNull() && arg->GetDefault())
709 sval = arg->GetDefault();
710
711 if (debug)
712 debug->append(
713 TString::Format(" Argument:%s Type:%s Value:%s \n", arg->GetName(), arg->GetFullTypeName(), sval.Data())
714 .Data());
715
716 if (call_args.Length() > 0)
717 call_args += ", ";
718
719 Bool_t isstr = (strcmp(arg->GetFullTypeName(), "const char*") == 0) ||
720 (strcmp(arg->GetFullTypeName(), "Option_t*") == 0) ||
721 (strcmp(arg->GetFullTypeName(), "string") == 0);
722
723 if (isstr) {
724 // check that quotes provided for the string argument
725 // all special characters were escaped before
726 if (sval.IsNull())
727 sval = "\"\"";
728 else {
729 if (sval[0] != '"')
730 sval.Prepend("\"");
731 if (sval[sval.Length() - 1] != '"')
732 sval.Append("\"");
733 }
734 } else {
735 // for numeric types keep only numeric and alphabetic characters
736 // exclude others - especially remove all escape characters
737 if (sanitize_numeric) {
738 TString sanitized;
739 for(Size_t i = 0; i < sval.Length(); ++i) {
740 if (std::isalnum(sval[i]) || std::strchr(".:+-", sval[i]))
741 sanitized.Append(sval[i]);
742 }
743 sval = sanitized;
744 }
745 if (sval.IsNull())
746 sval = "0";
747 }
748
749 call_args.Append(sval);
750 }
751
752 TMethodCall *call = nullptr;
753
754 if (method != nullptr) {
755 call = new TMethodCall(obj_cl, method_name, call_args.Data());
756 if (debug)
757 debug->append(TString::Format("Calling obj->%s(%s);\n", method_name, call_args.Data()).Data());
758 } else {
759 call = new TMethodCall(funcname.Data(), call_args.Data());
760 if (debug)
761 debug->append(TString::Format("Calling %s(%s);\n", funcname.Data(), call_args.Data()).Data());
762 }
763
764 garbage.Add(call);
765
766 if (!call->IsValid()) {
767 if (debug)
768 debug->append("Fail: invalid TMethodCall\n");
769 return debug != nullptr;
770 }
771
772 Int_t compact = 0;
773 if (url.GetValueFromOptions("compact"))
774 compact = url.GetIntValueFromOptions("compact");
775
776 TString res = "null";
777 void *ret_obj = nullptr;
778 TClass *ret_cl = nullptr;
779 TBufferFile *resbuf = nullptr;
780 if (reskind == 2) {
781 resbuf = new TBufferFile(TBuffer::kWrite, 10000);
782 garbage.Add(resbuf);
783 }
784
785 switch (call->ReturnType()) {
786 case TMethodCall::kLong: {
787 Longptr_t l(0);
788 if (method)
789 call->Execute(obj_ptr, l);
790 else
791 call->Execute(l);
792 if (resbuf)
793 resbuf->WriteLong(l);
794 else
795 res.Form("%zd", (size_t)l);
796 break;
797 }
799 Double_t d(0.);
800 if (method)
801 call->Execute(obj_ptr, d);
802 else
803 call->Execute(d);
804 if (resbuf)
805 resbuf->WriteDouble(d);
806 else
808 break;
809 }
811 char *txt = nullptr;
812 if (method)
813 call->Execute(obj_ptr, &txt);
814 else
815 call->Execute(0, &txt); // here 0 is artificial, there is no proper signature
816 if (txt != nullptr) {
817 if (resbuf)
818 resbuf->WriteString(txt);
819 else
820 res.Form("\"%s\"", txt);
821 }
822 break;
823 }
824 case TMethodCall::kOther: {
825 std::string ret_kind = func ? func->GetReturnTypeNormalizedName() : method->GetReturnTypeNormalizedName();
826 if ((ret_kind.length() > 0) && (ret_kind[ret_kind.length() - 1] == '*')) {
827 ret_kind.resize(ret_kind.length() - 1);
828 ret_cl = gROOT->GetClass(ret_kind.c_str(), kTRUE, kTRUE);
829 }
830
831 if (ret_cl != nullptr) {
832 Longptr_t l(0);
833 if (method)
834 call->Execute(obj_ptr, l);
835 else
836 call->Execute(l);
837 if (l != 0)
838 ret_obj = (void *)l;
839 } else {
840 if (method)
841 call->Execute(obj_ptr);
842 else
843 call->Execute();
844 }
845
846 break;
847 }
848 case TMethodCall::kNone: {
849 if (method)
850 call->Execute(obj_ptr);
851 else
852 call->Execute();
853 break;
854 }
855 }
856
857 const char *_ret_object_ = url.GetValueFromOptions("_ret_object_");
858 if (_ret_object_ != nullptr) {
859 TObject *obj = nullptr;
860 if (gDirectory)
861 obj = gDirectory->Get(_ret_object_);
862 if (debug)
863 debug->append(TString::Format("Return object %s found %s\n", _ret_object_, obj ? "true" : "false").Data());
864
865 if (obj == nullptr) {
866 res = "null";
867 } else {
868 ret_obj = obj;
869 ret_cl = obj->IsA();
870 }
871 }
872
873 if (ret_obj && ret_cl) {
874 if ((resbuf != nullptr) && (ret_cl->GetBaseClassOffset(TObject::Class()) == 0)) {
875 TObject *obj = (TObject *)ret_obj;
876 resbuf->MapObject(obj);
877 obj->Streamer(*resbuf);
878 if (fCurrentArg)
879 fCurrentArg->SetExtraHeader("RootClassName", ret_cl->GetName());
880 } else {
881 res = TBufferJSON::ConvertToJSON(ret_obj, ret_cl, compact);
882 }
883 }
884
885 if ((resbuf != nullptr) && (resbuf->Length() > 0)) {
886 res_str.resize(resbuf->Length());
887 std::copy((const char *)resbuf->Buffer(), (const char *)resbuf->Buffer() + resbuf->Length(), res_str.begin());
888 }
889
890 if (debug)
891 debug->append(TString::Format("Result = %s\n", res.Data()).Data());
892
893 if (reskind == 1)
894 res_str = res.Data();
895
896 if (url.HasOption("_destroy_result_") && ret_obj && ret_cl) {
897 ret_cl->Destructor(ret_obj);
898 if (debug)
899 debug->append("Destroy result object at the end\n");
900 }
901
902 // delete all garbage objects, but should be also done with any return
903 garbage.Delete();
904
905 return kTRUE;
906}
#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
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Size_t
Definition RtypesCore.h:96
long Longptr_t
Definition RtypesCore.h:82
unsigned long ULong_t
Definition RtypesCore.h:55
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
#define gDirectory
Definition TDirectory.h:384
#define gFile
Definition TFile.h:347
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void w
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
char name[80]
Definition TGX11.cxx:110
Int_t i
Int_t gDebug
Definition TROOT.cxx:622
#define gROOT
Definition TROOT.h:414
#define free
Definition civetweb.c:1539
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:270
@ 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:81
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:5005
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:4483
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5427
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4901
Int_t GetBaseClassOffset(const TClass *toBase, void *address=nullptr, bool isDerivedObject=true)
Definition TClass.cxx:2792
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition TClass.cxx:4411
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:2969
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
Write object from pointer of class classname in this directory.
Describe directory structure in memory.
Definition TDirectory.h:45
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:53
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:971
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.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
static TClass * Class()
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
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:35
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:241
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:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:775
static TClass * Class()
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:19
Long64_t GetSize() const override
Return the current size of the memory file.
Definition TMemFile.cxx:290
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:255
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:307
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
TNamed()
Definition TNamed.h:36
static TClass * Class()
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:439
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:888
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
static TClass * Class()
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:525
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual TClass * IsA() const
Definition TObject.h:240
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
TObject()
TObject constructor.
Definition TObject.h:251
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:961
The most important graphics class in the ROOT system.
Definition TPad.h:28
static TClass * Class()
static TClass * Class()
Extends TRootSniffer for many ROOT classes.
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.
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.
Bool_t fAllowPostObject
! when true allow to deserialize objects received via POST requests
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:139
Ssiz_t Length() const
Definition TString.h:417
const char * Data() const
Definition TString.h:376
TString & Prepend(const char *cs)
Definition TString.h:673
Bool_t IsNull() const
Definition TString.h:414
TString & Append(const char *cs)
Definition TString.h:572
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:2378
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
A TTree represents a columnar dataset.
Definition TTree.h:79
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:660
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:672
void SetOptions(const char *opt)
Definition TUrl.h:87
void ParseOptions() const
Parse URL options into a key/value map.
Definition TUrl.cxx:626
Bool_t HasOption(const char *key) const
Returns true if the given key appears in the URL options list.
Definition TUrl.cxx:683
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