Logo ROOT   6.13/01
Reference Guide
TObject.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 26/12/94
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 /** \class TObject
13 \ingroup Base
14 
15 Mother of all ROOT objects.
16 
17 The TObject class provides default behaviour and protocol for all
18 objects in the ROOT system. It provides protocol for object I/O,
19 error handling, sorting, inspection, printing, drawing, etc.
20 Every object which inherits from TObject can be stored in the
21 ROOT collection classes.
22 
23 TObject's bits can be used as flags, bits 0 - 13 and 24-31 are
24 reserved as global bits while bits 14 - 23 can be used in different
25 class hierarchies (watch out for overlaps).
26 */
27 
28 #include <string.h>
29 #if !defined(WIN32) && !defined(__MWERKS__) && !defined(R__SOLARIS)
30 #include <strings.h>
31 #endif
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sstream>
35 
36 #include "Varargs.h"
37 #include "Riostream.h"
38 #include "TObject.h"
39 #include "TClass.h"
40 #include "TGuiFactory.h"
41 #include "TMethod.h"
42 #include "TROOT.h"
43 #include "TError.h"
44 #include "TObjectTable.h"
45 #include "TVirtualPad.h"
46 #include "TInterpreter.h"
47 #include "TMemberInspector.h"
48 #include "TObjString.h"
49 #include "TRefTable.h"
50 #include "TProcessID.h"
51 
54 
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Copy this to obj.
60 
61 void TObject::Copy(TObject &obj) const
62 {
63  obj.fUniqueID = fUniqueID; // when really unique don't copy
64  if (obj.IsOnHeap()) { // test uses fBits so don't move next line
65  obj.fBits = fBits;
66  obj.fBits |= kIsOnHeap;
67  } else {
68  obj.fBits = fBits;
69  obj.fBits &= ~kIsOnHeap;
70  }
71  obj.fBits &= ~kIsReferenced;
72  obj.fBits &= ~kCanDelete;
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// TObject destructor. Removes object from all canvases and object browsers
77 /// if observer bit is on and remove from the global object table.
78 
80 {
81  // if (!TestBit(kNotDeleted))
82  // Fatal("~TObject", "object deleted twice");
83 
85 
86  fBits &= ~kNotDeleted;
87 
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Private helper function which will dispatch to
93 /// TObjectTable::AddObj.
94 /// Included here to avoid circular dependency between header files.
95 
97 {
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Append graphics object to current pad. In case no current pad is set
103 /// yet, create a default canvas with the name "c1".
104 
106 {
107  if (!gPad) {
108  gROOT->MakeDefCanvas();
109  }
110  if (!gPad->IsEditable()) return;
112  gPad->GetListOfPrimitives()->Add(this,option);
113  gPad->Modified(kTRUE);
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Browse object. May be overridden for another default action
118 
120 {
121  //Inspect();
122  TClass::AutoBrowse(this,b);
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Returns name of class to which the object belongs.
127 
128 const char *TObject::ClassName() const
129 {
130  return IsA()->GetName();
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Make a clone of an object using the Streamer facility.
135 /// If the object derives from TNamed, this function is called
136 /// by TNamed::Clone. TNamed::Clone uses the optional argument to set
137 /// a new name to the newly created object.
138 ///
139 /// If the object class has a DirectoryAutoAdd function, it will be
140 /// called at the end of the function with the parameter gDirectory.
141 /// This usually means that the object will be appended to the current
142 /// ROOT directory.
143 
144 TObject *TObject::Clone(const char *) const
145 {
146  if (gDirectory) {
147  return gDirectory->CloneObject(this);
148  } else {
149  Fatal("Clone","No gDirectory set");
150  return 0;
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Compare abstract method. Must be overridden if a class wants to be able
156 /// to compare itself with other objects. Must return -1 if this is smaller
157 /// than obj, 0 if objects are equal and 1 if this is larger than obj.
158 
160 {
161  AbstractMethod("Compare");
162  return 0;
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Delete this object. Typically called as a command via the interpreter.
167 /// Normally use "delete" operator when object has been allocated on the heap.
168 
170 {
171  if (IsOnHeap()) {
172  // Delete object from CINT symbol table so it can not be used anymore.
173  // CINT object are always on the heap.
174  gInterpreter->DeleteGlobal(this);
175 
176  delete this;
177  }
178 }
179 
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Computes distance from point (px,py) to the object.
183 /// This member function must be implemented for each graphics primitive.
184 /// This default function returns a big number (999999).
185 
187 {
188  // AbstractMethod("DistancetoPrimitive");
189  return 999999;
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Default Draw method for all objects
194 
195 void TObject::Draw(Option_t *option)
196 {
197  AppendPad(option);
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Draw class inheritance tree of the class to which this object belongs.
202 /// If a class B inherits from a class A, description of B is drawn
203 /// on the right side of description of A.
204 /// Member functions overridden by B are shown in class A with a blue line
205 /// crossing-out the corresponding member function.
206 /// The following picture is the class inheritance tree of class TPaveLabel:
207 ///
208 /// \image html base_object.png
209 
210 void TObject::DrawClass() const
211 {
212  IsA()->Draw();
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Draw a clone of this object in the current selected pad for instance with:
217 /// `gROOT->SetSelectedPad(gPad)`.
218 
220 {
221  TVirtualPad *pad = gROOT->GetSelectedPad();
222  TVirtualPad *padsav = gPad;
223  if (pad) pad->cd();
224 
225  TObject *newobj = Clone();
226  if (!newobj) return 0;
227  if (pad) {
228  if (strlen(option)) pad->GetListOfPrimitives()->Add(newobj,option);
229  else pad->GetListOfPrimitives()->Add(newobj,GetDrawOption());
230  pad->Modified(kTRUE);
231  pad->Update();
232  if (padsav) padsav->cd();
233  return newobj;
234  }
235  if (strlen(option)) newobj->Draw(option);
236  else newobj->Draw(GetDrawOption());
237  if (padsav) padsav->cd();
238 
239  return newobj;
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Dump contents of object on stdout.
244 /// Using the information in the object dictionary (class TClass)
245 /// each data member is interpreted.
246 /// If a data member is a pointer, the pointer value is printed
247 ///
248 /// The following output is the Dump of a TArrow object:
249 /// ~~~ {.cpp}
250 /// fAngle 0 Arrow opening angle (degrees)
251 /// fArrowSize 0.2 Arrow Size
252 /// fOption.*fData
253 /// fX1 0.1 X of 1st point
254 /// fY1 0.15 Y of 1st point
255 /// fX2 0.67 X of 2nd point
256 /// fY2 0.83 Y of 2nd point
257 /// fUniqueID 0 object unique identifier
258 /// fBits 50331648 bit field status word
259 /// fLineColor 1 line color
260 /// fLineStyle 1 line style
261 /// fLineWidth 1 line width
262 /// fFillColor 19 fill area color
263 /// fFillStyle 1001 fill area style
264 /// ~~~
265 
266 void TObject::Dump() const
267 {
268  // Get the actual address of the object.
269  const void *actual = IsA()->DynamicCast(TObject::Class(),this,kFALSE);
270  IsA()->Dump(actual);
271 }
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Execute method on this object with the given parameter string, e.g.
275 /// "3.14,1,\"text\"".
276 
277 void TObject::Execute(const char *method, const char *params, Int_t *error)
278 {
279  if (!IsA()) return;
280 
281  Bool_t must_cleanup = TestBit(kMustCleanup);
282 
283  gInterpreter->Execute(this, IsA(), method, params, error);
284 
285  if (gPad && must_cleanup) gPad->Modified();
286 }
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// Execute method on this object with parameters stored in the TObjArray.
290 /// The TObjArray should contain an argv vector like:
291 /// ~~~ {.cpp}
292 /// argv[0] ... argv[n] = the list of TObjString parameters
293 /// ~~~
294 
295 void TObject::Execute(TMethod *method, TObjArray *params, Int_t *error)
296 {
297  if (!IsA()) return;
298 
299  Bool_t must_cleanup = TestBit(kMustCleanup);
300 
301  gInterpreter->Execute(this, IsA(), method, params, error);
302 
303  if (gPad && must_cleanup) gPad->Modified();
304 }
305 
306 
307 ////////////////////////////////////////////////////////////////////////////////
308 /// Execute action corresponding to an event at (px,py). This method
309 /// must be overridden if an object can react to graphics events.
310 
312 {
313  // AbstractMethod("ExecuteEvent");
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Must be redefined in derived classes.
318 /// This function is typically used with TCollections, but can also be used
319 /// to find an object by name inside this object.
320 
321 TObject *TObject::FindObject(const char *) const
322 {
323  return 0;
324 }
325 
326 ////////////////////////////////////////////////////////////////////////////////
327 /// Must be redefined in derived classes.
328 /// This function is typically used with TCollections, but can also be used
329 /// to find an object inside this object.
330 
332 {
333  return 0;
334 }
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// Get option used by the graphics system to draw this object.
338 /// Note that before calling object.GetDrawOption(), you must
339 /// have called object.Draw(..) before in the current pad.
340 
342 {
343  if (!gPad) return "";
344 
345  TListIter next(gPad->GetListOfPrimitives());
346  TObject *obj;
347  while ((obj = next())) {
348  if (obj == this) return next.GetOption();
349  }
350  return "";
351 }
352 
353 ////////////////////////////////////////////////////////////////////////////////
354 /// Returns name of object. This default method returns the class name.
355 /// Classes that give objects a name should override this method.
356 
357 const char *TObject::GetName() const
358 {
359  return IsA()->GetName();
360 }
361 
362 ////////////////////////////////////////////////////////////////////////////////
363 /// Returns mime type name of object. Used by the TBrowser (via TGMimeTypes
364 /// class). Override for class of which you would like to have different
365 /// icons for objects of the same class.
366 
367 const char *TObject::GetIconName() const
368 {
369  return 0;
370 }
371 
372 ////////////////////////////////////////////////////////////////////////////////
373 /// Return the unique object id.
374 
376 {
377  return fUniqueID;
378 }
379 
380 ////////////////////////////////////////////////////////////////////////////////
381 /// Returns string containing info about the object at position (px,py).
382 /// This method is typically overridden by classes of which the objects
383 /// can report peculiarities for different positions.
384 /// Returned string will be re-used (lock in MT environment).
385 
386 char *TObject::GetObjectInfo(Int_t px, Int_t py) const
387 {
388  if (!gPad) return (char*)"";
389  static char info[64];
390  Float_t x = gPad->AbsPixeltoX(px);
391  Float_t y = gPad->AbsPixeltoY(py);
392  snprintf(info,64,"x=%g, y=%g",gPad->PadtoX(x),gPad->PadtoY(y));
393  return info;
394 }
395 
396 ////////////////////////////////////////////////////////////////////////////////
397 /// Returns title of object. This default method returns the class title
398 /// (i.e. description). Classes that give objects a title should override
399 /// this method.
400 
401 const char *TObject::GetTitle() const
402 {
403  return IsA()->GetTitle();
404 }
405 
406 
407 ////////////////////////////////////////////////////////////////////////////////
408 /// Execute action in response of a timer timing out. This method
409 /// must be overridden if an object has to react to timers.
410 
412 {
413  return kFALSE;
414 }
415 
416 ////////////////////////////////////////////////////////////////////////////////
417 /// Return hash value for this object.
418 ///
419 /// Note: If this routine is overloaded in a derived class, this derived class
420 /// should also add
421 /// ~~~ {.cpp}
422 /// ROOT::CallRecursiveRemoveIfNeeded(*this)
423 /// ~~~
424 /// Otherwise, when RecursiveRemove is called (by ~TObject or example) for this
425 /// type of object, the transversal of THashList and THashTable containers will
426 /// will have to be done without call Hash (and hence be linear rather than
427 /// logarithmic complexity). You will also see warnings like
428 /// ~~~
429 /// Error in <ROOT::Internal::TCheckHashRecurveRemoveConsistency::CheckRecursiveRemove>: The class SomeName overrides TObject::Hash but does not call TROOT::RecursiveRemove in its destructor.
430 /// ~~~
431 ///
432 
434 {
435  //return (ULong_t) this >> 2;
436  const void *ptr = this;
437  return TString::Hash(&ptr, sizeof(void*));
438 }
439 
440 ////////////////////////////////////////////////////////////////////////////////
441 /// Returns kTRUE if object inherits from class "classname".
442 
443 Bool_t TObject::InheritsFrom(const char *classname) const
444 {
445  return IsA()->InheritsFrom(classname);
446 }
447 
448 ////////////////////////////////////////////////////////////////////////////////
449 /// Returns kTRUE if object inherits from TClass cl.
450 
452 {
453  return IsA()->InheritsFrom(cl);
454 }
455 
456 ////////////////////////////////////////////////////////////////////////////////
457 /// Dump contents of this object in a graphics canvas.
458 /// Same action as Dump but in a graphical form.
459 /// In addition pointers to other objects can be followed.
460 ///
461 /// The following picture is the Inspect of a histogram object:
462 /// \image html base_inspect.png
463 
464 void TObject::Inspect() const
465 {
466  gGuiFactory->CreateInspectorImp(this, 400, 200);
467 }
468 
469 ////////////////////////////////////////////////////////////////////////////////
470 /// Returns kTRUE in case object contains browsable objects (like containers
471 /// or lists of other objects).
472 
474 {
475  return kFALSE;
476 }
477 
478 ////////////////////////////////////////////////////////////////////////////////
479 /// Default equal comparison (objects are equal if they have the same
480 /// address in memory). More complicated classes might want to override
481 /// this function.
482 
483 Bool_t TObject::IsEqual(const TObject *obj) const
484 {
485  return obj == this;
486 }
487 
488 ////////////////////////////////////////////////////////////////////////////////
489 /// The ls function lists the contents of a class on stdout. Ls output
490 /// is typically much less verbose then Dump().
491 
492 void TObject::ls(Option_t *option) const
493 {
495  std::cout <<"OBJ: " << IsA()->GetName() << "\t" << GetName() << "\t" << GetTitle() << " : ";
496  std::cout << Int_t(TestBit(kCanDelete));
497  if (option && strstr(option,"noaddr")==0) {
498  std::cout <<" at: "<< this ;
499  }
500  std::cout << std::endl;
501 }
502 
503 ////////////////////////////////////////////////////////////////////////////////
504 /// This method must be overridden to handle object notification.
505 
507 {
508  return kFALSE;
509 }
510 
511 ////////////////////////////////////////////////////////////////////////////////
512 /// This method must be overridden if a class wants to paint itself.
513 /// The difference between Paint() and Draw() is that when a object
514 /// draws itself it is added to the display list of the pad in
515 /// which it is drawn (and automatically redrawn whenever the pad is
516 /// redrawn). While paint just draws the object without adding it to
517 /// the pad display list.
518 
520 {
521  // AbstractMethod("Paint");
522 }
523 
524 ////////////////////////////////////////////////////////////////////////////////
525 /// Pop on object drawn in a pad to the top of the display list. I.e. it
526 /// will be drawn last and on top of all other primitives.
527 
529 {
530  if (!gPad) return;
531 
532  if (this == gPad->GetListOfPrimitives()->Last()) return;
533 
534  TListIter next(gPad->GetListOfPrimitives());
535  TObject *obj;
536  while ((obj = next()))
537  if (obj == this) {
538  char *opt = StrDup(next.GetOption());
539  gPad->GetListOfPrimitives()->Remove((TObject*)this);
540  gPad->GetListOfPrimitives()->AddLast(this, opt);
541  gPad->Modified();
542  delete [] opt;
543  return;
544  }
545 }
546 
547 ////////////////////////////////////////////////////////////////////////////////
548 /// This method must be overridden when a class wants to print itself.
549 
551 {
552  std::cout <<"OBJ: " << IsA()->GetName() << "\t" << GetName() << "\t" << GetTitle() << std::endl;
553 }
554 
555 ////////////////////////////////////////////////////////////////////////////////
556 /// Read contents of object with specified name from the current directory.
557 /// First the key with the given name is searched in the current directory,
558 /// next the key buffer is deserialized into the object.
559 /// The object must have been created before via the default constructor.
560 /// See TObject::Write().
561 
563 {
564  if (gDirectory) return gDirectory->ReadTObject(this,name);
565  else return 0;
566 }
567 
568 ////////////////////////////////////////////////////////////////////////////////
569 /// Recursively remove this object from a list. Typically implemented
570 /// by classes that can contain multiple references to a same object.
571 
573 {
574 }
575 
576 
577 ////////////////////////////////////////////////////////////////////////////////
578 /// Save this object in the file specified by filename.
579 ///
580 /// - if "filename" contains ".root" the object is saved in filename as root
581 /// binary file.
582 ///
583 /// - if "filename" contains ".xml" the object is saved in filename as a xml
584 /// ascii file.
585 ///
586 /// - if "filename" contains ".cc" the object is saved in filename as C code
587 /// independant from ROOT. The code is generated via SavePrimitive().
588 /// Specific code should be implemented in each object to handle this
589 /// option. Like in TF1::SavePrimitive().
590 ///
591 /// - otherwise the object is written to filename as a CINT/C++ script. The
592 /// C++ code to rebuild this object is generated via SavePrimitive(). The
593 /// "option" parameter is passed to SavePrimitive. By default it is an empty
594 /// string. It can be used to specify the Draw option in the code generated
595 /// by SavePrimitive.
596 ///
597 /// The function is available via the object context menu.
598 
599 void TObject::SaveAs(const char *filename, Option_t *option) const
600 {
601  //==============Save object as a root file===================================
602  if (filename && strstr(filename,".root")) {
603  if (gDirectory) gDirectory->SaveObjectAs(this,filename,"");
604  return;
605  }
606 
607  //==============Save object as a XML file====================================
608  if (filename && strstr(filename,".xml")) {
609  if (gDirectory) gDirectory->SaveObjectAs(this,filename,"");
610  return;
611  }
612 
613  //==============Save object as a JSON file================================
614  if (filename && strstr(filename,".json")) {
615  if (gDirectory) gDirectory->SaveObjectAs(this,filename,option);
616  return;
617  }
618 
619  //==============Save object as a C, ROOT independant, file===================
620  if (filename && strstr(filename,".cc")) {
621  char *fname = 0;
622  if (filename && strlen(filename) > 0) {
623  fname = (char*)filename;
624  } else {
625  fname = Form("%s.cc", GetName());
626  }
627  std::ofstream out;
628  out.open(fname, std::ios::out);
629  if (!out.good ()) {
630  Error("SaveAs", "cannot open file: %s", fname);
631  return;
632  }
633  ((TObject*)this)->SavePrimitive(out,"cc");
634  out.close();
635  Info("SaveAs", "cc file: %s has been generated", fname);
636  return;
637  }
638 
639  //==============Save as a C++ CINT file======================================
640  char *fname = 0;
641  if (filename && strlen(filename) > 0) {
642  fname = (char*)filename;
643  } else {
644  fname = Form("%s.C", GetName());
645  }
646  std::ofstream out;
647  out.open(fname, std::ios::out);
648  if (!out.good ()) {
649  Error("SaveAs", "cannot open file: %s", fname);
650  return;
651  }
652  out <<"{"<<std::endl;
653  out <<"//========= Macro generated from object: "<<GetName()<<"/"<<GetTitle()<<std::endl;
654  out <<"//========= by ROOT version"<<gROOT->GetVersion()<<std::endl;
655  ((TObject*)this)->SavePrimitive(out,option);
656  out <<"}"<<std::endl;
657  out.close();
658  Info("SaveAs", "C++ Macro file: %s has been generated", fname);
659 }
660 
661 ////////////////////////////////////////////////////////////////////////////////
662 /// Save a primitive as a C++ statement(s) on output stream "out".
663 
664 void TObject::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
665 {
666  out << "//Primitive: " << GetName() << "/" << GetTitle()
667  <<". You must implement " << ClassName() << "::SavePrimitive" << std::endl;
668 }
669 
670 ////////////////////////////////////////////////////////////////////////////////
671 /// Set drawing option for object. This option only affects
672 /// the drawing style and is stored in the option field of the
673 /// TObjOptLink supporting a TPad's primitive list (TList).
674 /// Note that it does not make sense to call object.SetDrawOption(option)
675 /// before having called object.Draw().
676 
678 {
679  if (!gPad || !option) return;
680 
681  TListIter next(gPad->GetListOfPrimitives());
682  delete gPad->FindObject("Tframe");
683  TObject *obj;
684  while ((obj = next()))
685  if (obj == this) {
686  next.SetOption(option);
687  return;
688  }
689 }
690 
691 ////////////////////////////////////////////////////////////////////////////////
692 /// Set or unset the user status bits as specified in f.
693 
695 {
696  if (set)
697  SetBit(f);
698  else
699  ResetBit(f);
700 }
701 
702 ////////////////////////////////////////////////////////////////////////////////
703 /// Set the unique object id.
704 
706 {
707  fUniqueID = uid;
708 }
709 
710 ////////////////////////////////////////////////////////////////////////////////
711 /// Set current style settings in this object
712 /// This function is called when either TCanvas::UseCurrentStyle
713 /// or TROOT::ForceStyle have been invoked.
714 
716 {
717 }
718 
719 ////////////////////////////////////////////////////////////////////////////////
720 /// Write this object to the current directory.
721 /// The data structure corresponding to this object is serialized.
722 /// The corresponding buffer is written to the current directory
723 /// with an associated key with name "name".
724 ///
725 /// Writing an object to a file involves the following steps:
726 ///
727 /// - Creation of a support TKey object in the current directory.
728 /// The TKey object creates a TBuffer object.
729 ///
730 /// - The TBuffer object is filled via the class::Streamer function.
731 ///
732 /// - If the file is compressed (default) a second buffer is created to
733 /// hold the compressed buffer.
734 ///
735 /// - Reservation of the corresponding space in the file by looking
736 /// in the TFree list of free blocks of the file.
737 ///
738 /// - The buffer is written to the file.
739 ///
740 /// Bufsize can be given to force a given buffer size to write this object.
741 /// By default, the buffersize will be taken from the average buffer size
742 /// of all objects written to the current file so far.
743 ///
744 /// If a name is specified, it will be the name of the key.
745 /// If name is not given, the name of the key will be the name as returned
746 /// by GetName().
747 ///
748 /// The option can be a combination of: kSingleKey, kOverwrite or kWriteDelete
749 /// Using the kOverwrite option a previous key with the same name is
750 /// overwritten. The previous key is deleted before writing the new object.
751 /// Using the kWriteDelete option a previous key with the same name is
752 /// deleted only after the new object has been written. This option
753 /// is safer than kOverwrite but it is slower.
754 /// The kSingleKey option is only used by TCollection::Write() to write
755 /// a container with a single key instead of each object in the container
756 /// with its own key.
757 ///
758 /// An object is read from the file into memory via TKey::Read() or
759 /// via TObject::Read().
760 ///
761 /// The function returns the total number of bytes written to the file.
762 /// It returns 0 if the object cannot be written.
763 
764 Int_t TObject::Write(const char *name, Int_t option, Int_t bufsize) const
765 {
766  TString opt = "";
767  if (option & kSingleKey) opt += "SingleKey";
768  if (option & kOverwrite) opt += "OverWrite";
769  if (option & kWriteDelete) opt += "WriteDelete";
770 
771  if (gDirectory) return gDirectory->WriteTObject(this,name,opt.Data(),bufsize);
772  else {
773  const char *objname = "no name specified";
774  if (name) objname = name;
775  else objname = GetName();
776  Error("Write","The current directory (gDirectory) is null. The object (%s) has not been written.",objname);
777  return 0;
778  }
779 }
780 
781 ////////////////////////////////////////////////////////////////////////////////
782 /// Write this object to the current directory. For more see the
783 /// const version of this method.
784 
785 Int_t TObject::Write(const char *name, Int_t option, Int_t bufsize)
786 {
787  return ((const TObject*)this)->Write(name, option, bufsize);
788 }
789 
790 ////////////////////////////////////////////////////////////////////////////////
791 /// Stream an object of class TObject.
792 
793 void TObject::Streamer(TBuffer &R__b)
794 {
795  if (IsA()->CanIgnoreTObjectStreamer()) return;
796  UShort_t pidf;
797  if (R__b.IsReading()) {
798  R__b.SkipVersion(); // Version_t R__v = R__b.ReadVersion(); if (R__v) { }
799  R__b >> fUniqueID;
800  R__b >> fBits;
801  fBits |= kIsOnHeap; // by definition de-serialized object is on heap
802  if (TestBit(kIsReferenced)) {
803  //if the object is referenced, we must read its old address
804  //and store it in the ProcessID map in gROOT
805  R__b >> pidf;
806  pidf += R__b.GetPidOffset();
807  TProcessID *pid = R__b.ReadProcessID(pidf);
808  if (pid) {
809  UInt_t gpid = pid->GetUniqueID();
810  if (gpid>=0xff) {
811  fUniqueID = fUniqueID | 0xff000000;
812  } else {
813  fUniqueID = ( fUniqueID & 0xffffff) + (gpid<<24);
814  }
815  pid->PutObjectWithID(this);
816  }
817  }
818  } else {
819  R__b.WriteVersion(TObject::IsA());
820  if (!TestBit(kIsReferenced)) {
821  R__b << fUniqueID;
822  R__b << fBits;
823  } else {
824  //if the object is referenced, we must save its address/file_pid
825  UInt_t uid = fUniqueID & 0xffffff;
826  R__b << uid;
827  R__b << fBits;
829  //add uid to the TRefTable if there is one
831  if(table) table->Add(uid, pid);
832  pidf = R__b.WriteProcessID(pid);
833  R__b << pidf;
834  }
835  }
836 }
837 
838 ////////////////////////////////////////////////////////////////////////////////
839 /// Interface to ErrorHandler (protected).
840 
841 void TObject::DoError(int level, const char *location, const char *fmt, va_list va) const
842 {
843  const char *classname = "UnknownClass";
844  if (TROOT::Initialized())
845  classname = ClassName();
846 
847  ::ErrorHandler(level, Form("%s::%s", classname, location), fmt, va);
848 }
849 
850 ////////////////////////////////////////////////////////////////////////////////
851 /// Issue info message. Use "location" to specify the method where the
852 /// warning occurred. Accepts standard printf formatting arguments.
853 
854 void TObject::Info(const char *location, const char *va_(fmt), ...) const
855 {
856  va_list ap;
857  va_start(ap, va_(fmt));
858  DoError(kInfo, location, va_(fmt), ap);
859  va_end(ap);
860 }
861 
862 ////////////////////////////////////////////////////////////////////////////////
863 /// Issue warning message. Use "location" to specify the method where the
864 /// warning occurred. Accepts standard printf formatting arguments.
865 
866 void TObject::Warning(const char *location, const char *va_(fmt), ...) const
867 {
868  va_list ap;
869  va_start(ap, va_(fmt));
870  DoError(kWarning, location, va_(fmt), ap);
871  va_end(ap);
872  if (TROOT::Initialized())
873  gROOT->Message(1001, this);
874 }
875 
876 ////////////////////////////////////////////////////////////////////////////////
877 /// Issue error message. Use "location" to specify the method where the
878 /// error occurred. Accepts standard printf formatting arguments.
879 
880 void TObject::Error(const char *location, const char *va_(fmt), ...) const
881 {
882  va_list ap;
883  va_start(ap, va_(fmt));
884  DoError(kError, location, va_(fmt), ap);
885  va_end(ap);
886  if (TROOT::Initialized())
887  gROOT->Message(1002, this);
888 }
889 
890 ////////////////////////////////////////////////////////////////////////////////
891 /// Issue system error message. Use "location" to specify the method where
892 /// the system error occurred. Accepts standard printf formatting arguments.
893 
894 void TObject::SysError(const char *location, const char *va_(fmt), ...) const
895 {
896  va_list ap;
897  va_start(ap, va_(fmt));
898  DoError(kSysError, location, va_(fmt), ap);
899  va_end(ap);
900  if (TROOT::Initialized())
901  gROOT->Message(1003, this);
902 }
903 
904 ////////////////////////////////////////////////////////////////////////////////
905 /// Issue fatal error message. Use "location" to specify the method where the
906 /// fatal error occurred. Accepts standard printf formatting arguments.
907 
908 void TObject::Fatal(const char *location, const char *va_(fmt), ...) const
909 {
910  va_list ap;
911  va_start(ap, va_(fmt));
912  DoError(kFatal, location, va_(fmt), ap);
913  va_end(ap);
914  if (TROOT::Initialized())
915  gROOT->Message(1004, this);
916 }
917 
918 ////////////////////////////////////////////////////////////////////////////////
919 /// Use this method to implement an "abstract" method that you don't
920 /// want to leave purely abstract.
921 
922 void TObject::AbstractMethod(const char *method) const
923 {
924  Warning(method, "this method must be overridden!");
925 }
926 
927 ////////////////////////////////////////////////////////////////////////////////
928 /// Use this method to signal that a method (defined in a base class)
929 /// may not be called in a derived class (in principle against good
930 /// design since a child class should not provide less functionality
931 /// than its parent, however, sometimes it is necessary).
932 
933 void TObject::MayNotUse(const char *method) const
934 {
935  Warning(method, "may not use this method");
936 }
937 
938 ////////////////////////////////////////////////////////////////////////////////
939 /// Use this method to declare a method obsolete. Specify as of which version
940 /// the method is obsolete and as from which version it will be removed.
941 
942 void TObject::Obsolete(const char *method, const char *asOfVers, const char *removedFromVers) const
943 {
944  const char *classname = "UnknownClass";
945  if (TROOT::Initialized())
946  classname = ClassName();
947 
948  ::Obsolete(Form("%s::%s", classname, method), asOfVers, removedFromVers);
949 }
950 
951 ////////////////////////////////////////////////////////////////////////////////
952 /// Get status of object stat flag.
953 
955 {
956  return fgObjectStat;
957 }
958 ////////////////////////////////////////////////////////////////////////////////
959 /// Turn on/off tracking of objects in the TObjectTable.
960 
962 {
963  fgObjectStat = stat;
964 }
965 
966 ////////////////////////////////////////////////////////////////////////////////
967 /// Return destructor only flag
968 
970 {
971  return fgDtorOnly;
972 }
973 
974 ////////////////////////////////////////////////////////////////////////////////
975 /// Set destructor only flag
976 
977 void TObject::SetDtorOnly(void *obj)
978 {
979  fgDtorOnly = (Long_t) obj;
980 }
981 
982 ////////////////////////////////////////////////////////////////////////////////
983 /// Operator delete
984 
985 void TObject::operator delete(void *ptr)
986 {
987  if ((Long_t) ptr != fgDtorOnly)
989  else
990  fgDtorOnly = 0;
991 }
992 
993 ////////////////////////////////////////////////////////////////////////////////
994 /// Operator delete []
995 
996 void TObject::operator delete[](void *ptr)
997 {
998  if ((Long_t) ptr != fgDtorOnly)
1000  else
1001  fgDtorOnly = 0;
1002 }
1003 
1004 #ifdef R__SIZEDDELETE
1005 ////////////////////////////////////////////////////////////////////////////////
1006 /// Operator delete for sized deallocation.
1007 
1008 void TObject::operator delete(void *ptr, size_t size)
1009 {
1010  if ((Long_t) ptr != fgDtorOnly)
1011  TStorage::ObjectDealloc(ptr, size);
1012  else
1013  fgDtorOnly = 0;
1014 }
1015 
1016 ////////////////////////////////////////////////////////////////////////////////
1017 /// Operator delete [] for sized deallocation.
1018 
1019 void TObject::operator delete[](void *ptr, size_t size)
1020 {
1021  if ((Long_t) ptr != fgDtorOnly)
1022  TStorage::ObjectDealloc(ptr, size);
1023  else
1024  fgDtorOnly = 0;
1025 }
1026 #endif
1027 
1028 ////////////////////////////////////////////////////////////////////////////////
1029 /// Print value overload
1030 
1031 std::string cling::printValue(TObject *val) {
1032  std::ostringstream strm;
1033  strm << "Name: " << val->GetName() << " Title: " << val->GetTitle();
1034  return strm.str();
1035 }
1036 
1037 #ifdef R__PLACEMENTDELETE
1038 ////////////////////////////////////////////////////////////////////////////////
1039 /// Only called by placement new when throwing an exception.
1040 
1041 void TObject::operator delete(void *ptr, void *vp)
1042 {
1043  TStorage::ObjectDealloc(ptr, vp);
1044 }
1045 
1046 ////////////////////////////////////////////////////////////////////////////////
1047 /// Only called by placement new[] when throwing an exception.
1048 
1049 void TObject::operator delete[](void *ptr, void *vp)
1050 {
1051  TStorage::ObjectDealloc(ptr, vp);
1052 }
1053 #endif
virtual void DoError(int level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler (protected).
Definition: TObject.cxx:841
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition: TObject.cxx:894
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:785
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:375
Bool_t IsReading() const
Definition: TBuffer.h:83
UInt_t fUniqueID
object unique identifier
Definition: TObject.h:40
void PutObjectWithID(TObject *obj, UInt_t uid=0)
stores the object at the uid th slot in the table of objects The object uniqued is set as well as its...
Definition: TProcessID.cxx:344
An array of TObjects.
Definition: TObjArray.h:37
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TObject.cxx:433
object is on heap
Definition: TObject.h:77
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad for instance with: gROOT->SetSelectedPad(gPad...
Definition: TObject.cxx:219
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition: TROOT.h:382
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Computes distance from point (px,py) to the object.
Definition: TObject.cxx:186
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
virtual const char * GetIconName() const
Returns mime type name of object.
Definition: TObject.cxx:367
write object, then delete previous key with same name
Definition: TObject.h:89
virtual void Update()=0
unsigned short UShort_t
Definition: RtypesCore.h:36
static void SetDtorOnly(void *obj)
Set destructor only flag.
Definition: TObject.cxx:977
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
static void SetObjectStat(Bool_t stat)
Turn on/off tracking of objects in the TObjectTable.
Definition: TObject.cxx:961
overwrite existing object with same name
Definition: TObject.h:88
#define gROOT
Definition: TROOT.h:393
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
void RemoveQuietly(TObject *obj)
Remove an object from the object table.
Basic string class.
Definition: TString.h:125
virtual void Browse(TBrowser *b)
Browse object. May be overridden for another default action.
Definition: TObject.cxx:119
virtual Int_t Add(Int_t uid, TProcessID *context=0)
Add a new uid to the table.
Definition: TRefTable.cxx:88
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:195
#define gInterpreter
Definition: TInterpreter.h:507
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:715
virtual UShort_t GetPidOffset() const =0
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:550
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
virtual char * GetObjectInfo(Int_t px, Int_t py) const
Returns string containing info about the object at position (px,py).
Definition: TObject.cxx:386
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Iterator of linked list.
Definition: TList.h:197
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:616
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
static Long_t GetDtorOnly()
Return destructor only flag.
Definition: TObject.cxx:969
if object in a list can be deleted
Definition: TObject.h:58
virtual void RecursiveRemove(TObject *obj)
Recursively remove this object from a list.
Definition: TObject.cxx:572
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:105
static Long_t fgDtorOnly
object for which to call dtor only (i.e. no delete)
Definition: TObject.h:43
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
Double_t x[n]
Definition: legend1.C:17
virtual TProcessID * ReadProcessID(UShort_t pidf)=0
Return the current Process-ID.
Definition: TBuffer.cxx:323
void Class()
Definition: Class.C:29
const Int_t kSysError
Definition: TError.h:41
virtual void SaveAs(const char *filename="", Option_t *option="") const
Save this object in the file specified by filename.
Definition: TObject.cxx:599
virtual void Copy(TObject &object) const
Copy this to obj.
Definition: TObject.cxx:61
virtual Bool_t Notify()
This method must be overridden to handle object notification.
Definition: TObject.cxx:506
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition: TROOT.cxx:2754
const Int_t kFatal
Definition: TError.h:42
virtual Bool_t HandleTimer(TTimer *timer)
Execute action in response of a timer timing out.
Definition: TObject.cxx:411
virtual void Pop()
Pop on object drawn in a pad to the top of the display list.
Definition: TObject.cxx:528
#define va_(arg)
Definition: Varargs.h:41
void MayNotUse(const char *method) const
Use this method to signal that a method (defined in a base class) may not be called in a derived clas...
Definition: TObject.cxx:933
virtual Int_t Read(const char *name)
Read contents of object with specified name from the current directory.
Definition: TObject.cxx:562
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:69
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
R__EXTERN TGuiFactory * gGuiFactory
Definition: TGuiFactory.h:66
void Obsolete(const char *method, const char *asOfVers, const char *removedFromVers) const
Use this method to declare a method obsolete.
Definition: TObject.cxx:942
virtual UShort_t WriteProcessID(TProcessID *pid)=0
Always return 0 (current processID).
Definition: TBuffer.cxx:332
virtual void ls(Option_t *option="") const
The ls function lists the contents of a class on stdout.
Definition: TObject.cxx:492
object has not been deleted
Definition: TObject.h:78
virtual Bool_t IsEqual(const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory).
Definition: TObject.cxx:483
virtual void DrawClass() const
Draw class inheritance tree of the class to which this object belongs.
Definition: TObject.cxx:210
virtual Option_t * GetDrawOption() const
Get option used by the graphics system to draw this object.
Definition: TObject.cxx:341
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition: TObject.cxx:705
virtual void Delete(Option_t *option="")
Delete this object.
Definition: TObject.cxx:169
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
std::string printValue(const TDatime *val)
Print a TDatime at the prompt.
Definition: TDatime.cxx:514
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:321
static TProcessID * GetProcessWithUID(const TObject *obj)
static function returning a pointer to TProcessID with its pid encoded in the highest byte of obj->Ge...
Definition: TProcessID.cxx:267
const Int_t kInfo
Definition: TError.h:37
write collection with single key
Definition: TObject.h:87
virtual void Execute(const char *method, const char *params, Int_t *error=0)
Execute method on this object with the given parameter string, e.g.
Definition: TObject.cxx:277
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
virtual void Inspect() const
Dump contents of this object in a graphics canvas.
Definition: TObject.cxx:464
R__ALWAYS_INLINE Bool_t IsOnHeap() const
Definition: TObject.h:133
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
char * Form(const char *fmt,...)
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
virtual TInspectorImp * CreateInspectorImp(const TObject *obj, UInt_t width, UInt_t height)
Create a batch version of TInspectorImp.
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2746
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:72
virtual TList * GetListOfPrimitives() const =0
const Int_t kWarning
Definition: TError.h:38
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TObject.cxx:159
static Bool_t GetObjectStat()
Get status of object stat flag.
Definition: TObject.cxx:954
if object destructor must call RecursiveRemove()
Definition: TObject.h:60
static void AddObj(TObject *obj)
Add an object to the global object table gObjectTable.
R__EXTERN TObjectTable * gObjectTable
Definition: TObjectTable.h:82
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2524
const Bool_t kFALSE
Definition: RtypesCore.h:88
UInt_t fBits
bit field status word
Definition: TObject.h:41
long Long_t
Definition: RtypesCore.h:50
virtual void Modified(Bool_t flag=1)=0
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition: TObject.cxx:519
static Bool_t fgObjectStat
if true keep track of objects in TObjectTable
Definition: TObject.h:44
#define ClassImp(name)
Definition: Rtypes.h:359
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Execute action corresponding to an event at (px,py).
Definition: TObject.cxx:311
static TRefTable * GetRefTable()
Static function returning the current TRefTable.
Definition: TRefTable.cxx:287
virtual void Dump() const
Dump contents of object on stdout.
Definition: TObject.cxx:266
unsigned long ULong_t
Definition: RtypesCore.h:51
virtual ~TObject()
TObject destructor.
Definition: TObject.cxx:79
A TRefTable maintains the association between a referenced object and the parent object supporting th...
Definition: TRefTable.h:35
Double_t y[n]
Definition: legend1.C:17
virtual Option_t * GetOption() const
Definition: TObject.h:120
Mother of all ROOT objects.
Definition: TObject.h:37
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:144
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
void ErrorHandler(int level, const char *location, const char *fmt, va_list va)
General error handler function. It calls the user set error handler.
Definition: TError.cxx:202
virtual void Add(TObject *obj)
Definition: TList.h:87
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
const Int_t kError
Definition: TError.h:39
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
if object is referenced by a TRef or TRefArray
Definition: TObject.h:61
#define snprintf
Definition: civetweb.c:1351
#define gPad
Definition: TVirtualPad.h:285
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TObject.cxx:473
#define gDirectory
Definition: TDirectory.h:213
void ResetBit(UInt_t f)
Definition: TObject.h:171
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:908
virtual void SetDrawOption(Option_t *option="")
Set drawing option for object.
Definition: TObject.cxx:677
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
const Bool_t kTRUE
Definition: RtypesCore.h:87
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don&#39;t want to leave purely abstract...
Definition: TObject.cxx:922
static void ObjectDealloc(void *vp)
Used to deallocate a TObject on the heap (via TObject::operator delete()).
Definition: TStorage.cxx:359
static Int_t AutoBrowse(TObject *obj, TBrowser *browser)
Browse external object inherited from TObject.
Definition: TClass.cxx:1877
char name[80]
Definition: TGX11.cxx:109
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
virtual void SkipVersion(const TClass *cl=0)=0
static void AddToTObjectTable(TObject *)
Private helper function which will dispatch to TObjectTable::AddObj.
Definition: TObject.cxx:96
const char * Data() const
Definition: TString.h:343
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TObject.cxx:664