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