Defining map

Hi,

    I have a  member funciton of a class which has two vector <TLorentzVector> as input and a map as the output. .eg.

======================================================
//Matching of objct B to A
void Analyzer::BestMatch(vector ObjectA, vector ObjectB, map<TLorentzVector ,int > *ABmap)
{
vectorBFlag;

for(int j = 0; j < ObjectB.size(); j++) { BFlag.push_back(0); }

for(int i = 0; i < ObjectA.size(); i++)
{
float DRAB = 1.0; int BNo = -1;

 for(int j = 0; j<ObjectB.size(); j++){

   if(BFlag[j] == 0)
  {

    float delr = GetDeltaR(ObjectA[i].Eta(),ObjectB[j].Eta(),ObjectA[i].Phi(),ObjectB[j].Phi());
    if(delr < DRAB)
     { DRAB = delr;
       BNo = j;
     }
  }
}

if(DRAB < 0.4)
 {
   (*ABmap)[ObjectA[i]] = BNo;
   BFlag[BNo] = 1;
 }

   else {(*ABmap)[ObjectA[i]] = -1;}

}

}

I define the funciton in class as
void BestMatch(vector ,vector
map<TLorentzVector,int >* );

and included the map.h, TLorentzVector and vector.h in the header file.
But on compiling to get .so file I get the following error:
++++++++++++++++++++++
Analyzer.h: In member function void Analyzer::BestMatch(std::vector<TLorentzVector, std::allocator<TLorentzVector> >, std::vector<TLorentzVector, std::allocator<TLorentzVector> >, std::map<TLorentzVector, int, std::less<TLorentzVector>, std::allocator<std::pair<const TLorentzVector, int> > >*)': Analyzer.h:1311: warning: comparison between signed and unsigned integer expressions Analyzer.h:1313: warning: comparison between signed and unsigned integer expressions Analyzer.h:1317: warning: comparison between signed and unsigned integer expressions Analyzer.C: In member functionvirtual void Analyzer::Loop()’:
Analyzer.C:430: warning: comparison between signed and unsigned integer expressions
Analyzer.C:533: warning: comparison between signed and unsigned integer expressions
Analyzer.C:583: warning: comparison between signed and unsigned integer expressions
Analyzer.C:649: warning: comparison between signed and unsigned integer expressions
Analyzer.C:691: warning: comparison between signed and unsigned integer expressions
Analyzer.C:748: warning: comparison between signed and unsigned integer expressions
Analyzer.C:819: warning: comparison between signed and unsigned integer expressions
Analyzer.C:948: warning: comparison between signed and unsigned integer expressions
Analyzer.C:46: warning: unused variable 'rPhot_r9’
Analyzer.C:48: warning: unused variable 'pPhotJet_CosTheta’
Analyzer.C:271: warning: unused variable 'idebug1’
Analyzer.C:272: warning: unused variable 'idebug2’
Analyzer.C:273: warning: unused variable 'idebug3’
Analyzer.C:274: warning: unused variable 'idebug4’
Analyzer.C:275: warning: unused variable 'idebug5’
Analyzer.C:276: warning: unused variable 'idebug6’
Analyzer.C:277: warning: unused variable 'idebug7’
Analyzer.C:278: warning: unused variable 'idebug8’
Analyzer.C:349: warning: unused variable ‘egHcalIsoEMin’
Analyzer.C:352: warning: unused variable ‘Rechits_threshold’
/usr/lib/gcc/i386-redhat-linux/3.4.6/…/…/…/…/include/c++/3.4.6/bits/stl_function.h: In member function bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = TLorentzVector]': /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_tree.h:1156: instantiated fromtypename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::lower_bound(const _Key&) [with _Key = TLorentzVector, _Val = std::pair<const TLorentzVector, int>, _KeyOfValue = std::_Select1st<std::pair<const TLorentzVector, int> >, _Compare = std::less, _Alloc = std::allocator<std::pair<const TLorentzVector, int> >]’
/usr/lib/gcc/i386-redhat-linux/3.4.6/…/…/…/…/include/c++/3.4.6/bits/stl_map.h:540: instantiated from typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::lower_bound(const _Key&) [with _Key = TLorentzVector, _Tp = int, _Compare = std::less<TLorentzVector>, _Alloc = std::allocator<std::pair<const TLorentzVector, int> >]' /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_map.h:336: instantiated from_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = TLorentzVector, _Tp = int, _Compare = std::less, _Alloc = std::allocator<std::pair<const TLorentzVector, int> >]'
Analyzer.h:1332: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/…/…/…/…/include/c++/3.4.6/bits/stl_function.h:227: error: no match for ‘operator<’ in ‘__x < __y’
/home/sushil/ROOT_5.16/root_514/include/TDatime.h:94: note: candidates are: Bool_t operator<(const TDatime&, const TDatime&)
/home/sushil/ROOT_5.16/root_514/include/TString.h:642: note: Bool_t operator<(const char*, const TString&)
/home/sushil/ROOT_5.16/root_514/include/TString.h:624: note: Bool_t operator<(const TString&, const char*)
/home/sushil/ROOT_5.16/root_514/include/TString.h:608: note: Bool_t operator<(const TString&, const TString&)
make: *** [Analyzer.so] Error 1
+++++++++++++++++++++++++++++++==

Could somebody help to solve it.

Thanks in advance.

with best,
sushil

[quote=“sushil”]Hi,

    I have a  member funciton of a class which has two vector <TLorentzVector> as input and a map as the output. .eg.

======================================================
//Matching of objct B to A
void Analyzer::BestMatch(vector ObjectA, vector ObjectB, map<TLorentzVector ,int > *ABmap)



/usr/lib/gcc/i386-redhat-linux/3.4.6/…/…/…/…/include/c++/3.4.6/bits/stl_function.h: In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = TLorentzVector]’:[/quote]
You need to define a comparison operator operator< for type TLorentzVector. It should return true if its first argument is less than its second argument, and false otherwise. See any STL documentation or your favorite STL book.

And please, pass arguments as const references!

Also, have a look to generic algorithms. They may make your code much nicer and more…

Thanks.

But what is the role of this comparision operator in the map i have defined???

with best,
sushil

[quote=“sushil”]But what is the role of this comparision operator in the map i have defined???
[/quote]

Look,

std::map is a Sorted Associative Container. By default it uses std::less function as a Compare parameter. std::less needs a less operator to help map to sort your keys - TLorentzVector’s. The TLorentzVector class has no operator< defined, that means you need to define it yourself in order to use map with TLorentzVector as a key type.

Hi ,
In my class I put the following lines. This is how it should look like…

struct liltr{
bool operator()(const TLorentzVector& x ,const TLorentzVector& y)const
{ if(x.Pt() > y.Pt()){return true;}
else(return false; }
};

void BestMatch(const vector& ,const vector& ,map<TLorentzVector,int>* , liltr );

Thanks in advance,

with best,
sushil

[quote=“sushil”]Hi ,
In my class I put the following lines. This is how it should look like…

struct liltr{
bool operator()(const TLorentzVector& x ,const TLorentzVector& y)const
{ if(x.Pt() > y.Pt()){return true;}
else(return false; }
};

void BestMatch(const vector& ,const vector& ,map<TLorentzVector,int>* , liltr );

Thanks in advance,

with best,
sushil[/quote]
I am not sure that I understand what the code above is suppose to do :frowning:

See examples:
sgi.com/tech/stl/Map.html
+
google :wink:

P.S. BTW, use typedef’s, it will make you code more readable. typedef is a special skill of C++ and a friend of us :slight_smile:
at least something like that
typedef vector TLorentzVector_t;
and so on…