Changeset 890 for trunk/MagicSoft


Ignore:
Timestamp:
07/20/01 16:35:53 (23 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MParList.cc

    r867 r890  
    4343#include <TNamed.h>
    4444#include <TClass.h>
     45#include <TObjArray.h>
    4546
    4647#include "MLog.h"
     
    163164// --------------------------------------------------------------------------
    164165//
     166//  Add all entries of the TObjArray to the list.
     167//
     168void MParList::AddToList(TObjArray *list)
     169{
     170    //
     171    //  check if the object (you want to add) exists
     172    //
     173    if (!list)
     174        return;
     175
     176    TObjArrayIter Next(list);
     177
     178    MParContainer *cont = NULL;
     179    while ((cont=(MParContainer*)Next()))
     180    {
     181        //
     182        // Get Name of new container
     183        //
     184        const char *name = cont->GetName();
     185
     186        //
     187        // Check if the new container is already existing in the list
     188        //
     189        const TObject *objn = fContainer.FindObject(name);
     190        const TObject *objt = fContainer.FindObject(cont);
     191
     192        if (objn || objt)
     193        {
     194            //
     195            // If the container is already in the list ignore it.
     196            //
     197            if (objt || objn==cont)
     198            {
     199                *fLog << dbginf << "Warning: Container '" << cont->GetName() << ", 0x" << (void*)cont;
     200                *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
     201                continue;
     202            }
     203
     204            //
     205            // Otherwise add it to the list, but print a warning message
     206            //
     207            *fLog << dbginf << "Warning: Container with the same name '" << cont->GetName();
     208            *fLog << "' already existing in '" << GetName() << "'." << endl;
     209            *fLog << "You may not be able to get a pointer to container task by name." << endl;
     210        }
     211
     212        *fLog << "Adding " << name << " to " << GetName() << "... " << flush;
     213
     214        fContainer.Add(cont);
     215
     216        *fLog << "Done." << endl;
     217    }
     218}
     219
     220// --------------------------------------------------------------------------
     221//
    165222//  Find an object in the list.
    166223//  'name' is the name of the object you are searching for.
     
    178235{
    179236    return fContainer.FindObject(obj);
     237}
     238
     239// --------------------------------------------------------------------------
     240//
     241//  returns the ClassName without anything which is behind that last ';' in
     242//  string.
     243//
     244TString MParList::GetClassName(const char *classname)
     245{
     246    TString cname(classname);
     247    const char *semicolon = strrchr(cname, ';');
     248
     249    if (semicolon)
     250        cname.Remove(semicolon-cname);
     251
     252    return cname;
     253}
     254
     255// --------------------------------------------------------------------------
     256//
     257//  returns the ObjectName. It is created from a class and object name.
     258//  If no object name is given the objectname is the same than the
     259//  class name. Leading dots are removed from the object name
     260//
     261TString MParList::GetObjectName(const char *classname, const char *objname)
     262{
     263    TString cname(classname);
     264    const char *semicolon = strrchr(cname, ';');
     265
     266    TString oname(objname ? objname : classname);
     267
     268    if (semicolon)
     269    {
     270        //
     271        // Remove leading dots from objectname (eg. "MMcTrig;5.")
     272        //
     273        Int_t sz = oname.Sizeof()-2;
     274
     275        while (sz>=0 && oname[sz]=='.')
     276            oname.Remove(sz--);
     277    }
     278    return oname;
    180279}
    181280
     
    208307    // List) is given use it's classname as the objectname
    209308    //
    210     if (!objname)
    211         objname = classname;
    212309
    213310    //
     
    221318    // the new object deleted automatically
    222319    //
    223     TString cname(classname);
    224     const char *semicolon = strrchr(cname, ';');
    225 
    226     TString oname(objname);
    227 
    228     if (semicolon)
    229     {
    230         cname.Remove(semicolon-cname);
    231 
    232         //
    233         // Remove leading dots from objectname (eg. "MMcTrig;5.")
    234         //
    235         Int_t sz = oname.Sizeof()-2;
    236 
    237         while (sz>=0 && oname[sz]=='.')
    238             oname.Remove(sz--);
    239     }
     320    TString cname = GetClassName(classname);
     321    TString oname = GetObjectName(classname, objname);
    240322
    241323    //
     
    273355
    274356    //
    275     // If a name different to the classname was given,
    276     // set the new object name of the object
     357    // Set the name of the container
    277358    //
    278359    pcont->SetName(oname);
     
    326407}
    327408
     409// --------------------------------------------------------------------------
     410//
     411//   Reset all containers in the list
     412//
    328413void MParList::Reset()
    329414{
     
    338423        cont->Reset();
    339424}
     425
     426// --------------------------------------------------------------------------
     427//
     428//  This finds numbered objects. The objects are returned in a copy of a
     429//  TObjArray.
     430//
     431//  If from only is given (or to=0) object are assumed numbered
     432//  from 1 to from.
     433//
     434TObjArray MParList::FindObjectList(const char *name, const UInt_t from, const UInt_t to) const
     435{
     436    TObjArray list;
     437
     438    if (to>0 && to<=from)
     439    {
     440        *fLog << dbginf << "Cannot create entries backwards (to<from)...skipped." << endl;
     441        return list;
     442    }
     443
     444    const UInt_t len = strlen(name);
     445
     446    char *auxname = new char[len+7];
     447    strcpy(auxname, name);
     448
     449    //
     450    // If only 'from' is specified the number of entries are ment
     451    //
     452    const Bool_t exc = from>0 && to==0;
     453
     454    const UInt_t first = exc ?    0 : from;
     455    const UInt_t last  = exc ? from : to;
     456
     457    for (UInt_t num=first; num<last; num++)
     458    {
     459        if (from!=0 || to!=0)
     460            sprintf(auxname+len, ";%d", num+1);
     461
     462        TObject *obj = FindObject(auxname);
     463        if (!obj)
     464            continue;
     465
     466        list.AddLast(obj);
     467    }
     468    delete auxname;
     469
     470    return list;
     471}
     472
     473// --------------------------------------------------------------------------
     474//
     475//  This finds numbered objects. The objects are returned in a copy of a
     476//  TObjArray. If one of the objects doesn't exist it is created from the
     477//  meaning of cname and oname (s. FindCreateObj)
     478//
     479//  If from only is given (or to=0) object are assumed numbered
     480//  from 1 to from.
     481//
     482TObjArray MParList::FindCreateObjList(const char *cname, const UInt_t from, const UInt_t to, const char *oname)
     483{
     484    TObjArray list;
     485
     486    if (to>0 && to<=from)
     487    {
     488        *fLog << dbginf << "Cannot create entries backwards (to<from)...skipped." << endl;
     489        return list;
     490    }
     491
     492    const UInt_t len = strlen(cname);
     493
     494    char *auxname = new char[len+7];
     495    strcpy(auxname, cname);
     496
     497    //
     498    // If only 'from' is specified the number of entries are ment
     499    //
     500    const Bool_t exc = from>0 && to==0;
     501
     502    const UInt_t first = exc ?    0 : from;
     503    const UInt_t last  = exc ? from : to;
     504
     505    for (UInt_t num=first; num<last; num++)
     506    {
     507        if (from!=0 || to!=0)
     508            sprintf(auxname+len, ";%d", num+1);
     509
     510        TObject *obj = FindCreateObj(auxname, oname);
     511        if (!obj)
     512            break;
     513
     514        list.AddLast(obj);
     515    }
     516    delete auxname;
     517
     518    return list;
     519}
     520
     521// --------------------------------------------------------------------------
     522//
     523//  This finds numbered objects. The objects are returned in a copy of a
     524//  TObjArray. If one of the objects doesn't exist it is created from the
     525//  meaning of cname and oname (s. FindCreateObj)
     526//
     527//  If from only is given (or to=0) object are assumed numbered
     528//  from 1 to from.
     529//
     530//  Remark: Because it is static the object are only created and not added to
     531//  the parameter list. You must also take care of deleting these objects!
     532//  This function is mainly made for use in root macros. Don't use it in
     533//  compiled programs if you are not 100% sure what you are doing.
     534//
     535TObjArray MParList::CreateObjList(const char *cname, const UInt_t from, const UInt_t to=0, const char *oname=NULL)
     536{
     537    TObjArray list;
     538
     539    //
     540    // try to get class from root environment
     541    //
     542    TClass *cls = gROOT->GetClass(cname);
     543
     544    if (!cls)
     545    {
     546        //
     547        // if class is not existing in the root environment
     548        //
     549        gLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
     550        return list;
     551    }
     552
     553    const UInt_t len = strlen(cname);
     554
     555    char *auxname = new char[len+7];
     556    strcpy(auxname, cname);
     557
     558    //
     559    // If only 'from' is specified the number of entries are ment
     560    //
     561    const Bool_t exc = from>0 && to==0;
     562
     563    const UInt_t first = exc ?    0 : from;
     564    const UInt_t last  = exc ? from : to;
     565
     566    for (UInt_t num=first; num<last; num++)
     567    {
     568        if (from!=0 || to!=0)
     569            sprintf(auxname+len, ";%d", num+1);
     570
     571        //
     572        // create the parameter container of the the given class type
     573        //
     574        MParContainer *pcont = (MParContainer*)cls->New();
     575
     576        //
     577        // Set the name of the container
     578        //
     579        pcont->SetName(auxname);
     580
     581        //
     582        // Add new object to the return list
     583        //
     584        list.AddLast(pcont);
     585    }
     586    delete auxname;
     587
     588    return list;
     589}
Note: See TracChangeset for help on using the changeset viewer.