Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:04

0001 #include "CommonTools/Clustering1D/interface/Clusterizer1DCommons.h"
0002 
0003 #include <string>
0004 #include <vector>
0005 #include <map>
0006 #include <cmath>
0007 #include <iostream>
0008 #include <algorithm>
0009 #include <sstream>
0010 
0011 using namespace std;
0012 
0013 namespace {
0014   inline Cluster1D<string> createCluster(float pos, float err, float weight, string name) {
0015     vector<const string*> names;
0016     names.push_back(new string(name));
0017     Cluster1D<string> ret(Measurement1D(pos, err), names, weight);
0018     return ret;
0019   }
0020 
0021   char nameCtr = 'a';
0022 
0023   void resetClusterName() { nameCtr = 'a'; }
0024 
0025   Cluster1D<string> createCluster(float pos, float err, float weight) {
0026     vector<const string*> names;
0027     char fld[2];
0028     fld[0] = nameCtr;
0029     fld[1] = '\0';
0030     names.push_back(new string(fld));
0031     nameCtr++;
0032     Cluster1D<string> ret(Measurement1D(pos, err), names, weight);
0033     return ret;
0034   }
0035 
0036   inline void deleteCluster(vector<Cluster1D<string> >& clus) {
0037     cout << "[Deleting Sequence] ..." << flush;
0038     for (vector<Cluster1D<string> >::const_iterator i = clus.begin(); i != clus.end(); ++i) {
0039       vector<const string*> names = i->tracks();
0040       for (vector<const string*>::const_iterator nm = names.begin(); nm != names.end(); ++i) {
0041         delete *nm;
0042       };
0043     };
0044     cout << " done." << endl;
0045   }
0046 
0047   // makes sure that the leftmost cluster is called 'a'
0048   // and so on
0049   vector<Cluster1D<string> > sortCluster(const vector<Cluster1D<string> >& in) {
0050     vector<Cluster1D<string> > ret;
0051     vector<Cluster1D<string> > tmp = in;
0052     partial_sort_copy(in.begin(), in.end(), tmp.begin(), tmp.end(), Clusterizer1DCommons::ComparePairs<string>());
0053     resetClusterName();
0054     for (vector<Cluster1D<string> >::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
0055       ret.push_back(createCluster(i->position().value(), i->position().error(), i->weight()));
0056     };
0057     return ret;
0058   }
0059 
0060   vector<Cluster1D<string> > trivialInput() {
0061     vector<Cluster1D<string> > ret;
0062     ret.push_back(createCluster(0.1, 0.05, .5));
0063     ret.push_back(createCluster(5.0, 0.32, .4));
0064     ret.push_back(createCluster(10.0, 0.22, .7));
0065     return ret;
0066   }
0067 
0068   vector<Cluster1D<string> > threeItems() {
0069     vector<Cluster1D<string> > ret;
0070     ret.push_back(createCluster(0.1, 0.05, .8));
0071     ret.push_back(createCluster(5.0, 0.32, .9));
0072     ret.push_back(createCluster(10.0, 0.22, .6));
0073     return ret;
0074   }
0075 
0076   vector<Cluster1D<string> > fourItems() {
0077     vector<Cluster1D<string> > ret;
0078     ret.push_back(createCluster(0.1, 0.05, .6));
0079     ret.push_back(createCluster(5.0, 0.32, .7));
0080     ret.push_back(createCluster(10.0, 0.22, .5));
0081     ret.push_back(createCluster(12.0, 0.22, .8));
0082     return ret;
0083   }
0084 
0085   /**
0086  *  User calls this, or the next.
0087  *  In this function the user gives the name of
0088  *  the "secnario"
0089  */
0090   inline vector<Cluster1D<string> > createInput(string name) {
0091     // that's a map that maps the function to ordinary names.
0092     map<string, vector<Cluster1D<string> > (*)()> inputs;
0093     inputs["Trivial"] = trivialInput;
0094     inputs["Three"] = threeItems;
0095     inputs["Four"] = fourItems;
0096 
0097     // give me the pointer to the function that implements the
0098     // "scenario"
0099     vector<Cluster1D<string> > (*addr)() = inputs[name];
0100     if (addr) {
0101       return ((*addr))();
0102       // vector < Cluster1D < string > > tmp = ((*addr))();
0103       // now make sure that the leftmost cluster is called 'a'
0104       // and so on
0105       // return sortCluster ( tmp );
0106     };
0107 
0108     cout << "[Input.cc] input " << name << " unknown" << endl;
0109     exit(-1);
0110   }
0111 
0112   /**
0113  *  The user gives the number of clusters
0114  */
0115   inline vector<Cluster1D<string> > createInput(int n) {
0116     vector<Cluster1D<string> > ret;
0117     for (int i = 0; i < n; ++i) {
0118       //        ret.push_back ( createCluster (  RandGauss::shoot( 0., 5. ),
0119       //                                         RandFlat::shoot(0., .05),
0120       //                                         pow ( RandFlat::shoot (0., .1 ), -2 ) ) );
0121       ret.push_back(createCluster(5.00 * drand48(), 0.05 * drand48(), pow(0.1 * drand48(), -2)));
0122     };
0123     return sortCluster(ret);
0124     //    sort ( ret.begin(), ret.end(), ClusterizerCommons::ComparePairs<string>() );
0125     //    return ret;
0126   }
0127 
0128 }  // namespace